blob: b016e23ee6eecfe01f6a55ff64b3ff705cf7cb28 [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>
23#include <thread>
Stephen Smalley69b80032014-07-24 15:23:05 -040024
Christopher Ferris9818bd22016-05-03 16:32:13 -070025#include <android-base/file.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070026#include <android-base/logging.h>
27#include <android-base/parseint.h>
Elliott Hughesae389232016-03-22 20:03:13 -070028#include <android-base/unique_fd.h>
Josh Gaoa04c8022016-08-11 12:50:32 -070029#include <debuggerd/client.h>
Josh Gao0915f232017-06-27 14:08:05 -070030#include <procinfo/process.h>
Narayan Kamath2d377cd2017-05-10 10:58:59 +010031#include "util.h"
Josh Gaoa04c8022016-08-11 12:50:32 -070032
Josh Gaocbe70cb2016-10-18 18:17:52 -070033using android::base::unique_fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
Josh Gaocbe70cb2016-10-18 18:17:52 -070035static void usage(int exit_code) {
36 fprintf(stderr, "usage: debuggerd [-b] PID\n");
Elliott Hughes12b71292017-03-02 19:01:20 -080037 fprintf(stderr, "\n");
38 fprintf(stderr, "-b, --backtrace just a backtrace rather than a full tombstone\n");
Josh Gaocbe70cb2016-10-18 18:17:52 -070039 _exit(exit_code);
40}
Christopher Ferris9774df62015-01-15 14:47:36 -080041
Josh Gaocbe70cb2016-10-18 18:17:52 -070042static std::thread spawn_redirect_thread(unique_fd fd) {
43 return std::thread([fd{ std::move(fd) }]() {
44 while (true) {
45 char buf[BUFSIZ];
46 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, sizeof(buf)));
47 if (rc <= 0) {
48 return;
49 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
Josh Gaocbe70cb2016-10-18 18:17:52 -070051 if (!android::base::WriteFully(STDOUT_FILENO, buf, rc)) {
52 return;
Christopher Ferris20303f82014-01-10 16:33:16 -080053 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070054 }
Josh Gaocbe70cb2016-10-18 18:17:52 -070055 });
Jeff Brown9524e412011-10-24 11:10:16 -070056}
Ben Cheng09e71372009-09-28 11:06:09 -070057
Josh Gaocbe70cb2016-10-18 18:17:52 -070058int main(int argc, char* argv[]) {
59 if (argc <= 1) usage(0);
60 if (argc > 3) usage(1);
Elliott Hughes12b71292017-03-02 19:01:20 -080061 if (argc == 3 && strcmp(argv[1], "-b") != 0 && strcmp(argv[1], "--backtrace") != 0) usage(1);
62 bool backtrace_only = argc == 3;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
Josh Gaocbe70cb2016-10-18 18:17:52 -070064 pid_t pid;
65 if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) {
66 usage(1);
Christopher Ferris20303f82014-01-10 16:33:16 -080067 }
Jeff Brown9524e412011-10-24 11:10:16 -070068
Josh Gao0915f232017-06-27 14:08:05 -070069 if (getuid() != 0) {
70 errx(1, "root is required");
71 }
72
73 // Check to see if the process exists and that we can actually send a signal to it.
74 android::procinfo::ProcessInfo proc_info;
75 if (!android::procinfo::GetProcessInfo(pid, &proc_info)) {
76 err(1, "failed to fetch info for process %d", pid);
77 }
78
79 if (proc_info.state == android::procinfo::kProcessStateZombie) {
80 errx(1, "process %d is a zombie", pid);
81 }
82
83 if (kill(pid, 0) != 0) {
84 err(1, "cannot send signal to process %d", pid);
85 }
86
Josh Gaocbe70cb2016-10-18 18:17:52 -070087 unique_fd piperead, pipewrite;
88 if (!Pipe(&piperead, &pipewrite)) {
89 err(1, "failed to create pipe");
Stephen Smalley69b80032014-07-24 15:23:05 -040090 }
91
Josh Gaocbe70cb2016-10-18 18:17:52 -070092 std::thread redirect_thread = spawn_redirect_thread(std::move(piperead));
Narayan Kamatha73df602017-05-24 15:07:25 +010093 if (!debuggerd_trigger_dump(pid, backtrace_only ? kDebuggerdNativeBacktrace : kDebuggerdTombstone,
94 0, std::move(pipewrite))) {
Josh Gaocbe70cb2016-10-18 18:17:52 -070095 redirect_thread.join();
96 errx(1, "failed to dump process %d", pid);
Stephen Smalley69b80032014-07-24 15:23:05 -040097 }
98
Josh Gaocbe70cb2016-10-18 18:17:52 -070099 redirect_thread.join();
Christopher Ferris20303f82014-01-10 16:33:16 -0800100 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101}