blob: 4870eeeec3c94a2ad3d07e6cc1c10871b38fdbac [file] [log] [blame]
Josh Gaocbe70cb2016-10-18 18:17:52 -07001/*
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 <arpa/inet.h>
18#include <dirent.h>
19#include <fcntl.h>
20#include <stdlib.h>
21#include <syscall.h>
22#include <sys/ptrace.h>
23#include <sys/types.h>
24#include <sys/un.h>
25#include <unistd.h>
26
27#include <limits>
28#include <memory>
29#include <set>
30#include <vector>
31
32#include <android-base/file.h>
33#include <android-base/logging.h>
34#include <android-base/parseint.h>
35#include <android-base/properties.h>
36#include <android-base/stringprintf.h>
37#include <android-base/unique_fd.h>
38#include <cutils/sockets.h>
Vijay Venkatramana95acea2017-01-23 20:11:51 -080039#include <log/log.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070040#include <procinfo/process.h>
41#include <selinux/selinux.h>
42
43#include "backtrace.h"
44#include "tombstone.h"
45#include "utility.h"
46
47#include "debuggerd/handler.h"
48#include "debuggerd/protocol.h"
49#include "debuggerd/util.h"
50
51using android::base::unique_fd;
52using android::base::StringPrintf;
53
Josh Gaofe902762017-02-01 16:31:43 -080054static bool pid_contains_tid(int pid_proc_fd, pid_t tid) {
55 struct stat st;
56 std::string task_path = StringPrintf("task/%d", tid);
57 return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -070058}
59
60// Attach to a thread, and verify that it's still a member of the given process
Josh Gaofe902762017-02-01 16:31:43 -080061static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error) {
Josh Gao122479f2017-01-22 16:42:32 -080062 if (ptrace(PTRACE_SEIZE, tid, 0, 0) != 0) {
Josh Gao42fd74b2017-01-20 12:51:11 -080063 *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno));
Josh Gaocbe70cb2016-10-18 18:17:52 -070064 return false;
65 }
66
67 // Make sure that the task we attached to is actually part of the pid we're dumping.
Josh Gaofe902762017-02-01 16:31:43 -080068 if (!pid_contains_tid(pid_proc_fd, tid)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -070069 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
70 PLOG(FATAL) << "failed to detach from thread " << tid;
71 }
Josh Gaofe902762017-02-01 16:31:43 -080072 *error = StringPrintf("thread %d is not in process", tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -070073 return false;
74 }
Josh Gao122479f2017-01-22 16:42:32 -080075
76 // Put the task into ptrace-stop state.
77 if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) != 0) {
78 PLOG(FATAL) << "failed to interrupt thread " << tid;
79 }
80
Josh Gaocbe70cb2016-10-18 18:17:52 -070081 return true;
82}
83
84static bool activity_manager_notify(int pid, int signal, const std::string& amfd_data) {
85 android::base::unique_fd amfd(socket_local_client("/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
86 if (amfd.get() == -1) {
87 PLOG(ERROR) << "unable to connect to activity manager";
88 return false;
89 }
90
91 struct timeval tv = {
92 .tv_sec = 1,
93 .tv_usec = 0,
94 };
95 if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
96 PLOG(ERROR) << "failed to set send timeout on activity manager socket";
97 return false;
98 }
99 tv.tv_sec = 3; // 3 seconds on handshake read
100 if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
101 PLOG(ERROR) << "failed to set receive timeout on activity manager socket";
102 return false;
103 }
104
105 // Activity Manager protocol: binary 32-bit network-byte-order ints for the
106 // pid and signal number, followed by the raw text of the dump, culminating
107 // in a zero byte that marks end-of-data.
108 uint32_t datum = htonl(pid);
109 if (!android::base::WriteFully(amfd, &datum, 4)) {
110 PLOG(ERROR) << "AM pid write failed";
111 return false;
112 }
113 datum = htonl(signal);
114 if (!android::base::WriteFully(amfd, &datum, 4)) {
115 PLOG(ERROR) << "AM signal write failed";
116 return false;
117 }
118 if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
119 PLOG(ERROR) << "AM data write failed";
120 return false;
121 }
122
123 // 3 sec timeout reading the ack; we're fine if the read fails.
124 char ack;
125 android::base::ReadFully(amfd, &ack, 1);
126 return true;
127}
128
129static bool tombstoned_connect(pid_t pid, unique_fd* tombstoned_socket, unique_fd* output_fd) {
130 unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
131 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
132 if (sockfd == -1) {
133 PLOG(ERROR) << "failed to connect to tombstoned";
134 return false;
135 }
136
137 TombstonedCrashPacket packet = {};
138 packet.packet_type = CrashPacketType::kDumpRequest;
139 packet.packet.dump_request.pid = pid;
140 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
141 PLOG(ERROR) << "failed to write DumpRequest packet";
142 return false;
143 }
144
145 unique_fd tmp_output_fd;
146 ssize_t rc = recv_fd(sockfd, &packet, sizeof(packet), &tmp_output_fd);
147 if (rc == -1) {
148 PLOG(ERROR) << "failed to read response to DumpRequest packet";
149 return false;
150 } else if (rc != sizeof(packet)) {
151 LOG(ERROR) << "read DumpRequest response packet of incorrect length (expected "
152 << sizeof(packet) << ", got " << rc << ")";
153 return false;
154 }
155
156 *tombstoned_socket = std::move(sockfd);
157 *output_fd = std::move(tmp_output_fd);
158 return true;
159}
160
161static bool tombstoned_notify_completion(int tombstoned_socket) {
162 TombstonedCrashPacket packet = {};
163 packet.packet_type = CrashPacketType::kCompletedDump;
164 if (TEMP_FAILURE_RETRY(write(tombstoned_socket, &packet, sizeof(packet))) != sizeof(packet)) {
165 return false;
166 }
167 return true;
168}
169
Josh Gao57594112017-01-22 17:41:15 -0800170static void signal_handler(int) {
171 // We can't log easily, because the heap might be corrupt.
172 // Just die and let the surrounding log context explain things.
173 _exit(1);
174}
175
Josh Gaocbe70cb2016-10-18 18:17:52 -0700176static void abort_handler(pid_t target, const bool& tombstoned_connected,
177 unique_fd& tombstoned_socket, unique_fd& output_fd,
178 const char* abort_msg) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700179 // If we abort before we get an output fd, contact tombstoned to let any
180 // potential listeners know that we failed.
181 if (!tombstoned_connected) {
182 if (!tombstoned_connect(target, &tombstoned_socket, &output_fd)) {
183 // We failed to connect, not much we can do.
184 LOG(ERROR) << "failed to connected to tombstoned to report failure";
185 _exit(1);
186 }
187 }
188
189 dprintf(output_fd.get(), "crash_dump failed to dump process %d: %s\n", target, abort_msg);
190
Josh Gaocbe70cb2016-10-18 18:17:52 -0700191 _exit(1);
192}
193
194static void check_process(int proc_fd, pid_t expected_pid) {
195 android::procinfo::ProcessInfo proc_info;
196 if (!android::procinfo::GetProcessInfoFromProcPidFd(proc_fd, &proc_info)) {
197 LOG(FATAL) << "failed to fetch process info";
198 }
199
200 if (proc_info.pid != expected_pid) {
201 LOG(FATAL) << "pid mismatch: expected " << expected_pid << ", actual " << proc_info.ppid;
202 }
203}
204
205int main(int argc, char** argv) {
206 pid_t target = getppid();
207 bool tombstoned_connected = false;
208 unique_fd tombstoned_socket;
209 unique_fd output_fd;
210
211 android::base::InitLogging(argv);
212 android::base::SetAborter([&](const char* abort_msg) {
213 abort_handler(target, tombstoned_connected, tombstoned_socket, output_fd, abort_msg);
214 });
215
Josh Gao57594112017-01-22 17:41:15 -0800216 // Don't try to dump ourselves.
217 struct sigaction action = {};
218 action.sa_handler = signal_handler;
219 debuggerd_register_handlers(&action);
220
Josh Gaocbe70cb2016-10-18 18:17:52 -0700221 if (argc != 2) {
222 return 1;
223 }
224
225 pid_t main_tid;
226
227 if (target == 1) {
228 LOG(FATAL) << "target died before we could attach";
229 }
230
231 if (!android::base::ParseInt(argv[1], &main_tid, 1, std::numeric_limits<pid_t>::max())) {
232 LOG(FATAL) << "invalid main tid: " << argv[1];
233 }
234
235 android::procinfo::ProcessInfo target_info;
236 if (!android::procinfo::GetProcessInfo(main_tid, &target_info)) {
237 LOG(FATAL) << "failed to fetch process info for target " << main_tid;
238 }
239
240 if (main_tid != target_info.tid || target != target_info.pid) {
241 LOG(FATAL) << "target info mismatch, expected pid " << target << ", tid " << main_tid
242 << ", received pid " << target_info.pid << ", tid " << target_info.tid;
243 }
244
245 // Open /proc/`getppid()` in the original process, and pass it down to the forked child.
246 std::string target_proc_path = "/proc/" + std::to_string(target);
247 int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY);
248 if (target_proc_fd == -1) {
249 PLOG(FATAL) << "failed to open " << target_proc_path;
250 }
251
252 // Reparent ourselves to init, so that the signal handler can waitpid on the
253 // original process to avoid leaving a zombie for non-fatal dumps.
254 pid_t forkpid = fork();
255 if (forkpid == -1) {
256 PLOG(FATAL) << "fork failed";
257 } else if (forkpid != 0) {
258 exit(0);
259 }
260
Josh Gao7c6e3132017-01-22 17:59:02 -0800261 // Die if we take too long.
262 alarm(20);
263
Josh Gaocbe70cb2016-10-18 18:17:52 -0700264 check_process(target_proc_fd, target);
265
Josh Gao42fd74b2017-01-20 12:51:11 -0800266 std::string attach_error;
Josh Gaofe902762017-02-01 16:31:43 -0800267 if (!ptrace_seize_thread(target_proc_fd, main_tid, &attach_error)) {
Josh Gao42fd74b2017-01-20 12:51:11 -0800268 LOG(FATAL) << attach_error;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700269 }
270
271 check_process(target_proc_fd, target);
272
273 LOG(INFO) << "obtaining output fd from tombstoned";
274 tombstoned_connected = tombstoned_connect(target, &tombstoned_socket, &output_fd);
275
276 // Write a '\1' to stdout to tell the crashing process to resume.
277 if (TEMP_FAILURE_RETRY(write(STDOUT_FILENO, "\1", 1)) == -1) {
278 PLOG(ERROR) << "failed to communicate to target process";
279 }
280
281 if (tombstoned_connected) {
282 if (TEMP_FAILURE_RETRY(dup2(output_fd.get(), STDOUT_FILENO)) == -1) {
283 PLOG(ERROR) << "failed to dup2 output fd (" << output_fd.get() << ") to STDOUT_FILENO";
284 }
285 } else {
286 unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
287 TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO));
Josh Gao0a379012017-01-24 15:20:42 -0800288 output_fd = std::move(devnull);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700289 }
290
Josh Gaocbe70cb2016-10-18 18:17:52 -0700291 LOG(INFO) << "performing dump of process " << target << " (target tid = " << main_tid << ")";
292
Josh Gao122479f2017-01-22 16:42:32 -0800293 // At this point, the thread that made the request has been attached and is
294 // in ptrace-stopped state. After resumption, the triggering signal that has
295 // been queued will be delivered.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700296 if (ptrace(PTRACE_CONT, main_tid, 0, 0) != 0) {
297 PLOG(ERROR) << "PTRACE_CONT(" << main_tid << ") failed";
298 exit(1);
299 }
300
301 siginfo_t siginfo = {};
302 if (!wait_for_signal(main_tid, &siginfo)) {
303 printf("failed to wait for signal in tid %d: %s\n", main_tid, strerror(errno));
304 exit(1);
305 }
306
307 int signo = siginfo.si_signo;
Josh Gaofe902762017-02-01 16:31:43 -0800308 bool fatal_signal = signo != DEBUGGER_SIGNAL;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700309 bool backtrace = false;
310 uintptr_t abort_address = 0;
311
312 // si_value can represent three things:
313 // 0: dump tombstone
314 // 1: dump backtrace
315 // everything else: abort message address (implies dump tombstone)
316 if (siginfo.si_value.sival_int == 1) {
317 backtrace = true;
318 } else if (siginfo.si_value.sival_ptr != nullptr) {
319 abort_address = reinterpret_cast<uintptr_t>(siginfo.si_value.sival_ptr);
320 }
321
322 // Now that we have the signal that kicked things off, attach all of the
323 // sibling threads, and then proceed.
Josh Gao42fd74b2017-01-20 12:51:11 -0800324 std::set<pid_t> attached_siblings;
Josh Gaofe902762017-02-01 16:31:43 -0800325 {
326 std::set<pid_t> siblings;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700327 if (!android::procinfo::GetProcessTids(target, &siblings)) {
328 PLOG(FATAL) << "failed to get process siblings";
329 }
330 siblings.erase(main_tid);
331
332 for (pid_t sibling_tid : siblings) {
Josh Gaofe902762017-02-01 16:31:43 -0800333 if (!ptrace_seize_thread(target_proc_fd, sibling_tid, &attach_error)) {
Josh Gao42fd74b2017-01-20 12:51:11 -0800334 LOG(WARNING) << attach_error;
335 } else {
336 attached_siblings.insert(sibling_tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700337 }
338 }
339 }
340
341 check_process(target_proc_fd, target);
342
343 // TODO: Use seccomp to lock ourselves down.
344
345 std::unique_ptr<BacktraceMap> backtrace_map(BacktraceMap::Create(main_tid));
346 std::string amfd_data;
347
348 if (backtrace) {
Josh Gao42fd74b2017-01-20 12:51:11 -0800349 dump_backtrace(output_fd.get(), backtrace_map.get(), target, main_tid, attached_siblings, 0);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700350 } else {
351 // Collect the list of open files.
352 OpenFilesList open_files;
353 populate_open_files_list(target, &open_files);
354
Josh Gao42fd74b2017-01-20 12:51:11 -0800355 engrave_tombstone(output_fd.get(), backtrace_map.get(), open_files, target, main_tid,
356 attached_siblings, abort_address, fatal_signal ? &amfd_data : nullptr);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700357 }
358
Josh Gao7c6e3132017-01-22 17:59:02 -0800359 // We don't actually need to PTRACE_DETACH, as long as our tracees aren't in
360 // group-stop state, which is true as long as no stopping signals are sent.
361
Josh Gaocbe70cb2016-10-18 18:17:52 -0700362 bool wait_for_gdb = android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false);
Josh Gao7c6e3132017-01-22 17:59:02 -0800363 if (!fatal_signal || siginfo.si_code == SI_USER) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700364 // Don't wait_for_gdb when the process didn't actually crash.
Josh Gao7c6e3132017-01-22 17:59:02 -0800365 wait_for_gdb = false;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700366 }
367
Josh Gao7c6e3132017-01-22 17:59:02 -0800368 // If the process crashed or we need to send it SIGSTOP for wait_for_gdb,
369 // get it in a state where it can receive signals, and then send the relevant
370 // signal.
371 if (wait_for_gdb || fatal_signal) {
372 if (ptrace(PTRACE_INTERRUPT, main_tid, 0, 0) != 0) {
373 PLOG(ERROR) << "failed to use PTRACE_INTERRUPT on " << main_tid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700374 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700375
Josh Gao7c6e3132017-01-22 17:59:02 -0800376 if (tgkill(target, main_tid, wait_for_gdb ? SIGSTOP : signo) != 0) {
377 PLOG(ERROR) << "failed to resend signal " << signo << " to " << main_tid;
378 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700379 }
380
381 if (wait_for_gdb) {
Josh Gao7c6e3132017-01-22 17:59:02 -0800382 // Use ALOGI to line up with output from engrave_tombstone.
383 ALOGI(
384 "***********************************************************\n"
385 "* Process %d has been suspended while crashing.\n"
386 "* To attach gdbserver and start gdb, run this on the host:\n"
387 "*\n"
388 "* gdbclient.py -p %d\n"
389 "*\n"
390 "***********************************************************",
391 target, main_tid);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700392 }
393
394 if (fatal_signal) {
395 activity_manager_notify(target, signo, amfd_data);
396 }
397
398 // Close stdout before we notify tombstoned of completion.
399 close(STDOUT_FILENO);
Josh Gao0a379012017-01-24 15:20:42 -0800400 if (tombstoned_connected && !tombstoned_notify_completion(tombstoned_socket.get())) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700401 LOG(ERROR) << "failed to notify tombstoned of completion";
402 }
403
404 return 0;
405}