stackwalk.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/stackwalk.h
  3. // Purpose: wxStackWalker and related classes, common part
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 2005-01-07
  7. // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_STACKWALK_H_
  11. #define _WX_STACKWALK_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_STACKWALKER
  14. class WXDLLIMPEXP_FWD_BASE wxStackFrame;
  15. #define wxSTACKWALKER_MAX_DEPTH (200)
  16. // ----------------------------------------------------------------------------
  17. // wxStackFrame: a single stack level
  18. // ----------------------------------------------------------------------------
  19. class WXDLLIMPEXP_BASE wxStackFrameBase
  20. {
  21. private:
  22. // put this inline function here so that it is defined before use
  23. wxStackFrameBase *ConstCast() const
  24. { return const_cast<wxStackFrameBase *>(this); }
  25. public:
  26. wxStackFrameBase(size_t level, void *address = NULL)
  27. {
  28. m_level = level;
  29. m_line =
  30. m_offset = 0;
  31. m_address = address;
  32. }
  33. // get the level of this frame (deepest/innermost one is 0)
  34. size_t GetLevel() const { return m_level; }
  35. // return the address of this frame
  36. void *GetAddress() const { return m_address; }
  37. // return the unmangled (if possible) name of the function containing this
  38. // frame
  39. wxString GetName() const { ConstCast()->OnGetName(); return m_name; }
  40. // return the instruction pointer offset from the start of the function
  41. size_t GetOffset() const { ConstCast()->OnGetName(); return m_offset; }
  42. // get the module this function belongs to (not always available)
  43. wxString GetModule() const { ConstCast()->OnGetName(); return m_module; }
  44. // return true if we have the filename and line number for this frame
  45. bool HasSourceLocation() const { return !GetFileName().empty(); }
  46. // return the name of the file containing this frame, empty if
  47. // unavailable (typically because debug info is missing)
  48. wxString GetFileName() const
  49. { ConstCast()->OnGetLocation(); return m_filename; }
  50. // return the line number of this frame, 0 if unavailable
  51. size_t GetLine() const { ConstCast()->OnGetLocation(); return m_line; }
  52. // return the number of parameters of this function (may return 0 if we
  53. // can't retrieve the parameters info even although the function does have
  54. // parameters)
  55. virtual size_t GetParamCount() const { return 0; }
  56. // get the name, type and value (in text form) of the given parameter
  57. //
  58. // any pointer may be NULL
  59. //
  60. // return true if at least some values could be retrieved
  61. virtual bool GetParam(size_t WXUNUSED(n),
  62. wxString * WXUNUSED(type),
  63. wxString * WXUNUSED(name),
  64. wxString * WXUNUSED(value)) const
  65. {
  66. return false;
  67. }
  68. // although this class is not supposed to be used polymorphically, give it
  69. // a virtual dtor to silence compiler warnings
  70. virtual ~wxStackFrameBase() { }
  71. protected:
  72. // hooks for derived classes to initialize some fields on demand
  73. virtual void OnGetName() { }
  74. virtual void OnGetLocation() { }
  75. // fields are protected, not private, so that OnGetXXX() could modify them
  76. // directly
  77. size_t m_level;
  78. wxString m_name,
  79. m_module,
  80. m_filename;
  81. size_t m_line;
  82. void *m_address;
  83. size_t m_offset;
  84. };
  85. // ----------------------------------------------------------------------------
  86. // wxStackWalker: class for enumerating stack frames
  87. // ----------------------------------------------------------------------------
  88. class WXDLLIMPEXP_BASE wxStackWalkerBase
  89. {
  90. public:
  91. // ctor does nothing, use Walk() to walk the stack
  92. wxStackWalkerBase() { }
  93. // dtor does nothing neither but should be virtual
  94. virtual ~wxStackWalkerBase() { }
  95. // enumerate stack frames from the current location, skipping the initial
  96. // number of them (this can be useful when Walk() is called from some known
  97. // location and you don't want to see the first few frames anyhow; also
  98. // notice that Walk() frame itself is not included if skip >= 1)
  99. virtual void Walk(size_t skip = 1, size_t maxDepth = wxSTACKWALKER_MAX_DEPTH) = 0;
  100. #if wxUSE_ON_FATAL_EXCEPTION
  101. // enumerate stack frames from the location of uncaught exception
  102. //
  103. // this version can only be called from wxApp::OnFatalException()
  104. virtual void WalkFromException(size_t maxDepth = wxSTACKWALKER_MAX_DEPTH) = 0;
  105. #endif // wxUSE_ON_FATAL_EXCEPTION
  106. protected:
  107. // this function must be overrided to process the given frame
  108. virtual void OnStackFrame(const wxStackFrame& frame) = 0;
  109. };
  110. #ifdef __WINDOWS__
  111. #include "wx/msw/stackwalk.h"
  112. #elif defined(__UNIX__)
  113. #include "wx/unix/stackwalk.h"
  114. #else
  115. #error "wxStackWalker is not supported, set wxUSE_STACKWALKER to 0"
  116. #endif
  117. #endif // wxUSE_STACKWALKER
  118. #endif // _WX_STACKWALK_H_