ReactOS  0.4.12-dev-36-g472787f
drwtsn32.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: Dr. Watson crash reporter
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Debug loop
5  * COPYRIGHT: Copyright 2017 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include "precomp.h"
9 #include <psapi.h>
10 
11 #define MS_VC_EXCEPTION_THREAD_NAME 0x406d1388
12 
14 {
15  BaseAddress = addr;
16  Size = 0;
17  Unloaded = false;
18 }
19 
21 {
22  MODULEINFO mi = {0};
23  GetModuleInformation(hProcess, (HMODULE)BaseAddress, &mi, sizeof(mi));
24  assert(BaseAddress == mi.lpBaseOfDll);
25  Size = mi.SizeOfImage;
26 
27  ModuleName.resize(MAX_PATH);
28  DWORD dwLen = GetModuleFileNameExA(hProcess, (HMODULE)BaseAddress, &ModuleName[0], ModuleName.size());
29  ModuleName.resize(dwLen);
30 }
31 
32 
34  : Handle(handle)
35 {
36  memset(&Context, 0, sizeof(Context));
37 }
38 
40 {
43 }
44 
46  :ProcessID(0)
47  ,ThreadID(0)
49  ,Event(NULL)
50  ,FirstBPHit(false)
51 {
52  memset(&ExceptionInfo, 0, sizeof(ExceptionInfo));
53 }
54 
55 
57 {
58  switch(evt.dwDebugEventCode)
59  {
61  {
62  data.ProcessPath.resize(MAX_PATH*2);
64  if (len)
65  {
66  data.ProcessPath.resize(len);
67  std::string::size_type pos = data.ProcessPath.find_last_of(L"\\/");
68  if (pos != std::string::npos)
69  data.ProcessName = data.ProcessPath.substr(pos+1);
70  }
71  else
72  {
73  data.ProcessPath = L"??";
74  }
75  if (data.ProcessName.empty())
76  data.ProcessName = data.ProcessPath;
77 
79  data.ProcessID = evt.dwProcessId;
82  }
83  break;
86  break;
88  {
89  ThreadMap::iterator it = data.Threads.find(evt.dwThreadId);
90  if (it != data.Threads.end())
91  {
92  data.Threads.erase(it);
93  }
94  }
95  break;
98  for (size_t n = 0; n < data.Modules.size(); ++n)
99  {
100  if (data.Modules[n].BaseAddress == evt.u.LoadDll.lpBaseOfDll)
101  {
102  data.Modules[n].Unloaded = false;
103  return true;
104  }
105  }
106  data.Modules.push_back(ModuleData(evt.u.LoadDll.lpBaseOfDll));
107  break;
109  for (size_t n = 0; n < data.Modules.size(); ++n)
110  {
111  if (data.Modules[n].BaseAddress == evt.u.UnloadDll.lpBaseOfDll)
112  data.Modules[n].Unloaded = true;
113  }
114  break;
115  case OUTPUT_DEBUG_STRING_EVENT: // ignore
116  break;
118  if (evt.u.Exception.dwFirstChance)
119  {
121  {
123  if (!data.FirstBPHit)
124  {
125  data.FirstBPHit = true;
126 
127  if (data.Event)
128  {
129  SetEvent(data.Event);
130  CloseHandle(data.Event);
131  data.Event = NULL;
132  }
133  return true;
134  }
135  break;
137  /* Thread name */
138  return true;
139  case DBG_CONTROL_C:
140  case DBG_CONTROL_BREAK:
141  return true;
142  }
143  }
144  data.ExceptionInfo = evt.u.Exception;
145  data.ThreadID = evt.dwThreadId;
146  return false;
148  //assert(FALSE);
149  return false;
150  case RIP_EVENT:
151  //assert(FALSE);
152  return false;
153  default:
154  assert(false);
155  }
156  return true;
157 }
158 
ThreadData(HANDLE handle=NULL)
Definition: drwtsn32.cpp:33
DWORD SizeOfImage
Definition: compat.h:568
#define CloseHandle
Definition: compat.h:398
std::wstring ProcessName
Definition: drwtsn32.h:40
DWORD dwThreadId
Definition: winbase.h:768
DumpData()
Definition: drwtsn32.cpp:45
void Update(HANDLE hProcess)
Definition: drwtsn32.cpp:20
union _DEBUG_EVENT::@3133 u
DWORD dwProcessId
Definition: winbase.h:767
void Update()
Definition: drwtsn32.cpp:39
static const size_t npos
Definition: _string_npos.h:26
#define assert(x)
Definition: debug.h:53
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:679
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
static MONITORINFO mi
Definition: win.c:7331
#define LOAD_DLL_DEBUG_EVENT
Definition: winbase.h:107
UNLOAD_DLL_DEBUG_INFO UnloadDll
Definition: winbase.h:776
DWORD ExceptionCode
Definition: compat.h:196
GLuint n
Definition: s_context.h:57
BOOL WINAPI GetThreadContext(IN HANDLE hThread, OUT LPCONTEXT lpContext)
Definition: thread.c:463
CREATE_THREAD_DEBUG_INFO CreateThread
Definition: winbase.h:771
EXCEPTION_DEBUG_INFO Exception
Definition: winbase.h:770
bool FirstBPHit
Definition: drwtsn32.h:48
ModuleData(void *addr)
Definition: drwtsn32.cpp:13
DWORD dwDebugEventCode
Definition: winbase.h:766
#define GetModuleFileNameExW(w, x, y, z)
Definition: compat.h:571
smooth NULL
Definition: ftsmooth.c:416
#define UNLOAD_DLL_DEBUG_EVENT
Definition: winbase.h:108
#define DBG_CONTROL_BREAK
Definition: ntstatus.h:55
CREATE_PROCESS_DEBUG_INFO CreateProcessInfo
Definition: winbase.h:772
LPVOID lpBaseOfDll
Definition: compat.h:567
DWORD ThreadID
Definition: drwtsn32.h:42
DWORD WINAPI GetModuleFileNameExA(HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize)
Definition: psapi.c:988
#define MS_VC_EXCEPTION_THREAD_NAME
Definition: drwtsn32.cpp:11
_In_ HANDLE Handle
Definition: extypes.h:390
#define EXIT_PROCESS_DEBUG_EVENT
Definition: winbase.h:106
#define CONTEXT_CONTROL
Definition: compat.h:265
bool Unloaded
Definition: drwtsn32.h:16
#define CREATE_THREAD_DEBUG_EVENT
Definition: winbase.h:103
std::wstring ProcessPath
Definition: drwtsn32.h:39
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
#define EXCEPTION_BREAKPOINT
Definition: winbase.h:310
DWORD ProcessID
Definition: drwtsn32.h:41
#define false
Definition: stdbool.h:38
#define DBG_CONTROL_C
Definition: ntstatus.h:52
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
ThreadMap Threads
Definition: drwtsn32.h:45
GLenum const GLvoid * addr
Definition: glext.h:9621
static const WCHAR L[]
Definition: oid.c:1087
EXCEPTION_RECORD ExceptionRecord
Definition: winbase.h:729
GLenum GLsizei len
Definition: glext.h:6722
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
void * BaseAddress
Definition: drwtsn32.h:14
#define EXCEPTION_DEBUG_EVENT
Definition: winbase.h:102
bool UpdateFromEvent(DEBUG_EVENT &evt, DumpData &data)
Definition: drwtsn32.cpp:56
BOOL WINAPI GetModuleInformation(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb)
Definition: psapi.c:1080
#define EXIT_THREAD_DEBUG_EVENT
Definition: winbase.h:105
std::string ModuleName
Definition: drwtsn32.h:13
ModuleList Modules
Definition: drwtsn32.h:44
#define OUTPUT_DEBUG_STRING_EVENT
Definition: winbase.h:109
#define CREATE_PROCESS_DEBUG_EVENT
Definition: winbase.h:104
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:403
#define memset(x, y, z)
Definition: compat.h:39
HANDLE ProcessHandle
Definition: drwtsn32.h:43
#define RIP_EVENT
Definition: winbase.h:110
LOAD_DLL_DEBUG_INFO LoadDll
Definition: winbase.h:775
#define CONTEXT_INTEGER
Definition: compat.h:266
#define CONTEXT_DEBUG_REGISTERS
Definition: compat.h:268
HANDLE Event
Definition: drwtsn32.h:47
EXCEPTION_DEBUG_INFO ExceptionInfo
Definition: drwtsn32.h:46