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