winStackWalker.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef __WIN_PLATFORM_STACKWALKER__
  23. #define __WIN_PLATFORM_STACKWALKER__
  24. #if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE )
  25. #include <windows.h>
  26. #include <dbghelp.h>
  27. class StackWalker
  28. {
  29. public:
  30. typedef enum StackWalkOptions
  31. {
  32. // RetrieveNone = 0, // No additional info will be retrieved (only the address is available)
  33. // RetrieveSymbol = 1, // Try to get the symbol-name
  34. // RetrieveLine = 2, // Try to get the line for this symbol
  35. // RetrieveModuleInfo = 4, // Try to retrieve the module-infos
  36. RetrieveFileVersion = 8, // Also retrieve the version for the DLL/EXE
  37. RetrieveVerbose = 0xF, // Contains all of the above retrieve options
  38. SymBuildPath = 0x10, // Generate a "good" symbol-search-path
  39. SymUseSymSrv = 0x20, // Also use a public Symbol Server
  40. SymAll = 0x30, // Contains all of the above Symbol options
  41. OutputSymPath = 0x80, //print out the symbol path
  42. OutputOS = 0x100, //print out the OS path
  43. OutputModules = 0x200, //print out the Modules
  44. OptionsDefault = 0x3F, // Less verbose output (default)
  45. OptionsAll = 0x2FF // Contains all options
  46. };
  47. StackWalker(DWORD optionFlags = OptionsDefault, LPCSTR szSymPath = NULL);
  48. virtual ~StackWalker();
  49. typedef BOOL (__stdcall *PReadProcessMemoryRoutine)(HANDLE hProcess,
  50. DWORD64 qwBaseAddress,
  51. PVOID lpBuffer,
  52. DWORD nSize,
  53. LPDWORD lpNumberOfBytesRead,
  54. LPVOID pUserData // optional data, which was passed in "ShowCallstack"
  55. );
  56. // pUserData is optional to identify some data in the 'readMemoryFunction'-callback
  57. bool ShowCallstack(HANDLE hThread, CONTEXT const & Context, PReadProcessMemoryRoutine readMemoryFunction = NULL, LPVOID pUserData = NULL);
  58. void setOutputBuffer(char * buffer);
  59. private:
  60. typedef enum CallstackEntryType {firstEntry, nextEntry, lastEntry};
  61. HANDLE m_hProcess;
  62. DWORD m_dwProcessId;
  63. bool m_modulesLoaded;
  64. LPSTR m_szSymPath;
  65. S32 m_options;
  66. char * m_pOutputBuffer;
  67. static BOOL __stdcall myReadProcMem(HANDLE hProcess, DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead);
  68. bool Init(LPCSTR szSymPath);
  69. virtual void OnSymInit(LPCSTR szSearchPath, DWORD symOptions, LPCSTR szUserName);
  70. virtual void OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion);
  71. virtual void OnCallstackEntry(CallstackEntryType eType, struct CallstackEntry &entry);
  72. virtual void OnDbgHelpErr(LPCSTR szFuncName, DWORD gle, DWORD64 addr);
  73. virtual void OnOutput(LPCSTR szText);
  74. bool LoadModules();
  75. DWORD LoadModule(HANDLE hProcess, LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size);
  76. bool GetModuleListTH32(HANDLE hProcess, DWORD pid);
  77. bool GetModuleListPSAPI(HANDLE hProcess);
  78. bool GetModuleInfo(HANDLE hProcess, DWORD64 baseAddr, IMAGEHLP_MODULE64 *pModuleInfo);
  79. };
  80. void dGetStackTrace(char * traceBuffer, CONTEXT const & ContextRecord);
  81. #endif
  82. #endif