blob: 360ea95b3af1fe269bd99ce68de3a5f69244f55b [file] [log] [blame]
Christopher Ferris20303f82014-01-10 16:33:16 -08001/*
Josh Gaocbe70cb2016-10-18 18:17:52 -07002 * Copyright 2016, The Android Open Source Project
Christopher Ferris20303f82014-01-10 16:33:16 -08003 *
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
Josh Gaocbe70cb2016-10-18 18:17:52 -070017#include <err.h>
Josh Gao7c89f9e2016-01-13 17:57:14 -080018#include <stdio.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070019#include <stdlib.h>
20#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021
Josh Gaocbe70cb2016-10-18 18:17:52 -070022#include <limits>
Josh Gao24113ae2018-06-14 15:03:12 -070023#include <string_view>
Josh Gaocbe70cb2016-10-18 18:17:52 -070024#include <thread>
Stephen Smalley69b80032014-07-24 15:23:05 -040025
Christopher Ferris9818bd22016-05-03 16:32:13 -070026#include <android-base/file.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070027#include <android-base/logging.h>
28#include <android-base/parseint.h>
Elliott Hughesae389232016-03-22 20:03:13 -070029#include <android-base/unique_fd.h>
Josh Gaoa04c8022016-08-11 12:50:32 -070030#include <debuggerd/client.h>
Josh Gao0915f232017-06-27 14:08:05 -070031#include <procinfo/process.h>
Narayan Kamath2d377cd2017-05-10 10:58:59 +010032#include "util.h"
Josh Gaoa04c8022016-08-11 12:50:32 -070033
Josh Gaocbe70cb2016-10-18 18:17:52 -070034using android::base::unique_fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Josh Gaocbe70cb2016-10-18 18:17:52 -070036static void usage(int exit_code) {
Josh Gao24113ae2018-06-14 15:03:12 -070037 fprintf(stderr, "usage: debuggerd [-bj] PID\n");
Elliott Hughes12b71292017-03-02 19:01:20 -080038 fprintf(stderr, "\n");
39 fprintf(stderr, "-b, --backtrace just a backtrace rather than a full tombstone\n");
Josh Gao24113ae2018-06-14 15:03:12 -070040 fprintf(stderr, "-j collect java traces\n");
Josh Gaocbe70cb2016-10-18 18:17:52 -070041 _exit(exit_code);
42}
Christopher Ferris9774df62015-01-15 14:47:36 -080043
Josh Gaocbe70cb2016-10-18 18:17:52 -070044static std::thread spawn_redirect_thread(unique_fd fd) {
45 return std::thread([fd{ std::move(fd) }]() {
46 while (true) {
47 char buf[BUFSIZ];
48 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, sizeof(buf)));
49 if (rc <= 0) {
50 return;
51 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Josh Gaocbe70cb2016-10-18 18:17:52 -070053 if (!android::base::WriteFully(STDOUT_FILENO, buf, rc)) {
54 return;
Christopher Ferris20303f82014-01-10 16:33:16 -080055 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070056 }
Josh Gaocbe70cb2016-10-18 18:17:52 -070057 });
Jeff Brown9524e412011-10-24 11:10:16 -070058}
Ben Cheng09e71372009-09-28 11:06:09 -070059
Josh Gaocbe70cb2016-10-18 18:17:52 -070060int main(int argc, char* argv[]) {
61 if (argc <= 1) usage(0);
62 if (argc > 3) usage(1);
Josh Gao24113ae2018-06-14 15:03:12 -070063
64 DebuggerdDumpType dump_type = kDebuggerdTombstone;
65
66 if (argc == 3) {
67 std::string_view flag = argv[1];
68 if (flag == "-b" || flag == "--backtrace") {
69 dump_type = kDebuggerdNativeBacktrace;
70 } else if (flag == "-j") {
71 dump_type = kDebuggerdJavaBacktrace;
72 } else {
73 usage(1);
74 }
75 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Josh Gaocbe70cb2016-10-18 18:17:52 -070077 pid_t pid;
78 if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) {
79 usage(1);
Christopher Ferris20303f82014-01-10 16:33:16 -080080 }
Jeff Brown9524e412011-10-24 11:10:16 -070081
Josh Gao0915f232017-06-27 14:08:05 -070082 if (getuid() != 0) {
83 errx(1, "root is required");
84 }
85
86 // Check to see if the process exists and that we can actually send a signal to it.
87 android::procinfo::ProcessInfo proc_info;
88 if (!android::procinfo::GetProcessInfo(pid, &proc_info)) {
89 err(1, "failed to fetch info for process %d", pid);
90 }
91
92 if (proc_info.state == android::procinfo::kProcessStateZombie) {
93 errx(1, "process %d is a zombie", pid);
94 }
95
96 if (kill(pid, 0) != 0) {
97 err(1, "cannot send signal to process %d", pid);
98 }
99
Josh Gaocbe70cb2016-10-18 18:17:52 -0700100 unique_fd piperead, pipewrite;
101 if (!Pipe(&piperead, &pipewrite)) {
102 err(1, "failed to create pipe");
Stephen Smalley69b80032014-07-24 15:23:05 -0400103 }
104
Josh Gaocbe70cb2016-10-18 18:17:52 -0700105 std::thread redirect_thread = spawn_redirect_thread(std::move(piperead));
Josh Gao24113ae2018-06-14 15:03:12 -0700106 if (!debuggerd_trigger_dump(pid, dump_type, 0, std::move(pipewrite))) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700107 redirect_thread.join();
108 errx(1, "failed to dump process %d", pid);
Stephen Smalley69b80032014-07-24 15:23:05 -0400109 }
110
Josh Gaocbe70cb2016-10-18 18:17:52 -0700111 redirect_thread.join();
Christopher Ferris20303f82014-01-10 16:33:16 -0800112 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113}