blob: 6ef3ed65048a5006359787bd5068d5371e5d255e [file] [log] [blame]
Josh Gaocbe70cb2016-10-18 18:17:52 -07001/*
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 Gao85bcaf62017-02-01 16:35:31 -080021#include <sys/capability.h>
22#include <sys/prctl.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070023#include <sys/ptrace.h>
24#include <sys/types.h>
25#include <sys/un.h>
Josh Gao85bcaf62017-02-01 16:35:31 -080026#include <syscall.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070027#include <unistd.h>
28
29#include <limits>
Josh Gao57f58f82017-03-15 23:23:22 -070030#include <map>
Josh Gaocbe70cb2016-10-18 18:17:52 -070031#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 Gao57f58f82017-03-15 23:23:22 -070040#include <android-base/strings.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070041#include <android-base/unique_fd.h>
42#include <cutils/sockets.h>
Vijay Venkatramana95acea2017-01-23 20:11:51 -080043#include <log/log.h>
Josh Gaob0e51e32017-06-01 12:08:10 -070044#include <private/android_filesystem_config.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070045#include <procinfo/process.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070046
Josh Gao8bb03902017-05-30 15:31:02 -070047#define ATRACE_TAG ATRACE_TAG_BIONIC
48#include <utils/Trace.h>
49
Josh Gaoc3706662017-08-29 13:08:32 -070050#include "libdebuggerd/backtrace.h"
51#include "libdebuggerd/tombstone.h"
52#include "libdebuggerd/utility.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070053
54#include "debuggerd/handler.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010055#include "tombstoned/tombstoned.h"
Josh Gaoc3706662017-08-29 13:08:32 -070056
57#include "protocol.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010058#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070059
60using android::base::unique_fd;
Josh Gao57f58f82017-03-15 23:23:22 -070061using android::base::ReadFileToString;
Josh Gaocbe70cb2016-10-18 18:17:52 -070062using android::base::StringPrintf;
Josh Gao57f58f82017-03-15 23:23:22 -070063using android::base::Trim;
64
65static 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
71static 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 Gaocbe70cb2016-10-18 18:17:52 -070076
Josh Gaofe902762017-02-01 16:31:43 -080077static 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 Gaocbe70cb2016-10-18 18:17:52 -070081}
82
Josh Gaofd13bf02017-08-18 15:37:26 -070083static 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 Gaocbe70cb2016-10-18 18:17:52 -070092// Attach to a thread, and verify that it's still a member of the given process
Josh Gaofe902762017-02-01 16:31:43 -080093static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error) {
Josh Gao122479f2017-01-22 16:42:32 -080094 if (ptrace(PTRACE_SEIZE, tid, 0, 0) != 0) {
Josh Gaofd13bf02017-08-18 15:37:26 -070095 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 Gao42fd74b2017-01-20 12:51:11 -0800104 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700105 return false;
106 }
107
108 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -0800109 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700110 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
111 PLOG(FATAL) << "failed to detach from thread " << tid;
112 }
Josh Gaofe902762017-02-01 16:31:43 -0800113 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700114 return false;
115 }
Josh Gao122479f2017-01-22 16:42:32 -0800116
Josh Gao3407d7c2017-06-13 17:21:12 +0000117 // 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 Gaocbe70cb2016-10-18 18:17:52 -0700122 return true;
123}
124
Josh Gaob0e51e32017-06-01 12:08:10 -0700125static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
Josh Gao8bb03902017-05-30 15:31:02 -0700126 ATRACE_CALL();
Josh Gaob0e51e32017-06-01 12:08:10 -0700127 android::base::unique_fd amfd(socket_local_client(
128 "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700129 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 Gao57594112017-01-22 17:41:15 -0800172static 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 Kamatha73df602017-05-24 15:07:25 +0100178static void abort_handler(pid_t target, const bool tombstoned_connected,
Josh Gaocbe70cb2016-10-18 18:17:52 -0700179 unique_fd& tombstoned_socket, unique_fd& output_fd,
180 const char* abort_msg) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700181 // 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 Kamatha73df602017-05-24 15:07:25 +0100184 if (!tombstoned_connect(target, &tombstoned_socket, &output_fd, kDebuggerdAnyIntercept)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700185 // 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 Gao428daaf2017-03-10 14:49:19 -0800191 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 Gaocbe70cb2016-10-18 18:17:52 -0700197
Josh Gaocbe70cb2016-10-18 18:17:52 -0700198 _exit(1);
199}
200
Josh Gao85bcaf62017-02-01 16:35:31 -0800201static void drop_capabilities() {
Josh Gao8bb03902017-05-30 15:31:02 -0700202 ATRACE_CALL();
Josh Gao85bcaf62017-02-01 16:35:31 -0800203 __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 Gaocbe70cb2016-10-18 18:17:52 -0700220int main(int argc, char** argv) {
Josh Gao8bb03902017-05-30 15:31:02 -0700221 atrace_begin(ATRACE_TAG, "before reparent");
222
Josh Gaocbe70cb2016-10-18 18:17:52 -0700223 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 Gao57594112017-01-22 17:41:15 -0800233 // Don't try to dump ourselves.
234 struct sigaction action = {};
235 action.sa_handler = signal_handler;
236 debuggerd_register_handlers(&action);
237
Josh Gaoe7402502017-06-01 11:55:25 -0700238 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 Kamatha73df602017-05-24 15:07:25 +0100244 if (argc != 4) {
245 LOG(FATAL) << "Wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700246 }
247
248 pid_t main_tid;
Josh Gao2f11a252017-02-13 14:46:19 -0800249 pid_t pseudothread_tid;
Narayan Kamatha73df602017-05-24 15:07:25 +0100250 int dump_type;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700251
Josh Gaocbe70cb2016-10-18 18:17:52 -0700252 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 Gao2f11a252017-02-13 14:46:19 -0800256 if (!android::base::ParseInt(argv[2], &pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800257 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800258 }
259
Narayan Kamatha73df602017-05-24 15:07:25 +0100260 if (!android::base::ParseInt(argv[3], &dump_type, 0, 1)) {
261 LOG(FATAL) << "invalid requested dump type: " << argv[3];
262 }
263
Josh Gaoc7fe0602017-03-13 14:13:29 -0700264 if (target == 1) {
265 LOG(FATAL) << "target died before we could attach (received main tid = " << main_tid << ")";
266 }
267
Josh Gaocbe70cb2016-10-18 18:17:52 -0700268 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 Gao2a18b822017-02-16 19:17:28 -0800285 // Make sure our parent didn't die.
286 if (getppid() != target) {
287 PLOG(FATAL) << "parent died";
288 }
289
Josh Gao8bb03902017-05-30 15:31:02 -0700290 atrace_end(ATRACE_TAG);
291
Josh Gaocbe70cb2016-10-18 18:17:52 -0700292 // 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 Gao8bb03902017-05-30 15:31:02 -0700301 ATRACE_NAME("after reparent");
302
Josh Gao7c6e3132017-01-22 17:59:02 -0800303 // Die if we take too long.
Andreas Gampeb02851a2017-06-22 19:45:53 -0700304 //
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 Gao7c6e3132017-01-22 17:59:02 -0800308
Josh Gao42fd74b2017-01-20 12:51:11 -0800309 std::string attach_error;
Josh Gao2f11a252017-02-13 14:46:19 -0800310
Josh Gao57f58f82017-03-15 23:23:22 -0700311 std::map<pid_t, std::string> threads;
Josh Gao8bb03902017-05-30 15:31:02 -0700312
Josh Gao2f11a252017-02-13 14:46:19 -0800313 {
Josh Gao3407d7c2017-06-13 17:21:12 +0000314 ATRACE_NAME("ptrace");
Josh Gao8bb03902017-05-30 15:31:02 -0700315 // Seize the main thread.
316 if (!ptrace_seize_thread(target_proc_fd, main_tid, &attach_error)) {
317 LOG(FATAL) << attach_error;
Josh Gao2f11a252017-02-13 14:46:19 -0800318 }
319
Josh Gao3407d7c2017-06-13 17:21:12 +0000320 // 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 Gao2f11a252017-02-13 14:46:19 -0800325 }
Josh Gao3407d7c2017-06-13 17:21:12 +0000326
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 Gao2f11a252017-02-13 14:46:19 -0800339 }
340 }
341
Josh Gao57f58f82017-03-15 23:23:22 -0700342 // Collect the backtrace map, open files, and process/thread names, while we still have caps.
Josh Gao8bb03902017-05-30 15:31:02 -0700343 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 Gao2f11a252017-02-13 14:46:19 -0800350 }
Christopher Ferris9a8c8552017-08-11 15:29:19 -0700351 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 Gao2f11a252017-02-13 14:46:19 -0800356
357 // Collect the list of open files.
358 OpenFilesList open_files;
Josh Gao8bb03902017-05-30 15:31:02 -0700359 {
360 ATRACE_NAME("open files");
361 populate_open_files_list(target, &open_files);
362 }
Josh Gao2f11a252017-02-13 14:46:19 -0800363
Josh Gao3407d7c2017-06-13 17:21:12 +0000364 std::string process_name = get_process_name(main_tid);
365 threads.emplace(main_tid, get_thread_name(main_tid));
366
Josh Gao2f11a252017-02-13 14:46:19 -0800367 // Drop our capabilities now that we've attached to the threads we care about.
368 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700369
Josh Gao8bb03902017-05-30 15:31:02 -0700370 {
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 Gaocbe70cb2016-10-18 18:17:52 -0700376
377 // Write a '\1' to stdout to tell the crashing process to resume.
Josh Gao2f11a252017-02-13 14:46:19 -0800378 // It also restores the value of PR_SET_DUMPABLE at this point.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700379 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 Gao0a379012017-01-24 15:20:42 -0800390 output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700391 }
392
Josh Gaocbe70cb2016-10-18 18:17:52 -0700393 LOG(INFO) << "performing dump of process " << target << " (target tid = " << main_tid << ")";
394
Josh Gao122479f2017-01-22 16:42:32 -0800395 // 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 Gaocbe70cb2016-10-18 18:17:52 -0700398 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 Gao8bb03902017-05-30 15:31:02 -0700404 {
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 Gaocbe70cb2016-10-18 18:17:52 -0700410 }
411
412 int signo = siginfo.si_signo;
Josh Gaofe902762017-02-01 16:31:43 -0800413 bool fatal_signal = signo != DEBUGGER_SIGNAL;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700414 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 Gaocbe70cb2016-10-18 18:17:52 -0700427 // TODO: Use seccomp to lock ourselves down.
428
Josh Gaocbe70cb2016-10-18 18:17:52 -0700429 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700430 if (backtrace) {
Josh Gao8bb03902017-05-30 15:31:02 -0700431 ATRACE_NAME("dump_backtrace");
Josh Gao57f58f82017-03-15 23:23:22 -0700432 dump_backtrace(output_fd.get(), backtrace_map.get(), target, main_tid, process_name, threads, 0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700433 } else {
Josh Gao8bb03902017-05-30 15:31:02 -0700434 ATRACE_NAME("engrave_tombstone");
Christopher Ferris9a8c8552017-08-11 15:29:19 -0700435 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 Gaocbe70cb2016-10-18 18:17:52 -0700438 }
439
Josh Gao7c6e3132017-01-22 17:59:02 -0800440 // 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 Gaocbe70cb2016-10-18 18:17:52 -0700443 bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
Josh Gao7c6e3132017-01-22 17:59:02 -0800444 if (!fatal_signal || siginfo.si_code == SI_USER) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700445 // Don't wait_for_gdb when the process didn't actually crash.
Josh Gao7c6e3132017-01-22 17:59:02 -0800446 wait_for_gdb = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700447 }
448
Josh Gao7c6e3132017-01-22 17:59:02 -0800449 // 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 Gaocbe70cb2016-10-18 18:17:52 -0700455 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700456
Josh Gao7c6e3132017-01-22 17:59:02 -0800457 if (tgkill(target, main_tid, wait_for_gdb ? SIGSTOP : signo) != 0) {
458 PLOG(ERROR) << "failed to resend signal " << signo << " to " << main_tid;
459 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700460 }
461
462 if (wait_for_gdb) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800463 // Use ALOGI to line up with output from engrave_tombstone.
464 ALOGI(
dimitry6429e202017-09-12 10:46:20 +0200465 "***********************************************************\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 Gaocbe70cb2016-10-18 18:17:52 -0700473 }
474
475 if (fatal_signal) {
Josh Gaob0e51e32017-06-01 12:08:10 -0700476 // 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 Gaocbe70cb2016-10-18 18:17:52 -0700480 }
481
482 // Close stdout before we notify tombstoned of completion.
483 close(STDOUT_FILENO);
Josh Gao0a379012017-01-24 15:20:42 -0800484 if (tombstoned_connected && !tombstoned_notify_completion(tombstoned_socket.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700485 LOG(ERROR) << "failed to notify tombstoned of completion";
486 }
487
488 return 0;
489}