|
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 // This is a cross platform interface for helper functions related to debuggers. |
|
6 // You should use this to test if you're running under a debugger, and if you |
|
7 // would like to yield (breakpoint) into the debugger. |
|
8 |
|
9 #ifndef BASE_DEBUG_UTIL_H_ |
|
10 #define BASE_DEBUG_UTIL_H_ |
|
11 |
|
12 #include <ostream> |
|
13 #include <vector> |
|
14 |
|
15 #include "base/basictypes.h" |
|
16 |
|
17 // A stacktrace can be helpful in debugging. For example, you can include a |
|
18 // stacktrace member in a object (probably around #ifndef NDEBUG) so that you |
|
19 // can later see where the given object was created from. |
|
20 class StackTrace { |
|
21 public: |
|
22 // Create a stacktrace from the current location |
|
23 StackTrace(); |
|
24 // Get an array of instruction pointer values. |
|
25 // count: (output) the number of elements in the returned array |
|
26 const void *const *Addresses(size_t* count); |
|
27 // Print a backtrace to stderr |
|
28 void PrintBacktrace(); |
|
29 |
|
30 // Resolve backtrace to symbols and write to stream. |
|
31 void OutputToStream(std::ostream* os); |
|
32 |
|
33 private: |
|
34 std::vector<void*> trace_; |
|
35 |
|
36 DISALLOW_EVIL_CONSTRUCTORS(StackTrace); |
|
37 }; |
|
38 |
|
39 class DebugUtil { |
|
40 public: |
|
41 // Starts the registered system-wide JIT debugger to attach it to specified |
|
42 // process. |
|
43 static bool SpawnDebuggerOnProcess(unsigned process_id); |
|
44 |
|
45 // Waits wait_seconds seconds for a debugger to attach to the current process. |
|
46 // When silent is false, an exception is thrown when a debugger is detected. |
|
47 static bool WaitForDebugger(int wait_seconds, bool silent); |
|
48 |
|
49 // Are we running under a debugger? |
|
50 // On OS X, the underlying mechanism doesn't work when the sandbox is enabled. |
|
51 // To get around this, this function caches its value. |
|
52 // WARNING: Because of this, on OS X, a call MUST be made to this function |
|
53 // BEFORE the sandbox is enabled. |
|
54 static bool BeingDebugged(); |
|
55 |
|
56 // Break into the debugger, assumes a debugger is present. |
|
57 static void BreakDebugger(); |
|
58 |
|
59 #if defined(OS_MACOSX) |
|
60 // On OS X, it can take a really long time for the OS Crash handler to |
|
61 // process a Chrome crash. This translates into a long wait till the process |
|
62 // actually dies. |
|
63 // This method disables OS Crash reporting entireley. |
|
64 // TODO(playmobil): Remove this when we have Breakpad integration enabled - |
|
65 // see http://crbug.com/7652 |
|
66 static void DisableOSCrashDumps(); |
|
67 #endif // defined(OS_MACOSX) |
|
68 }; |
|
69 |
|
70 namespace mozilla { |
|
71 |
|
72 class EnvironmentLog |
|
73 { |
|
74 public: |
|
75 EnvironmentLog(const char* varname); |
|
76 ~EnvironmentLog(); |
|
77 |
|
78 void print(const char* format, ...); |
|
79 |
|
80 private: |
|
81 std::string fname_; |
|
82 |
|
83 DISALLOW_EVIL_CONSTRUCTORS(EnvironmentLog); |
|
84 }; |
|
85 |
|
86 } // namespace mozilla |
|
87 |
|
88 #endif // BASE_DEBUG_UTIL_H_ |