winStackWalker.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include "platform/types.h"
  28. class StackWalker
  29. {
  30. public:
  31. typedef enum StackWalkOptions
  32. {
  33. // RetrieveNone = 0, // No additional info will be retrieved (only the address is available)
  34. // RetrieveSymbol = 1, // Try to get the symbol-name
  35. // RetrieveLine = 2, // Try to get the line for this symbol
  36. // RetrieveModuleInfo = 4, // Try to retrieve the module-infos
  37. RetrieveFileVersion = 8, // Also retrieve the version for the DLL/EXE
  38. RetrieveVerbose = 0xF, // Contains all of the above retrieve options
  39. SymBuildPath = 0x10, // Generate a "good" symbol-search-path
  40. SymUseSymSrv = 0x20, // Also use a public Symbol Server
  41. SymAll = 0x30, // Contains all of the above Symbol options
  42. OutputSymPath = 0x80, //print out the symbol path
  43. OutputOS = 0x100, //print out the OS path
  44. OutputModules = 0x200, //print out the Modules
  45. OptionsDefault = 0x3F, // Less verbose output (default)
  46. OptionsAll = 0x2FF // Contains all options
  47. };
  48. StackWalker(DWORD optionFlags = OptionsDefault, LPCSTR szSymPath = NULL);
  49. virtual ~StackWalker();
  50. typedef BOOL (__stdcall *PReadProcessMemoryRoutine)(HANDLE hProcess,
  51. DWORD64 qwBaseAddress,
  52. PVOID lpBuffer,
  53. DWORD nSize,
  54. LPDWORD lpNumberOfBytesRead,
  55. LPVOID pUserData // optional data, which was passed in "ShowCallstack"
  56. );
  57. // pUserData is optional to identify some data in the 'readMemoryFunction'-callback
  58. bool ShowCallstack(HANDLE hThread, CONTEXT const & Context, PReadProcessMemoryRoutine readMemoryFunction = NULL, LPVOID pUserData = NULL);
  59. void setOutputBuffer(char * buffer);
  60. private:
  61. typedef enum CallstackEntryType {firstEntry, nextEntry, lastEntry};
  62. HANDLE m_hProcess;
  63. DWORD m_dwProcessId;
  64. bool m_modulesLoaded;
  65. LPSTR m_szSymPath;
  66. S32 m_options;
  67. char * m_pOutputBuffer;
  68. static BOOL __stdcall myReadProcMem(HANDLE hProcess, DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead);
  69. bool Init(LPCSTR szSymPath);
  70. virtual void OnSymInit(LPCSTR szSearchPath, DWORD symOptions, LPCSTR szUserName);
  71. virtual void OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion);
  72. virtual void OnCallstackEntry(CallstackEntryType eType, struct CallstackEntry &entry);
  73. virtual void OnDbgHelpErr(LPCSTR szFuncName, DWORD gle, DWORD64 addr);
  74. virtual void OnOutput(LPCSTR szText);
  75. bool LoadModules();
  76. DWORD LoadModule(HANDLE hProcess, LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size);
  77. bool GetModuleListTH32(HANDLE hProcess, DWORD pid);
  78. bool GetModuleListPSAPI(HANDLE hProcess);
  79. bool GetModuleInfo(HANDLE hProcess, DWORD64 baseAddr, IMAGEHLP_MODULE64 *pModuleInfo);
  80. };
  81. void dGetStackTrace(char * traceBuffer, CONTEXT const & ContextRecord);
  82. #endif
  83. #endif