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