[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_DEBUG_STACK_TRACE_H_ |
| 6 | #define BASE_DEBUG_STACK_TRACE_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <iosfwd> |
| 10 | |
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 11 | #include "base/base_api.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 12 | #include "build/build_config.h" |
| 13 | |
| 14 | #if defined(OS_WIN) |
| 15 | struct _EXCEPTION_POINTERS; |
| 16 | #endif |
| 17 | |
| 18 | namespace base { |
| 19 | namespace debug { |
| 20 | |
| 21 | // A stacktrace can be helpful in debugging. For example, you can include a |
| 22 | // stacktrace member in a object (probably around #ifndef NDEBUG) so that you |
| 23 | // can later see where the given object was created from. |
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 24 | class BASE_API StackTrace { |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 25 | public: |
| 26 | // Creates a stacktrace from the current location. |
| 27 | StackTrace(); |
| 28 | |
| 29 | #if defined(OS_WIN) |
| 30 | // Creates a stacktrace for an exception. |
| 31 | // Note: this function will throw an import not found (StackWalk64) exception |
| 32 | // on system without dbghelp 5.1. |
| 33 | StackTrace(_EXCEPTION_POINTERS* exception_pointers); |
| 34 | #endif |
| 35 | |
| 36 | // Copying and assignment are allowed with the default functions. |
| 37 | |
| 38 | ~StackTrace(); |
| 39 | |
| 40 | // Gets an array of instruction pointer values. |*count| will be set to the |
| 41 | // number of elements in the returned array. |
[email protected] | 501dfc4 | 2011-04-14 16:34:00 | [diff] [blame^] | 42 | const void* const* Addresses(size_t* count) const; |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 43 | |
| 44 | // Prints a backtrace to stderr |
[email protected] | 501dfc4 | 2011-04-14 16:34:00 | [diff] [blame^] | 45 | void PrintBacktrace() const; |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 46 | |
| 47 | // Resolves backtrace to symbols and write to stream. |
[email protected] | 501dfc4 | 2011-04-14 16:34:00 | [diff] [blame^] | 48 | void OutputToStream(std::ostream* os) const; |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 49 | |
| 50 | private: |
| 51 | // From http://msdn.microsoft.com/en-us/library/bb204633.aspx, |
| 52 | // the sum of FramesToSkip and FramesToCapture must be less than 63, |
| 53 | // so set it to 62. Even if on POSIX it could be a larger value, it usually |
| 54 | // doesn't give much more information. |
| 55 | static const int kMaxTraces = 62; |
| 56 | |
| 57 | void* trace_[kMaxTraces]; |
| 58 | int count_; |
| 59 | }; |
| 60 | |
| 61 | } // namespace debug |
| 62 | } // namespace base |
| 63 | |
| 64 | #endif // BASE_DEBUG_STACK_TRACE_H_ |