Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 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 <arpa/inet.h> |
| 18 | #include <dirent.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdlib.h> |
Josh Gao | 85bcaf6 | 2017-02-01 16:35:31 -0800 | [diff] [blame] | 21 | #include <sys/prctl.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 22 | #include <sys/ptrace.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/un.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 25 | #include <sys/wait.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Sijie Chen | cb53fe1 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 28 | #include <cstdint> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 29 | #include <limits> |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 30 | #include <map> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 31 | #include <memory> |
| 32 | #include <set> |
| 33 | #include <vector> |
| 34 | |
Elliott Hughes | a029d98 | 2021-06-21 12:36:47 -0700 | [diff] [blame] | 35 | #include <android-base/errno_restorer.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 36 | #include <android-base/file.h> |
| 37 | #include <android-base/logging.h> |
Josh Gao | 8d44b14 | 2018-09-18 13:22:22 -0700 | [diff] [blame] | 38 | #include <android-base/macros.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 39 | #include <android-base/parseint.h> |
| 40 | #include <android-base/properties.h> |
| 41 | #include <android-base/stringprintf.h> |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 42 | #include <android-base/strings.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 43 | #include <android-base/unique_fd.h> |
Mitch Phillips | e4adff0 | 2021-01-21 20:41:50 -0800 | [diff] [blame] | 44 | #include <bionic/macros.h> |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 45 | #include <bionic/reserved_signals.h> |
Sijie Chen | cb53fe1 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 46 | #include <bionic/tls_defines.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 47 | #include <cutils/sockets.h> |
Vijay Venkatraman | a95acea | 2017-01-23 20:11:51 -0800 | [diff] [blame] | 48 | #include <log/log.h> |
Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 49 | #include <private/android_filesystem_config.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 50 | #include <procinfo/process.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 51 | |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 52 | #define ATRACE_TAG ATRACE_TAG_BIONIC |
| 53 | #include <utils/Trace.h> |
| 54 | |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 55 | #include <unwindstack/AndroidUnwinder.h> |
| 56 | #include <unwindstack/Error.h> |
Sijie Chen | cb53fe1 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 57 | #include <unwindstack/MachineArm.h> |
| 58 | #include <unwindstack/MachineArm64.h> |
| 59 | #include <unwindstack/MachineRiscv64.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 60 | #include <unwindstack/Regs.h> |
Sijie Chen | cb53fe1 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 61 | #include <unwindstack/RegsArm.h> |
| 62 | #include <unwindstack/RegsArm64.h> |
| 63 | #include <unwindstack/RegsRiscv64.h> |
| 64 | #include <unwindstack/UserArm.h> |
| 65 | #include <unwindstack/UserArm64.h> |
| 66 | #include <unwindstack/UserRiscv64.h> |
| 67 | |
| 68 | #include <native_bridge_support/guest_state_accessor/accessor.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 69 | |
Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 70 | #include "libdebuggerd/backtrace.h" |
| 71 | #include "libdebuggerd/tombstone.h" |
| 72 | #include "libdebuggerd/utility.h" |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 73 | |
| 74 | #include "debuggerd/handler.h" |
Shikha Panwar | abde59e | 2023-02-03 15:05:18 +0000 | [diff] [blame] | 75 | #include "tombstone_handler.h" |
Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 76 | |
| 77 | #include "protocol.h" |
Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 78 | #include "util.h" |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 79 | |
Elliott Hughes | a029d98 | 2021-06-21 12:36:47 -0700 | [diff] [blame] | 80 | using android::base::ErrnoRestorer; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 81 | using android::base::StringPrintf; |
Elliott Hughes | a029d98 | 2021-06-21 12:36:47 -0700 | [diff] [blame] | 82 | using android::base::unique_fd; |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 83 | |
Sijie Chen | c802793 | 2024-05-10 18:40:48 +0000 | [diff] [blame^] | 84 | // This stores guest architecture. When the architecture is supported, tombstone file will output |
| 85 | // guest state information. |
| 86 | static Architecture g_guest_arch; |
| 87 | |
Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 88 | static bool pid_contains_tid(int pid_proc_fd, pid_t tid) { |
| 89 | struct stat st; |
| 90 | std::string task_path = StringPrintf("task/%d", tid); |
| 91 | return fstatat(pid_proc_fd, task_path.c_str(), &st, 0) == 0; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 94 | static pid_t get_tracer(pid_t tracee) { |
| 95 | // Check to see if the thread is being ptraced by another process. |
| 96 | android::procinfo::ProcessInfo process_info; |
| 97 | if (android::procinfo::GetProcessInfo(tracee, &process_info)) { |
| 98 | return process_info.tracer; |
| 99 | } |
| 100 | return -1; |
| 101 | } |
| 102 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 103 | // Attach to a thread, and verify that it's still a member of the given process |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 104 | static bool ptrace_seize_thread(int pid_proc_fd, pid_t tid, std::string* error, int flags = 0) { |
| 105 | if (ptrace(PTRACE_SEIZE, tid, 0, flags) != 0) { |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 106 | if (errno == EPERM) { |
Elliott Hughes | a029d98 | 2021-06-21 12:36:47 -0700 | [diff] [blame] | 107 | ErrnoRestorer errno_restorer; // In case get_tracer() fails and we fall through. |
| 108 | pid_t tracer_pid = get_tracer(tid); |
| 109 | if (tracer_pid > 0) { |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 110 | *error = StringPrintf("failed to attach to thread %d, already traced by %d (%s)", tid, |
Elliott Hughes | a029d98 | 2021-06-21 12:36:47 -0700 | [diff] [blame] | 111 | tracer_pid, get_process_name(tracer_pid).c_str()); |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | |
Josh Gao | 42fd74b | 2017-01-20 12:51:11 -0800 | [diff] [blame] | 116 | *error = StringPrintf("failed to attach to thread %d: %s", tid, strerror(errno)); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
| 120 | // Make sure that the task we attached to is actually part of the pid we're dumping. |
Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 121 | if (!pid_contains_tid(pid_proc_fd, tid)) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 122 | if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 123 | PLOG(WARNING) << "failed to detach from thread " << tid; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 124 | } |
Josh Gao | fe90276 | 2017-02-01 16:31:43 -0800 | [diff] [blame] | 125 | *error = StringPrintf("thread %d is not in process", tid); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 126 | return false; |
| 127 | } |
Josh Gao | 122479f | 2017-01-22 16:42:32 -0800 | [diff] [blame] | 128 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | |
| 132 | static bool wait_for_stop(pid_t tid, int* received_signal) { |
| 133 | while (true) { |
| 134 | int status; |
| 135 | pid_t result = waitpid(tid, &status, __WALL); |
| 136 | if (result != tid) { |
| 137 | PLOG(ERROR) << "waitpid failed on " << tid << " while detaching"; |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | if (WIFSTOPPED(status)) { |
| 142 | if (status >> 16 == PTRACE_EVENT_STOP) { |
| 143 | *received_signal = 0; |
| 144 | } else { |
| 145 | *received_signal = WSTOPSIG(status); |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Interrupt a process and wait for it to be interrupted. |
| 153 | static bool ptrace_interrupt(pid_t tid, int* received_signal) { |
| 154 | if (ptrace(PTRACE_INTERRUPT, tid, 0, 0) == 0) { |
| 155 | return wait_for_stop(tid, received_signal); |
Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 158 | PLOG(ERROR) << "failed to interrupt " << tid << " to detach"; |
| 159 | return false; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Mitch Phillips | dd5a80d | 2023-03-13 15:08:17 -0700 | [diff] [blame] | 162 | static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data, |
Mitch Phillips | 3140791 | 2024-03-14 14:18:47 +0100 | [diff] [blame] | 163 | bool recoverable_crash) { |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 164 | ATRACE_CALL(); |
Josh Gao | b0e51e3 | 2017-06-01 12:08:10 -0700 | [diff] [blame] | 165 | android::base::unique_fd amfd(socket_local_client( |
| 166 | "/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM)); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 167 | if (amfd.get() == -1) { |
| 168 | PLOG(ERROR) << "unable to connect to activity manager"; |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | struct timeval tv = { |
Peter Collingbourne | fb5eac9 | 2021-03-11 12:51:25 -0800 | [diff] [blame] | 173 | .tv_sec = 1 * android::base::HwTimeoutMultiplier(), |
Evgenii Stepanov | 2a55e1a | 2021-01-29 11:50:30 -0800 | [diff] [blame] | 174 | .tv_usec = 0, |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 175 | }; |
| 176 | if (setsockopt(amfd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) { |
| 177 | PLOG(ERROR) << "failed to set send timeout on activity manager socket"; |
| 178 | return false; |
| 179 | } |
Peter Collingbourne | fb5eac9 | 2021-03-11 12:51:25 -0800 | [diff] [blame] | 180 | tv.tv_sec = 3 * android::base::HwTimeoutMultiplier(); // 3 seconds on handshake read |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 181 | if (setsockopt(amfd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) { |
| 182 | PLOG(ERROR) << "failed to set receive timeout on activity manager socket"; |
| 183 | return false; |
| 184 | } |
| 185 | |
Mitch Phillips | dd5a80d | 2023-03-13 15:08:17 -0700 | [diff] [blame] | 186 | // Activity Manager protocol: |
| 187 | // - 32-bit network-byte-order: pid |
| 188 | // - 32-bit network-byte-order: signal number |
Mitch Phillips | 3140791 | 2024-03-14 14:18:47 +0100 | [diff] [blame] | 189 | // - byte: recoverable_crash |
Mitch Phillips | dd5a80d | 2023-03-13 15:08:17 -0700 | [diff] [blame] | 190 | // - bytes: raw text of the dump |
| 191 | // - null terminator |
| 192 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 193 | uint32_t datum = htonl(pid); |
Mitch Phillips | dd5a80d | 2023-03-13 15:08:17 -0700 | [diff] [blame] | 194 | if (!android::base::WriteFully(amfd, &datum, sizeof(datum))) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 195 | PLOG(ERROR) << "AM pid write failed"; |
| 196 | return false; |
| 197 | } |
Mitch Phillips | dd5a80d | 2023-03-13 15:08:17 -0700 | [diff] [blame] | 198 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 199 | datum = htonl(signal); |
Mitch Phillips | dd5a80d | 2023-03-13 15:08:17 -0700 | [diff] [blame] | 200 | if (!android::base::WriteFully(amfd, &datum, sizeof(datum))) { |
| 201 | PLOG(ERROR) << "AM signo write failed"; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
Mitch Phillips | dd5a80d | 2023-03-13 15:08:17 -0700 | [diff] [blame] | 204 | |
Mitch Phillips | 3140791 | 2024-03-14 14:18:47 +0100 | [diff] [blame] | 205 | uint8_t recoverable_crash_byte = recoverable_crash ? 1 : 0; |
| 206 | if (!android::base::WriteFully(amfd, &recoverable_crash_byte, sizeof(recoverable_crash_byte))) { |
| 207 | PLOG(ERROR) << "AM recoverable_crash_byte write failed"; |
Mitch Phillips | dd5a80d | 2023-03-13 15:08:17 -0700 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 211 | if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) { |
| 212 | PLOG(ERROR) << "AM data write failed"; |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | // 3 sec timeout reading the ack; we're fine if the read fails. |
| 217 | char ack; |
| 218 | android::base::ReadFully(amfd, &ack, 1); |
| 219 | return true; |
| 220 | } |
| 221 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 222 | // Globals used by the abort handler. |
| 223 | static pid_t g_target_thread = -1; |
| 224 | static bool g_tombstoned_connected = false; |
| 225 | static unique_fd g_tombstoned_socket; |
| 226 | static unique_fd g_output_fd; |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 227 | static unique_fd g_proto_fd; |
Josh Gao | 5759411 | 2017-01-22 17:41:15 -0800 | [diff] [blame] | 228 | |
Josh Gao | 38ac45d | 2018-04-27 13:31:47 -0700 | [diff] [blame] | 229 | static void DefuseSignalHandlers() { |
| 230 | // Don't try to dump ourselves. |
| 231 | struct sigaction action = {}; |
| 232 | action.sa_handler = SIG_DFL; |
| 233 | debuggerd_register_handlers(&action); |
| 234 | |
| 235 | sigset_t mask; |
| 236 | sigemptyset(&mask); |
| 237 | if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) { |
| 238 | PLOG(FATAL) << "failed to set signal mask"; |
| 239 | } |
| 240 | } |
| 241 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 242 | static void Initialize(char** argv) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 243 | android::base::InitLogging(argv); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 244 | android::base::SetAborter([](const char* abort_msg) { |
| 245 | // If we abort before we get an output fd, contact tombstoned to let any |
| 246 | // potential listeners know that we failed. |
| 247 | if (!g_tombstoned_connected) { |
Shikha Panwar | abde59e | 2023-02-03 15:05:18 +0000 | [diff] [blame] | 248 | if (!connect_tombstone_server(g_target_thread, &g_tombstoned_socket, &g_output_fd, |
| 249 | &g_proto_fd, kDebuggerdAnyIntercept)) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 250 | // We failed to connect, not much we can do. |
| 251 | LOG(ERROR) << "failed to connected to tombstoned to report failure"; |
| 252 | _exit(1); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | dprintf(g_output_fd.get(), "crash_dump failed to dump process"); |
| 257 | if (g_target_thread != 1) { |
| 258 | dprintf(g_output_fd.get(), " %d: %s\n", g_target_thread, abort_msg); |
| 259 | } else { |
| 260 | dprintf(g_output_fd.get(), ": %s\n", abort_msg); |
| 261 | } |
| 262 | |
| 263 | _exit(1); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 264 | }); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 265 | } |
Josh Gao | e740250 | 2017-06-01 11:55:25 -0700 | [diff] [blame] | 266 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 267 | static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) { |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 268 | if (argc != 4) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 269 | LOG(FATAL) << "wrong number of args: " << argc << " (expected 4)"; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 272 | if (!android::base::ParseInt(argv[1], &g_target_thread, 1, std::numeric_limits<pid_t>::max())) { |
| 273 | LOG(FATAL) << "invalid target tid: " << argv[1]; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 276 | if (!android::base::ParseInt(argv[2], pseudothread_tid, 1, std::numeric_limits<pid_t>::max())) { |
Josh Gao | f6ad585 | 2017-02-15 12:21:11 -0800 | [diff] [blame] | 277 | LOG(FATAL) << "invalid pseudothread tid: " << argv[2]; |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 280 | int dump_type_int; |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 281 | if (!android::base::ParseInt(argv[3], &dump_type_int, 0)) { |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 282 | LOG(FATAL) << "invalid requested dump type: " << argv[3]; |
| 283 | } |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 284 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 285 | *dump_type = static_cast<DebuggerdDumpType>(dump_type_int); |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 286 | switch (*dump_type) { |
| 287 | case kDebuggerdNativeBacktrace: |
| 288 | case kDebuggerdTombstone: |
| 289 | case kDebuggerdTombstoneProto: |
| 290 | break; |
| 291 | |
| 292 | default: |
| 293 | LOG(FATAL) << "invalid requested dump type: " << dump_type_int; |
| 294 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 295 | } |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 296 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 297 | static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo, |
Mitch Phillips | 8fe5127 | 2023-02-07 17:02:30 -0800 | [diff] [blame] | 298 | std::unique_ptr<unwindstack::Regs>* regs, ProcessInfo* process_info, |
Mitch Phillips | 3140791 | 2024-03-14 14:18:47 +0100 | [diff] [blame] | 299 | bool* recoverable_crash) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 300 | std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf; |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 301 | CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 302 | ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf))); |
Mitch Phillips | 3140791 | 2024-03-14 14:18:47 +0100 | [diff] [blame] | 303 | *recoverable_crash = false; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 304 | if (rc == -1) { |
| 305 | PLOG(FATAL) << "failed to read target ucontext"; |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 306 | } else { |
| 307 | ssize_t expected_size = 0; |
| 308 | switch (crash_info->header.version) { |
| 309 | case 1: |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 310 | case 2: |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 311 | case 3: |
| 312 | expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataStatic); |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 313 | break; |
| 314 | |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 315 | case 4: |
| 316 | expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataDynamic); |
Mitch Phillips | e0b4bb1 | 2020-02-14 14:54:31 -0800 | [diff] [blame] | 317 | break; |
| 318 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 319 | default: |
| 320 | LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version; |
| 321 | break; |
| 322 | }; |
| 323 | |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 324 | if (rc < expected_size) { |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 325 | LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected " |
| 326 | << expected_size; |
| 327 | } |
Josh Gao | c7fe060 | 2017-03-13 14:13:29 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 330 | switch (crash_info->header.version) { |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 331 | case 4: |
| 332 | process_info->fdsan_table_address = crash_info->data.d.fdsan_table_address; |
| 333 | process_info->gwp_asan_state = crash_info->data.d.gwp_asan_state; |
| 334 | process_info->gwp_asan_metadata = crash_info->data.d.gwp_asan_metadata; |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 335 | process_info->scudo_stack_depot = crash_info->data.d.scudo_stack_depot; |
Florian Mayer | e8fcfee | 2023-12-04 16:39:15 -0800 | [diff] [blame] | 336 | process_info->scudo_stack_depot_size = crash_info->data.d.scudo_stack_depot_size; |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 337 | process_info->scudo_region_info = crash_info->data.d.scudo_region_info; |
Peter Collingbourne | bb4b49c | 2021-01-06 21:02:19 -0800 | [diff] [blame] | 338 | process_info->scudo_ring_buffer = crash_info->data.d.scudo_ring_buffer; |
Florian Mayer | bd49c38 | 2022-12-22 16:15:23 -0800 | [diff] [blame] | 339 | process_info->scudo_ring_buffer_size = crash_info->data.d.scudo_ring_buffer_size; |
Mitch Phillips | 3140791 | 2024-03-14 14:18:47 +0100 | [diff] [blame] | 340 | *recoverable_crash = crash_info->data.d.recoverable_crash; |
Florian Mayer | 5fa6663 | 2024-02-07 16:42:23 -0800 | [diff] [blame] | 341 | process_info->crash_detail_page = crash_info->data.d.crash_detail_page; |
Josh Gao | 8d44b14 | 2018-09-18 13:22:22 -0700 | [diff] [blame] | 342 | FALLTHROUGH_INTENDED; |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 343 | case 1: |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 344 | case 2: |
| 345 | case 3: |
| 346 | process_info->abort_msg_address = crash_info->data.s.abort_msg_address; |
| 347 | *siginfo = crash_info->data.s.siginfo; |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 348 | if (signal_has_si_addr(siginfo)) { |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 349 | process_info->has_fault_address = true; |
Mitch Phillips | e4adff0 | 2021-01-21 20:41:50 -0800 | [diff] [blame] | 350 | process_info->maybe_tagged_fault_address = reinterpret_cast<uintptr_t>(siginfo->si_addr); |
| 351 | process_info->untagged_fault_address = |
| 352 | untag_address(reinterpret_cast<uintptr_t>(siginfo->si_addr)); |
Peter Collingbourne | f03af88 | 2020-03-20 18:09:00 -0700 | [diff] [blame] | 353 | } |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 354 | regs->reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), |
Peter Collingbourne | f3d542f | 2020-03-05 16:46:15 -0800 | [diff] [blame] | 355 | &crash_info->data.s.ucontext)); |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 356 | break; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 357 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 358 | default: |
| 359 | __builtin_unreachable(); |
| 360 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Wait for a process to clone and return the child's pid. |
| 364 | // Note: this leaves the parent in PTRACE_EVENT_STOP. |
| 365 | static pid_t wait_for_clone(pid_t pid, bool resume_child) { |
| 366 | int status; |
| 367 | pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL)); |
| 368 | if (result == -1) { |
| 369 | PLOG(FATAL) << "failed to waitpid"; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 372 | if (WIFEXITED(status)) { |
| 373 | LOG(FATAL) << "traced process exited with status " << WEXITSTATUS(status); |
| 374 | } else if (WIFSIGNALED(status)) { |
| 375 | LOG(FATAL) << "traced process exited with signal " << WTERMSIG(status); |
| 376 | } else if (!WIFSTOPPED(status)) { |
| 377 | LOG(FATAL) << "process didn't stop? (status = " << status << ")"; |
| 378 | } |
| 379 | |
| 380 | if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) { |
| 381 | LOG(FATAL) << "process didn't stop due to PTRACE_O_TRACECLONE (status = " << status << ")"; |
| 382 | } |
| 383 | |
| 384 | pid_t child; |
| 385 | if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &child) != 0) { |
| 386 | PLOG(FATAL) << "failed to get child pid via PTRACE_GETEVENTMSG"; |
| 387 | } |
| 388 | |
| 389 | int stop_signal; |
| 390 | if (!wait_for_stop(child, &stop_signal)) { |
| 391 | PLOG(FATAL) << "failed to waitpid on child"; |
| 392 | } |
| 393 | |
| 394 | CHECK_EQ(0, stop_signal); |
| 395 | |
| 396 | if (resume_child) { |
| 397 | if (ptrace(PTRACE_CONT, child, 0, 0) != 0) { |
| 398 | PLOG(FATAL) << "failed to resume child (pid = " << child << ")"; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | return child; |
| 403 | } |
| 404 | |
| 405 | static pid_t wait_for_vm_process(pid_t pseudothread_tid) { |
| 406 | // The pseudothread will double-fork, we want its grandchild. |
| 407 | pid_t intermediate = wait_for_clone(pseudothread_tid, true); |
| 408 | pid_t vm_pid = wait_for_clone(intermediate, false); |
| 409 | if (ptrace(PTRACE_DETACH, intermediate, 0, 0) != 0) { |
| 410 | PLOG(FATAL) << "failed to detach from intermediate vm process"; |
| 411 | } |
| 412 | |
| 413 | return vm_pid; |
| 414 | } |
| 415 | |
Jinguang Dong | 8ac2f27 | 2018-11-24 17:12:33 +0800 | [diff] [blame] | 416 | static void InstallSigPipeHandler() { |
| 417 | struct sigaction action = {}; |
| 418 | action.sa_handler = SIG_IGN; |
| 419 | action.sa_flags = SA_RESTART; |
| 420 | sigaction(SIGPIPE, &action, nullptr); |
| 421 | } |
| 422 | |
Sijie Chen | cb53fe1 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 423 | static bool PtracePeek(int request, pid_t tid, uintptr_t addr, void* data, std::string_view err_msg, |
| 424 | uintptr_t* result) { |
| 425 | errno = 0; |
| 426 | *result = ptrace(request, tid, addr, data); |
| 427 | if (errno != 0) { |
| 428 | PLOG(ERROR) << err_msg; |
| 429 | return false; |
| 430 | } |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | static bool GetGuestRegistersFromCrashedProcess([[maybe_unused]] pid_t tid, |
| 435 | NativeBridgeGuestRegs* guest_regs) { |
| 436 | auto process_memory = unwindstack::Memory::CreateProcessMemoryCached(tid); |
| 437 | |
| 438 | uintptr_t header_ptr = 0; |
| 439 | uintptr_t base = 0; |
| 440 | #if defined(__x86_64__) |
| 441 | if (!PtracePeek(PTRACE_PEEKUSER, tid, offsetof(user_regs_struct, fs_base), nullptr, |
| 442 | "failed to read thread register for thread " + std::to_string(tid), &base)) { |
| 443 | return false; |
| 444 | } |
| 445 | #elif defined(__aarch64__) |
| 446 | // base is implicitly casted to uint64_t. |
| 447 | struct iovec pt_iov { |
| 448 | .iov_base = &base, .iov_len = sizeof(base), |
| 449 | }; |
| 450 | |
| 451 | if (ptrace(PTRACE_GETREGSET, tid, NT_ARM_TLS, &pt_iov) != 0) { |
| 452 | PLOG(ERROR) << "failed to read thread register for thread " << tid; |
| 453 | } |
| 454 | #else |
| 455 | // TODO(b/339287219): Add case for Riscv host. |
| 456 | return false; |
| 457 | #endif |
| 458 | auto ptr_to_guest_slot = base + TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE * sizeof(uintptr_t); |
| 459 | if (!process_memory->ReadFully(ptr_to_guest_slot, &header_ptr, sizeof(uintptr_t))) { |
| 460 | PLOG(ERROR) << "failed to get guest state TLS slot content for thread " << tid; |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | NativeBridgeGuestStateHeader header; |
| 465 | if (!process_memory->ReadFully(header_ptr, &header, sizeof(NativeBridgeGuestStateHeader))) { |
| 466 | PLOG(ERROR) << "failed to get the guest state header for thread " << tid; |
| 467 | return false; |
| 468 | } |
| 469 | if (header.signature != NATIVE_BRIDGE_GUEST_STATE_SIGNATURE) { |
| 470 | // Return when ptr points to unmapped memory or no valid guest state. |
| 471 | return false; |
| 472 | } |
| 473 | auto guest_state_data_copy = std::make_unique<unsigned char[]>(header.guest_state_data_size); |
| 474 | if (!process_memory->ReadFully(reinterpret_cast<uintptr_t>(header.guest_state_data), |
| 475 | guest_state_data_copy.get(), header.guest_state_data_size)) { |
| 476 | PLOG(ERROR) << "failed to read the guest state data for thread " << tid; |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | LoadGuestStateRegisters(guest_state_data_copy.get(), header.guest_state_data_size, guest_regs); |
| 481 | return true; |
| 482 | } |
| 483 | |
| 484 | static void ReadGuestRegisters([[maybe_unused]] std::unique_ptr<unwindstack::Regs>* regs, |
| 485 | pid_t tid) { |
| 486 | // TODO: remove [[maybe_unused]], when the ARM32 case is removed from the native bridge support. |
| 487 | NativeBridgeGuestRegs guest_regs; |
| 488 | if (!GetGuestRegistersFromCrashedProcess(tid, &guest_regs)) { |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | switch (guest_regs.guest_arch) { |
| 493 | #if defined(__LP64__) |
| 494 | case NATIVE_BRIDGE_ARCH_ARM64: { |
| 495 | unwindstack::arm64_user_regs arm64_user_regs = {}; |
| 496 | for (size_t i = 0; i < unwindstack::ARM64_REG_R31; i++) { |
| 497 | arm64_user_regs.regs[i] = guest_regs.regs_arm64.x[i]; |
| 498 | } |
| 499 | arm64_user_regs.sp = guest_regs.regs_arm64.sp; |
| 500 | arm64_user_regs.pc = guest_regs.regs_arm64.ip; |
| 501 | regs->reset(unwindstack::RegsArm64::Read(&arm64_user_regs)); |
Sijie Chen | c802793 | 2024-05-10 18:40:48 +0000 | [diff] [blame^] | 502 | |
| 503 | g_guest_arch = Architecture::ARM64; |
Sijie Chen | cb53fe1 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 504 | break; |
| 505 | } |
| 506 | case NATIVE_BRIDGE_ARCH_RISCV64: { |
| 507 | unwindstack::riscv64_user_regs riscv64_user_regs = {}; |
| 508 | // RISCV64_REG_PC is at the first position. |
| 509 | riscv64_user_regs.regs[0] = guest_regs.regs_riscv64.ip; |
| 510 | for (size_t i = 1; i < unwindstack::RISCV64_REG_REAL_COUNT; i++) { |
| 511 | riscv64_user_regs.regs[i] = guest_regs.regs_riscv64.x[i]; |
| 512 | } |
| 513 | regs->reset(unwindstack::RegsRiscv64::Read(&riscv64_user_regs, tid)); |
Sijie Chen | c802793 | 2024-05-10 18:40:48 +0000 | [diff] [blame^] | 514 | |
| 515 | g_guest_arch = Architecture::RISCV64; |
Sijie Chen | cb53fe1 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 516 | break; |
| 517 | } |
| 518 | #endif |
| 519 | default: |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 524 | int main(int argc, char** argv) { |
Josh Gao | 38ac45d | 2018-04-27 13:31:47 -0700 | [diff] [blame] | 525 | DefuseSignalHandlers(); |
Jinguang Dong | 8ac2f27 | 2018-11-24 17:12:33 +0800 | [diff] [blame] | 526 | InstallSigPipeHandler(); |
Josh Gao | 38ac45d | 2018-04-27 13:31:47 -0700 | [diff] [blame] | 527 | |
Josh Gao | 18cb681 | 2019-04-16 13:17:08 -0700 | [diff] [blame] | 528 | // There appears to be a bug in the kernel where our death causes SIGHUP to |
| 529 | // be sent to our process group if we exit while it has stopped jobs (e.g. |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 530 | // because of wait_for_debugger). Use setsid to create a new process group to |
Josh Gao | 18cb681 | 2019-04-16 13:17:08 -0700 | [diff] [blame] | 531 | // avoid hitting this. |
| 532 | setsid(); |
| 533 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 534 | atrace_begin(ATRACE_TAG, "before reparent"); |
| 535 | pid_t target_process = getppid(); |
| 536 | |
| 537 | // Open /proc/`getppid()` before we daemonize. |
| 538 | std::string target_proc_path = "/proc/" + std::to_string(target_process); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 539 | int target_proc_fd = open(target_proc_path.c_str(), O_DIRECTORY | O_RDONLY); |
| 540 | if (target_proc_fd == -1) { |
| 541 | PLOG(FATAL) << "failed to open " << target_proc_path; |
| 542 | } |
| 543 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 544 | // Make sure getppid() hasn't changed. |
| 545 | if (getppid() != target_process) { |
| 546 | LOG(FATAL) << "parent died"; |
Josh Gao | 2a18b82 | 2017-02-16 19:17:28 -0800 | [diff] [blame] | 547 | } |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 548 | atrace_end(ATRACE_TAG); |
| 549 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 550 | // Reparent ourselves to init, so that the signal handler can waitpid on the |
| 551 | // original process to avoid leaving a zombie for non-fatal dumps. |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 552 | // Move the input/output pipes off of stdout/stderr, out of paranoia. |
| 553 | unique_fd output_pipe(dup(STDOUT_FILENO)); |
| 554 | unique_fd input_pipe(dup(STDIN_FILENO)); |
| 555 | |
| 556 | unique_fd fork_exit_read, fork_exit_write; |
| 557 | if (!Pipe(&fork_exit_read, &fork_exit_write)) { |
| 558 | PLOG(FATAL) << "failed to create pipe"; |
| 559 | } |
| 560 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 561 | pid_t forkpid = fork(); |
| 562 | if (forkpid == -1) { |
| 563 | PLOG(FATAL) << "fork failed"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 564 | } else if (forkpid == 0) { |
| 565 | fork_exit_read.reset(); |
| 566 | } else { |
| 567 | // We need the pseudothread to live until we get around to verifying the vm pid against it. |
| 568 | // The last thing it does is block on a waitpid on us, so wait until our child tells us to die. |
| 569 | fork_exit_write.reset(); |
| 570 | char buf; |
| 571 | TEMP_FAILURE_RETRY(read(fork_exit_read.get(), &buf, sizeof(buf))); |
| 572 | _exit(0); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 575 | ATRACE_NAME("after reparent"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 576 | pid_t pseudothread_tid; |
| 577 | DebuggerdDumpType dump_type; |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 578 | ProcessInfo process_info; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 579 | |
| 580 | Initialize(argv); |
| 581 | ParseArgs(argc, argv, &pseudothread_tid, &dump_type); |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 582 | |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 583 | // Die if we take too long. |
Andreas Gampe | b02851a | 2017-06-22 19:45:53 -0700 | [diff] [blame] | 584 | // |
| 585 | // Note: processes with many threads and minidebug-info can take a bit to |
| 586 | // unwind, do not make this too small. b/62828735 |
Peter Collingbourne | fb5eac9 | 2021-03-11 12:51:25 -0800 | [diff] [blame] | 587 | alarm(30 * android::base::HwTimeoutMultiplier()); |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 588 | |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 589 | // Collect the list of open files. |
| 590 | OpenFilesList open_files; |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 591 | { |
| 592 | ATRACE_NAME("open files"); |
Josh Gao | ce841d9 | 2018-08-06 18:26:42 -0700 | [diff] [blame] | 593 | populate_open_files_list(&open_files, g_target_thread); |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 594 | } |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 595 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 596 | // In order to reduce the duration that we pause the process for, we ptrace |
| 597 | // the threads, fetch their registers and associated information, and then |
| 598 | // fork a separate process as a snapshot of the process's address space. |
| 599 | std::set<pid_t> threads; |
| 600 | if (!android::procinfo::GetProcessTids(g_target_thread, &threads)) { |
| 601 | PLOG(FATAL) << "failed to get process threads"; |
| 602 | } |
Josh Gao | 3407d7c | 2017-06-13 17:21:12 +0000 | [diff] [blame] | 603 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 604 | std::map<pid_t, ThreadInfo> thread_info; |
| 605 | siginfo_t siginfo; |
| 606 | std::string error; |
Mitch Phillips | 3140791 | 2024-03-14 14:18:47 +0100 | [diff] [blame] | 607 | bool recoverable_crash = false; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 608 | |
| 609 | { |
| 610 | ATRACE_NAME("ptrace"); |
| 611 | for (pid_t thread : threads) { |
| 612 | // Trace the pseudothread separately, so we can use different options. |
| 613 | if (thread == pseudothread_tid) { |
| 614 | continue; |
| 615 | } |
| 616 | |
| 617 | if (!ptrace_seize_thread(target_proc_fd, thread, &error)) { |
| 618 | bool fatal = thread == g_target_thread; |
| 619 | LOG(fatal ? FATAL : WARNING) << error; |
| 620 | } |
| 621 | |
| 622 | ThreadInfo info; |
| 623 | info.pid = target_process; |
| 624 | info.tid = thread; |
Josh Gao | 5df504c | 2019-05-09 12:48:17 -0700 | [diff] [blame] | 625 | info.uid = getuid(); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 626 | info.thread_name = get_thread_name(thread); |
| 627 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 628 | unique_fd attr_fd(openat(target_proc_fd, "attr/current", O_RDONLY | O_CLOEXEC)); |
| 629 | if (!android::base::ReadFdToString(attr_fd, &info.selinux_label)) { |
| 630 | PLOG(WARNING) << "failed to read selinux label"; |
| 631 | } |
| 632 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 633 | if (!ptrace_interrupt(thread, &info.signo)) { |
| 634 | PLOG(WARNING) << "failed to ptrace interrupt thread " << thread; |
| 635 | ptrace(PTRACE_DETACH, thread, 0, 0); |
| 636 | continue; |
| 637 | } |
| 638 | |
Elliott Hughes | d13ea52 | 2022-01-13 09:20:26 -0800 | [diff] [blame] | 639 | struct iovec tagged_addr_iov = { |
Peter Collingbourne | 864f15d | 2020-09-14 20:27:36 -0700 | [diff] [blame] | 640 | &info.tagged_addr_ctrl, |
| 641 | sizeof(info.tagged_addr_ctrl), |
| 642 | }; |
| 643 | if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_TAGGED_ADDR_CTRL, |
Elliott Hughes | d13ea52 | 2022-01-13 09:20:26 -0800 | [diff] [blame] | 644 | reinterpret_cast<void*>(&tagged_addr_iov)) == -1) { |
Peter Collingbourne | 864f15d | 2020-09-14 20:27:36 -0700 | [diff] [blame] | 645 | info.tagged_addr_ctrl = -1; |
| 646 | } |
Peter Collingbourne | 864f15d | 2020-09-14 20:27:36 -0700 | [diff] [blame] | 647 | |
Elliott Hughes | d13ea52 | 2022-01-13 09:20:26 -0800 | [diff] [blame] | 648 | struct iovec pac_enabled_keys_iov = { |
| 649 | &info.pac_enabled_keys, |
| 650 | sizeof(info.pac_enabled_keys), |
| 651 | }; |
| 652 | if (ptrace(PTRACE_GETREGSET, thread, NT_ARM_PAC_ENABLED_KEYS, |
| 653 | reinterpret_cast<void*>(&pac_enabled_keys_iov)) == -1) { |
| 654 | info.pac_enabled_keys = -1; |
| 655 | } |
| 656 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 657 | if (thread == g_target_thread) { |
| 658 | // Read the thread's registers along with the rest of the crash info out of the pipe. |
Mitch Phillips | 3140791 | 2024-03-14 14:18:47 +0100 | [diff] [blame] | 659 | ReadCrashInfo(input_pipe, &siginfo, &info.registers, &process_info, &recoverable_crash); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 660 | info.siginfo = &siginfo; |
| 661 | info.signo = info.siginfo->si_signo; |
Josh Gao | 31348a7 | 2021-03-29 21:53:42 -0700 | [diff] [blame] | 662 | |
| 663 | info.command_line = get_command_line(g_target_thread); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 664 | } else { |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 665 | info.registers.reset(unwindstack::Regs::RemoteGet(thread)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 666 | if (!info.registers) { |
| 667 | PLOG(WARNING) << "failed to fetch registers for thread " << thread; |
| 668 | ptrace(PTRACE_DETACH, thread, 0, 0); |
| 669 | continue; |
| 670 | } |
| 671 | } |
Sijie Chen | cb53fe1 | 2024-05-10 18:40:48 +0000 | [diff] [blame] | 672 | ReadGuestRegisters(&info.guest_registers, thread); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 673 | |
| 674 | thread_info[thread] = std::move(info); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | // Trace the pseudothread with PTRACE_O_TRACECLONE and tell it to fork. |
| 679 | if (!ptrace_seize_thread(target_proc_fd, pseudothread_tid, &error, PTRACE_O_TRACECLONE)) { |
| 680 | LOG(FATAL) << "failed to seize pseudothread: " << error; |
| 681 | } |
| 682 | |
| 683 | if (TEMP_FAILURE_RETRY(write(output_pipe.get(), "\1", 1)) != 1) { |
| 684 | PLOG(FATAL) << "failed to write to pseudothread"; |
| 685 | } |
| 686 | |
| 687 | pid_t vm_pid = wait_for_vm_process(pseudothread_tid); |
| 688 | if (ptrace(PTRACE_DETACH, pseudothread_tid, 0, 0) != 0) { |
| 689 | PLOG(FATAL) << "failed to detach from pseudothread"; |
| 690 | } |
| 691 | |
| 692 | // The pseudothread can die now. |
| 693 | fork_exit_write.reset(); |
| 694 | |
| 695 | // Defer the message until later, for readability. |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 696 | bool wait_for_debugger = android::base::GetBoolProperty( |
| 697 | "debug.debuggerd.wait_for_debugger", |
| 698 | android::base::GetBoolProperty("debug.debuggerd.wait_for_gdb", false)); |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 699 | if (siginfo.si_signo == BIONIC_SIGNAL_DEBUGGER) { |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 700 | wait_for_debugger = false; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | // Detach from all of our attached threads before resuming. |
| 704 | for (const auto& [tid, thread] : thread_info) { |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 705 | int resume_signal = thread.signo == BIONIC_SIGNAL_DEBUGGER ? 0 : thread.signo; |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 706 | if (wait_for_debugger) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 707 | resume_signal = 0; |
| 708 | if (tgkill(target_process, tid, SIGSTOP) != 0) { |
| 709 | PLOG(WARNING) << "failed to send SIGSTOP to " << tid; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | LOG(DEBUG) << "detaching from thread " << tid; |
| 714 | if (ptrace(PTRACE_DETACH, tid, 0, resume_signal) != 0) { |
| 715 | PLOG(ERROR) << "failed to detach from thread " << tid; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | // Drop our capabilities now that we've fetched all of the information we need. |
Josh Gao | 2f11a25 | 2017-02-13 14:46:19 -0800 | [diff] [blame] | 720 | drop_capabilities(); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 721 | |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 722 | { |
| 723 | ATRACE_NAME("tombstoned_connect"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 724 | LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type; |
Shikha Panwar | abde59e | 2023-02-03 15:05:18 +0000 | [diff] [blame] | 725 | g_tombstoned_connected = connect_tombstone_server(g_target_thread, &g_tombstoned_socket, |
| 726 | &g_output_fd, &g_proto_fd, dump_type); |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 727 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 728 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 729 | if (g_tombstoned_connected) { |
| 730 | if (TEMP_FAILURE_RETRY(dup2(g_output_fd.get(), STDOUT_FILENO)) == -1) { |
| 731 | PLOG(ERROR) << "failed to dup2 output fd (" << g_output_fd.get() << ") to STDOUT_FILENO"; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 732 | } |
| 733 | } else { |
| 734 | unique_fd devnull(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); |
| 735 | TEMP_FAILURE_RETRY(dup2(devnull.get(), STDOUT_FILENO)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 736 | g_output_fd = std::move(devnull); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Josh Gao | 9da1f51 | 2018-08-06 15:38:29 -0700 | [diff] [blame] | 739 | LOG(INFO) << "performing dump of process " << target_process |
| 740 | << " (target tid = " << g_target_thread << ")"; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 741 | |
| 742 | int signo = siginfo.si_signo; |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 743 | bool fatal_signal = signo != BIONIC_SIGNAL_DEBUGGER; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 744 | bool backtrace = false; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 745 | |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 746 | // si_value is special when used with BIONIC_SIGNAL_DEBUGGER. |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 747 | // 0: dump tombstone |
| 748 | // 1: dump backtrace |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 749 | if (!fatal_signal) { |
| 750 | int si_val = siginfo.si_value.sival_int; |
| 751 | if (si_val == 0) { |
| 752 | backtrace = false; |
| 753 | } else if (si_val == 1) { |
| 754 | backtrace = true; |
| 755 | } else { |
| 756 | LOG(WARNING) << "unknown si_value value " << si_val; |
| 757 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 760 | // TODO: Use seccomp to lock ourselves down. |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 761 | |
| 762 | unwindstack::AndroidRemoteUnwinder unwinder(vm_pid, unwindstack::Regs::CurrentArch()); |
| 763 | unwindstack::ErrorData error_data; |
| 764 | if (!unwinder.Initialize(error_data)) { |
| 765 | LOG(FATAL) << "Failed to initialize unwinder object: " |
| 766 | << unwindstack::GetErrorCodeString(error_data.code); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 767 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 768 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 769 | std::string amfd_data; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 770 | if (backtrace) { |
Josh Gao | 8bb0390 | 2017-05-30 15:31:02 -0700 | [diff] [blame] | 771 | ATRACE_NAME("dump_backtrace"); |
Christopher Ferris | 60eb197 | 2019-01-15 15:18:43 -0800 | [diff] [blame] | 772 | dump_backtrace(std::move(g_output_fd), &unwinder, thread_info, g_target_thread); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 773 | } else { |
Josh Gao | ce841d9 | 2018-08-06 18:26:42 -0700 | [diff] [blame] | 774 | { |
| 775 | ATRACE_NAME("fdsan table dump"); |
Peter Collingbourne | 843f7e6 | 2020-02-28 19:07:33 -0800 | [diff] [blame] | 776 | populate_fdsan_table(&open_files, unwinder.GetProcessMemory(), |
| 777 | process_info.fdsan_table_address); |
Josh Gao | ce841d9 | 2018-08-06 18:26:42 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | { |
| 781 | ATRACE_NAME("engrave_tombstone"); |
Sijie Chen | c802793 | 2024-05-10 18:40:48 +0000 | [diff] [blame^] | 782 | unwindstack::ArchEnum regs_arch = unwindstack::ARCH_UNKNOWN; |
| 783 | switch (g_guest_arch) { |
| 784 | case Architecture::ARM32: { |
| 785 | regs_arch = unwindstack::ARCH_ARM; |
| 786 | break; |
| 787 | } |
| 788 | case Architecture::ARM64: { |
| 789 | regs_arch = unwindstack::ARCH_ARM64; |
| 790 | break; |
| 791 | } |
| 792 | case Architecture::RISCV64: { |
| 793 | regs_arch = unwindstack::ARCH_RISCV64; |
| 794 | break; |
| 795 | } |
| 796 | default: { |
| 797 | } |
| 798 | } |
| 799 | if (regs_arch == unwindstack::ARCH_UNKNOWN) { |
| 800 | engrave_tombstone(std::move(g_output_fd), std::move(g_proto_fd), &unwinder, thread_info, |
| 801 | g_target_thread, process_info, &open_files, &amfd_data); |
| 802 | } else { |
| 803 | unwindstack::AndroidRemoteUnwinder guest_unwinder(vm_pid, regs_arch); |
| 804 | engrave_tombstone(std::move(g_output_fd), std::move(g_proto_fd), &unwinder, thread_info, |
| 805 | g_target_thread, process_info, &open_files, &amfd_data, &g_guest_arch, |
| 806 | &guest_unwinder); |
| 807 | } |
Josh Gao | ce841d9 | 2018-08-06 18:26:42 -0700 | [diff] [blame] | 808 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Mitch Phillips | dd5a80d | 2023-03-13 15:08:17 -0700 | [diff] [blame] | 811 | if (fatal_signal) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 812 | // Don't try to notify ActivityManager if it just crashed, or we might hang until timeout. |
| 813 | if (thread_info[target_process].thread_name != "system_server") { |
Mitch Phillips | 3140791 | 2024-03-14 14:18:47 +0100 | [diff] [blame] | 814 | activity_manager_notify(target_process, signo, amfd_data, recoverable_crash); |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 815 | } |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 818 | if (wait_for_debugger) { |
Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 819 | // Use ALOGI to line up with output from engrave_tombstone. |
| 820 | ALOGI( |
dimitry | 6429e20 | 2017-09-12 10:46:20 +0200 | [diff] [blame] | 821 | "***********************************************************\n" |
| 822 | "* Process %d has been suspended while crashing.\n" |
Elliott Hughes | e4781d5 | 2021-03-17 09:15:15 -0700 | [diff] [blame] | 823 | "* To attach the debugger, run this on the host:\n" |
dimitry | 6429e20 | 2017-09-12 10:46:20 +0200 | [diff] [blame] | 824 | "*\n" |
Elliott Hughes | 5a4e625 | 2023-02-28 22:16:19 +0000 | [diff] [blame] | 825 | "* lldbclient.py -p %d\n" |
dimitry | 6429e20 | 2017-09-12 10:46:20 +0200 | [diff] [blame] | 826 | "*\n" |
| 827 | "***********************************************************", |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 828 | target_process, target_process); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | // Close stdout before we notify tombstoned of completion. |
| 832 | close(STDOUT_FILENO); |
Shikha Panwar | abde59e | 2023-02-03 15:05:18 +0000 | [diff] [blame] | 833 | if (g_tombstoned_connected && |
| 834 | !notify_completion(g_tombstoned_socket.get(), g_output_fd.get(), g_proto_fd.get())) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 835 | LOG(ERROR) << "failed to notify tombstoned of completion"; |
| 836 | } |
| 837 | |
| 838 | return 0; |
| 839 | } |