blob: a6d519983a094e062fd0e0040768365fb6cf2d87 [file] [log] [blame]
[email protected]96fd0032009-04-24 00:13:081// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]1ffe08c12008-08-13 11:15:114
[email protected]5485cdc2009-01-16 21:17:305#include "build/build_config.h"
[email protected]1ffe08c12008-08-13 11:15:116#include "base/debug_util.h"
7
[email protected]96fd0032009-04-24 00:13:088#include <errno.h>
[email protected]5485cdc2009-01-16 21:17:309#include <execinfo.h>
[email protected]e6f456242008-10-08 15:27:4010#include <fcntl.h>
[email protected]d1fc4372009-01-16 22:06:1911#include <stdio.h>
[email protected]e6f456242008-10-08 15:27:4012#include <sys/stat.h>
[email protected]1ffe08c12008-08-13 11:15:1113#include <sys/sysctl.h>
14#include <sys/types.h>
[email protected]e6f456242008-10-08 15:27:4015#include <unistd.h>
[email protected]1ffe08c12008-08-13 11:15:1116
[email protected]e6f456242008-10-08 15:27:4017#include "base/basictypes.h"
[email protected]157c61b2009-05-01 21:37:3118#include "base/eintr_wrapper.h"
[email protected]0dfc81b2008-08-25 03:44:4019#include "base/logging.h"
[email protected]96fd0032009-04-24 00:13:0820#include "base/scoped_ptr.h"
[email protected]1ffe08c12008-08-13 11:15:1121#include "base/string_piece.h"
22
[email protected]e6f456242008-10-08 15:27:4023// static
[email protected]1ffe08c12008-08-13 11:15:1124bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) {
25 NOTIMPLEMENTED();
26 return false;
27}
28
29#if defined(OS_MACOSX)
[email protected]e6f456242008-10-08 15:27:4030
31// Based on Apple's recommended method as described in
[email protected]1ffe08c12008-08-13 11:15:1132// http://developer.apple.com/qa/qa2004/qa1361.html
[email protected]e6f456242008-10-08 15:27:4033// static
[email protected]1ffe08c12008-08-13 11:15:1134bool DebugUtil::BeingDebugged() {
[email protected]dbd9bfc2009-02-05 18:25:2035 // 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]e6f456242008-10-08 15:27:4044 // 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]dbd9bfc2009-02-05 18:25:2060 if (sysctl_result != 0) {
61 is_set = true;
62 being_debugged = false;
63 return being_debugged;
64 }
[email protected]e6f456242008-10-08 15:27:4065
66 // This process is being debugged if the P_TRACED flag is set.
[email protected]dbd9bfc2009-02-05 18:25:2067 is_set = true;
68 being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0;
69 return being_debugged;
[email protected]1ffe08c12008-08-13 11:15:1170}
71
72#elif defined(OS_LINUX)
[email protected]e6f456242008-10-08 15:27:4073
[email protected]1ffe08c12008-08-13 11:15:1174// 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]e6f456242008-10-08 15:27:4078// static
[email protected]1ffe08c12008-08-13 11:15:1179bool 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]157c61b2009-05-01 21:37:3189 ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf)));
90 HANDLE_EINTR(close(status_fd));
[email protected]1ffe08c12008-08-13 11:15:1191
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]e6f456242008-10-08 15:27:40106
107#endif // OS_LINUX
[email protected]1ffe08c12008-08-13 11:15:11108
109// static
110void DebugUtil::BreakDebugger() {
[email protected]95fe3902009-05-04 21:13:42111#if !defined(ARCH_CPU_ARM_FAMILY)
[email protected]1ffe08c12008-08-13 11:15:11112 asm ("int3");
[email protected]95fe3902009-05-04 21:13:42113#endif
[email protected]1ffe08c12008-08-13 11:15:11114}
[email protected]5485cdc2009-01-16 21:17:30115
[email protected]5485cdc2009-01-16 21:17:30116StackTrace::StackTrace() {
[email protected]96fd0032009-04-24 00:13:08117 const int kMaxCallers = 256;
[email protected]5485cdc2009-01-16 21:17:30118
119 void* callers[kMaxCallers];
120 int count = backtrace(callers, kMaxCallers);
[email protected]96fd0032009-04-24 00:13:08121
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]5485cdc2009-01-16 21:17:30131}
132
133void StackTrace::PrintBacktrace() {
134 fflush(stderr);
135 backtrace_symbols_fd(&trace_[0], trace_.size(), STDERR_FILENO);
136}
[email protected]96fd0032009-04-24 00:13:08137
138void 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}