| 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 | int main(int argc, char* argv[]) { | 
|  | 45 | if (argc <= 1) usage(0); | 
|  | 46 | if (argc > 3) usage(1); | 
| Josh Gao | 24113ae | 2018-06-14 15:03:12 -0700 | [diff] [blame] | 47 |  | 
|  | 48 | DebuggerdDumpType dump_type = kDebuggerdTombstone; | 
|  | 49 |  | 
|  | 50 | if (argc == 3) { | 
|  | 51 | std::string_view flag = argv[1]; | 
|  | 52 | if (flag == "-b" || flag == "--backtrace") { | 
|  | 53 | dump_type = kDebuggerdNativeBacktrace; | 
|  | 54 | } else if (flag == "-j") { | 
|  | 55 | dump_type = kDebuggerdJavaBacktrace; | 
|  | 56 | } else { | 
|  | 57 | usage(1); | 
|  | 58 | } | 
|  | 59 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 61 | pid_t pid; | 
|  | 62 | if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) { | 
|  | 63 | usage(1); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 64 | } | 
| Jeff Brown | 9524e41 | 2011-10-24 11:10:16 -0700 | [diff] [blame] | 65 |  | 
| Josh Gao | 0915f23 | 2017-06-27 14:08:05 -0700 | [diff] [blame] | 66 | if (getuid() != 0) { | 
|  | 67 | errx(1, "root is required"); | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | // Check to see if the process exists and that we can actually send a signal to it. | 
|  | 71 | android::procinfo::ProcessInfo proc_info; | 
|  | 72 | if (!android::procinfo::GetProcessInfo(pid, &proc_info)) { | 
|  | 73 | err(1, "failed to fetch info for process %d", pid); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | if (proc_info.state == android::procinfo::kProcessStateZombie) { | 
|  | 77 | errx(1, "process %d is a zombie", pid); | 
|  | 78 | } | 
|  | 79 |  | 
| Christopher Ferris | a3e9a0b | 2021-07-29 12:38:07 -0700 | [diff] [blame] | 80 | // Send a signal to the main thread pid, not a side thread. The signal | 
|  | 81 | // handler always sets the crashing tid to the main thread pid when sent this | 
|  | 82 | // signal. This is to avoid a problem where the signal is sent to a process, | 
|  | 83 | // but happens on a side thread and the intercept mismatches since it | 
|  | 84 | // is looking for the main thread pid, not the tid of this random thread. | 
|  | 85 | // See b/194346289 for extra details. | 
|  | 86 | if (kill(proc_info.pid, 0) != 0) { | 
|  | 87 | if (pid == proc_info.pid) { | 
|  | 88 | err(1, "cannot send signal to process %d", pid); | 
|  | 89 | } else { | 
|  | 90 | err(1, "cannot send signal to main thread %d (requested thread %d)", proc_info.pid, pid); | 
|  | 91 | } | 
| Josh Gao | 0915f23 | 2017-06-27 14:08:05 -0700 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
| Frederick Mayle | 56abaa0 | 2022-12-13 15:40:57 -0800 | [diff] [blame] | 94 | unique_fd output_fd(fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0)); | 
|  | 95 | if (output_fd.get() == -1) { | 
|  | 96 | err(1, "failed to fcntl dup stdout"); | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 97 | } | 
| Frederick Mayle | 56abaa0 | 2022-12-13 15:40:57 -0800 | [diff] [blame] | 98 | if (!debuggerd_trigger_dump(proc_info.pid, dump_type, 0, std::move(output_fd))) { | 
| Christopher Ferris | a3e9a0b | 2021-07-29 12:38:07 -0700 | [diff] [blame] | 99 | if (pid == proc_info.pid) { | 
|  | 100 | errx(1, "failed to dump process %d", pid); | 
|  | 101 | } else { | 
|  | 102 | errx(1, "failed to dump main thread %d (requested thread %d)", proc_info.pid, pid); | 
|  | 103 | } | 
| Stephen Smalley | 69b8003 | 2014-07-24 15:23:05 -0400 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 106 | return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | } |