Home | History | Annotate | Download | only in debug
      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
      3 // found in the LICENSE file.
      4 
      5 // Slightly adapted for inclusion in V8.
      6 // Copyright 2016 the V8 project authors. All rights reserved.
      7 
      8 #ifndef V8_BASE_DEBUG_STACK_TRACE_H_
      9 #define V8_BASE_DEBUG_STACK_TRACE_H_
     10 
     11 #include <stddef.h>
     12 
     13 #include <iosfwd>
     14 #include <string>
     15 
     16 #include "src/base/base-export.h"
     17 #include "src/base/build_config.h"
     18 
     19 #if V8_OS_POSIX
     20 #include <unistd.h>
     21 #endif
     22 
     23 #if V8_OS_WIN
     24 struct _EXCEPTION_POINTERS;
     25 struct _CONTEXT;
     26 #endif
     27 
     28 namespace v8 {
     29 namespace base {
     30 namespace debug {
     31 
     32 // Enables stack dump to console output on exception and signals.
     33 // When enabled, the process will quit immediately. This is meant to be used in
     34 // tests only!
     35 V8_BASE_EXPORT bool EnableInProcessStackDumping();
     36 V8_BASE_EXPORT void DisableSignalStackDump();
     37 
     38 // A stacktrace can be helpful in debugging. For example, you can include a
     39 // stacktrace member in a object (probably around #ifndef NDEBUG) so that you
     40 // can later see where the given object was created from.
     41 class StackTrace {
     42  public:
     43   // Creates a stacktrace from the current location.
     44   StackTrace();
     45 
     46   // Creates a stacktrace from an existing array of instruction
     47   // pointers (such as returned by Addresses()).  |count| will be
     48   // trimmed to |kMaxTraces|.
     49   StackTrace(const void* const* trace, size_t count);
     50 
     51 #if V8_OS_WIN
     52   // Creates a stacktrace for an exception.
     53   // Note: this function will throw an import not found (StackWalk64) exception
     54   // on system without dbghelp 5.1.
     55   explicit StackTrace(_EXCEPTION_POINTERS* exception_pointers);
     56   explicit StackTrace(const _CONTEXT* context);
     57 #endif
     58 
     59   // Copying and assignment are allowed with the default functions.
     60 
     61   ~StackTrace();
     62 
     63   // Gets an array of instruction pointer values. |*count| will be set to the
     64   // number of elements in the returned array.
     65   const void* const* Addresses(size_t* count) const;
     66 
     67   // Prints the stack trace to stderr.
     68   void Print() const;
     69 
     70   // Resolves backtrace to symbols and write to stream.
     71   void OutputToStream(std::ostream* os) const;
     72 
     73   // Resolves backtrace to symbols and returns as string.
     74   std::string ToString() const;
     75 
     76  private:
     77 #if V8_OS_WIN
     78   void InitTrace(const _CONTEXT* context_record);
     79 #endif
     80 
     81   // From http://msdn.microsoft.com/en-us/library/bb204633.aspx,
     82   // the sum of FramesToSkip and FramesToCapture must be less than 63,
     83   // so set it to 62. Even if on POSIX it could be a larger value, it usually
     84   // doesn't give much more information.
     85   static const int kMaxTraces = 62;
     86 
     87   void* trace_[kMaxTraces];
     88 
     89   // The number of valid frames in |trace_|.
     90   size_t count_;
     91 };
     92 
     93 }  // namespace debug
     94 }  // namespace base
     95 }  // namespace v8
     96 
     97 #endif  // V8_BASE_DEBUG_STACK_TRACE_H_
     98