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 // Fast stack trace capture using frame pointers is only available under POSIX |
| 28 // platforms, and only for certain architectures, and only when frame pointers | |
| 29 // are enabled in the build. | |
| 27 #if defined(OS_POSIX) | 30 #if defined(OS_POSIX) |
| 31 #if BUILDFLAG(ENABLE_PROFILING) || !defined(NDEBUG) | |
| 28 | 32 |
| 29 #if defined(__i386__) || defined(__x86_64__) | 33 #if defined(__i386__) || defined(__x86_64__) |
| 30 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 | 34 #define HAVE_TRACE_STACK_FRAME_POINTERS() 1 |
|
Wez
2017/03/27 03:59:37
FYI: Turned this back into a non-function-style ma
| |
| 31 #elif defined(__arm__) && !defined(__thumb__) | 35 #elif defined(__arm__) && !defined(__thumb__) |
| 32 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 | 36 #define HAVE_TRACE_STACK_FRAME_POINTERS() 1 |
| 33 #else // defined(__arm__) && !defined(__thumb__) | 37 #else // defined(__arm__) && !defined(__thumb__) |
| 34 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 | 38 #define HAVE_TRACE_STACK_FRAME_POINTERS() 0 |
| 35 #endif // defined(__arm__) && !defined(__thumb__) | 39 #endif // defined(__arm__) && !defined(__thumb__) |
| 36 | 40 |
| 37 #elif defined(OS_WIN) | 41 #else // BUILDFLAG(ENABLE_PROFILING) || !defined(NDEBUG) |
| 38 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 | 42 #define HAVE_TRACE_STACK_FRAME_POINTERS() 0 |
| 43 #endif // BUILDFLAG(ENABLE_PROFILING) || !defined(NDEBUG) | |
| 39 | 44 |
| 40 #else // defined(OS_WIN) | 45 #else // defined(OS_POSIX) |
| 41 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 | 46 #define HAVE_TRACE_STACK_FRAME_POINTERS() 0 |
| 42 #endif // defined(OS_WIN) | 47 #endif // defined(OS_POSIX) |
| 43 | 48 |
| 44 namespace base { | 49 namespace base { |
| 45 namespace debug { | 50 namespace debug { |
| 46 | 51 |
| 47 // Enables stack dump to console output on exception and signals. | 52 // 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 | 53 // 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. | 54 // 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 | 55 // In sandboxed processes, this has to be called before the sandbox is turned |
| 51 // on. | 56 // on. |
| 52 // Calling this function on Linux opens /proc/self/maps and caches its | 57 // 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 | 58 // 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 | 59 // that are loaded in memory and caches their file descriptors (this cannot be |
| 55 // done in official builds because it has security implications). | 60 // done in official builds because it has security implications). |
| 56 BASE_EXPORT bool EnableInProcessStackDumping(); | 61 BASE_EXPORT bool EnableInProcessStackDumping(); |
| 57 | 62 |
| 58 // Returns end of the stack, or 0 if we couldn't get it. | 63 // Returns end of the stack, or 0 if we couldn't get it. |
| 59 #if HAVE_TRACE_STACK_FRAME_POINTERS | 64 #if HAVE_TRACE_STACK_FRAME_POINTERS() |
| 60 BASE_EXPORT uintptr_t GetStackEnd(); | 65 BASE_EXPORT uintptr_t GetStackEnd(); |
| 61 #endif | 66 #endif |
| 62 | 67 |
| 63 // A stacktrace can be helpful in debugging. For example, you can include a | 68 // 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 | 69 // stacktrace member in a object (probably around #ifndef NDEBUG) so that you |
| 65 // can later see where the given object was created from. | 70 // can later see where the given object was created from. |
| 66 class BASE_EXPORT StackTrace { | 71 class BASE_EXPORT StackTrace { |
| 67 public: | 72 public: |
| 68 // Creates a stacktrace from the current location. | 73 // Creates a stacktrace from the current location. |
| 69 StackTrace(); | 74 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 | 117 // so set it to 62. Even if on POSIX it could be a larger value, it usually |
| 113 // doesn't give much more information. | 118 // doesn't give much more information. |
| 114 static const int kMaxTraces = 62; | 119 static const int kMaxTraces = 62; |
| 115 | 120 |
| 116 void* trace_[kMaxTraces]; | 121 void* trace_[kMaxTraces]; |
| 117 | 122 |
| 118 // The number of valid frames in |trace_|. | 123 // The number of valid frames in |trace_|. |
| 119 size_t count_; | 124 size_t count_; |
| 120 }; | 125 }; |
| 121 | 126 |
| 122 #if HAVE_TRACE_STACK_FRAME_POINTERS | 127 #if HAVE_TRACE_STACK_FRAME_POINTERS() |
| 123 // Traces the stack by using frame pointers. This function is faster but less | 128 // 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, | 129 // reliable than StackTrace. It should work for debug and profiling builds, |
| 125 // but not for release builds (although there are some exceptions). | 130 // but not for release builds (although there are some exceptions). |
| 126 // | 131 // |
| 127 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| | 132 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| |
| 128 // after skipping |skip_initial| frames. Note that the function itself is not | 133 // 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. | 134 // added to the trace so |skip_initial| should be 0 in most cases. |
| 130 // Returns number of frames written. | 135 // Returns number of frames written. |
| 131 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, | 136 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, |
| 132 size_t max_depth, | 137 size_t max_depth, |
| 133 size_t skip_initial); | 138 size_t skip_initial); |
| 134 | 139 |
| 135 #if !defined(OS_WIN) | |
| 136 // Links stack frame |fp| to |parent_fp|, so that during stack unwinding | 140 // Links stack frame |fp| to |parent_fp|, so that during stack unwinding |
| 137 // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. | 141 // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. |
| 138 // Both frame pointers must come from __builtin_frame_address(). | 142 // Both frame pointers must come from __builtin_frame_address(). |
| 139 // Destructor restores original linkage of |fp| to avoid corrupting caller's | 143 // Destructor restores original linkage of |fp| to avoid corrupting caller's |
| 140 // frame register on return. | 144 // frame register on return. |
| 141 // | 145 // |
| 142 // This class can be used to repair broken stack frame chain in cases | 146 // This class can be used to repair broken stack frame chain in cases |
| 143 // when execution flow goes into code built without frame pointers: | 147 // when execution flow goes into code built without frame pointers: |
| 144 // | 148 // |
| 145 // void DoWork() { | 149 // void DoWork() { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 175 ScopedStackFrameLinker(void* fp, void* parent_fp); | 179 ScopedStackFrameLinker(void* fp, void* parent_fp); |
| 176 ~ScopedStackFrameLinker(); | 180 ~ScopedStackFrameLinker(); |
| 177 | 181 |
| 178 private: | 182 private: |
| 179 void* fp_; | 183 void* fp_; |
| 180 void* parent_fp_; | 184 void* parent_fp_; |
| 181 void* original_parent_fp_; | 185 void* original_parent_fp_; |
| 182 | 186 |
| 183 DISALLOW_COPY_AND_ASSIGN(ScopedStackFrameLinker); | 187 DISALLOW_COPY_AND_ASSIGN(ScopedStackFrameLinker); |
| 184 }; | 188 }; |
| 185 #endif // !defined(OS_WIN) | |
| 186 | 189 |
| 187 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 190 #endif // HAVE_TRACE_STACK_FRAME_POINTERS() |
| 188 | 191 |
| 189 namespace internal { | 192 namespace internal { |
| 190 | 193 |
| 191 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 194 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 192 // POSIX doesn't define any async-signal safe function for converting | 195 // POSIX doesn't define any async-signal safe function for converting |
| 193 // an integer to ASCII. We'll have to define our own version. | 196 // 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 | 197 // 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" | 198 // 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 | 199 // bytes. Output will be truncated as needed, and a NUL character is always |
| 197 // appended. | 200 // appended. |
| 198 BASE_EXPORT char *itoa_r(intptr_t i, | 201 BASE_EXPORT char *itoa_r(intptr_t i, |
| 199 char *buf, | 202 char *buf, |
| 200 size_t sz, | 203 size_t sz, |
| 201 int base, | 204 int base, |
| 202 size_t padding); | 205 size_t padding); |
| 203 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) | 206 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
| 204 | 207 |
| 205 } // namespace internal | 208 } // namespace internal |
| 206 | 209 |
| 207 } // namespace debug | 210 } // namespace debug |
| 208 } // namespace base | 211 } // namespace base |
| 209 | 212 |
| 210 #endif // BASE_DEBUG_STACK_TRACE_H_ | 213 #endif // BASE_DEBUG_STACK_TRACE_H_ |
| OLD | NEW |