| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2016, The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | #include <arpa/inet.h> | 
 | 18 | #include <dirent.h> | 
 | 19 | #include <fcntl.h> | 
 | 20 | #include <stdlib.h> | 
| Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame] | 21 | #include <sys/capability.h> | 
 | 22 | #include <sys/prctl.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 23 | #include <sys/ptrace.h> | 
 | 24 | #include <sys/types.h> | 
 | 25 | #include <sys/un.h> | 
| Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame] | 26 | #include <syscall.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 27 | #include <unistd.h> | 
 | 28 |  | 
 | 29 | #include <limits> | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 30 | #include <map> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 31 | #include <memory> | 
 | 32 | #include <set> | 
 | 33 | #include <vector> | 
 | 34 |  | 
 | 35 | #include <android-base/file.h> | 
 | 36 | #include <android-base/logging.h> | 
 | 37 | #include <android-base/parseint.h> | 
 | 38 | #include <android-base/properties.h> | 
 | 39 | #include <android-base/stringprintf.h> | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 40 | #include <android-base/strings.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 41 | #include <android-base/unique_fd.h> | 
 | 42 | #include <cutils/sockets.h> | 
| Vijay Venkatraman | a95acea | 2017-01-23 20:11:51 -0800 | [diff] [blame] | 43 | #include <log/log.h> | 
| Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 44 | #include <private/android_filesystem_config.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 45 | #include <procinfo/process.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 46 |  | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 47 | #define ATRACE_TAG ATRACE_TAG_BIONIC | 
 | 48 | #include <utils/Trace.h> | 
 | 49 |  | 
| Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 50 | #include "libdebuggerd/backtrace.h" | 
 | 51 | #include "libdebuggerd/tombstone.h" | 
 | 52 | #include "libdebuggerd/utility.h" | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 53 |  | 
 | 54 | #include "debuggerd/handler.h" | 
| Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 55 | #include "tombstoned/tombstoned.h" | 
| Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 56 |  | 
 | 57 | #include "protocol.h" | 
| Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 58 | #include "util.h" | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 59 |  | 
 | 60 | using android::base::unique_fd; | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 61 | using android::base::ReadFileToString; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 62 | using android::base::StringPrintf; | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 63 | using android::base::Trim; | 
 | 64 |  | 
 | 65 | static std::string get_process_name(pid_t pid) { | 
 | 66 |   std::string result = "<unknown>"; | 
 | 67 |   ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &result); | 
 | 68 |   return result; | 
 | 69 | } | 
 | 70 |  | 
 | 71 | static std::string get_thread_name(pid_t tid) { | 
 | 72 |   std::string result = "<unknown>"; | 
 | 73 |   ReadFileToString(StringPrintf("/proc/%d/comm", tid), &result); | 
 | 74 |   return Trim(result); | 
 | 75 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 76 |  | 
| Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 77 | static bool pid_contains_tid(int pid_proc_fd, pid_t tid) { | 
 | 78 |   struct stat st; | 
 | 79 |   std::string task_path = StringPrintf("task/%d", tid); | 
 | 80 |   return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 81 | } | 
 | 82 |  | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 83 | static pid_t get_tracer(pid_t tracee) { | 
 | 84 |   // Check to see if the thread is being ptraced by another process. | 
 | 85 |   android::procinfo::ProcessInfo process_info; | 
 | 86 |   if (android::procinfo::GetProcessInfo(tracee, &process_info)) { | 
 | 87 |     return process_info.tracer; | 
 | 88 |   } | 
 | 89 |   return -1; | 
 | 90 | } | 
 | 91 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 92 | // Attach to a thread, and verify that it's still a member of the given process | 
| Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 93 | static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error) { | 
| Josh Gao | 122479f | 2017-01-22 16:42:32 -0800 | [diff] [blame] | 94 |   if (ptrace(PTRACE_SEIZE, tid, 0, 0) != 0) { | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 95 |     if (errno == EPERM) { | 
 | 96 |       pid_t tracer = get_tracer(tid); | 
 | 97 |       if (tracer != -1) { | 
 | 98 |         *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid, | 
 | 99 |                               tracer, get_process_name(tracer).c_str()); | 
 | 100 |         return false; | 
 | 101 |       } | 
 | 102 |     } | 
 | 103 |  | 
| Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 104 |     *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno)); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 105 |     return false; | 
 | 106 |   } | 
 | 107 |  | 
 | 108 |   // Make sure that the task we attached to is actually part of the pid we're dumping. | 
| Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 109 |   if (!pid_contains_tid(pid_proc_fd, tid)) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 110 |     if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) { | 
 | 111 |       PLOG(FATAL) << "failed to detach from thread " << tid; | 
 | 112 |     } | 
| Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 113 |     *error = StringPrintf("thread %d is not in process", tid); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 114 |     return false; | 
 | 115 |   } | 
| Josh Gao | 122479f | 2017-01-22 16:42:32 -0800 | [diff] [blame] | 116 |  | 
| Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 117 |   // Put the task into ptrace-stop state. | 
 | 118 |   if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) != 0) { | 
 | 119 |     PLOG(FATAL) << "failed to interrupt thread " << tid; | 
 | 120 |   } | 
 | 121 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 122 |   return true; | 
 | 123 | } | 
 | 124 |  | 
| Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 125 | static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) { | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 126 |   ATRACE_CALL(); | 
| Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 127 |   android::base::unique_fd amfd(socket_local_client( | 
 | 128 |       "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM)); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 129 |   if (amfd.get() == -1) { | 
 | 130 |     PLOG(ERROR) << "unable to connect to activity manager"; | 
 | 131 |     return false; | 
 | 132 |   } | 
 | 133 |  | 
 | 134 |   struct timeval tv = { | 
 | 135 |     .tv_sec = 1, | 
 | 136 |     .tv_usec = 0, | 
 | 137 |   }; | 
 | 138 |   if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) { | 
 | 139 |     PLOG(ERROR) << "failed to set send timeout on activity manager socket"; | 
 | 140 |     return false; | 
 | 141 |   } | 
 | 142 |   tv.tv_sec = 3;  // 3 seconds on handshake read | 
 | 143 |   if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) { | 
 | 144 |     PLOG(ERROR) << "failed to set receive timeout on activity manager socket"; | 
 | 145 |     return false; | 
 | 146 |   } | 
 | 147 |  | 
 | 148 |   // Activity Manager protocol: binary 32-bit network-byte-order ints for the | 
 | 149 |   // pid and signal number, followed by the raw text of the dump, culminating | 
 | 150 |   // in a zero byte that marks end-of-data. | 
 | 151 |   uint32_t datum = htonl(pid); | 
 | 152 |   if (!android::base::WriteFully(amfd, &datum, 4)) { | 
 | 153 |     PLOG(ERROR) << "AM pid write failed"; | 
 | 154 |     return false; | 
 | 155 |   } | 
 | 156 |   datum = htonl(signal); | 
 | 157 |   if (!android::base::WriteFully(amfd, &datum, 4)) { | 
 | 158 |     PLOG(ERROR) << "AM signal write failed"; | 
 | 159 |     return false; | 
 | 160 |   } | 
 | 161 |   if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) { | 
 | 162 |     PLOG(ERROR) << "AM data write failed"; | 
 | 163 |     return false; | 
 | 164 |   } | 
 | 165 |  | 
 | 166 |   // 3 sec timeout reading the ack; we're fine if the read fails. | 
 | 167 |   char ack; | 
 | 168 |   android::base::ReadFully(amfd, &ack, 1); | 
 | 169 |   return true; | 
 | 170 | } | 
 | 171 |  | 
| Josh Gao | 5759411 | 2017-01-22 17:41:15 -0800 | [diff] [blame] | 172 | static void signal_handler(int) { | 
 | 173 |   // We can't log easily, because the heap might be corrupt. | 
 | 174 |   // Just die and let the surrounding log context explain things. | 
 | 175 |   _exit(1); | 
 | 176 | } | 
 | 177 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 178 | static void abort_handler(pid_t target, const bool tombstoned_connected, | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 179 |                           unique_fd& tombstoned_socket, unique_fd& output_fd, | 
 | 180 |                           const char* abort_msg) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 181 |   // If we abort before we get an output fd, contact tombstoned to let any | 
 | 182 |   // potential listeners know that we failed. | 
 | 183 |   if (!tombstoned_connected) { | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 184 |     if (!tombstoned_connect(target, &tombstoned_socket, &output_fd, kDebuggerdAnyIntercept)) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 185 |       // We failed to connect, not much we can do. | 
 | 186 |       LOG(ERROR) << "failed to connected to tombstoned to report failure"; | 
 | 187 |       _exit(1); | 
 | 188 |     } | 
 | 189 |   } | 
 | 190 |  | 
| Josh Gao | 428daaf | 2017-03-10 14:49:19 -0800 | [diff] [blame] | 191 |   dprintf(output_fd.get(), "crash_dump failed to dump process"); | 
 | 192 |   if (target != 1) { | 
 | 193 |     dprintf(output_fd.get(), " %d: %s\n", target, abort_msg); | 
 | 194 |   } else { | 
 | 195 |     dprintf(output_fd.get(), ": %s\n", abort_msg); | 
 | 196 |   } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 197 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 198 |   _exit(1); | 
 | 199 | } | 
 | 200 |  | 
| Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame] | 201 | static void drop_capabilities() { | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 202 |   ATRACE_CALL(); | 
| Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame] | 203 |   __user_cap_header_struct capheader; | 
 | 204 |   memset(&capheader, 0, sizeof(capheader)); | 
 | 205 |   capheader.version = _LINUX_CAPABILITY_VERSION_3; | 
 | 206 |   capheader.pid = 0; | 
 | 207 |  | 
 | 208 |   __user_cap_data_struct capdata[2]; | 
 | 209 |   memset(&capdata, 0, sizeof(capdata)); | 
 | 210 |  | 
 | 211 |   if (capset(&capheader, &capdata[0]) == -1) { | 
 | 212 |     PLOG(FATAL) << "failed to drop capabilities"; | 
 | 213 |   } | 
 | 214 |  | 
 | 215 |   if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) { | 
 | 216 |     PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS"; | 
 | 217 |   } | 
 | 218 | } | 
 | 219 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 220 | int main(int argc, char** argv) { | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 221 |   atrace_begin(ATRACE_TAG, "before reparent"); | 
 | 222 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 223 |   pid_t target = getppid(); | 
 | 224 |   bool tombstoned_connected = false; | 
 | 225 |   unique_fd tombstoned_socket; | 
 | 226 |   unique_fd output_fd; | 
 | 227 |  | 
 | 228 |   android::base::InitLogging(argv); | 
 | 229 |   android::base::SetAborter([&](const char* abort_msg) { | 
 | 230 |     abort_handler(target, tombstoned_connected, tombstoned_socket, output_fd, abort_msg); | 
 | 231 |   }); | 
 | 232 |  | 
| Josh Gao | 5759411 | 2017-01-22 17:41:15 -0800 | [diff] [blame] | 233 |   // Don't try to dump ourselves. | 
 | 234 |   struct sigaction action = {}; | 
 | 235 |   action.sa_handler = signal_handler; | 
 | 236 |   debuggerd_register_handlers(&action); | 
 | 237 |  | 
| Josh Gao | e740250 | 2017-06-01 11:55:25 -0700 | [diff] [blame] | 238 |   sigset_t mask; | 
 | 239 |   sigemptyset(&mask); | 
 | 240 |   if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) { | 
 | 241 |     PLOG(FATAL) << "failed to set signal mask"; | 
 | 242 |   } | 
 | 243 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 244 |   if (argc != 4) { | 
 | 245 |     LOG(FATAL) << "Wrong number of args: " << argc << " (expected 4)"; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 246 |   } | 
 | 247 |  | 
 | 248 |   pid_t main_tid; | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 249 |   pid_t pseudothread_tid; | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 250 |   int dump_type; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 251 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 252 |   if (!android::base::ParseInt(argv[1], &main_tid, 1, std::numeric_limits<pid_t>::max())) { | 
 | 253 |     LOG(FATAL) << "invalid main tid: " << argv[1]; | 
 | 254 |   } | 
 | 255 |  | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 256 |   if (!android::base::ParseInt(argv[2], &pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) { | 
| Josh Gao | f6ad585 | 2017-02-15 12:21:11 -0800 | [diff] [blame] | 257 |     LOG(FATAL) << "invalid pseudothread tid: " << argv[2]; | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 258 |   } | 
 | 259 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 260 |   if (!android::base::ParseInt(argv[3], &dump_type, 0, 1)) { | 
 | 261 |     LOG(FATAL) << "invalid requested dump type: " << argv[3]; | 
 | 262 |   } | 
 | 263 |  | 
| Josh Gao | c7fe060 | 2017-03-13 14:13:29 -0700 | [diff] [blame] | 264 |   if (target == 1) { | 
 | 265 |     LOG(FATAL) << "target died before we could attach (received main tid = " << main_tid << ")"; | 
 | 266 |   } | 
 | 267 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 268 |   android::procinfo::ProcessInfo target_info; | 
 | 269 |   if (!android::procinfo::GetProcessInfo(main_tid, &target_info)) { | 
 | 270 |     LOG(FATAL) << "failed to fetch process info for target " << main_tid; | 
 | 271 |   } | 
 | 272 |  | 
 | 273 |   if (main_tid != target_info.tid || target != target_info.pid) { | 
 | 274 |     LOG(FATAL) << "target info mismatch, expected pid " << target << ", tid " << main_tid | 
 | 275 |                << ", received pid " << target_info.pid << ", tid " << target_info.tid; | 
 | 276 |   } | 
 | 277 |  | 
 | 278 |   // Open /proc/`getppid()` in the original process, and pass it down to the forked child. | 
 | 279 |   std::string target_proc_path = "/proc/" + std::to_string(target); | 
 | 280 |   int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY); | 
 | 281 |   if (target_proc_fd == -1) { | 
 | 282 |     PLOG(FATAL) << "failed to open " << target_proc_path; | 
 | 283 |   } | 
 | 284 |  | 
| Josh Gao | 2a18b82 | 2017-02-16 19:17:28 -0800 | [diff] [blame] | 285 |   // Make sure our parent didn't die. | 
 | 286 |   if (getppid() != target) { | 
 | 287 |     PLOG(FATAL) << "parent died"; | 
 | 288 |   } | 
 | 289 |  | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 290 |   atrace_end(ATRACE_TAG); | 
 | 291 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 292 |   // Reparent ourselves to init, so that the signal handler can waitpid on the | 
 | 293 |   // original process to avoid leaving a zombie for non-fatal dumps. | 
 | 294 |   pid_t forkpid = fork(); | 
 | 295 |   if (forkpid == -1) { | 
 | 296 |     PLOG(FATAL) << "fork failed"; | 
 | 297 |   } else if (forkpid != 0) { | 
 | 298 |     exit(0); | 
 | 299 |   } | 
 | 300 |  | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 301 |   ATRACE_NAME("after reparent"); | 
 | 302 |  | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 303 |   // Die if we take too long. | 
| Andreas Gampe | b02851a | 2017-06-22 19:45:53 -0700 | [diff] [blame] | 304 |   // | 
 | 305 |   // Note: processes with many threads and minidebug-info can take a bit to | 
 | 306 |   //       unwind, do not make this too small. b/62828735 | 
 | 307 |   alarm(5); | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 308 |  | 
| Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 309 |   std::string attach_error; | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 310 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 311 |   std::map<pid_t, std::string> threads; | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 312 |  | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 313 |   { | 
| Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 314 |     ATRACE_NAME("ptrace"); | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 315 |     // Seize the main thread. | 
 | 316 |     if (!ptrace_seize_thread(target_proc_fd, main_tid, &attach_error)) { | 
 | 317 |       LOG(FATAL) << attach_error; | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 318 |     } | 
 | 319 |  | 
| Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 320 |     // Seize the siblings. | 
 | 321 |     { | 
 | 322 |       std::set<pid_t> siblings; | 
 | 323 |       if (!android::procinfo::GetProcessTids(target, &siblings)) { | 
 | 324 |         PLOG(FATAL) << "failed to get process siblings"; | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 325 |       } | 
| Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 326 |  | 
 | 327 |       // but not the already attached main thread. | 
 | 328 |       siblings.erase(main_tid); | 
 | 329 |       // or the handler pseudothread. | 
 | 330 |       siblings.erase(pseudothread_tid); | 
 | 331 |  | 
 | 332 |       for (pid_t sibling_tid : siblings) { | 
 | 333 |         if (!ptrace_seize_thread(target_proc_fd, sibling_tid, &attach_error)) { | 
 | 334 |           LOG(WARNING) << attach_error; | 
 | 335 |         } else { | 
 | 336 |           threads.emplace(sibling_tid, get_thread_name(sibling_tid)); | 
 | 337 |         } | 
 | 338 |       } | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 339 |     } | 
 | 340 |   } | 
 | 341 |  | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 342 |   // Collect the backtrace map, open files, and process/thread names, while we still have caps. | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 343 |   std::unique_ptr<BacktraceMap> backtrace_map; | 
 | 344 |   { | 
 | 345 |     ATRACE_NAME("backtrace map"); | 
 | 346 |     backtrace_map.reset(BacktraceMap::Create(main_tid)); | 
 | 347 |     if (!backtrace_map) { | 
 | 348 |       LOG(FATAL) << "failed to create backtrace map"; | 
 | 349 |     } | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 350 |   } | 
| Christopher Ferris | 9a8c855 | 2017-08-11 15:29:19 -0700 | [diff] [blame] | 351 |   std::unique_ptr<BacktraceMap> backtrace_map_new; | 
 | 352 |   backtrace_map_new.reset(BacktraceMap::CreateNew(main_tid)); | 
 | 353 |   if (!backtrace_map_new) { | 
 | 354 |     LOG(FATAL) << "failed to create backtrace map new"; | 
 | 355 |   } | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 356 |  | 
 | 357 |   // Collect the list of open files. | 
 | 358 |   OpenFilesList open_files; | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 359 |   { | 
 | 360 |     ATRACE_NAME("open files"); | 
 | 361 |     populate_open_files_list(target, &open_files); | 
 | 362 |   } | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 363 |  | 
| Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 364 |   std::string process_name = get_process_name(main_tid); | 
 | 365 |   threads.emplace(main_tid, get_thread_name(main_tid)); | 
 | 366 |  | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 367 |   // Drop our capabilities now that we've attached to the threads we care about. | 
 | 368 |   drop_capabilities(); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 369 |  | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 370 |   { | 
 | 371 |     ATRACE_NAME("tombstoned_connect"); | 
 | 372 |     const DebuggerdDumpType dump_type_enum = static_cast<DebuggerdDumpType>(dump_type); | 
 | 373 |     LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type_enum; | 
 | 374 |     tombstoned_connected = tombstoned_connect(target, &tombstoned_socket, &output_fd, dump_type_enum); | 
 | 375 |   } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 376 |  | 
 | 377 |   // Write a '\1' to stdout to tell the crashing process to resume. | 
| Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 378 |   // It also restores the value of PR_SET_DUMPABLE at this point. | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 379 |   if (TEMP_FAILURE_RETRY(write(STDOUT_FILENO, "\1", 1)) == -1) { | 
 | 380 |     PLOG(ERROR) << "failed to communicate to target process"; | 
 | 381 |   } | 
 | 382 |  | 
 | 383 |   if (tombstoned_connected) { | 
 | 384 |     if (TEMP_FAILURE_RETRY(dup2(output_fd.get(), STDOUT_FILENO)) == -1) { | 
 | 385 |       PLOG(ERROR) << "failed to dup2 output fd (" << output_fd.get() << ") to STDOUT_FILENO"; | 
 | 386 |     } | 
 | 387 |   } else { | 
 | 388 |     unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); | 
 | 389 |     TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO)); | 
| Josh Gao | 0a37901 | 2017-01-24 15:20:42 -0800 | [diff] [blame] | 390 |     output_fd = std::move(devnull); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 391 |   } | 
 | 392 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 393 |   LOG(INFO) << "performing dump of process " << target << " (target tid = " << main_tid << ")"; | 
 | 394 |  | 
| Josh Gao | 122479f | 2017-01-22 16:42:32 -0800 | [diff] [blame] | 395 |   // At this point, the thread that made the request has been attached and is | 
 | 396 |   // in ptrace-stopped state. After resumption, the triggering signal that has | 
 | 397 |   // been queued will be delivered. | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 398 |   if (ptrace(PTRACE_CONT, main_tid, 0, 0) != 0) { | 
 | 399 |     PLOG(ERROR) << "PTRACE_CONT(" << main_tid << ") failed"; | 
 | 400 |     exit(1); | 
 | 401 |   } | 
 | 402 |  | 
 | 403 |   siginfo_t siginfo = {}; | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 404 |   { | 
 | 405 |     ATRACE_NAME("wait_for_signal"); | 
 | 406 |     if (!wait_for_signal(main_tid, &siginfo)) { | 
 | 407 |       printf("failed to wait for signal in tid %d: %s\n", main_tid, strerror(errno)); | 
 | 408 |       exit(1); | 
 | 409 |     } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 410 |   } | 
 | 411 |  | 
 | 412 |   int signo = siginfo.si_signo; | 
| Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 413 |   bool fatal_signal = signo != DEBUGGER_SIGNAL; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 414 |   bool backtrace = false; | 
 | 415 |   uintptr_t abort_address = 0; | 
 | 416 |  | 
 | 417 |   // si_value can represent three things: | 
 | 418 |   //   0: dump tombstone | 
 | 419 |   //   1: dump backtrace | 
 | 420 |   //   everything else: abort message address (implies dump tombstone) | 
 | 421 |   if (siginfo.si_value.sival_int == 1) { | 
 | 422 |     backtrace = true; | 
 | 423 |   } else if (siginfo.si_value.sival_ptr != nullptr) { | 
 | 424 |     abort_address = reinterpret_cast<uintptr_t>(siginfo.si_value.sival_ptr); | 
 | 425 |   } | 
 | 426 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 427 |   // TODO: Use seccomp to lock ourselves down. | 
 | 428 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 429 |   std::string amfd_data; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 430 |   if (backtrace) { | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 431 |     ATRACE_NAME("dump_backtrace"); | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 432 |     dump_backtrace(output_fd.get(), backtrace_map.get(), target, main_tid, process_name, threads, 0); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 433 |   } else { | 
| Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 434 |     ATRACE_NAME("engrave_tombstone"); | 
| Christopher Ferris | 9a8c855 | 2017-08-11 15:29:19 -0700 | [diff] [blame] | 435 |     engrave_tombstone(output_fd.get(), backtrace_map.get(), backtrace_map_new.get(), &open_files, | 
 | 436 |                       target, main_tid, process_name, threads, abort_address, | 
 | 437 |                       fatal_signal ? &amfd_data : nullptr); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 438 |   } | 
 | 439 |  | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 440 |   // We don't actually need to PTRACE_DETACH, as long as our tracees aren't in | 
 | 441 |   // group-stop state, which is true as long as no stopping signals are sent. | 
 | 442 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 443 |   bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false); | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 444 |   if (!fatal_signal || siginfo.si_code == SI_USER) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 445 |     // Don't wait_for_gdb when the process didn't actually crash. | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 446 |     wait_for_gdb = false; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 447 |   } | 
 | 448 |  | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 449 |   // If the process crashed or we need to send it SIGSTOP for wait_for_gdb, | 
 | 450 |   // get it in a state where it can receive signals, and then send the relevant | 
 | 451 |   // signal. | 
 | 452 |   if (wait_for_gdb || fatal_signal) { | 
 | 453 |     if (ptrace(PTRACE_INTERRUPT, main_tid, 0, 0) != 0) { | 
 | 454 |       PLOG(ERROR) << "failed to use PTRACE_INTERRUPT on " << main_tid; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 455 |     } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 456 |  | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 457 |     if (tgkill(target, main_tid, wait_for_gdb ? SIGSTOP : signo) != 0) { | 
 | 458 |       PLOG(ERROR) << "failed to resend signal " << signo << " to " << main_tid; | 
 | 459 |     } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 460 |   } | 
 | 461 |  | 
 | 462 |   if (wait_for_gdb) { | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 463 |     // Use ALOGI to line up with output from engrave_tombstone. | 
 | 464 |     ALOGI( | 
| dimitry | 6429e20 | 2017-09-12 10:46:20 +0200 | [diff] [blame] | 465 |         "***********************************************************\n" | 
 | 466 |         "* Process %d has been suspended while crashing.\n" | 
 | 467 |         "* To attach gdbserver and start gdb, run this on the host:\n" | 
 | 468 |         "*\n" | 
 | 469 |         "*     gdbclient.py -p %d\n" | 
 | 470 |         "*\n" | 
 | 471 |         "***********************************************************", | 
 | 472 |         target, target); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 473 |   } | 
 | 474 |  | 
 | 475 |   if (fatal_signal) { | 
| Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 476 |     // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout. | 
 | 477 |     if (target_info.name != "system_server" || target_info.uid != AID_SYSTEM) { | 
 | 478 |       activity_manager_notify(target, signo, amfd_data); | 
 | 479 |     } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 480 |   } | 
 | 481 |  | 
 | 482 |   // Close stdout before we notify tombstoned of completion. | 
 | 483 |   close(STDOUT_FILENO); | 
| Josh Gao | 0a37901 | 2017-01-24 15:20:42 -0800 | [diff] [blame] | 484 |   if (tombstoned_connected && !tombstoned_notify_completion(tombstoned_socket.get())) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 485 |     LOG(ERROR) << "failed to notify tombstoned of completion"; | 
 | 486 |   } | 
 | 487 |  | 
 | 488 |   return 0; | 
 | 489 | } |