• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016, The Android Open Source Project
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  */
16 
17 #include <err.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <limits>
23 #include <string_view>
24 #include <thread>
25 
26 #include <android-base/file.h>
27 #include <android-base/logging.h>
28 #include <android-base/parseint.h>
29 #include <android-base/unique_fd.h>
30 #include <debuggerd/client.h>
31 #include <procinfo/process.h>
32 #include "util.h"
33 
34 using android::base::unique_fd;
35 
usage(int exit_code)36 static void usage(int exit_code) {
37   fprintf(stderr, "usage: debuggerd [-bj] PID\n");
38   fprintf(stderr, "\n");
39   fprintf(stderr, "-b, --backtrace    just a backtrace rather than a full tombstone\n");
40   fprintf(stderr, "-j                 collect java traces\n");
41   _exit(exit_code);
42 }
43 
main(int argc,char * argv[])44 int main(int argc, char* argv[]) {
45   if (argc <= 1) usage(0);
46   if (argc > 3) usage(1);
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   }
60 
61   pid_t pid;
62   if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) {
63     usage(1);
64   }
65 
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 
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     }
92   }
93 
94   // unfreeze if pid is frozen.
95   const std::string freeze_file = android::base::StringPrintf(
96       "/sys/fs/cgroup/uid_%d/pid_%d/cgroup.freeze", proc_info.uid, proc_info.pid);
97   if (std::string freeze_status;
98       android::base::ReadFileToString(freeze_file, &freeze_status) && freeze_status[0] == '1') {
99     android::base::WriteStringToFile("0", freeze_file);
100     // we don't restore the frozen state as this is considered a benign change.
101   }
102 
103   unique_fd output_fd(fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0));
104   if (output_fd.get() == -1) {
105     err(1, "failed to fcntl dup stdout");
106   }
107   if (!debuggerd_trigger_dump(proc_info.pid, dump_type, 0, std::move(output_fd))) {
108     if (pid == proc_info.pid) {
109       errx(1, "failed to dump process %d", pid);
110     } else {
111       errx(1, "failed to dump main thread %d (requested thread %d)", proc_info.pid, pid);
112     }
113   }
114 
115   return 0;
116 }
117