blob: dc86c8c5720d4fb7b817b3d69ef422ca36618be4 [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]e6f456242008-10-08 15:27:409#include <fcntl.h>
[email protected]d1fc4372009-01-16 22:06:1910#include <stdio.h>
[email protected]e6f456242008-10-08 15:27:4011#include <sys/stat.h>
[email protected]1ffe08c12008-08-13 11:15:1112#include <sys/sysctl.h>
13#include <sys/types.h>
[email protected]e6f456242008-10-08 15:27:4014#include <unistd.h>
[email protected]1ffe08c12008-08-13 11:15:1115
[email protected]6e4bf0b2009-09-17 16:12:3616#if defined(OS_MACOSX)
17#include <AvailabilityMacros.h>
18#endif
19
[email protected]e6f456242008-10-08 15:27:4020#include "base/basictypes.h"
[email protected]9543af052009-09-15 22:42:5921#include "base/compat_execinfo.h"
[email protected]157c61b2009-05-01 21:37:3122#include "base/eintr_wrapper.h"
[email protected]0dfc81b2008-08-25 03:44:4023#include "base/logging.h"
[email protected]96fd0032009-04-24 00:13:0824#include "base/scoped_ptr.h"
[email protected]1ffe08c12008-08-13 11:15:1125#include "base/string_piece.h"
26
[email protected]e6f456242008-10-08 15:27:4027// static
[email protected]1ffe08c12008-08-13 11:15:1128bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) {
29 NOTIMPLEMENTED();
30 return false;
31}
32
33#if defined(OS_MACOSX)
[email protected]e6f456242008-10-08 15:27:4034
35// Based on Apple's recommended method as described in
[email protected]1ffe08c12008-08-13 11:15:1136// http://developer.apple.com/qa/qa2004/qa1361.html
[email protected]e6f456242008-10-08 15:27:4037// static
[email protected]1ffe08c12008-08-13 11:15:1138bool DebugUtil::BeingDebugged() {
[email protected]dbd9bfc2009-02-05 18:25:2039 // If the process is sandboxed then we can't use the sysctl, so cache the
40 // value.
41 static bool is_set = false;
42 static bool being_debugged = false;
43
44 if (is_set) {
45 return being_debugged;
46 }
47
[email protected]e6f456242008-10-08 15:27:4048 // Initialize mib, which tells sysctl what info we want. In this case,
49 // we're looking for information about a specific process ID.
50 int mib[] = {
51 CTL_KERN,
52 KERN_PROC,
53 KERN_PROC_PID,
54 getpid()
55 };
56
57 // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and
58 // binary interfaces may change.
59 struct kinfo_proc info;
60 size_t info_size = sizeof(info);
61
62 int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0);
63 DCHECK(sysctl_result == 0);
[email protected]dbd9bfc2009-02-05 18:25:2064 if (sysctl_result != 0) {
65 is_set = true;
66 being_debugged = false;
67 return being_debugged;
68 }
[email protected]e6f456242008-10-08 15:27:4069
70 // This process is being debugged if the P_TRACED flag is set.
[email protected]dbd9bfc2009-02-05 18:25:2071 is_set = true;
72 being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0;
73 return being_debugged;
[email protected]1ffe08c12008-08-13 11:15:1174}
75
76#elif defined(OS_LINUX)
[email protected]e6f456242008-10-08 15:27:4077
[email protected]1ffe08c12008-08-13 11:15:1178// We can look in /proc/self/status for TracerPid. We are likely used in crash
79// handling, so we are careful not to use the heap or have side effects.
80// Another option that is common is to try to ptrace yourself, but then we
81// can't detach without forking(), and that's not so great.
[email protected]e6f456242008-10-08 15:27:4082// static
[email protected]1ffe08c12008-08-13 11:15:1183bool DebugUtil::BeingDebugged() {
84 int status_fd = open("/proc/self/status", O_RDONLY);
85 if (status_fd == -1)
86 return false;
87
88 // We assume our line will be in the first 1024 characters and that we can
89 // read this much all at once. In practice this will generally be true.
90 // This simplifies and speeds up things considerably.
91 char buf[1024];
92
[email protected]157c61b2009-05-01 21:37:3193 ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf)));
94 HANDLE_EINTR(close(status_fd));
[email protected]1ffe08c12008-08-13 11:15:1195
96 if (num_read <= 0)
97 return false;
98
[email protected]8a16266e2009-09-10 21:08:3999 base::StringPiece status(buf, num_read);
100 base::StringPiece tracer("TracerPid:\t");
[email protected]1ffe08c12008-08-13 11:15:11101
[email protected]8a16266e2009-09-10 21:08:39102 base::StringPiece::size_type pid_index = status.find(tracer);
103 if (pid_index == base::StringPiece::npos)
[email protected]1ffe08c12008-08-13 11:15:11104 return false;
105
106 // Our pid is 0 without a debugger, assume this for any pid starting with 0.
107 pid_index += tracer.size();
108 return pid_index < status.size() && status[pid_index] != '0';
109}
[email protected]e6f456242008-10-08 15:27:40110
111#endif // OS_LINUX
[email protected]1ffe08c12008-08-13 11:15:11112
113// static
114void DebugUtil::BreakDebugger() {
[email protected]a68eb892009-05-07 15:38:28115#if defined(ARCH_CPU_ARM_FAMILY)
116 asm("bkpt 0");
117#else
118 asm("int3");
[email protected]95fe3902009-05-04 21:13:42119#endif
[email protected]1ffe08c12008-08-13 11:15:11120}
[email protected]5485cdc2009-01-16 21:17:30121
[email protected]5485cdc2009-01-16 21:17:30122StackTrace::StackTrace() {
[email protected]6e4bf0b2009-09-17 16:12:36123#if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
[email protected]f358a0a2009-09-18 15:27:56124 if (backtrace == NULL) {
[email protected]9543af052009-09-15 22:42:59125 count_ = 0;
[email protected]6e4bf0b2009-09-17 16:12:36126 return;
[email protected]9543af052009-09-15 22:42:59127 }
[email protected]6e4bf0b2009-09-17 16:12:36128#endif
129 // Though the backtrace API man page does not list any possible negative
130 // return values, we take no chance.
131 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0);
[email protected]5485cdc2009-01-16 21:17:30132}
133
134void StackTrace::PrintBacktrace() {
[email protected]6e4bf0b2009-09-17 16:12:36135#if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
[email protected]f358a0a2009-09-18 15:27:56136 if (backtrace_symbols_fd == NULL)
[email protected]6e4bf0b2009-09-17 16:12:36137 return;
138#endif
139 fflush(stderr);
140 backtrace_symbols_fd(trace_, count_, STDERR_FILENO);
[email protected]5485cdc2009-01-16 21:17:30141}
[email protected]96fd0032009-04-24 00:13:08142
143void StackTrace::OutputToStream(std::ostream* os) {
[email protected]6e4bf0b2009-09-17 16:12:36144#if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
[email protected]f358a0a2009-09-18 15:27:56145 if (backtrace_symbols == NULL)
[email protected]6e4bf0b2009-09-17 16:12:36146 return;
147#endif
148 scoped_ptr_malloc<char*> trace_symbols(backtrace_symbols(trace_, count_));
[email protected]96fd0032009-04-24 00:13:08149
[email protected]6e4bf0b2009-09-17 16:12:36150 // If we can't retrieve the symbols, print an error and just dump the raw
151 // addresses.
152 if (trace_symbols.get() == NULL) {
153 (*os) << "Unable get symbols for backtrace (" << strerror(errno)
154 << "). Dumping raw addresses in trace:\n";
155 for (int i = 0; i < count_; ++i) {
156 (*os) << "\t" << trace_[i] << "\n";
157 }
158 } else {
159 (*os) << "Backtrace:\n";
160 for (int i = 0; i < count_; ++i) {
161 (*os) << "\t" << trace_symbols.get()[i] << "\n";
[email protected]96fd0032009-04-24 00:13:08162 }
163 }
164}