blob: 558bc721a9f898404df9dd47487acc5008158843 [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
47#include "backtrace.h"
48#include "tombstone.h"
49#include "utility.h"
50
51#include "debuggerd/handler.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010052#include "protocol.h"
53#include "tombstoned/tombstoned.h"
54#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070055
56using android::base::unique_fd;
Josh Gao57f58f82017-03-15 23:23:22 -070057using android::base::ReadFileToString;
Josh Gaocbe70cb2016-10-18 18:17:52 -070058using android::base::StringPrintf;
Josh Gao57f58f82017-03-15 23:23:22 -070059using android::base::Trim;
60
61static std::string get_process_name(pid_t pid) {
62 std::string result = "<unknown>";
63 ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &result);
64 return result;
65}
66
67static std::string get_thread_name(pid_t tid) {
68 std::string result = "<unknown>";
69 ReadFileToString(StringPrintf("/proc/%d/comm", tid), &result);
70 return Trim(result);
71}
Josh Gaocbe70cb2016-10-18 18:17:52 -070072
Josh Gaofe902762017-02-01 16:31:43 -080073static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
74 struct stat st;
75 std::string task_path = StringPrintf("task/%d", tid);
76 return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -070077}
78
79// Attach to a thread, and verify that it's still a member of the given process
Josh Gaofe902762017-02-01 16:31:43 -080080static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error) {
Josh Gao122479f2017-01-22 16:42:32 -080081 if (ptrace(PTRACE_SEIZE, tid, 0, 0) != 0) {
Josh Gao42fd74b2017-01-20 12:51:11 -080082 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -070083 return false;
84 }
85
86 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -080087 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -070088 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
89 PLOG(FATAL) << "failed to detach from thread " << tid;
90 }
Josh Gaofe902762017-02-01 16:31:43 -080091 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -070092 return false;
93 }
Josh Gao122479f2017-01-22 16:42:32 -080094
95 // Put the task into ptrace-stop state.
96 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) != 0) {
97 PLOG(FATAL) << "failed to interrupt thread " << tid;
98 }
99
Josh Gaocbe70cb2016-10-18 18:17:52 -0700100 return true;
101}
102
Josh Gaob0e51e32017-06-01 12:08:10 -0700103static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
104 android::base::unique_fd amfd(socket_local_client(
105 "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700106 if (amfd.get() == -1) {
107 PLOG(ERROR) << "unable to connect to activity manager";
108 return false;
109 }
110
111 struct timeval tv = {
112 .tv_sec = 1,
113 .tv_usec = 0,
114 };
115 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
116 PLOG(ERROR) << "failed to set send timeout on activity manager socket";
117 return false;
118 }
119 tv.tv_sec = 3; // 3 seconds on handshake read
120 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
121 PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
122 return false;
123 }
124
125 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
126 // pid and signal number, followed by the raw text of the dump, culminating
127 // in a zero byte that marks end-of-data.
128 uint32_t datum = htonl(pid);
129 if (!android::base::WriteFully(amfd, &datum, 4)) {
130 PLOG(ERROR) << "AM pid write failed";
131 return false;
132 }
133 datum = htonl(signal);
134 if (!android::base::WriteFully(amfd, &datum, 4)) {
135 PLOG(ERROR) << "AM signal write failed";
136 return false;
137 }
138 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
139 PLOG(ERROR) << "AM data write failed";
140 return false;
141 }
142
143 // 3 sec timeout reading the ack; we're fine if the read fails.
144 char ack;
145 android::base::ReadFully(amfd, &ack, 1);
146 return true;
147}
148
Josh Gao57594112017-01-22 17:41:15 -0800149static void signal_handler(int) {
150 // We can't log easily, because the heap might be corrupt.
151 // Just die and let the surrounding log context explain things.
152 _exit(1);
153}
154
Narayan Kamatha73df602017-05-24 15:07:25 +0100155static void abort_handler(pid_t target, const bool tombstoned_connected,
Josh Gaocbe70cb2016-10-18 18:17:52 -0700156 unique_fd& tombstoned_socket, unique_fd& output_fd,
157 const char* abort_msg) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700158 // If we abort before we get an output fd, contact tombstoned to let any
159 // potential listeners know that we failed.
160 if (!tombstoned_connected) {
Narayan Kamatha73df602017-05-24 15:07:25 +0100161 if (!tombstoned_connect(target, &tombstoned_socket, &output_fd, kDebuggerdAnyIntercept)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700162 // We failed to connect, not much we can do.
163 LOG(ERROR) << "failed to connected to tombstoned to report failure";
164 _exit(1);
165 }
166 }
167
Josh Gao428daaf2017-03-10 14:49:19 -0800168 dprintf(output_fd.get(), "crash_dump failed to dump process");
169 if (target != 1) {
170 dprintf(output_fd.get(), " %d: %s\n", target, abort_msg);
171 } else {
172 dprintf(output_fd.get(), ": %s\n", abort_msg);
173 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700174
Josh Gaocbe70cb2016-10-18 18:17:52 -0700175 _exit(1);
176}
177
Josh Gao85bcaf62017-02-01 16:35:31 -0800178static void drop_capabilities() {
179 __user_cap_header_struct capheader;
180 memset(&capheader, 0, sizeof(capheader));
181 capheader.version = _LINUX_CAPABILITY_VERSION_3;
182 capheader.pid = 0;
183
184 __user_cap_data_struct capdata[2];
185 memset(&capdata, 0, sizeof(capdata));
186
187 if (capset(&capheader, &capdata[0]) == -1) {
188 PLOG(FATAL) << "failed to drop capabilities";
189 }
190
191 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
192 PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS";
193 }
194}
195
Josh Gaocbe70cb2016-10-18 18:17:52 -0700196int main(int argc, char** argv) {
197 pid_t target = getppid();
198 bool tombstoned_connected = false;
199 unique_fd tombstoned_socket;
200 unique_fd output_fd;
201
202 android::base::InitLogging(argv);
203 android::base::SetAborter([&](const char* abort_msg) {
204 abort_handler(target, tombstoned_connected, tombstoned_socket, output_fd, abort_msg);
205 });
206
Josh Gao57594112017-01-22 17:41:15 -0800207 // Don't try to dump ourselves.
208 struct sigaction action = {};
209 action.sa_handler = signal_handler;
210 debuggerd_register_handlers(&action);
211
Josh Gaoe7402502017-06-01 11:55:25 -0700212 sigset_t mask;
213 sigemptyset(&mask);
214 if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
215 PLOG(FATAL) << "failed to set signal mask";
216 }
217
Narayan Kamatha73df602017-05-24 15:07:25 +0100218 if (argc != 4) {
219 LOG(FATAL) << "Wrong number of args: " << argc << " (expected 4)";
Josh Gaocbe70cb2016-10-18 18:17:52 -0700220 }
221
222 pid_t main_tid;
Josh Gao2f11a252017-02-13 14:46:19 -0800223 pid_t pseudothread_tid;
Narayan Kamatha73df602017-05-24 15:07:25 +0100224 int dump_type;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700225
Josh Gaocbe70cb2016-10-18 18:17:52 -0700226 if (!android::base::ParseInt(argv[1], &main_tid, 1, std::numeric_limits<pid_t>::max())) {
227 LOG(FATAL) << "invalid main tid: " << argv[1];
228 }
229
Josh Gao2f11a252017-02-13 14:46:19 -0800230 if (!android::base::ParseInt(argv[2], &pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) {
Josh Gaof6ad5852017-02-15 12:21:11 -0800231 LOG(FATAL) << "invalid pseudothread tid: " << argv[2];
Josh Gao2f11a252017-02-13 14:46:19 -0800232 }
233
Narayan Kamatha73df602017-05-24 15:07:25 +0100234 if (!android::base::ParseInt(argv[3], &dump_type, 0, 1)) {
235 LOG(FATAL) << "invalid requested dump type: " << argv[3];
236 }
237
Josh Gaoc7fe0602017-03-13 14:13:29 -0700238 if (target == 1) {
239 LOG(FATAL) << "target died before we could attach (received main tid = " << main_tid << ")";
240 }
241
Josh Gaocbe70cb2016-10-18 18:17:52 -0700242 android::procinfo::ProcessInfo target_info;
243 if (!android::procinfo::GetProcessInfo(main_tid, &target_info)) {
244 LOG(FATAL) << "failed to fetch process info for target " << main_tid;
245 }
246
247 if (main_tid != target_info.tid || target != target_info.pid) {
248 LOG(FATAL) << "target info mismatch, expected pid " << target << ", tid " << main_tid
249 << ", received pid " << target_info.pid << ", tid " << target_info.tid;
250 }
251
252 // Open /proc/`getppid()` in the original process, and pass it down to the forked child.
253 std::string target_proc_path = "/proc/" + std::to_string(target);
254 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
255 if (target_proc_fd == -1) {
256 PLOG(FATAL) << "failed to open " << target_proc_path;
257 }
258
Josh Gao2a18b822017-02-16 19:17:28 -0800259 // Make sure our parent didn't die.
260 if (getppid() != target) {
261 PLOG(FATAL) << "parent died";
262 }
263
Josh Gaocbe70cb2016-10-18 18:17:52 -0700264 // Reparent ourselves to init, so that the signal handler can waitpid on the
265 // original process to avoid leaving a zombie for non-fatal dumps.
266 pid_t forkpid = fork();
267 if (forkpid == -1) {
268 PLOG(FATAL) << "fork failed";
269 } else if (forkpid != 0) {
270 exit(0);
271 }
272
Josh Gao7c6e3132017-01-22 17:59:02 -0800273 // Die if we take too long.
Josh Gaoe7402502017-06-01 11:55:25 -0700274 alarm(2);
Josh Gao7c6e3132017-01-22 17:59:02 -0800275
Josh Gao42fd74b2017-01-20 12:51:11 -0800276 std::string attach_error;
Josh Gao2f11a252017-02-13 14:46:19 -0800277
278 // Seize the main thread.
Josh Gaofe902762017-02-01 16:31:43 -0800279 if (!ptrace_seize_thread(target_proc_fd, main_tid, &attach_error)) {
Josh Gao42fd74b2017-01-20 12:51:11 -0800280 LOG(FATAL) << attach_error;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700281 }
282
Josh Gao2f11a252017-02-13 14:46:19 -0800283 // Seize the siblings.
Josh Gao57f58f82017-03-15 23:23:22 -0700284 std::map<pid_t, std::string> threads;
Josh Gao2f11a252017-02-13 14:46:19 -0800285 {
286 std::set<pid_t> siblings;
287 if (!android::procinfo::GetProcessTids(target, &siblings)) {
288 PLOG(FATAL) << "failed to get process siblings";
289 }
290
291 // but not the already attached main thread.
292 siblings.erase(main_tid);
293 // or the handler pseudothread.
294 siblings.erase(pseudothread_tid);
295
296 for (pid_t sibling_tid : siblings) {
297 if (!ptrace_seize_thread(target_proc_fd, sibling_tid, &attach_error)) {
298 LOG(WARNING) << attach_error;
299 } else {
Josh Gao57f58f82017-03-15 23:23:22 -0700300 threads.emplace(sibling_tid, get_thread_name(sibling_tid));
Josh Gao2f11a252017-02-13 14:46:19 -0800301 }
302 }
303 }
304
Josh Gao57f58f82017-03-15 23:23:22 -0700305 // Collect the backtrace map, open files, and process/thread names, while we still have caps.
Josh Gao2f11a252017-02-13 14:46:19 -0800306 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(main_tid));
307 if (!backtrace_map) {
308 LOG(FATAL) << "failed to create backtrace map";
309 }
310
311 // Collect the list of open files.
312 OpenFilesList open_files;
313 populate_open_files_list(target, &open_files);
314
Josh Gao57f58f82017-03-15 23:23:22 -0700315 std::string process_name = get_process_name(main_tid);
316 threads.emplace(main_tid, get_thread_name(main_tid));
317
Josh Gao2f11a252017-02-13 14:46:19 -0800318 // Drop our capabilities now that we've attached to the threads we care about.
319 drop_capabilities();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700320
Narayan Kamatha73df602017-05-24 15:07:25 +0100321 const DebuggerdDumpType dump_type_enum = static_cast<DebuggerdDumpType>(dump_type);
322 LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type_enum;
323 tombstoned_connected = tombstoned_connect(target, &tombstoned_socket, &output_fd, dump_type_enum);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700324
325 // Write a '\1' to stdout to tell the crashing process to resume.
Josh Gao2f11a252017-02-13 14:46:19 -0800326 // It also restores the value of PR_SET_DUMPABLE at this point.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700327 if (TEMP_FAILURE_RETRY(write(STDOUT_FILENO, "\1", 1)) == -1) {
328 PLOG(ERROR) << "failed to communicate to target process";
329 }
330
331 if (tombstoned_connected) {
332 if (TEMP_FAILURE_RETRY(dup2(output_fd.get(), STDOUT_FILENO)) == -1) {
333 PLOG(ERROR) << "failed to dup2 output fd (" << output_fd.get() << ") to STDOUT_FILENO";
334 }
335 } else {
336 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
337 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao0a379012017-01-24 15:20:42 -0800338 output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700339 }
340
Josh Gaocbe70cb2016-10-18 18:17:52 -0700341 LOG(INFO) << "performing dump of process " << target << " (target tid = " << main_tid << ")";
342
Josh Gao122479f2017-01-22 16:42:32 -0800343 // At this point, the thread that made the request has been attached and is
344 // in ptrace-stopped state. After resumption, the triggering signal that has
345 // been queued will be delivered.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700346 if (ptrace(PTRACE_CONT, main_tid, 0, 0) != 0) {
347 PLOG(ERROR) << "PTRACE_CONT(" << main_tid << ") failed";
348 exit(1);
349 }
350
351 siginfo_t siginfo = {};
352 if (!wait_for_signal(main_tid, &siginfo)) {
353 printf("failed to wait for signal in tid %d: %s\n", main_tid, strerror(errno));
354 exit(1);
355 }
356
357 int signo = siginfo.si_signo;
Josh Gaofe902762017-02-01 16:31:43 -0800358 bool fatal_signal = signo != DEBUGGER_SIGNAL;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700359 bool backtrace = false;
360 uintptr_t abort_address = 0;
361
362 // si_value can represent three things:
363 // 0: dump tombstone
364 // 1: dump backtrace
365 // everything else: abort message address (implies dump tombstone)
366 if (siginfo.si_value.sival_int == 1) {
367 backtrace = true;
368 } else if (siginfo.si_value.sival_ptr != nullptr) {
369 abort_address = reinterpret_cast<uintptr_t>(siginfo.si_value.sival_ptr);
370 }
371
Josh Gaocbe70cb2016-10-18 18:17:52 -0700372 // TODO: Use seccomp to lock ourselves down.
373
Josh Gaocbe70cb2016-10-18 18:17:52 -0700374 std::string amfd_data;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700375 if (backtrace) {
Josh Gao57f58f82017-03-15 23:23:22 -0700376 dump_backtrace(output_fd.get(), backtrace_map.get(), target, main_tid, process_name, threads, 0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700377 } else {
Josh Gaoe73c9322017-02-08 16:06:26 -0800378 engrave_tombstone(output_fd.get(), backtrace_map.get(), &open_files, target, main_tid,
Josh Gao57f58f82017-03-15 23:23:22 -0700379 process_name, threads, abort_address, fatal_signal ? &amfd_data : nullptr);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700380 }
381
Josh Gao7c6e3132017-01-22 17:59:02 -0800382 // We don't actually need to PTRACE_DETACH, as long as our tracees aren't in
383 // group-stop state, which is true as long as no stopping signals are sent.
384
Josh Gaocbe70cb2016-10-18 18:17:52 -0700385 bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
Josh Gao7c6e3132017-01-22 17:59:02 -0800386 if (!fatal_signal || siginfo.si_code == SI_USER) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700387 // Don't wait_for_gdb when the process didn't actually crash.
Josh Gao7c6e3132017-01-22 17:59:02 -0800388 wait_for_gdb = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700389 }
390
Josh Gao7c6e3132017-01-22 17:59:02 -0800391 // If the process crashed or we need to send it SIGSTOP for wait_for_gdb,
392 // get it in a state where it can receive signals, and then send the relevant
393 // signal.
394 if (wait_for_gdb || fatal_signal) {
395 if (ptrace(PTRACE_INTERRUPT, main_tid, 0, 0) != 0) {
396 PLOG(ERROR) << "failed to use PTRACE_INTERRUPT on " << main_tid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700397 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700398
Josh Gao7c6e3132017-01-22 17:59:02 -0800399 if (tgkill(target, main_tid, wait_for_gdb ? SIGSTOP : signo) != 0) {
400 PLOG(ERROR) << "failed to resend signal " << signo << " to " << main_tid;
401 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700402 }
403
404 if (wait_for_gdb) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800405 // Use ALOGI to line up with output from engrave_tombstone.
406 ALOGI(
407 "***********************************************************\n"
408 "* Process %d has been suspended while crashing.\n"
409 "* To attach gdbserver and start gdb, run this on the host:\n"
410 "*\n"
411 "* gdbclient.py -p %d\n"
412 "*\n"
413 "***********************************************************",
414 target, main_tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700415 }
416
417 if (fatal_signal) {
Josh Gaob0e51e32017-06-01 12:08:10 -0700418 // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
419 if (target_info.name != "system_server" || target_info.uid != AID_SYSTEM) {
420 activity_manager_notify(target, signo, amfd_data);
421 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700422 }
423
424 // Close stdout before we notify tombstoned of completion.
425 close(STDOUT_FILENO);
Josh Gao0a379012017-01-24 15:20:42 -0800426 if (tombstoned_connected && !tombstoned_notify_completion(tombstoned_socket.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700427 LOG(ERROR) << "failed to notify tombstoned of completion";
428 }
429
430 return 0;
431}