Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "build/build_config.h" |
michael@0 | 6 | #include "base/debug_util.h" |
michael@0 | 7 | |
michael@0 | 8 | #define MOZ_HAVE_EXECINFO_H (defined(OS_LINUX) && !defined(ANDROID)) |
michael@0 | 9 | |
michael@0 | 10 | #include <errno.h> |
michael@0 | 11 | #include <fcntl.h> |
michael@0 | 12 | #include <stdio.h> |
michael@0 | 13 | #include <limits.h> |
michael@0 | 14 | #include <sys/stat.h> |
michael@0 | 15 | #include <sys/param.h> |
michael@0 | 16 | #include <sys/types.h> |
michael@0 | 17 | #include <unistd.h> |
michael@0 | 18 | #if MOZ_HAVE_EXECINFO_H |
michael@0 | 19 | #include <execinfo.h> |
michael@0 | 20 | #endif |
michael@0 | 21 | |
michael@0 | 22 | #if defined(OS_MACOSX) || defined(OS_BSD) |
michael@0 | 23 | #if defined(OS_OPENBSD) |
michael@0 | 24 | #include <sys/proc.h> |
michael@0 | 25 | #endif |
michael@0 | 26 | #include <sys/sysctl.h> |
michael@0 | 27 | #endif |
michael@0 | 28 | |
michael@0 | 29 | #if defined(OS_DRAGONFLY) || defined(OS_FREEBSD) |
michael@0 | 30 | #include <sys/user.h> |
michael@0 | 31 | #endif |
michael@0 | 32 | |
michael@0 | 33 | #include "base/basictypes.h" |
michael@0 | 34 | #include "base/eintr_wrapper.h" |
michael@0 | 35 | #include "base/logging.h" |
michael@0 | 36 | #include "base/scoped_ptr.h" |
michael@0 | 37 | #include "base/string_piece.h" |
michael@0 | 38 | |
michael@0 | 39 | #if defined(OS_NETBSD) |
michael@0 | 40 | #undef KERN_PROC |
michael@0 | 41 | #define KERN_PROC KERN_PROC2 |
michael@0 | 42 | #define KINFO_PROC struct kinfo_proc2 |
michael@0 | 43 | #else |
michael@0 | 44 | #define KINFO_PROC struct kinfo_proc |
michael@0 | 45 | #endif |
michael@0 | 46 | |
michael@0 | 47 | #if defined(OS_MACOSX) |
michael@0 | 48 | #define KP_FLAGS kp_proc.p_flag |
michael@0 | 49 | #elif defined(OS_DRAGONFLY) |
michael@0 | 50 | #define KP_FLAGS kp_flags |
michael@0 | 51 | #elif defined(OS_FREEBSD) |
michael@0 | 52 | #define KP_FLAGS ki_flag |
michael@0 | 53 | #elif defined(OS_OPENBSD) && !defined(_P_TRACED) |
michael@0 | 54 | #define KP_FLAGS p_psflags |
michael@0 | 55 | #define P_TRACED PS_TRACED |
michael@0 | 56 | #else |
michael@0 | 57 | #define KP_FLAGS p_flag |
michael@0 | 58 | #endif |
michael@0 | 59 | |
michael@0 | 60 | // static |
michael@0 | 61 | bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) { |
michael@0 | 62 | NOTIMPLEMENTED(); |
michael@0 | 63 | return false; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | #if defined(OS_MACOSX) || defined(OS_BSD) |
michael@0 | 67 | |
michael@0 | 68 | // Based on Apple's recommended method as described in |
michael@0 | 69 | // http://developer.apple.com/qa/qa2004/qa1361.html |
michael@0 | 70 | // static |
michael@0 | 71 | bool DebugUtil::BeingDebugged() { |
michael@0 | 72 | // If the process is sandboxed then we can't use the sysctl, so cache the |
michael@0 | 73 | // value. |
michael@0 | 74 | static bool is_set = false; |
michael@0 | 75 | static bool being_debugged = false; |
michael@0 | 76 | |
michael@0 | 77 | if (is_set) { |
michael@0 | 78 | return being_debugged; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | // Initialize mib, which tells sysctl what info we want. In this case, |
michael@0 | 82 | // we're looking for information about a specific process ID. |
michael@0 | 83 | int mib[] = { |
michael@0 | 84 | CTL_KERN, |
michael@0 | 85 | KERN_PROC, |
michael@0 | 86 | KERN_PROC_PID, |
michael@0 | 87 | getpid(), |
michael@0 | 88 | #if defined(OS_NETBSD) || defined(OS_OPENBSD) |
michael@0 | 89 | sizeof(KINFO_PROC), |
michael@0 | 90 | 1, |
michael@0 | 91 | #endif |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and |
michael@0 | 95 | // binary interfaces may change. |
michael@0 | 96 | KINFO_PROC info; |
michael@0 | 97 | size_t info_size = sizeof(info); |
michael@0 | 98 | |
michael@0 | 99 | int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0); |
michael@0 | 100 | DCHECK(sysctl_result == 0); |
michael@0 | 101 | if (sysctl_result != 0) { |
michael@0 | 102 | is_set = true; |
michael@0 | 103 | being_debugged = false; |
michael@0 | 104 | return being_debugged; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | // This process is being debugged if the P_TRACED flag is set. |
michael@0 | 108 | is_set = true; |
michael@0 | 109 | being_debugged = (info.KP_FLAGS & P_TRACED) != 0; |
michael@0 | 110 | return being_debugged; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | #elif defined(OS_LINUX) |
michael@0 | 114 | |
michael@0 | 115 | // We can look in /proc/self/status for TracerPid. We are likely used in crash |
michael@0 | 116 | // handling, so we are careful not to use the heap or have side effects. |
michael@0 | 117 | // Another option that is common is to try to ptrace yourself, but then we |
michael@0 | 118 | // can't detach without forking(), and that's not so great. |
michael@0 | 119 | // static |
michael@0 | 120 | bool DebugUtil::BeingDebugged() { |
michael@0 | 121 | int status_fd = open("/proc/self/status", O_RDONLY); |
michael@0 | 122 | if (status_fd == -1) |
michael@0 | 123 | return false; |
michael@0 | 124 | |
michael@0 | 125 | // We assume our line will be in the first 1024 characters and that we can |
michael@0 | 126 | // read this much all at once. In practice this will generally be true. |
michael@0 | 127 | // This simplifies and speeds up things considerably. |
michael@0 | 128 | char buf[1024]; |
michael@0 | 129 | |
michael@0 | 130 | ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf))); |
michael@0 | 131 | HANDLE_EINTR(close(status_fd)); |
michael@0 | 132 | |
michael@0 | 133 | if (num_read <= 0) |
michael@0 | 134 | return false; |
michael@0 | 135 | |
michael@0 | 136 | StringPiece status(buf, num_read); |
michael@0 | 137 | StringPiece tracer("TracerPid:\t"); |
michael@0 | 138 | |
michael@0 | 139 | StringPiece::size_type pid_index = status.find(tracer); |
michael@0 | 140 | if (pid_index == StringPiece::npos) |
michael@0 | 141 | return false; |
michael@0 | 142 | |
michael@0 | 143 | // Our pid is 0 without a debugger, assume this for any pid starting with 0. |
michael@0 | 144 | pid_index += tracer.size(); |
michael@0 | 145 | return pid_index < status.size() && status[pid_index] != '0'; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | #endif // OS_LINUX |
michael@0 | 149 | |
michael@0 | 150 | // static |
michael@0 | 151 | void DebugUtil::BreakDebugger() { |
michael@0 | 152 | #if defined(ARCH_CPU_X86_FAMILY) |
michael@0 | 153 | asm ("int3"); |
michael@0 | 154 | #endif |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | StackTrace::StackTrace() { |
michael@0 | 158 | const int kMaxCallers = 256; |
michael@0 | 159 | |
michael@0 | 160 | void* callers[kMaxCallers]; |
michael@0 | 161 | #if MOZ_HAVE_EXECINFO_H |
michael@0 | 162 | int count = backtrace(callers, kMaxCallers); |
michael@0 | 163 | #else |
michael@0 | 164 | int count = 0; |
michael@0 | 165 | #endif |
michael@0 | 166 | |
michael@0 | 167 | // Though the backtrace API man page does not list any possible negative |
michael@0 | 168 | // return values, we still still exclude them because they would break the |
michael@0 | 169 | // memcpy code below. |
michael@0 | 170 | if (count > 0) { |
michael@0 | 171 | trace_.resize(count); |
michael@0 | 172 | memcpy(&trace_[0], callers, sizeof(callers[0]) * count); |
michael@0 | 173 | } else { |
michael@0 | 174 | trace_.resize(0); |
michael@0 | 175 | } |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | void StackTrace::PrintBacktrace() { |
michael@0 | 179 | fflush(stderr); |
michael@0 | 180 | #if MOZ_HAVE_EXECINFO_H |
michael@0 | 181 | backtrace_symbols_fd(&trace_[0], trace_.size(), STDERR_FILENO); |
michael@0 | 182 | #endif |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | void StackTrace::OutputToStream(std::ostream* os) { |
michael@0 | 186 | return; |
michael@0 | 187 | } |