| 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" | |
| 15 #include "base/macros.h" | 14 #include "base/macros.h" |
| 16 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 17 | 16 |
| 18 #if defined(OS_POSIX) | 17 #if defined(OS_POSIX) |
| 19 #include <unistd.h> | 18 #include <unistd.h> |
| 20 #endif | 19 #endif |
| 21 | 20 |
| 22 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
| 23 struct _EXCEPTION_POINTERS; | 22 struct _EXCEPTION_POINTERS; |
| 24 struct _CONTEXT; | 23 struct _CONTEXT; |
| 25 #endif | 24 #endif |
| 26 | 25 |
| 27 // Fast stack trace capture using frame pointers is only available under POSIX | 26 // TODO(699863): Clean up HAVE_TRACE_STACK_FRAME_POINTERS. |
| 28 // platforms, and only for certain architectures, and only when frame pointers | |
| 29 // are enabled in the build. | |
| 30 #if defined(OS_POSIX) | 27 #if defined(OS_POSIX) |
| 31 #if BUILDFLAG(ENABLE_PROFILING) || !defined(NDEBUG) | |
| 32 | 28 |
| 33 #if defined(__i386__) || defined(__x86_64__) | 29 #if defined(__i386__) || defined(__x86_64__) |
| 34 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 | 30 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 |
| 35 #elif defined(__arm__) && !defined(__thumb__) | 31 #elif defined(__arm__) && !defined(__thumb__) |
| 36 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 | 32 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 |
| 37 #else // defined(__arm__) && !defined(__thumb__) | 33 #else // defined(__arm__) && !defined(__thumb__) |
| 38 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 | 34 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 |
| 39 #endif // defined(__arm__) && !defined(__thumb__) | 35 #endif // defined(__arm__) && !defined(__thumb__) |
| 40 | 36 |
| 41 #else // BUILDFLAG(ENABLE_PROFILING) || !defined(NDEBUG) | 37 #elif defined(OS_WIN) |
| 38 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 |
| 39 |
| 40 #else // defined(OS_WIN) |
| 42 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 | 41 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 |
| 43 #endif // BUILDFLAG(ENABLE_PROFILING) || !defined(NDEBUG) | 42 #endif // defined(OS_WIN) |
| 44 | |
| 45 #else // defined(OS_POSIX) | |
| 46 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 | |
| 47 #endif // defined(OS_POSIX) | |
| 48 | 43 |
| 49 namespace base { | 44 namespace base { |
| 50 namespace debug { | 45 namespace debug { |
| 51 | 46 |
| 52 // Enables stack dump to console output on exception and signals. | 47 // Enables stack dump to console output on exception and signals. |
| 53 // When enabled, the process will quit immediately. This is meant to be used in | 48 // When enabled, the process will quit immediately. This is meant to be used in |
| 54 // unit_tests only! This is not thread-safe: only call from main thread. | 49 // unit_tests only! This is not thread-safe: only call from main thread. |
| 55 // In sandboxed processes, this has to be called before the sandbox is turned | 50 // In sandboxed processes, this has to be called before the sandbox is turned |
| 56 // on. | 51 // on. |
| 57 // Calling this function on Linux opens /proc/self/maps and caches its | 52 // Calling this function on Linux opens /proc/self/maps and caches its |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 // but not for release builds (although there are some exceptions). | 125 // but not for release builds (although there are some exceptions). |
| 131 // | 126 // |
| 132 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| | 127 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| |
| 133 // after skipping |skip_initial| frames. Note that the function itself is not | 128 // after skipping |skip_initial| frames. Note that the function itself is not |
| 134 // added to the trace so |skip_initial| should be 0 in most cases. | 129 // added to the trace so |skip_initial| should be 0 in most cases. |
| 135 // Returns number of frames written. | 130 // Returns number of frames written. |
| 136 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, | 131 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, |
| 137 size_t max_depth, | 132 size_t max_depth, |
| 138 size_t skip_initial); | 133 size_t skip_initial); |
| 139 | 134 |
| 135 #if !defined(OS_WIN) |
| 140 // Links stack frame |fp| to |parent_fp|, so that during stack unwinding | 136 // Links stack frame |fp| to |parent_fp|, so that during stack unwinding |
| 141 // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. | 137 // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. |
| 142 // Both frame pointers must come from __builtin_frame_address(). | 138 // Both frame pointers must come from __builtin_frame_address(). |
| 143 // Destructor restores original linkage of |fp| to avoid corrupting caller's | 139 // Destructor restores original linkage of |fp| to avoid corrupting caller's |
| 144 // frame register on return. | 140 // frame register on return. |
| 145 // | 141 // |
| 146 // This class can be used to repair broken stack frame chain in cases | 142 // This class can be used to repair broken stack frame chain in cases |
| 147 // when execution flow goes into code built without frame pointers: | 143 // when execution flow goes into code built without frame pointers: |
| 148 // | 144 // |
| 149 // void DoWork() { | 145 // void DoWork() { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 179 ScopedStackFrameLinker(void* fp, void* parent_fp); | 175 ScopedStackFrameLinker(void* fp, void* parent_fp); |
| 180 ~ScopedStackFrameLinker(); | 176 ~ScopedStackFrameLinker(); |
| 181 | 177 |
| 182 private: | 178 private: |
| 183 void* fp_; | 179 void* fp_; |
| 184 void* parent_fp_; | 180 void* parent_fp_; |
| 185 void* original_parent_fp_; | 181 void* original_parent_fp_; |
| 186 | 182 |
| 187 DISALLOW_COPY_AND_ASSIGN(ScopedStackFrameLinker); | 183 DISALLOW_COPY_AND_ASSIGN(ScopedStackFrameLinker); |
| 188 }; | 184 }; |
| 185 #endif // !defined(OS_WIN) |
| 189 | 186 |
| 190 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | 187 #endif // HAVE_TRACE_STACK_FRAME_POINTERS |
| 191 | 188 |
| 192 namespace internal { | 189 namespace internal { |
| 193 | 190 |
| 194 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 191 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 195 // POSIX doesn't define any async-signal safe function for converting | 192 // POSIX doesn't define any async-signal safe function for converting |
| 196 // an integer to ASCII. We'll have to define our own version. | 193 // an integer to ASCII. We'll have to define our own version. |
| 197 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the | 194 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the |
| 198 // conversion was successful or NULL otherwise. It never writes more than "sz" | 195 // conversion was successful or NULL otherwise. It never writes more than "sz" |
| 199 // bytes. Output will be truncated as needed, and a NUL character is always | 196 // bytes. Output will be truncated as needed, and a NUL character is always |
| 200 // appended. | 197 // appended. |
| 201 BASE_EXPORT char *itoa_r(intptr_t i, | 198 BASE_EXPORT char *itoa_r(intptr_t i, |
| 202 char *buf, | 199 char *buf, |
| 203 size_t sz, | 200 size_t sz, |
| 204 int base, | 201 int base, |
| 205 size_t padding); | 202 size_t padding); |
| 206 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) | 203 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
| 207 | 204 |
| 208 } // namespace internal | 205 } // namespace internal |
| 209 | 206 |
| 210 } // namespace debug | 207 } // namespace debug |
| 211 } // namespace base | 208 } // namespace base |
| 212 | 209 |
| 213 #endif // BASE_DEBUG_STACK_TRACE_H_ | 210 #endif // BASE_DEBUG_STACK_TRACE_H_ |
| OLD | NEW |