| 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> | 
| Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 31 | #include "util.h" | 
| Josh Gao | a04c802 | 2016-08-11 12:50:32 -0700 | [diff] [blame] | 32 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 33 | using android::base::unique_fd; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 35 | static void usage(int exit_code) { | 
|  | 36 | fprintf(stderr, "usage: debuggerd [-b] PID\n"); | 
| Elliott Hughes | 12b7129 | 2017-03-02 19:01:20 -0800 | [diff] [blame] | 37 | fprintf(stderr, "\n"); | 
|  | 38 | 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] | 39 | _exit(exit_code); | 
|  | 40 | } | 
| Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 41 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 42 | static 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 51 | if (!android::base::WriteFully(STDOUT_FILENO, buf, rc)) { | 
|  | 52 | return; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 53 | } | 
| Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 54 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 55 | }); | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 56 | } | 
| Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 57 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 58 | int main(int argc, char* argv[]) { | 
|  | 59 | if (argc <= 1) usage(0); | 
|  | 60 | if (argc > 3) usage(1); | 
| Elliott Hughes | 12b7129 | 2017-03-02 19:01:20 -0800 | [diff] [blame] | 61 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 64 | pid_t pid; | 
|  | 65 | if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) { | 
|  | 66 | usage(1); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 67 | } | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 68 |  | 
| Josh Gao | 0915f23 | 2017-06-27 14:08:05 -0700 | [diff] [blame] | 69 | 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 Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 87 | unique_fd piperead, pipewrite; | 
|  | 88 | if (!Pipe(&piperead, &pipewrite)) { | 
|  | 89 | err(1, "failed to create pipe"); | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 92 | std::thread redirect_thread = spawn_redirect_thread(std::move(piperead)); | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 93 | if (!debuggerd_trigger_dump(pid, backtrace_only ? kDebuggerdNativeBacktrace : kDebuggerdTombstone, | 
|  | 94 | 0, std::move(pipewrite))) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 95 | redirect_thread.join(); | 
|  | 96 | errx(1, "failed to dump process %d", pid); | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 99 | redirect_thread.join(); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 100 | return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 101 | } |