Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cStackTrace.h
Go to the documentation of this file.
1 //
4 
5 #ifndef _INC_cStackTrace_H
6 #define _INC_cStackTrace_H
7 #ifndef NO_PRAGMA_ONCE
8 #pragma once
9 #endif
10 
11 #include "../GrayLibBase.h"
12 
13 #if defined(_MSC_VER) && ! USE_UNICODE && ! defined(UNDER_CE)
16 
17 namespace GrayLib
18 {
19  UNITTEST2_PREDEF(cStackTrace);
20 
21  class cStackTraceInternal; // forward
22  class GRAYLIB_LINK cStackTrace
23  {
24  // similar to the __linux__ backtrace()
25  // similar to .NET System.Diagnostics.StackTrace
26 
27  friend class cStackTraceInternal;
28  public:
29  enum StackTraceOptions
30  {
31  // RetrieveNone, // No addition info will be retrieved (only the address is available)
32  RetrieveSymbol=1, // Try to get the symbol-name
33  RetrieveLine=2, // Try to get the line for this symbol
34  RetrieveModuleInfo = 4, // Try to retrieve the module-infos
35  RetrieveFileVersion = 8, // Also retrieve the version for the DLL/EXE
36  RetrieveVerbose = 0xF, // Contains all the above
37  BuildSymPath = 0x10, // Generate a "good" symbol-search-path
38  // SymUseSymSrv = 0x20, // Also use the public Microsoft-Symbol-Server
39  SymAll = 0x30, // Contains all the above "Sym"-options
40  OptionsAll = 0x3F // Contains all options (default)
41  };
42 
43  cStackTrace(
44  UINT options = OptionsAll, // 'int' is by design, to combine the enum-flags
45  const char* pszSymPath = nullptr,
46  PROCESSID_t dwProcessId = cAppState::get_CurrentProcessId(),
47  HANDLE hProcess = ::GetCurrentProcess()
48  );
49  cStackTrace(PROCESSID_t dwProcessId, HANDLE hProcess);
50  virtual ~cStackTrace();
51 
52  typedef BOOL (__stdcall *PReadProcessMemoryRoutine)(
53  HANDLE hProcess,
54  UINT_PTR qwBaseAddress,
55  PVOID lpBuffer,
56  DWORD nSize,
57  LPDWORD lpNumberOfBytesRead,
58  LPVOID pUserData // optional data, which was passed in "ShowCallstack"
59  );
60 
61  BOOL LoadModules();
62 
63  BOOL ShowCallstack(
64  HANDLE hThread = ::GetCurrentThread(),
65  const CONTEXT *context = nullptr, // registers etc.
66  PReadProcessMemoryRoutine readMemoryFunction = nullptr,
67  LPVOID pUserData = nullptr // optional to identify some data in the 'readMemoryFunction'-callback
68  );
69 
70  UNITTEST2_PREDEF(cStackTrace);
71 
72  enum { StackTrace_MAX_NAMELEN = 1024 }; // max name length for found symbols
73 
74  protected:
75  // Entry for each Callstack-Entry
76  struct StackFrameA
77  {
78  // Similar to System.Diagnostics.StackFrame
79  UINT_PTR offset; // if 0, we have no valid entry
80  char name[StackTrace_MAX_NAMELEN];
81  char undName[StackTrace_MAX_NAMELEN];
82  char undFullName[StackTrace_MAX_NAMELEN];
83  UINT_PTR offsetFromSmybol;
84  DWORD offsetFromLine;
85  DWORD lineNumber;
86  char lineFileName[StackTrace_MAX_NAMELEN];
87  DWORD symType;
88  const char* symTypeString;
89  char moduleName[StackTrace_MAX_NAMELEN];
90  UINT_PTR baseOfImage;
91  char loadedImageName[StackTrace_MAX_NAMELEN];
92  };
93 
94  enum CallstackEntryType {firstEntry, nextEntry, lastEntry};
95 
96  virtual void OnSymInit(const char* szSearchPath, DWORD symOptions, const char* szUserName);
97  virtual void OnLoadModule(const FILECHAR_t* img, const char* mod, UINT_PTR baseAddr, DWORD size, const char* symType, const char* pdbName, ULONGLONG fileVersion);
98  virtual void OnCallstackEntry(CallstackEntryType eType, StackFrameA &entry);
99  virtual void OnDbgHelpErr(const char* szFuncName, HRESULT gle, UINT_PTR addr);
100  virtual void OnOutput(const char* szText);
101 
102  cStackTraceInternal* m_sw; // my internal helper hides dynamic bindings to the debug modules.
103  HANDLE m_hProcess; // cOSHandle.
104  PROCESSID_t m_dwProcessId;
105  bool m_bLoadModules;
106  cStringF m_sSymPath;
107 
108  UINT m_options; // StackTraceOptions
109 
110  // PREAD_PROCESS_MEMORY_ROUTINE
111 #ifdef USE_64BIT
112  static BOOL __stdcall myReadProcMem(HANDLE hProcess, UINT_PTR qwBaseAddress, PVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead);
113 #else
114  static BOOL __stdcall myReadProcMem(HANDLE hProcess, DWORD qwBaseAddress, PVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead);
115 #endif
116 
117  };
118 };
119 #endif // defined(_MSC_VER) && !USE_UNICODE
120 #endif // _INC_cStackTrace_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
static PROCESSID_t __stdcall get_CurrentProcessId()
Definition: cAppState.h:175
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
cStringT< FILECHAR_t > cStringF
A file name. checks USE_UNICODE_FN. related to cFilePath.
Definition: cFilePath.h:17
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22