blob: 0d4b91f75700c6c084d051c0db728e5805c67832 [file] [log] [blame]
Christopher Ferris20303f82014-01-10 16:33:16 -08001/*
Josh Gaocbe70cb2016-10-18 18:17:52 -07002 * Copyright 2016, The Android Open Source Project
Christopher Ferris20303f82014-01-10 16:33:16 -08003 *
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 Projectdd7bc332009-03-03 19:32:55 -080016
Josh Gaocbe70cb2016-10-18 18:17:52 -070017#include <err.h>
Josh Gao7c89f9e2016-01-13 17:57:14 -080018#include <stdio.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070019#include <stdlib.h>
20#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021
Josh Gaocbe70cb2016-10-18 18:17:52 -070022#include <limits>
Josh Gao24113ae2018-06-14 15:03:12 -070023#include <string_view>
Josh Gaocbe70cb2016-10-18 18:17:52 -070024#include <thread>
Stephen Smalley69b80032014-07-24 15:23:05 -040025
Christopher Ferris9818bd22016-05-03 16:32:13 -070026#include <android-base/file.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070027#include <android-base/logging.h>
28#include <android-base/parseint.h>
Elliott Hughesae389232016-03-22 20:03:13 -070029#include <android-base/unique_fd.h>
Josh Gaoa04c8022016-08-11 12:50:32 -070030#include <debuggerd/client.h>
Josh Gao0915f232017-06-27 14:08:05 -070031#include <procinfo/process.h>
Narayan Kamath2d377cd2017-05-10 10:58:59 +010032#include "util.h"
Josh Gaoa04c8022016-08-11 12:50:32 -070033
Josh Gaocbe70cb2016-10-18 18:17:52 -070034using android::base::unique_fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Josh Gaocbe70cb2016-10-18 18:17:52 -070036static void usage(int exit_code) {
Josh Gao24113ae2018-06-14 15:03:12 -070037 fprintf(stderr, "usage: debuggerd [-bj] PID\n");
Elliott Hughes12b71292017-03-02 19:01:20 -080038 fprintf(stderr, "\n");
39 fprintf(stderr, "-b, --backtrace just a backtrace rather than a full tombstone\n");
Josh Gao24113ae2018-06-14 15:03:12 -070040 fprintf(stderr, "-j collect java traces\n");
Josh Gaocbe70cb2016-10-18 18:17:52 -070041 _exit(exit_code);
42}
Christopher Ferris9774df62015-01-15 14:47:36 -080043
Josh Gaocbe70cb2016-10-18 18:17:52 -070044int main(int argc, char* argv[]) {
45 if (argc <= 1) usage(0);
46 if (argc > 3) usage(1);
Josh Gao24113ae2018-06-14 15:03:12 -070047
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 Projectdd7bc332009-03-03 19:32:55 -080060
Josh Gaocbe70cb2016-10-18 18:17:52 -070061 pid_t pid;
62 if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) {
63 usage(1);
Christopher Ferris20303f82014-01-10 16:33:16 -080064 }
Jeff Brown9524e412011-10-24 11:10:16 -070065
Josh Gao0915f232017-06-27 14:08:05 -070066 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 Ferrisa3e9a0b2021-07-29 12:38:07 -070080 // 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 Gao0915f232017-06-27 14:08:05 -070092 }
93
Andy Hung42f8cd32024-01-11 18:57:27 -080094 // 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
Frederick Mayle56abaa02022-12-13 15:40:57 -0800103 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");
Stephen Smalley69b80032014-07-24 15:23:05 -0400106 }
Frederick Mayle56abaa02022-12-13 15:40:57 -0800107 if (!debuggerd_trigger_dump(proc_info.pid, dump_type, 0, std::move(output_fd))) {
Christopher Ferrisa3e9a0b2021-07-29 12:38:07 -0700108 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 }
Stephen Smalley69b80032014-07-24 15:23:05 -0400113 }
114
Christopher Ferris20303f82014-01-10 16:33:16 -0800115 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116}