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 "base/macros.h" | 14 #include "base/macros.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 | 16 |
17 #if defined(OS_POSIX) | 17 #if defined(OS_POSIX) |
18 #include <unistd.h> | 18 #include <unistd.h> |
19 #endif | 19 #endif |
20 | 20 |
21 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
22 struct _EXCEPTION_POINTERS; | 22 struct _EXCEPTION_POINTERS; |
23 struct _CONTEXT; | 23 struct _CONTEXT; |
24 #endif | 24 #endif |
25 | 25 |
26 // TODO(699863): Clean up HAVE_TRACE_STACK_FRAME_POINTERS. | 26 // Unwinding using frame pointers requires stack traces to have frame pointers |
27 #if defined(OS_POSIX) | 27 // by default. This still doesn't work on thumb architectures, due to an llvm |
28 | 28 // bug. https://bugs.llvm.org/show_bug.cgi?id=18505#c5 |
29 #if defined(__i386__) || defined(__x86_64__) | 29 #if ENABLED_FRAME_POINTERS && !(defined(__arm__) && defined(__thumb__)) |
Mark Mentovai
2017/03/30 23:43:29
Don’t you need to #include something to get ENABLE
erikchen
2017/04/03 06:33:39
yup
| |
30 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 | 30 #define CAN_UNWIND_WITH_FRAME_POINTERS 1 |
31 #elif defined(__arm__) && !defined(__thumb__) | 31 #else |
32 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 | 32 #define CAN_UNWIND_WITH_FRAME_POINTERS 0 |
33 #else // defined(__arm__) && !defined(__thumb__) | 33 #endif |
34 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 | |
35 #endif // defined(__arm__) && !defined(__thumb__) | |
36 | |
37 #elif defined(OS_WIN) | |
38 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 | |
39 | |
40 #else // defined(OS_WIN) | |
41 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 | |
42 #endif // defined(OS_WIN) | |
43 | 34 |
44 namespace base { | 35 namespace base { |
45 namespace debug { | 36 namespace debug { |
46 | 37 |
47 // Enables stack dump to console output on exception and signals. | 38 // Enables stack dump to console output on exception and signals. |
48 // When enabled, the process will quit immediately. This is meant to be used in | 39 // When enabled, the process will quit immediately. This is meant to be used in |
49 // unit_tests only! This is not thread-safe: only call from main thread. | 40 // unit_tests only! This is not thread-safe: only call from main thread. |
50 // In sandboxed processes, this has to be called before the sandbox is turned | 41 // In sandboxed processes, this has to be called before the sandbox is turned |
51 // on. | 42 // on. |
52 // Calling this function on Linux opens /proc/self/maps and caches its | 43 // Calling this function on Linux opens /proc/self/maps and caches its |
53 // contents. In non-official builds, this function also opens the object files | 44 // contents. In non-official builds, this function also opens the object files |
54 // that are loaded in memory and caches their file descriptors (this cannot be | 45 // that are loaded in memory and caches their file descriptors (this cannot be |
55 // done in official builds because it has security implications). | 46 // done in official builds because it has security implications). |
56 BASE_EXPORT bool EnableInProcessStackDumping(); | 47 BASE_EXPORT bool EnableInProcessStackDumping(); |
57 | 48 |
58 // Returns end of the stack, or 0 if we couldn't get it. | 49 // Returns end of the stack, or 0 if we couldn't get it. |
59 #if HAVE_TRACE_STACK_FRAME_POINTERS | 50 #if CAN_UNWIND_WITH_FRAME_POINTERS |
60 BASE_EXPORT uintptr_t GetStackEnd(); | 51 BASE_EXPORT uintptr_t GetStackEnd(); |
61 #endif | 52 #endif |
62 | 53 |
63 // A stacktrace can be helpful in debugging. For example, you can include a | 54 // A stacktrace can be helpful in debugging. For example, you can include a |
64 // stacktrace member in a object (probably around #ifndef NDEBUG) so that you | 55 // stacktrace member in a object (probably around #ifndef NDEBUG) so that you |
65 // can later see where the given object was created from. | 56 // can later see where the given object was created from. |
66 class BASE_EXPORT StackTrace { | 57 class BASE_EXPORT StackTrace { |
67 public: | 58 public: |
68 // Creates a stacktrace from the current location. | 59 // Creates a stacktrace from the current location. |
69 StackTrace(); | 60 StackTrace(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 // so set it to 62. Even if on POSIX it could be a larger value, it usually | 103 // so set it to 62. Even if on POSIX it could be a larger value, it usually |
113 // doesn't give much more information. | 104 // doesn't give much more information. |
114 static const int kMaxTraces = 62; | 105 static const int kMaxTraces = 62; |
115 | 106 |
116 void* trace_[kMaxTraces]; | 107 void* trace_[kMaxTraces]; |
117 | 108 |
118 // The number of valid frames in |trace_|. | 109 // The number of valid frames in |trace_|. |
119 size_t count_; | 110 size_t count_; |
120 }; | 111 }; |
121 | 112 |
122 #if HAVE_TRACE_STACK_FRAME_POINTERS | 113 #if CAN_UNWIND_WITH_FRAME_POINTERS |
123 // Traces the stack by using frame pointers. This function is faster but less | 114 // Traces the stack by using frame pointers. This function is faster but less |
124 // reliable than StackTrace. It should work for debug and profiling builds, | 115 // reliable than StackTrace. It should work for debug and profiling builds, |
125 // but not for release builds (although there are some exceptions). | 116 // but not for release builds (although there are some exceptions). |
126 // | 117 // |
127 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| | 118 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| |
128 // after skipping |skip_initial| frames. Note that the function itself is not | 119 // after skipping |skip_initial| frames. Note that the function itself is not |
129 // added to the trace so |skip_initial| should be 0 in most cases. | 120 // added to the trace so |skip_initial| should be 0 in most cases. |
130 // Returns number of frames written. | 121 // Returns number of frames written. |
131 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, | 122 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, |
132 size_t max_depth, | 123 size_t max_depth, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 | 168 |
178 private: | 169 private: |
179 void* fp_; | 170 void* fp_; |
180 void* parent_fp_; | 171 void* parent_fp_; |
181 void* original_parent_fp_; | 172 void* original_parent_fp_; |
182 | 173 |
183 DISALLOW_COPY_AND_ASSIGN(ScopedStackFrameLinker); | 174 DISALLOW_COPY_AND_ASSIGN(ScopedStackFrameLinker); |
184 }; | 175 }; |
185 #endif // !defined(OS_WIN) | 176 #endif // !defined(OS_WIN) |
186 | 177 |
187 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 178 #endif // CAN_UNWIND_WITH_FRAME_POINTERS |
188 | 179 |
189 namespace internal { | 180 namespace internal { |
190 | 181 |
191 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 182 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
192 // POSIX doesn't define any async-signal safe function for converting | 183 // POSIX doesn't define any async-signal safe function for converting |
193 // an integer to ASCII. We'll have to define our own version. | 184 // an integer to ASCII. We'll have to define our own version. |
194 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the | 185 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the |
195 // conversion was successful or NULL otherwise. It never writes more than "sz" | 186 // conversion was successful or NULL otherwise. It never writes more than "sz" |
196 // bytes. Output will be truncated as needed, and a NUL character is always | 187 // bytes. Output will be truncated as needed, and a NUL character is always |
197 // appended. | 188 // appended. |
198 BASE_EXPORT char *itoa_r(intptr_t i, | 189 BASE_EXPORT char *itoa_r(intptr_t i, |
199 char *buf, | 190 char *buf, |
200 size_t sz, | 191 size_t sz, |
201 int base, | 192 int base, |
202 size_t padding); | 193 size_t padding); |
203 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) | 194 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
204 | 195 |
205 } // namespace internal | 196 } // namespace internal |
206 | 197 |
207 } // namespace debug | 198 } // namespace debug |
208 } // namespace base | 199 } // namespace base |
209 | 200 |
210 #endif // BASE_DEBUG_STACK_TRACE_H_ | 201 #endif // BASE_DEBUG_STACK_TRACE_H_ |
OLD | NEW |