OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This is a cross platform interface for helper functions related to debuggers. | 5 #ifndef BASE_DEBUG_STACK_TRACE_H_ |
6 // You should use this to test if you're running under a debugger, and if you | 6 #define BASE_DEBUG_STACK_TRACE_H_ |
7 // would like to yield (breakpoint) into the debugger. | |
8 | |
9 #ifndef BASE_DEBUG_UTIL_H_ | |
10 #define BASE_DEBUG_UTIL_H_ | |
11 #pragma once | 7 #pragma once |
12 | 8 |
13 #include <iosfwd> | 9 #include <iosfwd> |
14 | 10 |
15 #include "base/basictypes.h" | 11 #include "build/build_config.h" |
16 | 12 |
17 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
18 struct _EXCEPTION_POINTERS; | 14 struct _EXCEPTION_POINTERS; |
19 #endif | 15 #endif |
20 | 16 |
| 17 namespace base { |
| 18 namespace debug { |
| 19 |
21 // A stacktrace can be helpful in debugging. For example, you can include a | 20 // A stacktrace can be helpful in debugging. For example, you can include a |
22 // stacktrace member in a object (probably around #ifndef NDEBUG) so that you | 21 // stacktrace member in a object (probably around #ifndef NDEBUG) so that you |
23 // can later see where the given object was created from. | 22 // can later see where the given object was created from. |
24 class StackTrace { | 23 class StackTrace { |
25 public: | 24 public: |
26 // Creates a stacktrace from the current location | 25 // Creates a stacktrace from the current location. |
27 StackTrace(); | 26 StackTrace(); |
28 | 27 |
29 // Note that the default copy constructor and assignment constructors | |
30 // are OK. | |
31 | |
32 #if defined(OS_WIN) | 28 #if defined(OS_WIN) |
33 // Creates a stacktrace for an exception. | 29 // Creates a stacktrace for an exception. |
34 // Note: this function will throw an import not found (StackWalk64) exception | 30 // Note: this function will throw an import not found (StackWalk64) exception |
35 // on system without dbghelp 5.1. | 31 // on system without dbghelp 5.1. |
36 StackTrace(_EXCEPTION_POINTERS* exception_pointers); | 32 StackTrace(_EXCEPTION_POINTERS* exception_pointers); |
37 #endif | 33 #endif |
38 // Gets an array of instruction pointer values. | 34 |
39 // count: (output) the number of elements in the returned array | 35 // Copying and assignment are allowed with the default functions. |
40 const void *const *Addresses(size_t* count); | 36 |
| 37 ~StackTrace(); |
| 38 |
| 39 // Gets an array of instruction pointer values. |*count| will be set to the |
| 40 // number of elements in the returned array. |
| 41 const void* const* Addresses(size_t* count); |
| 42 |
41 // Prints a backtrace to stderr | 43 // Prints a backtrace to stderr |
42 void PrintBacktrace(); | 44 void PrintBacktrace(); |
43 | 45 |
44 // Resolves backtrace to symbols and write to stream. | 46 // Resolves backtrace to symbols and write to stream. |
45 void OutputToStream(std::ostream* os); | 47 void OutputToStream(std::ostream* os); |
46 | 48 |
47 private: | 49 private: |
48 // From http://msdn.microsoft.com/en-us/library/bb204633.aspx, | 50 // From http://msdn.microsoft.com/en-us/library/bb204633.aspx, |
49 // the sum of FramesToSkip and FramesToCapture must be less than 63, | 51 // the sum of FramesToSkip and FramesToCapture must be less than 63, |
50 // so set it to 62. Even if on POSIX it could be a larger value, it usually | 52 // so set it to 62. Even if on POSIX it could be a larger value, it usually |
51 // doesn't give much more information. | 53 // doesn't give much more information. |
52 static const int MAX_TRACES = 62; | 54 static const int kMaxTraces = 62; |
53 void* trace_[MAX_TRACES]; | 55 |
| 56 void* trace_[kMaxTraces]; |
54 int count_; | 57 int count_; |
55 }; | 58 }; |
56 | 59 |
57 class DebugUtil { | 60 } // namespace debug |
58 public: | 61 } // namespace base |
59 // Starts the registered system-wide JIT debugger to attach it to specified | |
60 // process. | |
61 static bool SpawnDebuggerOnProcess(unsigned process_id); | |
62 | 62 |
63 // Waits wait_seconds seconds for a debugger to attach to the current process. | 63 #endif // BASE_DEBUG_STACK_TRACE_H_ |
64 // When silent is false, an exception is thrown when a debugger is detected. | |
65 static bool WaitForDebugger(int wait_seconds, bool silent); | |
66 | |
67 // Are we running under a debugger? | |
68 // On OS X, the underlying mechanism doesn't work when the sandbox is enabled. | |
69 // To get around this, this function caches its value. | |
70 // WARNING: Because of this, on OS X, a call MUST be made to this function | |
71 // BEFORE the sandbox is enabled. | |
72 static bool BeingDebugged(); | |
73 | |
74 // Break into the debugger, assumes a debugger is present. | |
75 static void BreakDebugger(); | |
76 | |
77 #if defined(OS_MACOSX) | |
78 // On Mac OS X, it can take a really long time for the OS crash handler to | |
79 // process a Chrome crash when debugging symbols are available. This | |
80 // translates into a long wait until the process actually dies. This call | |
81 // disables Apple Crash Reporter entirely. | |
82 static void DisableOSCrashDumps(); | |
83 #endif // defined(OS_MACOSX) | |
84 | |
85 // This should be used only in test code. | |
86 static void SuppressDialogs() { | |
87 suppress_dialogs_ = true; | |
88 } | |
89 | |
90 private: | |
91 // If true, avoid displaying any dialogs that could cause problems | |
92 // in non-interactive environments. | |
93 static bool suppress_dialogs_; | |
94 }; | |
95 | |
96 #endif // BASE_DEBUG_UTIL_H_ | |
OLD | NEW |