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 "build/build_config.h" | 14 #include "build/build_config.h" |
15 | 15 |
16 #if defined(OS_POSIX) | 16 #if defined(OS_POSIX) |
17 #include <unistd.h> | 17 #include <unistd.h> |
18 #endif | 18 #endif |
19 | 19 |
20 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
21 struct _EXCEPTION_POINTERS; | 21 struct _EXCEPTION_POINTERS; |
22 struct _CONTEXT; | 22 struct _CONTEXT; |
23 #endif | 23 #endif |
24 | 24 |
25 #if defined(OS_POSIX) && (defined(__i386__) || defined(__x86_64__)) | |
26 #define HAVE_TRACE_STACK_FRAME_POINTERS 1 | |
27 #else | |
28 #define HAVE_TRACE_STACK_FRAME_POINTERS 0 | |
29 #endif | |
30 | |
25 namespace base { | 31 namespace base { |
26 namespace debug { | 32 namespace debug { |
27 | 33 |
28 // Enables stack dump to console output on exception and signals. | 34 // Enables stack dump to console output on exception and signals. |
29 // When enabled, the process will quit immediately. This is meant to be used in | 35 // When enabled, the process will quit immediately. This is meant to be used in |
30 // unit_tests only! This is not thread-safe: only call from main thread. | 36 // unit_tests only! This is not thread-safe: only call from main thread. |
31 // In sandboxed processes, this has to be called before the sandbox is turned | 37 // In sandboxed processes, this has to be called before the sandbox is turned |
32 // on. | 38 // on. |
33 // Calling this function on Linux opens /proc/self/maps and caches its | 39 // Calling this function on Linux opens /proc/self/maps and caches its |
34 // contents. In non-official builds, this function also opens the object files | 40 // contents. In non-official builds, this function also opens the object files |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 void Print() const; | 75 void Print() const; |
70 | 76 |
71 #if !defined(__UCLIBC__) | 77 #if !defined(__UCLIBC__) |
72 // Resolves backtrace to symbols and write to stream. | 78 // Resolves backtrace to symbols and write to stream. |
73 void OutputToStream(std::ostream* os) const; | 79 void OutputToStream(std::ostream* os) const; |
74 #endif | 80 #endif |
75 | 81 |
76 // Resolves backtrace to symbols and returns as string. | 82 // Resolves backtrace to symbols and returns as string. |
77 std::string ToString() const; | 83 std::string ToString() const; |
78 | 84 |
85 static StackTrace FromFramePointers(size_t max_depth); | |
86 | |
79 private: | 87 private: |
80 #if defined(OS_WIN) | 88 #if defined(OS_WIN) |
81 void InitTrace(const _CONTEXT* context_record); | 89 void InitTrace(const _CONTEXT* context_record); |
82 #endif | 90 #endif |
83 | 91 |
84 // From http://msdn.microsoft.com/en-us/library/bb204633.aspx, | 92 // From http://msdn.microsoft.com/en-us/library/bb204633.aspx, |
85 // the sum of FramesToSkip and FramesToCapture must be less than 63, | 93 // the sum of FramesToSkip and FramesToCapture must be less than 63, |
86 // so set it to 62. Even if on POSIX it could be a larger value, it usually | 94 // so set it to 62. Even if on POSIX it could be a larger value, it usually |
87 // doesn't give much more information. | 95 // doesn't give much more information. |
88 static const int kMaxTraces = 62; | 96 static const int kMaxTraces = 62; |
89 | 97 |
90 void* trace_[kMaxTraces]; | 98 void* trace_[kMaxTraces]; |
91 | 99 |
92 // The number of valid frames in |trace_|. | 100 // The number of valid frames in |trace_|. |
93 size_t count_; | 101 size_t count_; |
94 }; | 102 }; |
95 | 103 |
104 #if HAVE_TRACE_STACK_FRAME_POINTERS | |
105 // Traces the stack by using frame pointers. This function is faster but less | |
106 // reliable than StackTrace. It should work for debug and profiling builds, | |
107 // but not for release builds (although there are some exceptions). | |
108 // | |
109 // Writes at most |max_depth| frames (instruction pointers) into |out_trace| | |
110 // after skipping |skip_count| frames. Note that the function itself is not | |
111 // added to the trace so |skip_count| should be 0 in most cases. | |
112 // Returns number of frames written. | |
113 BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, | |
114 size_t max_depth, | |
115 size_t skip_count); | |
bcwhite
2016/04/13 12:36:24
skip_initial?
| |
116 #endif // HAVE_TRACE_STACK_FRAME_POINTERS | |
117 | |
96 namespace internal { | 118 namespace internal { |
97 | 119 |
98 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 120 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
99 // POSIX doesn't define any async-signal safe function for converting | 121 // POSIX doesn't define any async-signal safe function for converting |
100 // an integer to ASCII. We'll have to define our own version. | 122 // an integer to ASCII. We'll have to define our own version. |
101 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the | 123 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the |
102 // conversion was successful or NULL otherwise. It never writes more than "sz" | 124 // conversion was successful or NULL otherwise. It never writes more than "sz" |
103 // bytes. Output will be truncated as needed, and a NUL character is always | 125 // bytes. Output will be truncated as needed, and a NUL character is always |
104 // appended. | 126 // appended. |
105 BASE_EXPORT char *itoa_r(intptr_t i, | 127 BASE_EXPORT char *itoa_r(intptr_t i, |
106 char *buf, | 128 char *buf, |
107 size_t sz, | 129 size_t sz, |
108 int base, | 130 int base, |
109 size_t padding); | 131 size_t padding); |
110 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) | 132 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
111 | 133 |
112 } // namespace internal | 134 } // namespace internal |
113 | 135 |
114 } // namespace debug | 136 } // namespace debug |
115 } // namespace base | 137 } // namespace base |
116 | 138 |
117 #endif // BASE_DEBUG_STACK_TRACE_H_ | 139 #endif // BASE_DEBUG_STACK_TRACE_H_ |
OLD | NEW |