blob: 1c7304c47c68be81104d15bff964d394a57761a1 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]58580352010-10-26 04:07:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/debug/debugger.h"
[email protected]651d9c02010-12-01 07:36:546#include "build/build_config.h"
[email protected]58580352010-10-26 04:07:507
8#include <errno.h>
9#include <fcntl.h>
10#include <stdio.h>
11#include <stdlib.h>
[email protected]0c6f3b0c2011-04-15 06:15:0412#include <sys/param.h>
[email protected]58580352010-10-26 04:07:5013#include <sys/stat.h>
[email protected]58580352010-10-26 04:07:5014#include <sys/types.h>
15#include <unistd.h>
16
17#include <string>
18#include <vector>
19
[email protected]4d0f93c2011-09-29 04:43:5420#if !defined(OS_ANDROID)
21#include <execinfo.h>
22#endif
23
[email protected]58580352010-10-26 04:07:5024#if defined(__GLIBCXX__)
25#include <cxxabi.h>
26#endif
27
28#if defined(OS_MACOSX)
29#include <AvailabilityMacros.h>
30#endif
31
[email protected]e37e88a02011-11-15 00:06:1632#if defined(OS_MACOSX) || defined(OS_BSD)
[email protected]94f8c952011-06-25 04:54:4133#include <sys/sysctl.h>
34#endif
35
[email protected]a5c0fd982011-09-01 20:19:5336#include <ostream>
[email protected]58580352010-10-26 04:07:5037
38#include "base/basictypes.h"
[email protected]58580352010-10-26 04:07:5039#include "base/eintr_wrapper.h"
40#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1541#include "base/memory/scoped_ptr.h"
[email protected]58580352010-10-26 04:07:5042#include "base/safe_strerror_posix.h"
[email protected]58580352010-10-26 04:07:5043#include "base/string_piece.h"
44#include "base/stringprintf.h"
45
46#if defined(USE_SYMBOLIZE)
47#include "base/third_party/symbolize/symbolize.h"
48#endif
49
[email protected]3132e35c2011-07-07 20:46:5050#if defined(OS_ANDROID)
[email protected]3132e35c2011-07-07 20:46:5051#include "base/threading/platform_thread.h"
52#endif
53
[email protected]58580352010-10-26 04:07:5054namespace base {
55namespace debug {
56
57bool SpawnDebuggerOnProcess(unsigned /* process_id */) {
58 NOTIMPLEMENTED();
59 return false;
60}
61
[email protected]e37e88a02011-11-15 00:06:1662#if defined(OS_MACOSX) || defined(OS_BSD)
[email protected]58580352010-10-26 04:07:5063
64// Based on Apple's recommended method as described in
65// http://developer.apple.com/qa/qa2004/qa1361.html
66bool BeingDebugged() {
67 // If the process is sandboxed then we can't use the sysctl, so cache the
68 // value.
69 static bool is_set = false;
70 static bool being_debugged = false;
71
72 if (is_set) {
73 return being_debugged;
74 }
75
76 // Initialize mib, which tells sysctl what info we want. In this case,
77 // we're looking for information about a specific process ID.
78 int mib[] = {
79 CTL_KERN,
80 KERN_PROC,
81 KERN_PROC_PID,
82 getpid()
[email protected]88b49f72011-10-18 17:41:0783#if defined(OS_OPENBSD)
84 , sizeof(struct kinfo_proc),
85 0
86#endif
[email protected]58580352010-10-26 04:07:5087 };
88
89 // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and
90 // binary interfaces may change.
91 struct kinfo_proc info;
92 size_t info_size = sizeof(info);
93
[email protected]88b49f72011-10-18 17:41:0794#if defined(OS_OPENBSD)
95 if (sysctl(mib, arraysize(mib), NULL, &info_size, NULL, 0) < 0)
96 return -1;
97
98 mib[5] = (info_size / sizeof(struct kinfo_proc));
99#endif
100
[email protected]58580352010-10-26 04:07:50101 int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0);
102 DCHECK_EQ(sysctl_result, 0);
103 if (sysctl_result != 0) {
104 is_set = true;
105 being_debugged = false;
106 return being_debugged;
107 }
108
109 // This process is being debugged if the P_TRACED flag is set.
110 is_set = true;
[email protected]e37e88a02011-11-15 00:06:16111#if defined(OS_BSD)
[email protected]88b49f72011-10-18 17:41:07112 being_debugged = (info.p_flag & P_TRACED) != 0;
113#else
[email protected]58580352010-10-26 04:07:50114 being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0;
[email protected]88b49f72011-10-18 17:41:07115#endif
[email protected]58580352010-10-26 04:07:50116 return being_debugged;
117}
118
[email protected]3132e35c2011-07-07 20:46:50119#elif defined(OS_LINUX) || defined(OS_ANDROID)
[email protected]58580352010-10-26 04:07:50120
121// We can look in /proc/self/status for TracerPid. We are likely used in crash
122// handling, so we are careful not to use the heap or have side effects.
123// Another option that is common is to try to ptrace yourself, but then we
124// can't detach without forking(), and that's not so great.
125// static
126bool BeingDebugged() {
127 int status_fd = open("/proc/self/status", O_RDONLY);
128 if (status_fd == -1)
129 return false;
130
131 // We assume our line will be in the first 1024 characters and that we can
132 // read this much all at once. In practice this will generally be true.
133 // This simplifies and speeds up things considerably.
134 char buf[1024];
135
136 ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf)));
137 if (HANDLE_EINTR(close(status_fd)) < 0)
138 return false;
139
140 if (num_read <= 0)
141 return false;
142
143 StringPiece status(buf, num_read);
144 StringPiece tracer("TracerPid:\t");
145
146 StringPiece::size_type pid_index = status.find(tracer);
147 if (pid_index == StringPiece::npos)
148 return false;
149
150 // Our pid is 0 without a debugger, assume this for any pid starting with 0.
151 pid_index += tracer.size();
152 return pid_index < status.size() && status[pid_index] != '0';
153}
154
[email protected]651d9c02010-12-01 07:36:54155#elif defined(OS_NACL)
156
157bool BeingDebugged() {
158 NOTIMPLEMENTED();
159 return false;
160}
161
[email protected]94f8c952011-06-25 04:54:41162#else
[email protected]58580352010-10-26 04:07:50163
[email protected]d0282962011-01-01 16:08:52164bool BeingDebugged() {
[email protected]58580352010-10-26 04:07:50165 // TODO(benl): can we determine this under FreeBSD?
166 NOTIMPLEMENTED();
167 return false;
168}
169
170#endif // defined(OS_FREEBSD)
171
172// We want to break into the debugger in Debug mode, and cause a crash dump in
173// Release mode. Breakpad behaves as follows:
174//
175// +-------+-----------------+-----------------+
176// | OS | Dump on SIGTRAP | Dump on SIGABRT |
177// +-------+-----------------+-----------------+
178// | Linux | N | Y |
179// | Mac | Y | N |
180// +-------+-----------------+-----------------+
181//
182// Thus we do the following:
183// Linux: Debug mode, send SIGTRAP; Release mode, send SIGABRT.
184// Mac: Always send SIGTRAP.
185
[email protected]3132e35c2011-07-07 20:46:50186#if defined(NDEBUG) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]58580352010-10-26 04:07:50187#define DEBUG_BREAK() abort()
[email protected]651d9c02010-12-01 07:36:54188#elif defined(OS_NACL)
189// The NaCl verifier doesn't let use use int3. For now, we call abort(). We
190// should ask for advice from some NaCl experts about the optimum thing here.
191// http://code.google.com/p/nativeclient/issues/detail?id=645
192#define DEBUG_BREAK() abort()
[email protected]58580352010-10-26 04:07:50193#elif defined(ARCH_CPU_ARM_FAMILY)
[email protected]3132e35c2011-07-07 20:46:50194#if defined(OS_ANDROID)
195// Though Android has a "helpful" process called debuggerd to catch native
196// signals on the general assumption that they are fatal errors, we've had great
197// difficulty continuing in a debugger once we stop from SIGINT triggered by
198// native code.
199//
200// Use GDB to set |go| to 1 to resume execution.
201#define DEBUG_BREAK() do { \
202 volatile int go = 0; \
203 while (!go) { base::PlatformThread::Sleep(100); } \
204} while (0)
205#else
206// ARM && !ANDROID
[email protected]58580352010-10-26 04:07:50207#define DEBUG_BREAK() asm("bkpt 0")
[email protected]3132e35c2011-07-07 20:46:50208#endif
[email protected]58580352010-10-26 04:07:50209#else
210#define DEBUG_BREAK() asm("int3")
211#endif
212
213void BreakDebugger() {
214 DEBUG_BREAK();
[email protected]f355c562011-01-21 05:02:17215#if defined(NDEBUG)
216 _exit(1);
217#endif
[email protected]58580352010-10-26 04:07:50218}
219
220} // namespace debug
221} // namespace base