OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_DEBUG_STACK_TRACE_H_ | 5 #ifndef BASE_DEBUG_STACK_TRACE_H_ |
6 #define BASE_DEBUG_STACK_TRACE_H_ | 6 #define BASE_DEBUG_STACK_TRACE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <iosfwd> | 10 #include <iosfwd> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 | 15 |
16 #if defined(OS_POSIX) | 16 #if defined(OS_POSIX) |
17 #include <unistd.h> | 17 #include <unistd.h> |
18 #endif | 18 #endif |
19 | 19 |
20 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
21 struct _EXCEPTION_POINTERS; | 21 struct _EXCEPTION_POINTERS; |
22 struct _CONTEXT; | 22 struct _CONTEXT; |
23 #endif | 23 #endif |
24 | 24 |
| 25 #if defined(OS_POSIX) && ( \ |
| 26 defined(__i386__) || defined(__x86_64__) || \ |
| 27 (defined(__arm__) && !defined(__thumb__))) |
| 28 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 |
| 29 #else |
| 30 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 |
| 31 #endif |
| 32 |
25 namespace base { | 33 namespace base { |
26 namespace debug { | 34 namespace debug { |
27 | 35 |
28 // Enables stack dump to console output on exception and signals. | 36 // Enables stack dump to console output on exception and signals. |
29 // When enabled, the process will quit immediately. This is meant to be used in | 37 // When enabled, the process will quit immediately. This is meant to be used in |
30 // unit_tests only! This is not thread-safe: only call from main thread. | 38 // unit_tests only! This is not thread-safe: only call from main thread. |
31 // In sandboxed processes, this has to be called before the sandbox is turned | 39 // In sandboxed processes, this has to be called before the sandbox is turned |
32 // on. | 40 // on. |
33 // Calling this function on Linux opens /proc/self/maps and caches its | 41 // Calling this function on Linux opens /proc/self/maps and caches its |
34 // contents. In non-official builds, this function also opens the object files | 42 // contents. In non-official builds, this function also opens the object files |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // so set it to 62. Even if on POSIX it could be a larger value, it usually | 94 // so set it to 62. Even if on POSIX it could be a larger value, it usually |
87 // doesn't give much more information. | 95 // doesn't give much more information. |
88 static const int kMaxTraces = 62; | 96 static const int kMaxTraces = 62; |
89 | 97 |
90 void* trace_[kMaxTraces]; | 98 void* trace_[kMaxTraces]; |
91 | 99 |
92 // The number of valid frames in |trace_|. | 100 // The number of valid frames in |trace_|. |
93 size_t count_; | 101 size_t count_; |
94 }; | 102 }; |
95 | 103 |
| 104 #if HAVE_TRACE_STACK_FRAME_POINTERS |
| 105 // Traces the stack by using frame pointers. This function is faster but less |
| 106 // reliable than StackTrace. It should work for debug and profiling builds, |
| 107 // but not for release builds (although there are some exceptions). |
| 108 // |
| 109 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| |
| 110 // after skipping |skip_initial| frames. Note that the function itself is not |
| 111 // added to the trace so |skip_initial| should be 0 in most cases. |
| 112 // Returns number of frames written. |
| 113 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, |
| 114 size_t max_depth, |
| 115 size_t skip_initial); |
| 116 #endif // HAVE_TRACE_STACK_FRAME_POINTERS |
| 117 |
96 namespace internal { | 118 namespace internal { |
97 | 119 |
98 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 120 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
99 // POSIX doesn't define any async-signal safe function for converting | 121 // POSIX doesn't define any async-signal safe function for converting |
100 // an integer to ASCII. We'll have to define our own version. | 122 // an integer to ASCII. We'll have to define our own version. |
101 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the | 123 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the |
102 // conversion was successful or NULL otherwise. It never writes more than "sz" | 124 // conversion was successful or NULL otherwise. It never writes more than "sz" |
103 // bytes. Output will be truncated as needed, and a NUL character is always | 125 // bytes. Output will be truncated as needed, and a NUL character is always |
104 // appended. | 126 // appended. |
105 BASE_EXPORT char *itoa_r(intptr_t i, | 127 BASE_EXPORT char *itoa_r(intptr_t i, |
106 char *buf, | 128 char *buf, |
107 size_t sz, | 129 size_t sz, |
108 int base, | 130 int base, |
109 size_t padding); | 131 size_t padding); |
110 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) | 132 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
111 | 133 |
112 } // namespace internal | 134 } // namespace internal |
113 | 135 |
114 } // namespace debug | 136 } // namespace debug |
115 } // namespace base | 137 } // namespace base |
116 | 138 |
117 #endif // BASE_DEBUG_STACK_TRACE_H_ | 139 #endif // BASE_DEBUG_STACK_TRACE_H_ |
OLD | NEW |