[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 4 | |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 5 | #include "build/build_config.h" |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 6 | #include "base/debug_util.h" |
| 7 | |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 8 | #include <errno.h> |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 9 | #include <execinfo.h> |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 10 | #include <fcntl.h> |
[email protected] | d1fc437 | 2009-01-16 22:06:19 | [diff] [blame] | 11 | #include <stdio.h> |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 12 | #include <sys/stat.h> |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 13 | #include <sys/sysctl.h> |
| 14 | #include <sys/types.h> |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 15 | #include <unistd.h> |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 16 | |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 17 | #include "base/basictypes.h" |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 18 | #include "base/eintr_wrapper.h" |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 19 | #include "base/logging.h" |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 20 | #include "base/scoped_ptr.h" |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 21 | #include "base/string_piece.h" |
| 22 | |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 23 | // static |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 24 | bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) { |
| 25 | NOTIMPLEMENTED(); |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | #if defined(OS_MACOSX) |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 30 | |
| 31 | // Based on Apple's recommended method as described in |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 32 | // http://developer.apple.com/qa/qa2004/qa1361.html |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 33 | // static |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 34 | bool DebugUtil::BeingDebugged() { |
[email protected] | dbd9bfc | 2009-02-05 18:25:20 | [diff] [blame] | 35 | // If the process is sandboxed then we can't use the sysctl, so cache the |
| 36 | // value. |
| 37 | static bool is_set = false; |
| 38 | static bool being_debugged = false; |
| 39 | |
| 40 | if (is_set) { |
| 41 | return being_debugged; |
| 42 | } |
| 43 | |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 44 | // Initialize mib, which tells sysctl what info we want. In this case, |
| 45 | // we're looking for information about a specific process ID. |
| 46 | int mib[] = { |
| 47 | CTL_KERN, |
| 48 | KERN_PROC, |
| 49 | KERN_PROC_PID, |
| 50 | getpid() |
| 51 | }; |
| 52 | |
| 53 | // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and |
| 54 | // binary interfaces may change. |
| 55 | struct kinfo_proc info; |
| 56 | size_t info_size = sizeof(info); |
| 57 | |
| 58 | int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0); |
| 59 | DCHECK(sysctl_result == 0); |
[email protected] | dbd9bfc | 2009-02-05 18:25:20 | [diff] [blame] | 60 | if (sysctl_result != 0) { |
| 61 | is_set = true; |
| 62 | being_debugged = false; |
| 63 | return being_debugged; |
| 64 | } |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 65 | |
| 66 | // This process is being debugged if the P_TRACED flag is set. |
[email protected] | dbd9bfc | 2009-02-05 18:25:20 | [diff] [blame] | 67 | is_set = true; |
| 68 | being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0; |
| 69 | return being_debugged; |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | #elif defined(OS_LINUX) |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 73 | |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 74 | // We can look in /proc/self/status for TracerPid. We are likely used in crash |
| 75 | // handling, so we are careful not to use the heap or have side effects. |
| 76 | // Another option that is common is to try to ptrace yourself, but then we |
| 77 | // can't detach without forking(), and that's not so great. |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 78 | // static |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 79 | bool DebugUtil::BeingDebugged() { |
| 80 | int status_fd = open("/proc/self/status", O_RDONLY); |
| 81 | if (status_fd == -1) |
| 82 | return false; |
| 83 | |
| 84 | // We assume our line will be in the first 1024 characters and that we can |
| 85 | // read this much all at once. In practice this will generally be true. |
| 86 | // This simplifies and speeds up things considerably. |
| 87 | char buf[1024]; |
| 88 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 89 | ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf))); |
| 90 | HANDLE_EINTR(close(status_fd)); |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 91 | |
| 92 | if (num_read <= 0) |
| 93 | return false; |
| 94 | |
| 95 | StringPiece status(buf, num_read); |
| 96 | StringPiece tracer("TracerPid:\t"); |
| 97 | |
| 98 | StringPiece::size_type pid_index = status.find(tracer); |
| 99 | if (pid_index == StringPiece::npos) |
| 100 | return false; |
| 101 | |
| 102 | // Our pid is 0 without a debugger, assume this for any pid starting with 0. |
| 103 | pid_index += tracer.size(); |
| 104 | return pid_index < status.size() && status[pid_index] != '0'; |
| 105 | } |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 106 | |
| 107 | #endif // OS_LINUX |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 108 | |
| 109 | // static |
| 110 | void DebugUtil::BreakDebugger() { |
[email protected] | 95fe390 | 2009-05-04 21:13:42 | [diff] [blame^] | 111 | #if !defined(ARCH_CPU_ARM_FAMILY) |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 112 | asm ("int3"); |
[email protected] | 95fe390 | 2009-05-04 21:13:42 | [diff] [blame^] | 113 | #endif |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 114 | } |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 115 | |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 116 | StackTrace::StackTrace() { |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 117 | const int kMaxCallers = 256; |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 118 | |
| 119 | void* callers[kMaxCallers]; |
| 120 | int count = backtrace(callers, kMaxCallers); |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 121 | |
| 122 | // Though the backtrace API man page does not list any possible negative |
| 123 | // return values, we still still exclude them because they would break the |
| 124 | // memcpy code below. |
| 125 | if (count > 0) { |
| 126 | trace_.resize(count); |
| 127 | memcpy(&trace_[0], callers, sizeof(callers[0]) * count); |
| 128 | } else { |
| 129 | trace_.resize(0); |
| 130 | } |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void StackTrace::PrintBacktrace() { |
| 134 | fflush(stderr); |
| 135 | backtrace_symbols_fd(&trace_[0], trace_.size(), STDERR_FILENO); |
| 136 | } |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 137 | |
| 138 | void StackTrace::OutputToStream(std::ostream* os) { |
| 139 | scoped_ptr_malloc<char*> trace_symbols( |
| 140 | backtrace_symbols(&trace_[0], trace_.size())); |
| 141 | |
| 142 | // If we can't retrieve the symbols, print an error and just dump the raw |
| 143 | // addresses. |
| 144 | if (trace_symbols.get() == NULL) { |
| 145 | (*os) << "Unable get symbols for backtrace (" << strerror(errno) |
| 146 | << "). Dumping raw addresses in trace:\n"; |
| 147 | for (size_t i = 0; i < trace_.size(); ++i) { |
| 148 | (*os) << "\t" << trace_[i] << "\n"; |
| 149 | } |
| 150 | } else { |
| 151 | (*os) << "Backtrace:\n"; |
| 152 | for (size_t i = 0; i < trace_.size(); ++i) { |
| 153 | (*os) << "\t" << trace_symbols.get()[i] << "\n"; |
| 154 | } |
| 155 | } |
| 156 | } |