stackwalk.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/unix/stackwalk.h
  3. // Purpose: declaration of wxStackWalker for Unix
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 2005-01-19
  7. // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwindows.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_UNIX_STACKWALK_H_
  11. #define _WX_UNIX_STACKWALK_H_
  12. // ----------------------------------------------------------------------------
  13. // wxStackFrame
  14. // ----------------------------------------------------------------------------
  15. class WXDLLIMPEXP_BASE wxStackFrame : public wxStackFrameBase
  16. {
  17. friend class wxStackWalker;
  18. public:
  19. // arguments are the stack depth of this frame, its address and the return
  20. // value of backtrace_symbols() for it
  21. //
  22. // NB: we don't copy syminfo pointer so it should have lifetime at least as
  23. // long as ours
  24. wxStackFrame(size_t level = 0, void *address = NULL, const char *syminfo = NULL)
  25. : wxStackFrameBase(level, address)
  26. {
  27. m_syminfo = syminfo;
  28. }
  29. protected:
  30. virtual void OnGetName();
  31. // optimized for the 2 step initialization done by wxStackWalker
  32. void Set(const wxString &name, const wxString &filename, const char* syminfo,
  33. size_t level, size_t numLine, void *address)
  34. {
  35. m_level = level;
  36. m_name = name;
  37. m_filename = filename;
  38. m_syminfo = syminfo;
  39. m_line = numLine;
  40. m_address = address;
  41. }
  42. private:
  43. const char *m_syminfo;
  44. };
  45. // ----------------------------------------------------------------------------
  46. // wxStackWalker
  47. // ----------------------------------------------------------------------------
  48. class WXDLLIMPEXP_BASE wxStackWalker : public wxStackWalkerBase
  49. {
  50. public:
  51. // we need the full path to the program executable to be able to use
  52. // addr2line, normally we can retrieve it from wxTheApp but if wxTheApp
  53. // doesn't exist or doesn't have the correct value, the path may be given
  54. // explicitly
  55. wxStackWalker(const char *argv0 = NULL)
  56. {
  57. ms_exepath = wxString::FromAscii(argv0);
  58. }
  59. ~wxStackWalker()
  60. {
  61. FreeStack();
  62. }
  63. virtual void Walk(size_t skip = 1, size_t maxDepth = wxSTACKWALKER_MAX_DEPTH);
  64. #if wxUSE_ON_FATAL_EXCEPTION
  65. virtual void WalkFromException(size_t maxDepth = wxSTACKWALKER_MAX_DEPTH) { Walk(2, maxDepth); }
  66. #endif // wxUSE_ON_FATAL_EXCEPTION
  67. static const wxString& GetExePath() { return ms_exepath; }
  68. // these two may be used to save the stack at some point (fast operation)
  69. // and then process it later (slow operation)
  70. void SaveStack(size_t maxDepth);
  71. void ProcessFrames(size_t skip);
  72. void FreeStack();
  73. private:
  74. int InitFrames(wxStackFrame *arr, size_t n, void **addresses, char **syminfo);
  75. static wxString ms_exepath;
  76. static void *ms_addresses[];
  77. static char **ms_symbols;
  78. static int m_depth;
  79. };
  80. #endif // _WX_UNIX_STACKWALK_H_