blob: 42234ef91f378d8c927d2037cf5f3e7fbd4b2e88 [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
[email protected]11b93faa2012-11-01 21:58:3021// Enables stack dump to console output on exception and signals.
22// When enabled, the process will quit immediately. This is meant to be used in
23// unit_tests only! This is not thread-safe: only call from main thread.
24BASE_EXPORT bool EnableInProcessStackDumping();
25
[email protected]58580352010-10-26 04:07:5026// A stacktrace can be helpful in debugging. For example, you can include a
27// stacktrace member in a object (probably around #ifndef NDEBUG) so that you
28// can later see where the given object was created from.
[email protected]0bea7252011-08-05 15:34:0029class BASE_EXPORT StackTrace {
[email protected]58580352010-10-26 04:07:5030 public:
31 // Creates a stacktrace from the current location.
32 StackTrace();
33
[email protected]f01ae9812011-08-30 19:33:0434 // Creates a stacktrace from an existing array of instruction
35 // pointers (such as returned by Addresses()). |count| will be
36 // trimmed to |kMaxTraces|.
37 StackTrace(const void* const* trace, size_t count);
38
[email protected]58580352010-10-26 04:07:5039#if defined(OS_WIN)
40 // Creates a stacktrace for an exception.
41 // Note: this function will throw an import not found (StackWalk64) exception
42 // on system without dbghelp 5.1.
43 StackTrace(_EXCEPTION_POINTERS* exception_pointers);
44#endif
45
46 // Copying and assignment are allowed with the default functions.
47
48 ~StackTrace();
49
50 // Gets an array of instruction pointer values. |*count| will be set to the
51 // number of elements in the returned array.
[email protected]501dfc42011-04-14 16:34:0052 const void* const* Addresses(size_t* count) const;
[email protected]58580352010-10-26 04:07:5053
54 // Prints a backtrace to stderr
[email protected]501dfc42011-04-14 16:34:0055 void PrintBacktrace() const;
[email protected]58580352010-10-26 04:07:5056
57 // Resolves backtrace to symbols and write to stream.
[email protected]501dfc42011-04-14 16:34:0058 void OutputToStream(std::ostream* os) const;
[email protected]58580352010-10-26 04:07:5059
[email protected]d4114ba2011-10-12 16:13:4060 // Resolves backtrace to symbols and returns as string.
61 std::string ToString() const;
62
[email protected]58580352010-10-26 04:07:5063 private:
64 // From http://msdn.microsoft.com/en-us/library/bb204633.aspx,
65 // the sum of FramesToSkip and FramesToCapture must be less than 63,
66 // so set it to 62. Even if on POSIX it could be a larger value, it usually
67 // doesn't give much more information.
68 static const int kMaxTraces = 62;
69
70 void* trace_[kMaxTraces];
[email protected]e645c7a2012-07-25 21:43:4471
72 // The number of valid frames in |trace_|.
73 size_t count_;
[email protected]58580352010-10-26 04:07:5074};
75
[email protected]1e218b72012-11-14 19:32:2376namespace internal {
77
78#if defined(OS_POSIX) && !defined(OS_ANDROID)
79// POSIX doesn't define any async-signal safe function for converting
80// an integer to ASCII. We'll have to define our own version.
81// itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the
82// conversion was successful or NULL otherwise. It never writes more than "sz"
83// bytes. Output will be truncated as needed, and a NUL character is always
84// appended.
85BASE_EXPORT char *itoa_r(intptr_t i, char *buf, size_t sz, int base);
86#endif // defined(OS_POSIX) && !defined(OS_ANDROID)
87
88} // namespace internal
89
[email protected]58580352010-10-26 04:07:5090} // namespace debug
91} // namespace base
92
93#endif // BASE_DEBUG_STACK_TRACE_H_