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> |
Josh Gao | 24113ae | 2018-06-14 15:03:12 -0700 | [diff] [blame] | 23 | #include <string_view> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 24 | #include <thread> |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 25 | |
Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 26 | #include <android-base/file.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 27 | #include <android-base/logging.h> |
| 28 | #include <android-base/parseint.h> |
Elliott Hughes | ae38923 | 2016-03-22 20:03:13 -0700 | [diff] [blame] | 29 | #include <android-base/unique_fd.h> |
Josh Gao | a04c802 | 2016-08-11 12:50:32 -0700 | [diff] [blame] | 30 | #include <debuggerd/client.h> |
Josh Gao | 0915f23 | 2017-06-27 14:08:05 -0700 | [diff] [blame] | 31 | #include <procinfo/process.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) { |
Josh Gao | 24113ae | 2018-06-14 15:03:12 -0700 | [diff] [blame] | 37 | fprintf(stderr, "usage: debuggerd [-bj] 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 | 24113ae | 2018-06-14 15:03:12 -0700 | [diff] [blame] | 40 | fprintf(stderr, "-j collect java traces\n"); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 41 | _exit(exit_code); |
| 42 | } |
Christopher Ferris | 9774df6 | 2015-01-15 14:47:36 -0800 | [diff] [blame] | 43 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 44 | static 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 52 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 53 | if (!android::base::WriteFully(STDOUT_FILENO, buf, rc)) { |
| 54 | return; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 55 | } |
Elliott Hughes | d9bf2b2 | 2014-05-16 19:16:22 -0700 | [diff] [blame] | 56 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 57 | }); |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 58 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 59 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 60 | int main(int argc, char* argv[]) { |
| 61 | if (argc <= 1) usage(0); |
| 62 | if (argc > 3) usage(1); |
Josh Gao | 24113ae | 2018-06-14 15:03:12 -0700 | [diff] [blame] | 63 | |
| 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 77 | pid_t pid; |
| 78 | if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) { |
| 79 | usage(1); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 80 | } |
Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 81 | |
Josh Gao | 0915f23 | 2017-06-27 14:08:05 -0700 | [diff] [blame] | 82 | 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 Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 100 | unique_fd piperead, pipewrite; |
| 101 | if (!Pipe(&piperead, &pipewrite)) { |
| 102 | err(1, "failed to create pipe"); |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 105 | std::thread redirect_thread = spawn_redirect_thread(std::move(piperead)); |
Josh Gao | 24113ae | 2018-06-14 15:03:12 -0700 | [diff] [blame] | 106 | if (!debuggerd_trigger_dump(pid, dump_type, 0, std::move(pipewrite))) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 107 | redirect_thread.join(); |
| 108 | errx(1, "failed to dump process %d", pid); |
Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 111 | redirect_thread.join(); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 112 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | } |