Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.6 KiB

  1. #ifndef StackWalk_H
  2. #define StackWalk_H 1
  3. #include <imagehlp.h>
  4. #include <wtypes.h>
  5. class Symbol {
  6. friend class StackWalker;
  7. private:
  8. Symbol(const char* moduleName, const char* symbolName, DWORD_PTR displacement);
  9. void Append(Symbol*);
  10. public:
  11. ~Symbol();
  12. const char* moduleName() const { return _moduleName; }
  13. const char* symbolName() const { return _symbolName; }
  14. DWORD_PTR displacement() const { return _displacement; }
  15. void AppendDisplacement(char * sz)
  16. {
  17. char szDisp[16];
  18. wsprintfA(szDisp, " + 0x%X", _displacement);
  19. lstrcatA(sz, szDisp);
  20. }
  21. Symbol* next() const { return _next; }
  22. private:
  23. char* _moduleName;
  24. char* _symbolName;
  25. DWORD_PTR _displacement;
  26. Symbol* _next;
  27. };
  28. class StackWalker {
  29. public:
  30. StackWalker(HANDLE hProcess);
  31. ~StackWalker();
  32. Symbol* ResolveAddress(DWORD_PTR addr);
  33. Symbol* CreateStackTrace(CONTEXT*);
  34. BOOL GetCallStack(Symbol * symbol, SIZE_T nChars, WCHAR * sz, SIZE_T nMaxLines=999999999);
  35. SIZE_T GetCallStackSize(Symbol* symbol);
  36. private:
  37. static DWORD_PTR __stdcall GetModuleBase(HANDLE hProcess, DWORD_PTR address);
  38. static DWORD_PTR LoadModule(HANDLE hProcess, DWORD_PTR address);
  39. private:
  40. typedef BOOL (__stdcall *SymGetModuleInfoFunc)(HANDLE hProcess,
  41. DWORD_PTR dwAddr,
  42. PIMAGEHLP_MODULE ModuleInfo);
  43. typedef BOOL (__stdcall *SymGetSymFromAddrFunc)(HANDLE hProcess,
  44. DWORD_PTR dwAddr,
  45. PDWORD_PTR pdwDisplacement,
  46. PIMAGEHLP_SYMBOL Symbol);
  47. typedef DWORD (__stdcall *SymLoadModuleFunc)(HANDLE hProcess,
  48. HANDLE hFile,
  49. PSTR ImageName,
  50. PSTR ModuleName,
  51. DWORD_PTR BaseOfDll,
  52. DWORD SizeOfDll);
  53. typedef BOOL (__stdcall *StackWalkFunc)(DWORD MachineType,
  54. HANDLE hProcess,
  55. HANDLE hThread,
  56. LPSTACKFRAME StackFrame,
  57. LPVOID ContextRecord,
  58. PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
  59. PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
  60. PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
  61. PTRANSLATE_ADDRESS_ROUTINE TranslateAddress);
  62. typedef BOOL (__stdcall *UndecorateSymbolNameFunc)(LPSTR DecName,
  63. LPSTR UnDecName,
  64. DWORD UnDecNameLength,
  65. DWORD Flags);
  66. private:
  67. HMODULE _imageHlpDLL;
  68. HANDLE _hProcess;
  69. EXCEPTION_POINTERS m_exceptionpts;
  70. static SymGetModuleInfoFunc _SymGetModuleInfo;
  71. static SymGetSymFromAddrFunc _SymGetSymFromAddr;
  72. static SymLoadModuleFunc _SymLoadModule;
  73. static StackWalkFunc _StackWalk;
  74. static UndecorateSymbolNameFunc _UndecorateSymbolName;
  75. static PFUNCTION_TABLE_ACCESS_ROUTINE _SymFunctionTableAccess;
  76. };
  77. #endif