blob: 88342099bef6814bccc8e809eae23cafce2dd3bf [file] [log] [blame]
Christopher Ferris20303f82014-01-10 16:33:16 -08001/*
2 * Copyright 2006, 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 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016
Christopher Ferris9818bd22016-05-03 16:32:13 -070017#include <arpa/inet.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#include <dirent.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070019#include <elf.h>
Josh Gao7c89f9e2016-01-13 17:57:14 -080020#include <errno.h>
21#include <fcntl.h>
22#include <pthread.h>
23#include <signal.h>
24#include <stdarg.h>
25#include <stdio.h>
Jeff Brown9524e412011-10-24 11:10:16 -070026#include <sys/poll.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080027#include <sys/prctl.h>
28#include <sys/ptrace.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070029#include <sys/socket.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080030#include <sys/stat.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070031#include <sys/types.h>
Josh Gaoe7a9e522015-11-17 13:57:03 -080032#include <sys/wait.h>
Christopher Ferris0fc89f32016-04-19 15:53:13 -070033#include <sys/un.h>
Josh Gaof5e8f0b2016-03-16 18:09:15 -070034#include <time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Christopher Ferris9818bd22016-05-03 16:32:13 -070036#include <memory>
Josh Gao7c89f9e2016-01-13 17:57:14 -080037#include <set>
Christopher Ferris9818bd22016-05-03 16:32:13 -070038#include <string>
Josh Gao7c89f9e2016-01-13 17:57:14 -080039
Stephen Smalley69b80032014-07-24 15:23:05 -040040#include <selinux/android.h>
41
Colin Cross9227bd32013-07-23 16:59:20 -070042#include <log/logger.h>
43
Christopher Ferris9818bd22016-05-03 16:32:13 -070044#include <android-base/file.h>
Elliott Hughesae389232016-03-22 20:03:13 -070045#include <android-base/unique_fd.h>
Jeff Brown053b8652012-06-06 16:25:03 -070046#include <cutils/debugger.h>
Josh Gao8ab7fd42015-11-16 17:26:33 -080047#include <cutils/properties.h>
48#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
50#include <linux/input.h>
51
52#include <private/android_filesystem_config.h>
53
Josh Gaoa04c8022016-08-11 12:50:32 -070054#include <debuggerd/client.h>
55
Jeff Brown053b8652012-06-06 16:25:03 -070056#include "backtrace.h"
Jeff Brown13e715b2011-10-21 12:14:56 -070057#include "getevent.h"
Josh Gaof5e8f0b2016-03-16 18:09:15 -070058#include "signal_sender.h"
Jeff Brown053b8652012-06-06 16:25:03 -070059#include "tombstone.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060#include "utility.h"
61
Christopher Ferris9774df62015-01-15 14:47:36 -080062// If the 32 bit executable is compiled on a 64 bit system,
63// use the 32 bit socket name.
64#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
65#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
66#else
67#define SOCKET_NAME DEBUGGER_SOCKET_NAME
68#endif
69
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080070struct debugger_request_t {
Christopher Ferris20303f82014-01-10 16:33:16 -080071 debugger_action_t action;
72 pid_t pid, tid;
73 uid_t uid, gid;
74 uintptr_t abort_msg_address;
Elliott Hughes0df8e4f2014-02-07 12:13:30 -080075};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Elliott Hughes39a28c22015-07-07 14:34:39 -070077static void wait_for_user_action(const debugger_request_t& request) {
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070078 // Explain how to attach the debugger.
Elliott Hughes39a28c22015-07-07 14:34:39 -070079 ALOGI("***********************************************************\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070080 "* Process %d has been suspended while crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070081 "* To attach gdbserver and start gdb, run this on the host:\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070082 "*\n"
Josh Gaoc362c452016-01-14 15:51:06 -080083 "* gdbclient.py -p %d\n"
Brigid Smith50eb5462014-06-18 14:17:57 -070084 "*\n"
85 "* Wait for gdb to start, then press the VOLUME DOWN key\n"
86 "* to let the process continue crashing.\n"
Elliott Hughes39a28c22015-07-07 14:34:39 -070087 "***********************************************************",
88 request.pid, request.tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070090 // Wait for VOLUME DOWN.
Josh Gaoc362c452016-01-14 15:51:06 -080091 while (true) {
92 input_event e;
93 if (get_event(&e, -1) == 0) {
94 if (e.type == EV_KEY && e.code == KEY_VOLUMEDOWN && e.value == 0) {
95 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080096 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070097 }
Christopher Ferris20303f82014-01-10 16:33:16 -080098 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
Brigid Smith75582952014-06-26 13:22:48 -0700100 ALOGI("debuggerd resuming process %d", request.pid);
Jeff Brown9524e412011-10-24 11:10:16 -0700101}
Ben Cheng09e71372009-09-28 11:06:09 -0700102
Jeff Brown9524e412011-10-24 11:10:16 -0700103static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 char path[64];
105 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106
Christopher Ferris20303f82014-01-10 16:33:16 -0800107 FILE* fp = fopen(path, "r");
108 if (!fp) {
109 return -1;
110 }
Jeff Brown9524e412011-10-24 11:10:16 -0700111
Christopher Ferris20303f82014-01-10 16:33:16 -0800112 int fields = 0;
113 char line[1024];
114 while (fgets(line, sizeof(line), fp)) {
115 size_t len = strlen(line);
116 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
117 *out_pid = atoi(line + 6);
118 fields |= 1;
119 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
120 *out_uid = atoi(line + 5);
121 fields |= 2;
122 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
123 *out_gid = atoi(line + 5);
124 fields |= 4;
Jeff Brown9524e412011-10-24 11:10:16 -0700125 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800126 }
127 fclose(fp);
128 return fields == 7 ? 0 : -1;
Jeff Brown9524e412011-10-24 11:10:16 -0700129}
130
Stephen Smalley69b80032014-07-24 15:23:05 -0400131/*
132 * Corresponds with debugger_action_t enum type in
133 * include/cutils/debugger.h.
134 */
135static const char *debuggerd_perms[] = {
136 NULL, /* crash is only used on self, no check applied */
137 "dump_tombstone",
138 "dump_backtrace"
139};
140
William Roberts46857392015-10-06 12:03:01 -0700141static int audit_callback(void* data, security_class_t /* cls */, char* buf, size_t len)
142{
143 struct debugger_request_t* req = reinterpret_cast<debugger_request_t*>(data);
144
145 if (!req) {
146 ALOGE("No debuggerd request audit data");
147 return 0;
148 }
149
150 snprintf(buf, len, "pid=%d uid=%d gid=%d", req->pid, req->uid, req->gid);
151 return 0;
152}
153
154static bool selinux_action_allowed(int s, debugger_request_t* request)
Stephen Smalley69b80032014-07-24 15:23:05 -0400155{
156 char *scon = NULL, *tcon = NULL;
157 const char *tclass = "debuggerd";
158 const char *perm;
159 bool allowed = false;
160
William Roberts46857392015-10-06 12:03:01 -0700161 if (request->action <= 0 || request->action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
162 ALOGE("SELinux: No permission defined for debugger action %d", request->action);
Stephen Smalley69b80032014-07-24 15:23:05 -0400163 return false;
164 }
165
William Roberts46857392015-10-06 12:03:01 -0700166 perm = debuggerd_perms[request->action];
Stephen Smalley69b80032014-07-24 15:23:05 -0400167
168 if (getpeercon(s, &scon) < 0) {
169 ALOGE("Cannot get peer context from socket\n");
170 goto out;
171 }
172
William Roberts46857392015-10-06 12:03:01 -0700173 if (getpidcon(request->tid, &tcon) < 0) {
174 ALOGE("Cannot get context for tid %d\n", request->tid);
Stephen Smalley69b80032014-07-24 15:23:05 -0400175 goto out;
176 }
177
William Roberts46857392015-10-06 12:03:01 -0700178 allowed = (selinux_check_access(scon, tcon, tclass, perm, reinterpret_cast<void*>(request)) == 0);
Stephen Smalley69b80032014-07-24 15:23:05 -0400179
180out:
181 freecon(scon);
182 freecon(tcon);
183 return allowed;
184}
185
Jeff Brown053b8652012-06-06 16:25:03 -0700186static int read_request(int fd, debugger_request_t* out_request) {
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800187 ucred cr;
188 socklen_t len = sizeof(cr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800189 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
190 if (status != 0) {
Christopher Ferris1072f912014-10-31 21:34:38 -0700191 ALOGE("cannot get credentials");
Christopher Ferris20303f82014-01-10 16:33:16 -0800192 return -1;
193 }
194
Christopher Ferris1072f912014-10-31 21:34:38 -0700195 ALOGV("reading tid");
Elliott Hughes0df8e4f2014-02-07 12:13:30 -0800196 pollfd pollfds[1];
Christopher Ferris20303f82014-01-10 16:33:16 -0800197 pollfds[0].fd = fd;
198 pollfds[0].events = POLLIN;
199 pollfds[0].revents = 0;
200 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
201 if (status != 1) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700202 ALOGE("timed out reading tid (from pid=%d uid=%d)\n", cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800203 return -1;
204 }
205
206 debugger_msg_t msg;
207 memset(&msg, 0, sizeof(msg));
208 status = TEMP_FAILURE_RETRY(read(fd, &msg, sizeof(msg)));
209 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700210 ALOGE("read failure? %s (pid=%d uid=%d)\n", strerror(errno), cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800211 return -1;
212 }
Elliott Hughese901c1b2014-06-19 11:46:27 -0700213 if (status != sizeof(debugger_msg_t)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700214 ALOGE("invalid crash request of size %d (from pid=%d uid=%d)\n", status, cr.pid, cr.uid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800215 return -1;
216 }
217
Christopher Ferris9774df62015-01-15 14:47:36 -0800218 out_request->action = static_cast<debugger_action_t>(msg.action);
Christopher Ferris20303f82014-01-10 16:33:16 -0800219 out_request->tid = msg.tid;
220 out_request->pid = cr.pid;
221 out_request->uid = cr.uid;
222 out_request->gid = cr.gid;
223 out_request->abort_msg_address = msg.abort_msg_address;
224
225 if (msg.action == DEBUGGER_ACTION_CRASH) {
226 // Ensure that the tid reported by the crashing process is valid.
227 char buf[64];
228 struct stat s;
229 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
230 if (stat(buf, &s)) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700231 ALOGE("tid %d does not exist in pid %d. ignoring debug request\n",
Christopher Ferris20303f82014-01-10 16:33:16 -0800232 out_request->tid, out_request->pid);
233 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800235 } else if (cr.uid == 0
Jeff Brown053b8652012-06-06 16:25:03 -0700236 || (cr.uid == AID_SYSTEM && msg.action == DEBUGGER_ACTION_DUMP_BACKTRACE)) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800237 // Only root or system can ask us to attach to any process and dump it explicitly.
238 // However, system is only allowed to collect backtraces but cannot dump tombstones.
239 status = get_process_info(out_request->tid, &out_request->pid,
240 &out_request->uid, &out_request->gid);
241 if (status < 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700242 ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800243 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400245
William Roberts46857392015-10-06 12:03:01 -0700246 if (!selinux_action_allowed(fd, out_request))
Stephen Smalley69b80032014-07-24 15:23:05 -0400247 return -1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800248 } else {
249 // No one else is allowed to dump arbitrary processes.
250 return -1;
251 }
252 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253}
254
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700255static int activity_manager_connect() {
256 android::base::unique_fd amfd(socket(PF_UNIX, SOCK_STREAM, 0));
257 if (amfd.get() < -1) {
258 ALOGE("debuggerd: Unable to connect to activity manager (socket failed: %s)", strerror(errno));
259 return -1;
260 }
261
262 struct sockaddr_un address;
263 memset(&address, 0, sizeof(address));
264 address.sun_family = AF_UNIX;
265 // The path used here must match the value defined in NativeCrashListener.java.
266 strncpy(address.sun_path, "/data/system/ndebugsocket", sizeof(address.sun_path));
267 if (TEMP_FAILURE_RETRY(connect(amfd.get(), reinterpret_cast<struct sockaddr*>(&address),
268 sizeof(address))) == -1) {
269 ALOGE("debuggerd: Unable to connect to activity manager (connect failed: %s)", strerror(errno));
270 return -1;
271 }
272
273 struct timeval tv;
274 memset(&tv, 0, sizeof(tv));
275 tv.tv_sec = 1; // tight leash
276 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
277 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_SNDTIMEO failed: %s)",
278 strerror(errno));
279 return -1;
280 }
281
282 tv.tv_sec = 3; // 3 seconds on handshake read
283 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
284 ALOGE("debuggerd: Unable to connect to activity manager (setsockopt SO_RCVTIMEO failed: %s)",
285 strerror(errno));
286 return -1;
287 }
288
289 return amfd.release();
290}
291
Christopher Ferris9818bd22016-05-03 16:32:13 -0700292static void activity_manager_write(int pid, int signal, int amfd, const std::string& amfd_data) {
293 if (amfd == -1) {
294 return;
295 }
296
297 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
298 // pid and signal number, followed by the raw text of the dump, culminating
299 // in a zero byte that marks end-of-data.
300 uint32_t datum = htonl(pid);
301 if (!android::base::WriteFully(amfd, &datum, 4)) {
302 ALOGE("AM pid write failed: %s\n", strerror(errno));
303 return;
304 }
305 datum = htonl(signal);
306 if (!android::base::WriteFully(amfd, &datum, 4)) {
307 ALOGE("AM signal write failed: %s\n", strerror(errno));
308 return;
309 }
310
311 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size())) {
312 ALOGE("AM data write failed: %s\n", strerror(errno));
313 return;
314 }
315
316 // Send EOD to the Activity Manager, then wait for its ack to avoid racing
317 // ahead and killing the target out from under it.
318 uint8_t eodMarker = 0;
319 if (!android::base::WriteFully(amfd, &eodMarker, 1)) {
320 ALOGE("AM eod write failed: %s\n", strerror(errno));
321 return;
322 }
323 // 3 sec timeout reading the ack; we're fine if the read fails.
324 android::base::ReadFully(amfd, &eodMarker, 1);
325}
326
Josh Gao676a7562016-03-17 15:14:43 -0700327static bool should_attach_gdb(const debugger_request_t& request) {
328 if (request.action == DEBUGGER_ACTION_CRASH) {
Christopher Ferrisd79f2be2015-07-01 15:42:05 -0700329 return property_get_bool("debug.debuggerd.wait_for_gdb", false);
Christopher Ferris20303f82014-01-10 16:33:16 -0800330 }
331 return false;
Jeff Brown9524e412011-10-24 11:10:16 -0700332}
Bruce Beare84924902010-10-13 14:21:30 -0700333
Christopher Ferris9774df62015-01-15 14:47:36 -0800334#if defined(__LP64__)
335static bool is32bit(pid_t tid) {
336 char* exeline;
337 if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
338 return false;
339 }
340 int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
341 int saved_errno = errno;
342 free(exeline);
343 if (fd == -1) {
344 ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
345 return false;
346 }
347
348 char ehdr[EI_NIDENT];
349 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
Elliott Hughes47b01342015-05-15 19:16:40 -0700350 close(fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800351 if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
352 return false;
353 }
354 if (ehdr[EI_CLASS] == ELFCLASS32) {
355 return true;
356 }
357 return false;
358}
359
360static void redirect_to_32(int fd, debugger_request_t* request) {
361 debugger_msg_t msg;
362 memset(&msg, 0, sizeof(msg));
363 msg.tid = request->tid;
364 msg.action = request->action;
365
366 int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
367 SOCK_STREAM | SOCK_CLOEXEC);
368 if (sock_fd < 0) {
369 ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
370 return;
371 }
372
373 if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
374 ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700375 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800376 return;
377 }
378
379 char ack;
380 if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
381 ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
Elliott Hughes47b01342015-05-15 19:16:40 -0700382 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800383 return;
384 }
385
386 char buffer[1024];
387 ssize_t bytes_read;
388 while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
389 ssize_t bytes_to_send = bytes_read;
390 ssize_t bytes_written;
391 do {
392 bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
393 bytes_to_send));
394 if (bytes_written == -1) {
395 if (errno == EAGAIN) {
396 // Retry the write.
397 continue;
398 }
399 ALOGE("Error while writing data to fd: %s", strerror(errno));
400 break;
401 }
402 bytes_to_send -= bytes_written;
403 } while (bytes_written != 0 && bytes_to_send > 0);
404 if (bytes_to_send != 0) {
405 ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
406 break;
407 }
408 }
Elliott Hughes47b01342015-05-15 19:16:40 -0700409 close(sock_fd);
Christopher Ferris9774df62015-01-15 14:47:36 -0800410}
411#endif
412
Josh Gao7c89f9e2016-01-13 17:57:14 -0800413static void ptrace_siblings(pid_t pid, pid_t main_tid, std::set<pid_t>& tids) {
414 char task_path[64];
415
416 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
417
418 std::unique_ptr<DIR, int (*)(DIR*)> d(opendir(task_path), closedir);
419
420 // Bail early if the task directory cannot be opened.
421 if (!d) {
422 ALOGE("debuggerd: failed to open /proc/%d/task: %s", pid, strerror(errno));
423 return;
424 }
425
426 struct dirent* de;
427 while ((de = readdir(d.get())) != NULL) {
428 // Ignore "." and "..".
429 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
430 continue;
431 }
432
433 char* end;
434 pid_t tid = strtoul(de->d_name, &end, 10);
435 if (*end) {
436 continue;
437 }
438
439 if (tid == main_tid) {
440 continue;
441 }
442
443 if (ptrace(PTRACE_ATTACH, tid, 0, 0) < 0) {
444 ALOGE("debuggerd: ptrace attach to %d failed: %s", tid, strerror(errno));
445 continue;
446 }
447
448 tids.insert(tid);
449 }
450}
451
452static bool perform_dump(const debugger_request_t& request, int fd, int tombstone_fd,
Josh Gao561497c2016-03-16 13:39:38 -0700453 BacktraceMap* backtrace_map, const std::set<pid_t>& siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700454 int* crash_signal, std::string* amfd_data) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800455 if (TEMP_FAILURE_RETRY(write(fd, "\0", 1)) != 1) {
456 ALOGE("debuggerd: failed to respond to client: %s\n", strerror(errno));
457 return false;
458 }
459
Josh Gao7c89f9e2016-01-13 17:57:14 -0800460 while (true) {
Josh Gaof5a960a2016-08-10 17:57:01 -0700461 // wait_for_signal waits for forever, but the watchdog process will kill us
462 // if it takes too long.
463 int signal = wait_for_signal(request.tid);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800464 switch (signal) {
465 case -1:
466 ALOGE("debuggerd: timed out waiting for signal");
467 return false;
468
469 case SIGSTOP:
470 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
471 ALOGV("debuggerd: stopped -- dumping to tombstone");
Josh Gaoa04c8022016-08-11 12:50:32 -0700472 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings,
473 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800474 } else if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE) {
475 ALOGV("debuggerd: stopped -- dumping to fd");
Christopher Ferris9818bd22016-05-03 16:32:13 -0700476 dump_backtrace(fd, backtrace_map, request.pid, request.tid, siblings, nullptr);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800477 } else {
478 ALOGV("debuggerd: stopped -- continuing");
479 if (ptrace(PTRACE_CONT, request.tid, 0, 0) != 0) {
480 ALOGE("debuggerd: ptrace continue failed: %s", strerror(errno));
481 return false;
482 }
483 continue; // loop again
484 }
485 break;
486
487 case SIGABRT:
488 case SIGBUS:
489 case SIGFPE:
490 case SIGILL:
491 case SIGSEGV:
492#ifdef SIGSTKFLT
493 case SIGSTKFLT:
494#endif
Josh Gaodfa163d2016-03-25 13:22:05 -0700495 case SIGSYS:
Josh Gao7c89f9e2016-01-13 17:57:14 -0800496 case SIGTRAP:
497 ALOGV("stopped -- fatal signal\n");
Josh Gao561497c2016-03-16 13:39:38 -0700498 *crash_signal = signal;
Josh Gaoa04c8022016-08-11 12:50:32 -0700499 engrave_tombstone(tombstone_fd, backtrace_map, request.pid, request.tid, siblings,
500 request.abort_msg_address, amfd_data);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800501 break;
502
503 default:
504 ALOGE("debuggerd: process stopped due to unexpected signal %d\n", signal);
505 break;
506 }
507 break;
508 }
509
510 return true;
511}
512
513static bool drop_privileges() {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700514 // AID_LOG: for reading the logs data associated with the crashing process.
515 // AID_READPROC: for reading /proc/<PID>/{comm,cmdline}.
516 gid_t groups[] = { AID_DEBUGGERD, AID_LOG, AID_READPROC };
517 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
518 ALOGE("debuggerd: failed to setgroups: %s", strerror(errno));
519 return false;
520 }
521
Josh Gao7c89f9e2016-01-13 17:57:14 -0800522 if (setresgid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700523 ALOGE("debuggerd: failed to setresgid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800524 return false;
525 }
526
527 if (setresuid(AID_DEBUGGERD, AID_DEBUGGERD, AID_DEBUGGERD) != 0) {
Christopher Ferrisedc23802016-05-05 11:13:50 -0700528 ALOGE("debuggerd: failed to setresuid: %s", strerror(errno));
Josh Gao7c89f9e2016-01-13 17:57:14 -0800529 return false;
530 }
531
532 return true;
533}
534
Josh Gao630bc802016-03-16 20:19:44 -0700535static void worker_process(int fd, debugger_request_t& request) {
Josh Gaoe7a9e522015-11-17 13:57:03 -0800536 // Open the tombstone file if we need it.
537 std::string tombstone_path;
538 int tombstone_fd = -1;
539 switch (request.action) {
540 case DEBUGGER_ACTION_DUMP_TOMBSTONE:
541 case DEBUGGER_ACTION_CRASH:
542 tombstone_fd = open_tombstone(&tombstone_path);
543 if (tombstone_fd == -1) {
544 ALOGE("debuggerd: failed to open tombstone file: %s\n", strerror(errno));
545 exit(1);
546 }
547 break;
548
549 case DEBUGGER_ACTION_DUMP_BACKTRACE:
550 break;
551
552 default:
553 ALOGE("debuggerd: unexpected request action: %d", request.action);
554 exit(1);
555 }
556
Josh Gao8ab7fd42015-11-16 17:26:33 -0800557 // At this point, the thread that made the request is blocked in
558 // a read() call. If the thread has crashed, then this gives us
559 // time to PTRACE_ATTACH to it before it has a chance to really fault.
560 //
561 // The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
562 // won't necessarily have stopped by the time ptrace() returns. (We
563 // currently assume it does.) We write to the file descriptor to
564 // ensure that it can run as soon as we call PTRACE_CONT below.
Josh Gao9c02dc52016-06-15 17:29:00 -0700565 // See details in client/debuggerd_client.cpp, in function
Josh Gao8ab7fd42015-11-16 17:26:33 -0800566 // debugger_signal_handler().
Josh Gao7c89f9e2016-01-13 17:57:14 -0800567
568 // Attach to the target process.
569 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0) != 0) {
570 ALOGE("debuggerd: ptrace attach failed: %s", strerror(errno));
Josh Gaoe7a9e522015-11-17 13:57:03 -0800571 exit(1);
572 }
573
Josh Gao7c89f9e2016-01-13 17:57:14 -0800574 // Don't attach to the sibling threads if we want to attach gdb.
575 // Supposedly, it makes the process less reliable.
Josh Gao676a7562016-03-17 15:14:43 -0700576 bool attach_gdb = should_attach_gdb(request);
Josh Gaoc362c452016-01-14 15:51:06 -0800577 if (attach_gdb) {
578 // Open all of the input devices we need to listen for VOLUMEDOWN before dropping privileges.
579 if (init_getevent() != 0) {
580 ALOGE("debuggerd: failed to initialize input device, not waiting for gdb");
581 attach_gdb = false;
582 }
583
Josh Gaoc362c452016-01-14 15:51:06 -0800584 }
585
Josh Gao7c89f9e2016-01-13 17:57:14 -0800586 std::set<pid_t> siblings;
587 if (!attach_gdb) {
588 ptrace_siblings(request.pid, request.tid, siblings);
589 }
590
Josh Gaoe7a9e522015-11-17 13:57:03 -0800591 // Generate the backtrace map before dropping privileges.
592 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(request.pid));
593
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700594 int amfd = -1;
Christopher Ferris9818bd22016-05-03 16:32:13 -0700595 std::unique_ptr<std::string> amfd_data;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700596 if (request.action == DEBUGGER_ACTION_CRASH) {
597 // Connect to the activity manager before dropping privileges.
598 amfd = activity_manager_connect();
Christopher Ferris9818bd22016-05-03 16:32:13 -0700599 amfd_data.reset(new std::string);
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700600 }
601
Josh Gao7c89f9e2016-01-13 17:57:14 -0800602 bool succeeded = false;
603
Josh Gaoe7a9e522015-11-17 13:57:03 -0800604 // Now that we've done everything that requires privileges, we can drop them.
Josh Gaof0c87232016-03-08 15:56:33 -0800605 if (!drop_privileges()) {
606 ALOGE("debuggerd: failed to drop privileges, exiting");
607 _exit(1);
608 }
609
Josh Gao561497c2016-03-16 13:39:38 -0700610 int crash_signal = SIGKILL;
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700611 succeeded = perform_dump(request, fd, tombstone_fd, backtrace_map.get(), siblings,
Christopher Ferris9818bd22016-05-03 16:32:13 -0700612 &crash_signal, amfd_data.get());
Josh Gaof0c87232016-03-08 15:56:33 -0800613 if (succeeded) {
614 if (request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
615 if (!tombstone_path.empty()) {
Christopher Ferris9818bd22016-05-03 16:32:13 -0700616 android::base::WriteFully(fd, tombstone_path.c_str(), tombstone_path.length());
Josh Gao7c89f9e2016-01-13 17:57:14 -0800617 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800618 }
Josh Gaof0c87232016-03-08 15:56:33 -0800619 }
Josh Gao8ab7fd42015-11-16 17:26:33 -0800620
Josh Gaof0c87232016-03-08 15:56:33 -0800621 if (attach_gdb) {
622 // Tell the signal process to send SIGSTOP to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700623 if (!send_signal(request.pid, 0, SIGSTOP)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800624 ALOGE("debuggerd: failed to stop process for gdb attach: %s", strerror(errno));
625 attach_gdb = false;
Josh Gao8ab7fd42015-11-16 17:26:33 -0800626 }
627 }
628
Christopher Ferris9818bd22016-05-03 16:32:13 -0700629 if (!attach_gdb) {
630 // Tell the Activity Manager about the crashing process. If we are
631 // waiting for gdb to attach, do not send this or Activity Manager
632 // might kill the process before anyone can attach.
633 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
634 }
635
Josh Gao7c89f9e2016-01-13 17:57:14 -0800636 if (ptrace(PTRACE_DETACH, request.tid, 0, 0) != 0) {
637 ALOGE("debuggerd: ptrace detach from %d failed: %s", request.tid, strerror(errno));
638 }
639
640 for (pid_t sibling : siblings) {
641 ptrace(PTRACE_DETACH, sibling, 0, 0);
642 }
643
Josh Gaof0c87232016-03-08 15:56:33 -0800644 // Send the signal back to the process if it crashed and we're not waiting for gdb.
645 if (!attach_gdb && request.action == DEBUGGER_ACTION_CRASH) {
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700646 if (!send_signal(request.pid, request.tid, crash_signal)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800647 ALOGE("debuggerd: failed to kill process %d: %s", request.pid, strerror(errno));
648 }
649 }
650
Josh Gaoc362c452016-01-14 15:51:06 -0800651 // Wait for gdb, if requested.
Christopher Ferris9818bd22016-05-03 16:32:13 -0700652 if (attach_gdb) {
Josh Gao7c89f9e2016-01-13 17:57:14 -0800653 wait_for_user_action(request);
Josh Gao7c89f9e2016-01-13 17:57:14 -0800654
Christopher Ferris9818bd22016-05-03 16:32:13 -0700655 // Now tell the activity manager about this process.
656 activity_manager_write(request.pid, crash_signal, amfd, *amfd_data.get());
657
Josh Gaoc362c452016-01-14 15:51:06 -0800658 // Tell the signal process to send SIGCONT to the target.
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700659 if (!send_signal(request.pid, 0, SIGCONT)) {
Josh Gaof0c87232016-03-08 15:56:33 -0800660 ALOGE("debuggerd: failed to resume process %d: %s", request.pid, strerror(errno));
661 }
Josh Gaoc362c452016-01-14 15:51:06 -0800662
663 uninit_getevent();
Josh Gaoc362c452016-01-14 15:51:06 -0800664 }
Josh Gao7c89f9e2016-01-13 17:57:14 -0800665
Christopher Ferris0fc89f32016-04-19 15:53:13 -0700666 close(amfd);
667
Josh Gao7c89f9e2016-01-13 17:57:14 -0800668 exit(!succeeded);
Jeff Brown9524e412011-10-24 11:10:16 -0700669}
670
Josh Gao630bc802016-03-16 20:19:44 -0700671static void monitor_worker_process(int child_pid, const debugger_request_t& request) {
672 struct timespec timeout = {.tv_sec = 10, .tv_nsec = 0 };
Josh Gao676a7562016-03-17 15:14:43 -0700673 if (should_attach_gdb(request)) {
674 // If wait_for_gdb is enabled, set the timeout to something large.
675 timeout.tv_sec = INT_MAX;
676 }
Josh Gao630bc802016-03-16 20:19:44 -0700677
678 sigset_t signal_set;
679 sigemptyset(&signal_set);
680 sigaddset(&signal_set, SIGCHLD);
681
682 bool kill_worker = false;
683 bool kill_target = false;
684 bool kill_self = false;
685
686 int status;
687 siginfo_t siginfo;
688 int signal = TEMP_FAILURE_RETRY(sigtimedwait(&signal_set, &siginfo, &timeout));
689 if (signal == SIGCHLD) {
Josh Gao28080052016-03-23 14:02:52 -0700690 pid_t rc = waitpid(-1, &status, WNOHANG | WUNTRACED);
Josh Gao630bc802016-03-16 20:19:44 -0700691 if (rc != child_pid) {
692 ALOGE("debuggerd: waitpid returned unexpected pid (%d), committing murder-suicide", rc);
Josh Gao28080052016-03-23 14:02:52 -0700693
694 if (WIFEXITED(status)) {
695 ALOGW("debuggerd: pid %d exited with status %d", rc, WEXITSTATUS(status));
696 } else if (WIFSIGNALED(status)) {
697 ALOGW("debuggerd: pid %d received signal %d", rc, WTERMSIG(status));
698 } else if (WIFSTOPPED(status)) {
699 ALOGW("debuggerd: pid %d stopped by signal %d", rc, WSTOPSIG(status));
700 } else if (WIFCONTINUED(status)) {
701 ALOGW("debuggerd: pid %d continued", rc);
702 }
703
Josh Gao630bc802016-03-16 20:19:44 -0700704 kill_worker = true;
705 kill_target = true;
706 kill_self = true;
Josh Gao24464182016-03-22 16:37:45 -0700707 } else if (WIFSIGNALED(status)) {
Josh Gao630bc802016-03-16 20:19:44 -0700708 ALOGE("debuggerd: worker process %d terminated due to signal %d", child_pid, WTERMSIG(status));
709 kill_worker = false;
710 kill_target = true;
711 } else if (WIFSTOPPED(status)) {
712 ALOGE("debuggerd: worker process %d stopped due to signal %d", child_pid, WSTOPSIG(status));
713 kill_worker = true;
714 kill_target = true;
715 }
716 } else {
717 ALOGE("debuggerd: worker process %d timed out", child_pid);
718 kill_worker = true;
719 kill_target = true;
720 }
721
722 if (kill_worker) {
723 // Something bad happened, kill the worker.
724 if (kill(child_pid, SIGKILL) != 0) {
725 ALOGE("debuggerd: failed to kill worker process %d: %s", child_pid, strerror(errno));
726 } else {
727 waitpid(child_pid, &status, 0);
728 }
729 }
730
Josh Gao24464182016-03-22 16:37:45 -0700731 int exit_signal = SIGCONT;
732 if (kill_target && request.action == DEBUGGER_ACTION_CRASH) {
733 ALOGE("debuggerd: killing target %d", request.pid);
734 exit_signal = SIGKILL;
735 } else {
736 ALOGW("debuggerd: resuming target %d", request.pid);
737 }
738
739 if (kill(request.pid, exit_signal) != 0) {
740 ALOGE("debuggerd: failed to send signal %d to target: %s", exit_signal, strerror(errno));
Josh Gao630bc802016-03-16 20:19:44 -0700741 }
742
743 if (kill_self) {
744 stop_signal_sender();
745 _exit(1);
746 }
747}
748
749static void handle_request(int fd) {
750 ALOGV("handle_request(%d)\n", fd);
751
Elliott Hughesae389232016-03-22 20:03:13 -0700752 android::base::unique_fd closer(fd);
Josh Gao630bc802016-03-16 20:19:44 -0700753 debugger_request_t request;
754 memset(&request, 0, sizeof(request));
755 int status = read_request(fd, &request);
756 if (status != 0) {
757 return;
758 }
759
760 ALOGW("debuggerd: handling request: pid=%d uid=%d gid=%d tid=%d\n", request.pid, request.uid,
761 request.gid, request.tid);
762
763#if defined(__LP64__)
764 // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
765 // to the 64 bit debuggerd. If the process is a 32 bit executable,
766 // redirect the request to the 32 bit debuggerd.
767 if (is32bit(request.tid)) {
768 // Only dump backtrace and dump tombstone requests can be redirected.
769 if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE ||
770 request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
771 redirect_to_32(fd, &request);
772 } else {
773 ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n", request.action);
774 }
775 return;
776 }
777#endif
778
779 // Fork a child to handle the rest of the request.
780 pid_t fork_pid = fork();
781 if (fork_pid == -1) {
782 ALOGE("debuggerd: failed to fork: %s\n", strerror(errno));
783 } else if (fork_pid == 0) {
784 worker_process(fd, request);
785 } else {
786 monitor_worker_process(fork_pid, request);
787 }
788}
789
Jeff Brown9524e412011-10-24 11:10:16 -0700790static int do_server() {
Elliott Hughesa323b502014-05-16 21:12:17 -0700791 // debuggerd crashes can't be reported to debuggerd.
792 // Reset all of the crash handlers.
Christopher Ferris20303f82014-01-10 16:33:16 -0800793 signal(SIGABRT, SIG_DFL);
794 signal(SIGBUS, SIG_DFL);
795 signal(SIGFPE, SIG_DFL);
Elliott Hughesa323b502014-05-16 21:12:17 -0700796 signal(SIGILL, SIG_DFL);
Christopher Ferris20303f82014-01-10 16:33:16 -0800797 signal(SIGSEGV, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700798#ifdef SIGSTKFLT
Christopher Ferris20303f82014-01-10 16:33:16 -0800799 signal(SIGSTKFLT, SIG_DFL);
Chris Dearman231e3c82012-08-10 17:06:20 -0700800#endif
Elliott Hughesa323b502014-05-16 21:12:17 -0700801 signal(SIGTRAP, SIG_DFL);
Andy McFadden44e12ec2011-07-29 12:36:47 -0700802
Christopher Ferris20303f82014-01-10 16:33:16 -0800803 // Ignore failed writes to closed sockets
804 signal(SIGPIPE, SIG_IGN);
Nick Kralevich96bcd482013-06-18 17:57:08 -0700805
Josh Gao630bc802016-03-16 20:19:44 -0700806 // Block SIGCHLD so we can sigtimedwait for it.
807 sigset_t sigchld;
808 sigemptyset(&sigchld);
809 sigaddset(&sigchld, SIGCHLD);
810 sigprocmask(SIG_SETMASK, &sigchld, nullptr);
Christopher Ferris20303f82014-01-10 16:33:16 -0800811
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800812 int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
813 SOCK_STREAM | SOCK_CLOEXEC);
814 if (s == -1) return 1;
Christopher Ferris20303f82014-01-10 16:33:16 -0800815
Josh Gaof5e8f0b2016-03-16 18:09:15 -0700816 // Fork a process that stays root, and listens on a pipe to pause and resume the target.
817 if (!start_signal_sender()) {
818 ALOGE("debuggerd: failed to fork signal sender");
819 return 1;
820 }
821
Dan Willemsen30622bb2015-10-22 13:04:22 -0700822 ALOGI("debuggerd: starting\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800823
824 for (;;) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700825 ALOGV("waiting for connection\n");
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700826 int fd = accept4(s, nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK);
Elliott Hughes17ba68d2016-02-19 18:13:02 -0800827 if (fd == -1) {
828 ALOGE("accept failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800829 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830 }
831
Christopher Ferris20303f82014-01-10 16:33:16 -0800832 handle_request(fd);
833 }
834 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800835}
Jeff Brown9524e412011-10-24 11:10:16 -0700836
Jeff Brown053b8652012-06-06 16:25:03 -0700837static int do_explicit_dump(pid_t tid, bool dump_backtrace) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800838 fprintf(stdout, "Sending request to dump task %d.\n", tid);
Jeff Brown9524e412011-10-24 11:10:16 -0700839
Christopher Ferris20303f82014-01-10 16:33:16 -0800840 if (dump_backtrace) {
841 fflush(stdout);
842 if (dump_backtrace_to_file(tid, fileno(stdout)) < 0) {
843 fputs("Error dumping backtrace.\n", stderr);
844 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700845 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800846 } else {
847 char tombstone_path[PATH_MAX];
848 if (dump_tombstone(tid, tombstone_path, sizeof(tombstone_path)) < 0) {
849 fputs("Error dumping tombstone.\n", stderr);
850 return 1;
851 }
852 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
853 }
854 return 0;
Jeff Brown9524e412011-10-24 11:10:16 -0700855}
856
Jeff Brown053b8652012-06-06 16:25:03 -0700857static void usage() {
Christopher Ferris20303f82014-01-10 16:33:16 -0800858 fputs("Usage: -b [<tid>]\n"
859 " -b dump backtrace to console, otherwise dump full tombstone file\n"
860 "\n"
861 "If tid specified, sends a request to debuggerd to dump that task.\n"
862 "Otherwise, starts the debuggerd server.\n", stderr);
Jeff Brown053b8652012-06-06 16:25:03 -0700863}
864
Jeff Brown9524e412011-10-24 11:10:16 -0700865int main(int argc, char** argv) {
Stephen Smalley69b80032014-07-24 15:23:05 -0400866 union selinux_callback cb;
Christopher Ferris20303f82014-01-10 16:33:16 -0800867 if (argc == 1) {
William Roberts46857392015-10-06 12:03:01 -0700868 cb.func_audit = audit_callback;
869 selinux_set_callback(SELINUX_CB_AUDIT, cb);
Stephen Smalley69b80032014-07-24 15:23:05 -0400870 cb.func_log = selinux_log_callback;
871 selinux_set_callback(SELINUX_CB_LOG, cb);
Christopher Ferris20303f82014-01-10 16:33:16 -0800872 return do_server();
873 }
Jeff Brown053b8652012-06-06 16:25:03 -0700874
Christopher Ferris20303f82014-01-10 16:33:16 -0800875 bool dump_backtrace = false;
876 bool have_tid = false;
877 pid_t tid = 0;
878 for (int i = 1; i < argc; i++) {
879 if (!strcmp(argv[i], "-b")) {
880 dump_backtrace = true;
881 } else if (!have_tid) {
882 tid = atoi(argv[i]);
883 have_tid = true;
884 } else {
885 usage();
886 return 1;
Jeff Brown9524e412011-10-24 11:10:16 -0700887 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800888 }
889 if (!have_tid) {
890 usage();
891 return 1;
892 }
893 return do_explicit_dump(tid, dump_backtrace);
Jeff Brown9524e412011-10-24 11:10:16 -0700894}