stackwalk.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: stackwalk.h
  3. // Purpose: interface of wxStackWalker
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. This is the default value of the wxStackWalker::Walk function.
  9. */
  10. #define wxSTACKWALKER_MAX_DEPTH (200)
  11. /**
  12. @class wxStackWalker
  13. wxStackWalker allows an application to enumerate, or walk, the stack frames
  14. (the function callstack).
  15. It is mostly useful in only two situations: inside wxApp::OnFatalException
  16. function to programmatically get the location of the crash and, in debug builds,
  17. in wxApp::OnAssertFailure to report the caller of the failed assert.
  18. wxStackWalker works by repeatedly calling the wxStackWalker::OnStackFrame
  19. method for each frame in the stack, so to use it you must derive your own
  20. class from it and override this method.
  21. This class will not return anything except raw stack frame addresses if the
  22. debug information is not available. Under Win32 this means that the PDB file
  23. matching the program being executed should be present.
  24. Note that if you use Microsoft Visual C++ compiler, you can create PDB files
  25. even for the programs built in release mode and it doesn't affect the program
  26. size (at least if you don't forget to add @c /opt:ref option which is suppressed
  27. by using @c /debug linker option by default but should be always enabled for
  28. release builds).
  29. Under Unix, you need to compile your program with debugging information
  30. (usually using @c -g compiler and linker options) to get the file and line
  31. numbers information, however function names should be available even without it.
  32. Of course, all this is only @true if you build using a recent enough version
  33. of GNU libc which provides the @c backtrace() function needed to walk the stack.
  34. See @ref overview_debugging for how to make it available.
  35. @library{wxbase}
  36. @category{debugging}
  37. @see wxStackFrame
  38. */
  39. class wxStackWalker
  40. {
  41. public:
  42. /**
  43. Constructor does nothing, use Walk() to walk the stack.
  44. */
  45. wxStackWalker(const char* argv0 = NULL);
  46. /**
  47. Destructor does nothing neither but should be virtual as this class is used as
  48. a base one.
  49. */
  50. virtual ~wxStackWalker();
  51. /**
  52. Enumerate stack frames from the current location, skipping the initial
  53. number of them (this can be useful when Walk() is called from some known
  54. location and you don't want to see the first few frames anyhow; also
  55. notice that Walk() frame itself is not included if skip = 1).
  56. Up to @a maxDepth frames are walked from the innermost to the outermost one.
  57. It defaults to ::wxSTACKWALKER_MAX_DEPTH.
  58. */
  59. virtual void Walk(size_t skip = 1, size_t maxDepth = wxSTACKWALKER_MAX_DEPTH);
  60. /**
  61. Enumerate stack frames from the location of uncaught exception.
  62. This method can only be called from wxApp::OnFatalException().
  63. Up to @a maxDepth frames are walked from the innermost to the outermost one.
  64. It defaults to ::wxSTACKWALKER_MAX_DEPTH.
  65. */
  66. virtual void WalkFromException(size_t maxDepth = wxSTACKWALKER_MAX_DEPTH);
  67. protected:
  68. /**
  69. This function must be overidden to process the given frame.
  70. */
  71. virtual void OnStackFrame(const wxStackFrame& frame) = 0;
  72. };
  73. /**
  74. @class wxStackFrame
  75. wxStackFrame represents a single stack frame, or a single function in the call
  76. stack, and is used exclusively together with wxStackWalker, see there for a more
  77. detailed discussion.
  78. @library{wxbase}
  79. @category{debugging}
  80. @see wxStackWalker
  81. */
  82. class wxStackFrame
  83. {
  84. public:
  85. /**
  86. Return the address of this frame.
  87. */
  88. void* GetAddress() const;
  89. /**
  90. Return the name of the file containing this frame, empty if unavailable
  91. (typically because debug info is missing).
  92. Use HasSourceLocation() to check whether the file name is available.
  93. */
  94. wxString GetFileName() const;
  95. /**
  96. Get the level of this frame (deepest/innermost one is 0).
  97. */
  98. size_t GetLevel() const;
  99. /**
  100. Return the line number of this frame, 0 if unavailable.
  101. @see GetFileName()
  102. */
  103. size_t GetLine() const;
  104. /**
  105. Get the module this function belongs to (empty if not available).
  106. */
  107. wxString GetModule() const;
  108. /**
  109. Return the unmangled (if possible) name of the function containing this frame.
  110. */
  111. wxString GetName() const;
  112. /**
  113. Return the return address of this frame.
  114. */
  115. size_t GetOffset() const;
  116. /**
  117. Get the name, type and value (in text form) of the given parameter.
  118. Any pointer may be @NULL if you're not interested in the corresponding value.
  119. Return @true if at least some values could be retrieved.
  120. This function currently is only implemented under Win32 and requires a PDB file.
  121. */
  122. virtual bool GetParam(size_t n, wxString* type, wxString* name,
  123. wxString* value) const;
  124. /**
  125. Return the number of parameters of this function (may return 0 if we
  126. can't retrieve the parameters info even although the function does have
  127. parameters).
  128. */
  129. virtual size_t GetParamCount() const;
  130. /**
  131. Return @true if we have the file name and line number for this frame.
  132. */
  133. bool HasSourceLocation() const;
  134. };