/
debug.cpp
executable file
·60 lines (43 loc) · 1.53 KB
/
debug.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "debug.h"
Debug::Debug(int pid) {
this->pid = pid;
this->symbolPath = "srv*c:\\symbols\\*http://msdl.microsoft.com/download/symbols";
}
void Debug::SetSymbolPath(string path) {
this->symbolPath = path;
}
string *Debug::GetSymbolForAddresss(string name, void *base, DWORD size, void* addr) {
SYMBOL_INFO *symInfo;
DWORD64 disp;
this->pHandle = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, this->pid);
if (this->pHandle == 0) {
return NULL;
}
if (!SymInitialize(this->pHandle, this->symbolPath.c_str(), FALSE)) {
return NULL;
}
SYMSRV_INDEX_INFO syminfo;
syminfo.sizeofstruct = sizeof(SYMSRV_INDEX_INFO);
if (!SymSrvGetFileIndexInfo(name.c_str(), &syminfo, 0)) {
return NULL;
}
char foundFile[1024];
memset(foundFile, 0, sizeof(foundFile));
// To use srv, you need dbghelp.dll, symsrv.dll in the same path
SymSetSearchPath(this->pHandle, this->symbolPath.c_str());
if (!SymFindFileInPath(this->pHandle, NULL, syminfo.pdbfile, reinterpret_cast<PVOID>(&syminfo.guid), syminfo.age, 0, SSRVOPT_GUIDPTR, foundFile, NULL, NULL)) {
return NULL;
}
if (!SymLoadModule64(this->pHandle, NULL, foundFile, NULL, (DWORD64)base, 0)) {
return NULL;
}
symInfo = (SYMBOL_INFO*)malloc(1000);
symInfo->SizeOfStruct = sizeof(SYMBOL_INFO);
symInfo->MaxNameLen = 1000;
if (!SymFromAddr(this->pHandle, (DWORD64)addr, &disp, symInfo)) {
return NULL;
}
SymCleanup(this->pHandle);
string* symname = new string(symInfo->Name);
return symname;
}