blob: b3c9bc803f5218e33d479f22cd8fddd0a0e2c571 [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>
[email protected]bed545e2010-12-30 00:16:199#include <execinfo.h>
[email protected]58580352010-10-26 04:07:5010#include <fcntl.h>
11#include <stdio.h>
12#include <stdlib.h>
[email protected]0c6f3b0c2011-04-15 06:15:0413#include <sys/param.h>
[email protected]58580352010-10-26 04:07:5014#include <sys/stat.h>
[email protected]58580352010-10-26 04:07:5015#include <sys/types.h>
16#include <unistd.h>
17
18#include <string>
19#include <vector>
20
21#if defined(__GLIBCXX__)
22#include <cxxabi.h>
23#endif
24
25#if defined(OS_MACOSX)
26#include <AvailabilityMacros.h>
27#endif
28
[email protected]94f8c952011-06-25 04:54:4129#if defined(OS_MACOSX) || defined(OS_OPENBSD) || defined(OS_FREEBSD)
30#include <sys/sysctl.h>
31#endif
32
[email protected]58580352010-10-26 04:07:5033#include <iostream>
34
35#include "base/basictypes.h"
[email protected]58580352010-10-26 04:07:5036#include "base/eintr_wrapper.h"
37#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1538#include "base/memory/scoped_ptr.h"
[email protected]58580352010-10-26 04:07:5039#include "base/safe_strerror_posix.h"
[email protected]58580352010-10-26 04:07:5040#include "base/string_piece.h"
41#include "base/stringprintf.h"
42
43#if defined(USE_SYMBOLIZE)
44#include "base/third_party/symbolize/symbolize.h"
45#endif
46
[email protected]3132e35c2011-07-07 20:46:5047#if defined(OS_ANDROID)
48#include <execinfo.h>
49#include "base/threading/platform_thread.h"
50#endif
51
[email protected]58580352010-10-26 04:07:5052namespace base {
53namespace debug {
54
55bool SpawnDebuggerOnProcess(unsigned /* process_id */) {
56 NOTIMPLEMENTED();
57 return false;
58}
59
60#if defined(OS_MACOSX)
61
62// Based on Apple's recommended method as described in
63// http://developer.apple.com/qa/qa2004/qa1361.html
64bool BeingDebugged() {
65 // If the process is sandboxed then we can't use the sysctl, so cache the
66 // value.
67 static bool is_set = false;
68 static bool being_debugged = false;
69
70 if (is_set) {
71 return being_debugged;
72 }
73
74 // Initialize mib, which tells sysctl what info we want. In this case,
75 // we're looking for information about a specific process ID.
76 int mib[] = {
77 CTL_KERN,
78 KERN_PROC,
79 KERN_PROC_PID,
80 getpid()
81 };
82
83 // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and
84 // binary interfaces may change.
85 struct kinfo_proc info;
86 size_t info_size = sizeof(info);
87
88 int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0);
89 DCHECK_EQ(sysctl_result, 0);
90 if (sysctl_result != 0) {
91 is_set = true;
92 being_debugged = false;
93 return being_debugged;
94 }
95
96 // This process is being debugged if the P_TRACED flag is set.
97 is_set = true;
98 being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0;
99 return being_debugged;
100}
101
[email protected]3132e35c2011-07-07 20:46:50102#elif defined(OS_LINUX) || defined(OS_ANDROID)
[email protected]58580352010-10-26 04:07:50103
104// We can look in /proc/self/status for TracerPid. We are likely used in crash
105// handling, so we are careful not to use the heap or have side effects.
106// Another option that is common is to try to ptrace yourself, but then we
107// can't detach without forking(), and that's not so great.
108// static
109bool BeingDebugged() {
110 int status_fd = open("/proc/self/status", O_RDONLY);
111 if (status_fd == -1)
112 return false;
113
114 // We assume our line will be in the first 1024 characters and that we can
115 // read this much all at once. In practice this will generally be true.
116 // This simplifies and speeds up things considerably.
117 char buf[1024];
118
119 ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf)));
120 if (HANDLE_EINTR(close(status_fd)) < 0)
121 return false;
122
123 if (num_read <= 0)
124 return false;
125
126 StringPiece status(buf, num_read);
127 StringPiece tracer("TracerPid:\t");
128
129 StringPiece::size_type pid_index = status.find(tracer);
130 if (pid_index == StringPiece::npos)
131 return false;
132
133 // Our pid is 0 without a debugger, assume this for any pid starting with 0.
134 pid_index += tracer.size();
135 return pid_index < status.size() && status[pid_index] != '0';
136}
137
[email protected]651d9c02010-12-01 07:36:54138#elif defined(OS_NACL)
139
140bool BeingDebugged() {
141 NOTIMPLEMENTED();
142 return false;
143}
144
[email protected]94f8c952011-06-25 04:54:41145#else
[email protected]58580352010-10-26 04:07:50146
[email protected]d0282962011-01-01 16:08:52147bool BeingDebugged() {
[email protected]58580352010-10-26 04:07:50148 // TODO(benl): can we determine this under FreeBSD?
149 NOTIMPLEMENTED();
150 return false;
151}
152
153#endif // defined(OS_FREEBSD)
154
155// We want to break into the debugger in Debug mode, and cause a crash dump in
156// Release mode. Breakpad behaves as follows:
157//
158// +-------+-----------------+-----------------+
159// | OS | Dump on SIGTRAP | Dump on SIGABRT |
160// +-------+-----------------+-----------------+
161// | Linux | N | Y |
162// | Mac | Y | N |
163// +-------+-----------------+-----------------+
164//
165// Thus we do the following:
166// Linux: Debug mode, send SIGTRAP; Release mode, send SIGABRT.
167// Mac: Always send SIGTRAP.
168
[email protected]3132e35c2011-07-07 20:46:50169#if defined(NDEBUG) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]58580352010-10-26 04:07:50170#define DEBUG_BREAK() abort()
[email protected]651d9c02010-12-01 07:36:54171#elif defined(OS_NACL)
172// The NaCl verifier doesn't let use use int3. For now, we call abort(). We
173// should ask for advice from some NaCl experts about the optimum thing here.
174// http://code.google.com/p/nativeclient/issues/detail?id=645
175#define DEBUG_BREAK() abort()
[email protected]58580352010-10-26 04:07:50176#elif defined(ARCH_CPU_ARM_FAMILY)
[email protected]3132e35c2011-07-07 20:46:50177#if defined(OS_ANDROID)
178// Though Android has a "helpful" process called debuggerd to catch native
179// signals on the general assumption that they are fatal errors, we've had great
180// difficulty continuing in a debugger once we stop from SIGINT triggered by
181// native code.
182//
183// Use GDB to set |go| to 1 to resume execution.
184#define DEBUG_BREAK() do { \
185 volatile int go = 0; \
186 while (!go) { base::PlatformThread::Sleep(100); } \
187} while (0)
188#else
189// ARM && !ANDROID
[email protected]58580352010-10-26 04:07:50190#define DEBUG_BREAK() asm("bkpt 0")
[email protected]3132e35c2011-07-07 20:46:50191#endif
[email protected]58580352010-10-26 04:07:50192#else
193#define DEBUG_BREAK() asm("int3")
194#endif
195
196void BreakDebugger() {
197 DEBUG_BREAK();
[email protected]f355c562011-01-21 05:02:17198#if defined(NDEBUG)
199 _exit(1);
200#endif
[email protected]58580352010-10-26 04:07:50201}
202
203} // namespace debug
204} // namespace base