blob: 3208230823e82739212432634b14a62846317125 [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 Gaocbe70cb2016-10-18 18:17:52 -070030#include <debuggerd/util.h>
31#include <selinux/selinux.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");
37 _exit(exit_code);
38}
Christopher Ferris9774df62015-01-15 14:47:36 -080039
Josh Gaocbe70cb2016-10-18 18:17:52 -070040static std::thread spawn_redirect_thread(unique_fd fd) {
41 return std::thread([fd{ std::move(fd) }]() {
42 while (true) {
43 char buf[BUFSIZ];
44 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, sizeof(buf)));
45 if (rc <= 0) {
46 return;
47 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
Josh Gaocbe70cb2016-10-18 18:17:52 -070049 if (!android::base::WriteFully(STDOUT_FILENO, buf, rc)) {
50 return;
Christopher Ferris20303f82014-01-10 16:33:16 -080051 }
Elliott Hughesd9bf2b22014-05-16 19:16:22 -070052 }
Josh Gaocbe70cb2016-10-18 18:17:52 -070053 });
Jeff Brown9524e412011-10-24 11:10:16 -070054}
Ben Cheng09e71372009-09-28 11:06:09 -070055
Josh Gaocbe70cb2016-10-18 18:17:52 -070056int main(int argc, char* argv[]) {
57 if (argc <= 1) usage(0);
58 if (argc > 3) usage(1);
59 if (argc == 3 && strcmp(argv[1], "-b") != 0) usage(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Josh Gaocbe70cb2016-10-18 18:17:52 -070061 pid_t pid;
62 if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) {
63 usage(1);
Christopher Ferris20303f82014-01-10 16:33:16 -080064 }
Jeff Brown9524e412011-10-24 11:10:16 -070065
Josh Gaocbe70cb2016-10-18 18:17:52 -070066 unique_fd piperead, pipewrite;
67 if (!Pipe(&piperead, &pipewrite)) {
68 err(1, "failed to create pipe");
Stephen Smalley69b80032014-07-24 15:23:05 -040069 }
70
Josh Gaocbe70cb2016-10-18 18:17:52 -070071 std::thread redirect_thread = spawn_redirect_thread(std::move(piperead));
72 bool backtrace = argc == 3;
73 if (!debuggerd_trigger_dump(pid, std::move(pipewrite),
74 backtrace ? kDebuggerdBacktrace : kDebuggerdBacktrace, 0)) {
75 redirect_thread.join();
76 errx(1, "failed to dump process %d", pid);
Stephen Smalley69b80032014-07-24 15:23:05 -040077 }
78
Josh Gaocbe70cb2016-10-18 18:17:52 -070079 redirect_thread.join();
Christopher Ferris20303f82014-01-10 16:33:16 -080080 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081}