blob: 34ad7de2e0329b0d3ad67eb2dd959a01fe2a1745 [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 Gaocbe70cb2016-10-18 18:17:52 -070050#include "backtrace.h"
51#include "tombstone.h"
52#include "utility.h"
53
54#include "debuggerd/handler.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010055#include "protocol.h"
56#include "tombstoned/tombstoned.h"
57#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070058
59using android::base::unique_fd;
Josh Gao57f58f82017-03-15 23:23:22 -070060using android::base::ReadFileToString;
Josh Gaocbe70cb2016-10-18 18:17:52 -070061using android::base::StringPrintf;
Josh Gao57f58f82017-03-15 23:23:22 -070062using android::base::Trim;
63
64static std::string get_process_name(pid_t pid) {
65 std::string result = "<unknown>";
66 ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &result);
67 return result;
68}
69
70static std::string get_thread_name(pid_t tid) {
71 std::string result = "<unknown>";
72 ReadFileToString(StringPrintf("/proc/%d/comm", tid), &result);
73 return Trim(result);
74}
Josh Gaocbe70cb2016-10-18 18:17:52 -070075
Josh Gaofe902762017-02-01 16:31:43 -080076static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
77 struct stat st;
78 std::string task_path = StringPrintf("task/%d", tid);
79 return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -070080}
81
82// Attach to a thread, and verify that it's still a member of the given process
Josh Gaofe902762017-02-01 16:31:43 -080083static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error) {
Josh Gao122479f2017-01-22 16:42:32 -080084 if (ptrace(PTRACE_SEIZE, tid, 0, 0) != 0) {
Josh Gao42fd74b2017-01-20 12:51:11 -080085 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -070086 return false;
87 }
88
89 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -080090 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -070091 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
92 PLOG(FATAL) << "failed to detach from thread " << tid;
93 }
Josh Gaofe902762017-02-01 16:31:43 -080094 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -070095 return false;
96 }
Josh Gao122479f2017-01-22 16:42:32 -080097
Josh Gao3407d7c2017-06-13 17:21:12 +000098 // Put the task into ptrace-stop state.
99 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) != 0) {
100 PLOG(FATAL) << "failed to interrupt thread " << tid;
101 }
102
Josh Gaocbe70cb2016-10-18 18:17:52 -0700103 return true;
104}
105
Josh Gaob0e51e32017-06-01 12:08:10 -0700106static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
Josh Gao8bb03902017-05-30 15:31:02 -0700107 ATRACE_CALL();
Josh Gaob0e51e32017-06-01 12:08:10 -0700108 android::base::unique_fd amfd(socket_local_client(
109 "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700110 if (amfd.get() == -1) {
111 PLOG(ERROR) << "unable to connect to activity manager";
112 return false;
113 }
114
115 struct timeval tv = {
116 .tv_sec = 1,
117 .tv_usec = 0,
118 };
119 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
120 PLOG(ERROR) << "failed to set send timeout on activity manager socket";
121 return false;
122 }
123 tv.tv_sec = 3; // 3 seconds on handshake read
124 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
125 PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
126 return false;
127 }
128
129 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
130 // pid and signal number, followed by the raw text of the dump, culminating
131 // in a zero byte that marks end-of-data.
132 uint32_t datum = htonl(pid);
133 if (!android::base::WriteFully(amfd, &datum, 4)) {
134 PLOG(ERROR) << "AM pid write failed";
135 return false;
136 }
137 datum = htonl(signal);
138 if (!android::base::WriteFully(amfd, &datum, 4)) {
139 PLOG(ERROR) << "AM signal write failed";
140 return false;
141 }
142 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
143 PLOG(ERROR) << "AM data write failed";
144 return false;
145 }
146
147 // 3 sec timeout reading the ack; we're fine if the read fails.
148 char ack;
149 android::base::ReadFully(amfd, &ack, 1);
150 return true;
151}
152
Josh Gao57594112017-01-22 17:41:15 -0800153static void signal_handler(int) {
154 // We can't log easily, because the heap might be corrupt.
155 // Just die and let the surrounding log context explain things.
156 _exit(1);
157}
158
Narayan Kamatha73df602017-05-24 15:07:25 +0100159static void abort_handler(pid_t target, const bool tombstoned_connected,
Josh Gaocbe70cb2016-10-18 18:17:52 -0700160 unique_fd& tombstoned_socket, unique_fd& output_fd,
161 const char* abort_msg) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700162 // If we abort before we get an output fd, contact tombstoned to let any
163 // potential listeners know that we failed.
164 if (!tombstoned_connected) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100165 if (!tombstoned_connect(target, &tombstoned_socket, &output_fd, kDebuggerdAnyIntercept)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700166 // We failed to connect, not much we can do.
167 LOG(ERROR) << "failed to connected to tombstoned to report failure";
168 _exit(1);
169 }
170 }
171
Josh Gao428daaf2017-03-10 14:49:19 -0800172 dprintf(output_fd.get(), "crash_dump failed to dump process");
173 if (target != 1) {
174 dprintf(output_fd.get(), " %d: %s\n", target, abort_msg);
175 } else {
176 dprintf(output_fd.get(), ": %s\n", abort_msg);
177 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700178
Josh Gaocbe70cb2016-10-18 18:17:52 -0700179 _exit(1);
180}
181
Josh Gao85bcaf62017-02-01 16:35:31 -0800182static void drop_capabilities() {
Josh Gao8bb03902017-05-30 15:31:02 -0700183 ATRACE_CALL();
Josh Gao85bcaf62017-02-01 16:35:31 -0800184 __user_cap_header_struct capheader;
185 memset(&capheader, 0, sizeof(capheader));
186 capheader.version = _LINUX_CAPABILITY_VERSION_3;
187 capheader.pid = 0;
188
189 __user_cap_data_struct capdata[2];
190 memset(&capdata, 0, sizeof(capdata));
191
192 if (capset(&capheader, &capdata[0]) == -1) {
193 PLOG(FATAL) << "failed to drop capabilities";
194 }
195
196 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
197 PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS";
198 }
199}
200
Josh Gaocbe70cb2016-10-18 18:17:52 -0700201int main(int argc, char** argv) {
Josh Gao8bb03902017-05-30 15:31:02 -0700202 atrace_begin(ATRACE_TAG, "before reparent");
203
Josh Gaocbe70cb2016-10-18 18:17:52 -0700204 pid_t target = getppid();
205 bool tombstoned_connected = false;
206 unique_fd tombstoned_socket;
207 unique_fd output_fd;
208
209 android::base::InitLogging(argv);
210 android::base::SetAborter([&](const char* abort_msg) {
211 abort_handler(target, tombstoned_connected, tombstoned_socket, output_fd, abort_msg);
212 });
213
Josh Gao57594112017-01-22 17:41:15 -0800214 // Don't try to dump ourselves.
215 struct sigaction action = {};
216 action.sa_handler = signal_handler;
217 debuggerd_register_handlers(&action);
218
Josh Gaoe7402502017-06-01 11:55:25 -0700219 sigset_t mask;
220 sigemptyset(&mask);
221 if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
222 PLOG(FATAL) << "failed to set signal mask";
223 }
224
Narayan Kamatha73df602017-05-24 15:07:25 +0100225 if (argc != 4) {
226 LOG(FATAL) << "Wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700227 }
228
229 pid_t main_tid;
Josh Gao2f11a252017-02-13 14:46:19 -0800230 pid_t pseudothread_tid;
Narayan Kamatha73df602017-05-24 15:07:25 +0100231 int dump_type;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700232
Josh Gaocbe70cb2016-10-18 18:17:52 -0700233 if (!android::base::ParseInt(argv[1], &main_tid, 1, std::numeric_limits<pid_t>::max())) {
234 LOG(FATAL) << "invalid main tid: " << argv[1];
235 }
236
Josh Gao2f11a252017-02-13 14:46:19 -0800237 if (!android::base::ParseInt(argv[2], &pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800238 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800239 }
240
Narayan Kamatha73df602017-05-24 15:07:25 +0100241 if (!android::base::ParseInt(argv[3], &dump_type, 0, 1)) {
242 LOG(FATAL) << "invalid requested dump type: " << argv[3];
243 }
244
Josh Gaoc7fe0602017-03-13 14:13:29 -0700245 if (target == 1) {
246 LOG(FATAL) << "target died before we could attach (received main tid = " << main_tid << ")";
247 }
248
Josh Gaocbe70cb2016-10-18 18:17:52 -0700249 android::procinfo::ProcessInfo target_info;
250 if (!android::procinfo::GetProcessInfo(main_tid, &target_info)) {
251 LOG(FATAL) << "failed to fetch process info for target " << main_tid;
252 }
253
254 if (main_tid != target_info.tid || target != target_info.pid) {
255 LOG(FATAL) << "target info mismatch, expected pid " << target << ", tid " << main_tid
256 << ", received pid " << target_info.pid << ", tid " << target_info.tid;
257 }
258
259 // Open /proc/`getppid()` in the original process, and pass it down to the forked child.
260 std::string target_proc_path = "/proc/" + std::to_string(target);
261 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
262 if (target_proc_fd == -1) {
263 PLOG(FATAL) << "failed to open " << target_proc_path;
264 }
265
Josh Gao2a18b822017-02-16 19:17:28 -0800266 // Make sure our parent didn't die.
267 if (getppid() != target) {
268 PLOG(FATAL) << "parent died";
269 }
270
Josh Gao8bb03902017-05-30 15:31:02 -0700271 atrace_end(ATRACE_TAG);
272
Josh Gaocbe70cb2016-10-18 18:17:52 -0700273 // Reparent ourselves to init, so that the signal handler can waitpid on the
274 // original process to avoid leaving a zombie for non-fatal dumps.
275 pid_t forkpid = fork();
276 if (forkpid == -1) {
277 PLOG(FATAL) << "fork failed";
278 } else if (forkpid != 0) {
279 exit(0);
280 }
281
Josh Gao8bb03902017-05-30 15:31:02 -0700282 ATRACE_NAME("after reparent");
283
Josh Gao7c6e3132017-01-22 17:59:02 -0800284 // Die if we take too long.
Andreas Gampeb02851a2017-06-22 19:45:53 -0700285 //
286 // Note: processes with many threads and minidebug-info can take a bit to
287 // unwind, do not make this too small. b/62828735
288 alarm(5);
Josh Gao7c6e3132017-01-22 17:59:02 -0800289
Josh Gao42fd74b2017-01-20 12:51:11 -0800290 std::string attach_error;
Josh Gao2f11a252017-02-13 14:46:19 -0800291
Josh Gao57f58f82017-03-15 23:23:22 -0700292 std::map<pid_t, std::string> threads;
Josh Gao8bb03902017-05-30 15:31:02 -0700293
Josh Gao2f11a252017-02-13 14:46:19 -0800294 {
Josh Gao3407d7c2017-06-13 17:21:12 +0000295 ATRACE_NAME("ptrace");
Josh Gao8bb03902017-05-30 15:31:02 -0700296 // Seize the main thread.
297 if (!ptrace_seize_thread(target_proc_fd, main_tid, &attach_error)) {
298 LOG(FATAL) << attach_error;
Josh Gao2f11a252017-02-13 14:46:19 -0800299 }
300
Josh Gao3407d7c2017-06-13 17:21:12 +0000301 // Seize the siblings.
302 {
303 std::set<pid_t> siblings;
304 if (!android::procinfo::GetProcessTids(target, &siblings)) {
305 PLOG(FATAL) << "failed to get process siblings";
Josh Gao2f11a252017-02-13 14:46:19 -0800306 }
Josh Gao3407d7c2017-06-13 17:21:12 +0000307
308 // but not the already attached main thread.
309 siblings.erase(main_tid);
310 // or the handler pseudothread.
311 siblings.erase(pseudothread_tid);
312
313 for (pid_t sibling_tid : siblings) {
314 if (!ptrace_seize_thread(target_proc_fd, sibling_tid, &attach_error)) {
315 LOG(WARNING) << attach_error;
316 } else {
317 threads.emplace(sibling_tid, get_thread_name(sibling_tid));
318 }
319 }
Josh Gao2f11a252017-02-13 14:46:19 -0800320 }
321 }
322
Josh Gao57f58f82017-03-15 23:23:22 -0700323 // Collect the backtrace map, open files, and process/thread names, while we still have caps.
Josh Gao8bb03902017-05-30 15:31:02 -0700324 std::unique_ptr<BacktraceMap> backtrace_map;
325 {
326 ATRACE_NAME("backtrace map");
327 backtrace_map.reset(BacktraceMap::Create(main_tid));
328 if (!backtrace_map) {
329 LOG(FATAL) << "failed to create backtrace map";
330 }
Josh Gao2f11a252017-02-13 14:46:19 -0800331 }
Christopher Ferris9a8c8552017-08-11 15:29:19 -0700332 std::unique_ptr<BacktraceMap> backtrace_map_new;
333 backtrace_map_new.reset(BacktraceMap::CreateNew(main_tid));
334 if (!backtrace_map_new) {
335 LOG(FATAL) << "failed to create backtrace map new";
336 }
Josh Gao2f11a252017-02-13 14:46:19 -0800337
338 // Collect the list of open files.
339 OpenFilesList open_files;
Josh Gao8bb03902017-05-30 15:31:02 -0700340 {
341 ATRACE_NAME("open files");
342 populate_open_files_list(target, &open_files);
343 }
Josh Gao2f11a252017-02-13 14:46:19 -0800344
Josh Gao3407d7c2017-06-13 17:21:12 +0000345 std::string process_name = get_process_name(main_tid);
346 threads.emplace(main_tid, get_thread_name(main_tid));
347
Josh Gao2f11a252017-02-13 14:46:19 -0800348 // Drop our capabilities now that we've attached to the threads we care about.
349 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700350
Josh Gao8bb03902017-05-30 15:31:02 -0700351 {
352 ATRACE_NAME("tombstoned_connect");
353 const DebuggerdDumpType dump_type_enum = static_cast<DebuggerdDumpType>(dump_type);
354 LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type_enum;
355 tombstoned_connected = tombstoned_connect(target, &tombstoned_socket, &output_fd, dump_type_enum);
356 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700357
358 // Write a '\1' to stdout to tell the crashing process to resume.
Josh Gao2f11a252017-02-13 14:46:19 -0800359 // It also restores the value of PR_SET_DUMPABLE at this point.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700360 if (TEMP_FAILURE_RETRY(write(STDOUT_FILENO, "\1", 1)) == -1) {
361 PLOG(ERROR) << "failed to communicate to target process";
362 }
363
364 if (tombstoned_connected) {
365 if (TEMP_FAILURE_RETRY(dup2(output_fd.get(), STDOUT_FILENO)) == -1) {
366 PLOG(ERROR) << "failed to dup2 output fd (" << output_fd.get() << ") to STDOUT_FILENO";
367 }
368 } else {
369 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
370 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao0a379012017-01-24 15:20:42 -0800371 output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700372 }
373
Josh Gaocbe70cb2016-10-18 18:17:52 -0700374 LOG(INFO) << "performing dump of process " << target << " (target tid = " << main_tid << ")";
375
Josh Gao122479f2017-01-22 16:42:32 -0800376 // At this point, the thread that made the request has been attached and is
377 // in ptrace-stopped state. After resumption, the triggering signal that has
378 // been queued will be delivered.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700379 if (ptrace(PTRACE_CONT, main_tid, 0, 0) != 0) {
380 PLOG(ERROR) << "PTRACE_CONT(" << main_tid << ") failed";
381 exit(1);
382 }
383
384 siginfo_t siginfo = {};
Josh Gao8bb03902017-05-30 15:31:02 -0700385 {
386 ATRACE_NAME("wait_for_signal");
387 if (!wait_for_signal(main_tid, &siginfo)) {
388 printf("failed to wait for signal in tid %d: %s\n", main_tid, strerror(errno));
389 exit(1);
390 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700391 }
392
393 int signo = siginfo.si_signo;
Josh Gaofe902762017-02-01 16:31:43 -0800394 bool fatal_signal = signo != DEBUGGER_SIGNAL;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700395 bool backtrace = false;
396 uintptr_t abort_address = 0;
397
398 // si_value can represent three things:
399 // 0: dump tombstone
400 // 1: dump backtrace
401 // everything else: abort message address (implies dump tombstone)
402 if (siginfo.si_value.sival_int == 1) {
403 backtrace = true;
404 } else if (siginfo.si_value.sival_ptr != nullptr) {
405 abort_address = reinterpret_cast<uintptr_t>(siginfo.si_value.sival_ptr);
406 }
407
Josh Gaocbe70cb2016-10-18 18:17:52 -0700408 // TODO: Use seccomp to lock ourselves down.
409
Josh Gaocbe70cb2016-10-18 18:17:52 -0700410 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700411 if (backtrace) {
Josh Gao8bb03902017-05-30 15:31:02 -0700412 ATRACE_NAME("dump_backtrace");
Josh Gao57f58f82017-03-15 23:23:22 -0700413 dump_backtrace(output_fd.get(), backtrace_map.get(), target, main_tid, process_name, threads, 0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700414 } else {
Josh Gao8bb03902017-05-30 15:31:02 -0700415 ATRACE_NAME("engrave_tombstone");
Christopher Ferris9a8c8552017-08-11 15:29:19 -0700416 engrave_tombstone(output_fd.get(), backtrace_map.get(), backtrace_map_new.get(), &open_files,
417 target, main_tid, process_name, threads, abort_address,
418 fatal_signal ? &amfd_data : nullptr);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700419 }
420
Josh Gao7c6e3132017-01-22 17:59:02 -0800421 // We don't actually need to PTRACE_DETACH, as long as our tracees aren't in
422 // group-stop state, which is true as long as no stopping signals are sent.
423
Josh Gaocbe70cb2016-10-18 18:17:52 -0700424 bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
Josh Gao7c6e3132017-01-22 17:59:02 -0800425 if (!fatal_signal || siginfo.si_code == SI_USER) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700426 // Don't wait_for_gdb when the process didn't actually crash.
Josh Gao7c6e3132017-01-22 17:59:02 -0800427 wait_for_gdb = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700428 }
429
Josh Gao7c6e3132017-01-22 17:59:02 -0800430 // If the process crashed or we need to send it SIGSTOP for wait_for_gdb,
431 // get it in a state where it can receive signals, and then send the relevant
432 // signal.
433 if (wait_for_gdb || fatal_signal) {
434 if (ptrace(PTRACE_INTERRUPT, main_tid, 0, 0) != 0) {
435 PLOG(ERROR) << "failed to use PTRACE_INTERRUPT on " << main_tid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700436 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700437
Josh Gao7c6e3132017-01-22 17:59:02 -0800438 if (tgkill(target, main_tid, wait_for_gdb ? SIGSTOP : signo) != 0) {
439 PLOG(ERROR) << "failed to resend signal " << signo << " to " << main_tid;
440 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700441 }
442
443 if (wait_for_gdb) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800444 // Use ALOGI to line up with output from engrave_tombstone.
445 ALOGI(
446 "***********************************************************\n"
447 "* Process %d has been suspended while crashing.\n"
448 "* To attach gdbserver and start gdb, run this on the host:\n"
449 "*\n"
450 "* gdbclient.py -p %d\n"
451 "*\n"
452 "***********************************************************",
453 target, main_tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700454 }
455
456 if (fatal_signal) {
Josh Gaob0e51e32017-06-01 12:08:10 -0700457 // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
458 if (target_info.name != "system_server" || target_info.uid != AID_SYSTEM) {
459 activity_manager_notify(target, signo, amfd_data);
460 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700461 }
462
463 // Close stdout before we notify tombstoned of completion.
464 close(STDOUT_FILENO);
Josh Gao0a379012017-01-24 15:20:42 -0800465 if (tombstoned_connected && !tombstoned_notify_completion(tombstoned_socket.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700466 LOG(ERROR) << "failed to notify tombstoned of completion";
467 }
468
469 return 0;
470}