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