blob: e19286890be1940da9ed298122064f3e3fe2b75b [file] [log] [blame]
[email protected]e645c7a2012-07-25 21:43:441// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]58580352010-10-26 04:07:502// 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_
[email protected]58580352010-10-26 04:07:507
8#include <iosfwd>
[email protected]d4114ba2011-10-12 16:13:409#include <string>
[email protected]58580352010-10-26 04:07:5010
[email protected]0bea7252011-08-05 15:34:0011#include "base/base_export.h"
[email protected]58580352010-10-26 04:07:5012#include "build/build_config.h"
13
14#if defined(OS_WIN)
15struct _EXCEPTION_POINTERS;
16#endif
17
18namespace base {
19namespace 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]0bea7252011-08-05 15:34:0024class BASE_EXPORT StackTrace {
[email protected]58580352010-10-26 04:07:5025 public:
26 // Creates a stacktrace from the current location.
27 StackTrace();
28
[email protected]f01ae9812011-08-30 19:33:0429 // Creates a stacktrace from an existing array of instruction
30 // pointers (such as returned by Addresses()). |count| will be
31 // trimmed to |kMaxTraces|.
32 StackTrace(const void* const* trace, size_t count);
33
[email protected]58580352010-10-26 04:07:5034#if defined(OS_WIN)
35 // Creates a stacktrace for an exception.
36 // Note: this function will throw an import not found (StackWalk64) exception
37 // on system without dbghelp 5.1.
38 StackTrace(_EXCEPTION_POINTERS* exception_pointers);
39#endif
40
41 // Copying and assignment are allowed with the default functions.
42
43 ~StackTrace();
44
45 // Gets an array of instruction pointer values. |*count| will be set to the
46 // number of elements in the returned array.
[email protected]501dfc42011-04-14 16:34:0047 const void* const* Addresses(size_t* count) const;
[email protected]58580352010-10-26 04:07:5048
49 // Prints a backtrace to stderr
[email protected]501dfc42011-04-14 16:34:0050 void PrintBacktrace() const;
[email protected]58580352010-10-26 04:07:5051
52 // Resolves backtrace to symbols and write to stream.
[email protected]501dfc42011-04-14 16:34:0053 void OutputToStream(std::ostream* os) const;
[email protected]58580352010-10-26 04:07:5054
[email protected]d4114ba2011-10-12 16:13:4055 // Resolves backtrace to symbols and returns as string.
56 std::string ToString() const;
57
[email protected]58580352010-10-26 04:07:5058 private:
59 // From http://msdn.microsoft.com/en-us/library/bb204633.aspx,
60 // the sum of FramesToSkip and FramesToCapture must be less than 63,
61 // so set it to 62. Even if on POSIX it could be a larger value, it usually
62 // doesn't give much more information.
63 static const int kMaxTraces = 62;
64
65 void* trace_[kMaxTraces];
[email protected]e645c7a2012-07-25 21:43:4466
67 // The number of valid frames in |trace_|.
68 size_t count_;
[email protected]58580352010-10-26 04:07:5069};
70
71} // namespace debug
72} // namespace base
73
74#endif // BASE_DEBUG_STACK_TRACE_H_