Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 1 | /* |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 2 | * Copyright 2016, The Android Open Source Project |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 17 | #include <err.h> |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 18 | #include <stdio.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 22 | #include <limits> |
| 23 | #include <thread> |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 24 | |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 25 | #include <android-base/file.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/parseint.h> |
Elliott Hughes | ae38923 | 2016-03-22 20:03:13 -0700 | [diff] [blame] | 28 | #include <android-base/unique_fd.h> |
Josh Gao | a04c802 | 2016-08-11 12:50:32 -0700 | [diff] [blame] | 29 | #include <debuggerd/client.h> |
Josh Gao | 0915f23 | 2017-06-27 14:08:05 -0700 | [diff] [blame^] | 30 | #include <procinfo/process.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 31 | #include <selinux/selinux.h> |
Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 32 | #include "util.h" |
Josh Gao | a04c802 | 2016-08-11 12:50:32 -0700 | [diff] [blame] | 33 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 34 | using android::base::unique_fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 36 | static void usage(int exit_code) { |
| 37 | fprintf(stderr, "usage: debuggerd [-b] PID\n"); |
Elliott Hughes | 12b7129 | 2017-03-02 19:01:20 -0800 | [diff] [blame] | 38 | fprintf(stderr, "\n"); |
| 39 | fprintf(stderr, "-b, --backtrace just a backtrace rather than a full tombstone\n"); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 40 | _exit(exit_code); |
| 41 | } |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 42 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 43 | static std::thread spawn_redirect_thread(unique_fd fd) { |
| 44 | return std::thread([fd{ std::move(fd) }]() { |
| 45 | while (true) { |
| 46 | char buf[BUFSIZ]; |
| 47 | ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, sizeof(buf))); |
| 48 | if (rc <= 0) { |
| 49 | return; |
| 50 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 52 | if (!android::base::WriteFully(STDOUT_FILENO, buf, rc)) { |
| 53 | return; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 54 | } |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 55 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 56 | }); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 57 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 58 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 59 | int main(int argc, char* argv[]) { |
| 60 | if (argc <= 1) usage(0); |
| 61 | if (argc > 3) usage(1); |
Elliott Hughes | 12b7129 | 2017-03-02 19:01:20 -0800 | [diff] [blame] | 62 | if (argc == 3 && strcmp(argv[1], "-b") != 0 && strcmp(argv[1], "--backtrace") != 0) usage(1); |
| 63 | bool backtrace_only = argc == 3; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 65 | pid_t pid; |
| 66 | if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) { |
| 67 | usage(1); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 68 | } |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 69 | |
Josh Gao | 0915f23 | 2017-06-27 14:08:05 -0700 | [diff] [blame^] | 70 | if (getuid() != 0) { |
| 71 | errx(1, "root is required"); |
| 72 | } |
| 73 | |
| 74 | // Check to see if the process exists and that we can actually send a signal to it. |
| 75 | android::procinfo::ProcessInfo proc_info; |
| 76 | if (!android::procinfo::GetProcessInfo(pid, &proc_info)) { |
| 77 | err(1, "failed to fetch info for process %d", pid); |
| 78 | } |
| 79 | |
| 80 | if (proc_info.state == android::procinfo::kProcessStateZombie) { |
| 81 | errx(1, "process %d is a zombie", pid); |
| 82 | } |
| 83 | |
| 84 | if (kill(pid, 0) != 0) { |
| 85 | err(1, "cannot send signal to process %d", pid); |
| 86 | } |
| 87 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 88 | unique_fd piperead, pipewrite; |
| 89 | if (!Pipe(&piperead, &pipewrite)) { |
| 90 | err(1, "failed to create pipe"); |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 93 | std::thread redirect_thread = spawn_redirect_thread(std::move(piperead)); |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 94 | if (!debuggerd_trigger_dump(pid, backtrace_only ? kDebuggerdNativeBacktrace : kDebuggerdTombstone, |
| 95 | 0, std::move(pipewrite))) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 96 | redirect_thread.join(); |
| 97 | errx(1, "failed to dump process %d", pid); |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 100 | redirect_thread.join(); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 101 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 102 | } |