| 6 |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <stddef.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <sys/param.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <unistd.h> |
| 16 |
| 17 | #include <memory> |
| 18 | #include <vector> |
| 19 |
| 20 | #include "base/clang_profiling_buildflags.h" |
| 21 | #include "base/stl_util.h" |
| 22 | #include "base/threading/platform_thread.h" |
| 23 | #include "base/time/time.h" |
| 24 | #include "build/build_config.h" |
| 25 |
| 26 | #if defined(__GLIBCXX__) |
| 27 | #include <cxxabi.h> |
| 28 | #endif |
| 29 |
| 30 | #if defined(OS_MACOSX) |
| 31 | #include <AvailabilityMacros.h> |
| 32 | #endif |
| 33 |
| 34 | #if defined(OS_MACOSX) || defined(OS_BSD) |
| 35 | #include <sys/sysctl.h> |
| 36 | #endif |
| 37 |
| 38 | #if defined(OS_FREEBSD) |
| 39 | #include <sys/user.h> |
| 40 | #endif |
| 41 |
| 42 | #if defined(OS_FUCHSIA) |
| 43 | #include <zircon/process.h> |
| 44 | #include <zircon/syscalls.h> |
| 45 | #endif |
| 46 |
| 47 | #include <ostream> |
| 48 |
| 49 | #include "base/debug/alias.h" |
| 50 | #include "base/debug/debugging_buildflags.h" |
| 51 | #include "base/environment.h" |
| 52 | #include "base/files/file_util.h" |
| 53 | #include "base/logging.h" |
| 54 | #include "base/posix/eintr_wrapper.h" |
| 55 | #include "base/process/process.h" |
| 56 | #include "base/strings/string_number_conversions.h" |
| 57 | #include "base/strings/string_piece.h" |
| 58 |
| 59 | #if BUILDFLAG(CLANG_PROFILING) |
| 60 | #include "base/test/clang_profiling.h" |
| 61 | #endif |
| 62 |
| 63 | #if defined(USE_SYMBOLIZE) |
| 64 | #include "base/third_party/symbolize/symbolize.h" |
| 65 | #endif |
| 66 |
| 67 | namespace base { |
| 68 | namespace debug { |
| 69 |
| 70 | #if defined(OS_MACOSX) || defined(OS_BSD) |
| 71 |
| 72 | // Based on Apple's recommended method as described in |
| 73 | // http://developer.apple.com/qa/qa2004/qa1361.html |
| 74 | bool BeingDebugged() { |
| 75 | // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 76 | // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 77 | // |
| 78 | // While some code used below may be async-signal unsafe, note how |
| 79 | // the result is cached (see |is_set| and |being_debugged| static variables |
| 80 | // right below). If this code is properly warmed-up early |
| 81 | // in the start-up process, it should be safe to use later. |
| 82 |
| 83 | // If the process is sandboxed then we can't use the sysctl, so cache the |
| 84 | // value. |
| 85 | static bool is_set = false; |
| 86 | static bool being_debugged = false; |
| 87 |
| 88 | if (is_set) |
| 89 | return being_debugged; |
| 90 |
| 91 | // Initialize mib, which tells sysctl what info we want. In this case, |
| 92 | // we're looking for information about a specific process ID. |
| 93 | int mib[] = { |
| 94 | CTL_KERN, |
| 95 | KERN_PROC, |
| 96 | KERN_PROC_PID, |
| 97 | getpid() |
| 98 | #if defined(OS_OPENBSD) |
| 99 | , sizeof(struct kinfo_proc), |
| 100 | 0 |
| 101 | #endif |
| 102 | }; |
| 103 |
| 104 | // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and |
| 105 | // binary interfaces may change. |
| 106 | struct kinfo_proc info; |
| 107 | size_t info_size = sizeof(info); |
| 108 |
| 109 | #if defined(OS_OPENBSD) |
| 110 | if (sysctl(mib, base::size(mib), NULL, &info_size, NULL, 0) < 0) |
| 111 | return -1; |
| 112 |
| 113 | mib[5] = (info_size / sizeof(struct kinfo_proc)); |
| 114 | #endif |
| 115 |
| 116 | int sysctl_result = sysctl(mib, base::size(mib), &info, &info_size, NULL, 0); |
| 117 | DCHECK_EQ(sysctl_result, 0); |
| 118 | if (sysctl_result != 0) { |
| 119 | is_set = true; |
| 120 | being_debugged = false; |
| 121 | return being_debugged; |
| 122 | } |
| 123 |
| 124 | // This process is being debugged if the P_TRACED flag is set. |
| 125 | is_set = true; |
| 126 | #if defined(OS_FREEBSD) |
| 127 | being_debugged = (info.ki_flag & P_TRACED) != 0; |
| 128 | #elif defined(OS_BSD) |
| 129 | being_debugged = (info.p_flag & P_TRACED) != 0; |
| 130 | #else |
| 131 | being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0; |
| 132 | #endif |
| 133 | return being_debugged; |
| 134 | } |
| 135 |
| 136 | void VerifyDebugger() { |
| 137 | #if BUILDFLAG(ENABLE_LLDBINIT_WARNING) |
| 138 | if (Environment::Create()->HasVar("CHROMIUM_LLDBINIT_SOURCED")) |
| 139 | return; |
| 140 | if (!BeingDebugged()) |
| 141 | return; |
| 142 | DCHECK(false) |
| 143 | << "Detected lldb without sourcing //tools/lldb/lldbinit.py. lldb may " |
| 144 | "not be able to find debug symbols. Please see debug instructions for " |
| 145 | "using //tools/lldb/lldbinit.py:\n" |
| 146 | "https://chromium.googlesource.com/chromium/src/+/master/docs/" |
| 147 | "lldbinit.md\n" |
| 148 | "To continue anyway, type 'continue' in lldb. To always skip this " |
| 149 | "check, define an environment variable CHROMIUM_LLDBINIT_SOURCED=1"; |
| 150 | #endif |
| 151 | } |
| 152 |
| 153 | #elif defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX) |
| 154 |
| 155 | // We can look in /proc/self/status for TracerPid. We are likely used in crash |
| 156 | // handling, so we are careful not to use the heap or have side effects. |
| 157 | // Another option that is common is to try to ptrace yourself, but then we |
| 158 | // can't detach without forking(), and that's not so great. |
| 159 | // static |
| 160 | Process GetDebuggerProcess() { |
| 161 | // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 162 | // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 163 |
| 164 | int status_fd = open("/proc/self/status", O_RDONLY); |
| 165 | if (status_fd == -1) |
| 166 | return Process(); |
| 167 |
| 168 | // We assume our line will be in the first 1024 characters and that we can |
| 169 | // read this much all at once. In practice this will generally be true. |
| 170 | // This simplifies and speeds up things considerably. |
| 171 | char buf[1024]; |
| 172 |
| 173 | ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf))); |
| 174 | if (IGNORE_EINTR(close(status_fd)) < 0) |
| 175 | return Process(); |
| 176 |
| 177 | if (num_read <= 0) |
| 178 | return Process(); |
| 179 |
| 180 | StringPiece status(buf, num_read); |
| 181 | StringPiece tracer("TracerPid:\t"); |
| 182 |
| 183 | StringPiece::size_type pid_index = status.find(tracer); |
| 184 | if (pid_index == StringPiece::npos) |
| 185 | return Process(); |
| 186 | pid_index += tracer.size(); |
| 187 | StringPiece::size_type pid_end_index = status.find('\n', pid_index); |
| 188 | if (pid_end_index == StringPiece::npos) |
| 189 | return Process(); |
| 190 |
| 191 | StringPiece pid_str(buf + pid_index, pid_end_index - pid_index); |
| 192 | int pid = 0; |
| 193 | if (!StringToInt(pid_str, &pid)) |
| 194 | return Process(); |
| 195 |
| 196 | return Process(pid); |
| 197 | } |
| 198 |
| 199 | bool BeingDebugged() { |
| 200 | return GetDebuggerProcess().IsValid(); |
| 201 | } |
| 202 |
| 203 | void VerifyDebugger() { |
| 204 | #if BUILDFLAG(ENABLE_GDBINIT_WARNING) |
| 205 | // Quick check before potentially slower GetDebuggerProcess(). |
| 206 | if (Environment::Create()->HasVar("CHROMIUM_GDBINIT_SOURCED")) |
| 207 | return; |
| 208 |
| 209 | Process proc = GetDebuggerProcess(); |
| 210 | if (!proc.IsValid()) |
| 211 | return; |
| 212 |
| 213 | FilePath cmdline_file = |
| 214 | FilePath("/proc").Append(NumberToString(proc.Handle())).Append("cmdline"); |
| 215 | std::string cmdline; |
| 216 | if (!ReadFileToString(cmdline_file, &cmdline)) |
| 217 | return; |
| 218 |
| 219 | // /proc/*/cmdline separates arguments with null bytes, but we only care about |
| 220 | // the executable name, so interpret |cmdline| as a null-terminated C string |
| 221 | // to extract the exe portion. |
| 222 | StringPiece exe(cmdline.c_str()); |
| 223 |
| 224 | DCHECK(ToLowerASCII(exe).find("gdb") == std::string::npos) |
| 225 | << "Detected gdb without sourcing //tools/gdb/gdbinit. gdb may not be " |
| 226 | "able to find debug symbols, and pretty-printing of STL types may not " |
| 227 | "work. Please see debug instructions for using //tools/gdb/gdbinit:\n" |
| 228 | "https://chromium.googlesource.com/chromium/src/+/master/docs/" |
| 229 | "gdbinit.md\n" |
| 230 | "To continue anyway, type 'continue' in gdb. To always skip this " |
| 231 | "check, define an environment variable CHROMIUM_GDBINIT_SOURCED=1"; |
| 232 | #endif |
| 233 | } |
| 234 |
| 235 | #elif defined(OS_FUCHSIA) |
| 236 |
| 237 | bool BeingDebugged() { |
| 238 | zx_info_process_t info = {}; |
| 239 | // Ignore failures. The 0-initialization above will result in "false" for |
| 240 | // error cases. |
| 241 | zx_object_get_info(zx_process_self(), ZX_INFO_PROCESS, &info, sizeof(info), |
| 242 | nullptr, nullptr); |
| 243 | return info.debugger_attached; |
| 244 | } |
| 245 |
| 246 | void VerifyDebugger() {} |
| 247 |
| 248 | #else |
| 249 |
| 250 | bool BeingDebugged() { |
| 251 | NOTIMPLEMENTED(); |
| 252 | return false; |
| 253 | } |
| 254 |
| 255 | void VerifyDebugger() {} |
| 256 |
| 257 | #endif |
| 258 |
| 259 | // We want to break into the debugger in Debug mode, and cause a crash dump in |
| 260 | // Release mode. Breakpad behaves as follows: |
| 261 | // |
| 262 | // +-------+-----------------+-----------------+ |
| 263 | // | OS | Dump on SIGTRAP | Dump on SIGABRT | |
| 264 | // +-------+-----------------+-----------------+ |
| 265 | // | Linux | N | Y | |
| 266 | // | Mac | Y | N | |
| 267 | // +-------+-----------------+-----------------+ |
| 268 | // |
| 269 | // Thus we do the following: |
| 270 | // Linux: Debug mode if a debugger is attached, send SIGTRAP; otherwise send |
| 271 | // SIGABRT |
| 272 | // Mac: Always send SIGTRAP. |
| 273 |
| 274 | #if defined(ARCH_CPU_ARMEL) |
| 275 | #define DEBUG_BREAK_ASM() asm("bkpt 0") |
| 276 | #elif defined(ARCH_CPU_ARM64) |
| 277 | #define DEBUG_BREAK_ASM() asm("brk 0") |
| 278 | #elif defined(ARCH_CPU_MIPS_FAMILY) |
| 279 | #define DEBUG_BREAK_ASM() asm("break 2") |
| 280 | #elif defined(ARCH_CPU_X86_FAMILY) |
| 281 | #define DEBUG_BREAK_ASM() asm("int3") |
| 282 | #endif |
| 283 |
| 284 | #if defined(NDEBUG) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 285 | #define DEBUG_BREAK() abort() |
| 286 | #elif defined(OS_NACL) |
| 287 | // The NaCl verifier doesn't let use use int3. For now, we call abort(). We |
| 288 | // should ask for advice from some NaCl experts about the optimum thing here. |
| 289 | // http://code.google.com/p/nativeclient/issues/detail?id=645 |
| 290 | #define DEBUG_BREAK() abort() |
| 291 | #elif !defined(OS_MACOSX) |
| 292 | // Though Android has a "helpful" process called debuggerd to catch native |
| 293 | // signals on the general assumption that they are fatal errors. If no debugger |
| 294 | // is attached, we call abort since Breakpad needs SIGABRT to create a dump. |
| 295 | // When debugger is attached, for ARM platform the bkpt instruction appears |
| 296 | // to cause SIGBUS which is trapped by debuggerd, and we've had great |
| 297 | // difficulty continuing in a debugger once we stop from SIG triggered by native |
| 298 | // code, use GDB to set |go| to 1 to resume execution; for X86 platform, use |
| 299 | // "int3" to setup breakpiont and raise SIGTRAP. |
| 300 | // |
| 301 | // On other POSIX architectures, except Mac OS X, we use the same logic to |
| 302 | // ensure that breakpad creates a dump on crashes while it is still possible to |
| 303 | // use a debugger. |
| 304 | namespace { |
| 305 | void DebugBreak() { |
| 306 | if (!BeingDebugged()) { |
| 307 | abort(); |
| 308 | } else { |
| 309 | #if defined(DEBUG_BREAK_ASM) |
| 310 | DEBUG_BREAK_ASM(); |
| 311 | #else |
| 312 | volatile int go = 0; |
| 313 | while (!go) |
| 314 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
| 315 | #endif |
| 316 | } |
| 317 | } |
| 318 | } // namespace |
| 319 | #define DEBUG_BREAK() DebugBreak() |
| 320 | #elif defined(DEBUG_BREAK_ASM) |
| 321 | #define DEBUG_BREAK() DEBUG_BREAK_ASM() |
| 322 | #else |
| 323 | #error "Don't know how to debug break on this architecture/OS" |
| 324 | #endif |
| 325 |
| 326 | void BreakDebugger() { |
| 327 | #if BUILDFLAG(CLANG_PROFILING) |
| 328 | WriteClangProfilingProfile(); |
| 329 | #endif |
| 330 |
| 331 | // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 332 | // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 333 |
| 334 | // Linker's ICF feature may merge this function with other functions with the |
| 335 | // same definition (e.g. any function whose sole job is to call abort()) and |
| 336 | // it may confuse the crash report processing system. http://crbug.com/508489 |
| 337 | static int static_variable_to_make_this_function_unique = 0; |
| 338 | Alias(&static_variable_to_make_this_function_unique); |
| 339 |
| 340 | DEBUG_BREAK(); |
| 341 | #if defined(OS_ANDROID) && !defined(OFFICIAL_BUILD) |
| 342 | // For Android development we always build release (debug builds are |
| 343 | // unmanageably large), so the unofficial build is used for debugging. It is |
| 344 | // helpful to be able to insert BreakDebugger() statements in the source, |
| 345 | // attach the debugger, inspect the state of the program and then resume it by |
| 346 | // setting the 'go' variable above. |
| 347 | #elif defined(NDEBUG) |
| 348 | // Terminate the program after signaling the debug break. |
| 349 | // When DEBUG_BREAK() expands to abort(), this is unreachable code. Rather |
| 350 | // than carefully tracking in which cases DEBUG_BREAK()s is noreturn, just |
| 351 | // disable the unreachable code warning here. |
| 352 | #pragma GCC diagnostic push |
| 353 | #pragma GCC diagnostic ignored "-Wunreachable-code" |
| 354 | _exit(1); |
| 355 | #pragma GCC diagnostic pop |
| 356 | #endif |
| 357 | } |
| 358 |
| 359 | } // namespace debug |
| 360 | } // namespace base |
| 361 |