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 Debugging API wrappers in ctypes.
30
31 @see: U{http://apps.sourceforge.net/trac/winappdbg/wiki/Win32APIWrappers}
32 """
33
34 __revision__ = "$Id: dbghelp.py 343 2009-08-03 22:58:22Z QvasiModo $"
35
36 from defines import *
37 from kernel32 import *
38
39 UNDNAME_32_BIT_DECODE = 0x0800
40 UNDNAME_COMPLETE = 0x0000
41 UNDNAME_NAME_ONLY = 0x1000
42 UNDNAME_NO_ACCESS_SPECIFIERS = 0x0080
43 UNDNAME_NO_ALLOCATION_LANGUAGE = 0x0010
44 UNDNAME_NO_ALLOCATION_MODEL = 0x0008
45 UNDNAME_NO_ARGUMENTS = 0x2000
46 UNDNAME_NO_CV_THISTYPE = 0x0040
47 UNDNAME_NO_FUNCTION_RETURNS = 0x0004
48 UNDNAME_NO_LEADING_UNDERSCORES = 0x0001
49 UNDNAME_NO_MEMBER_TYPE = 0x0200
50 UNDNAME_NO_MS_KEYWORDS = 0x0002
51 UNDNAME_NO_MS_THISTYPE = 0x0020
52 UNDNAME_NO_RETURN_UDT_MODEL = 0x0400
53 UNDNAME_NO_SPECIAL_SYMS = 0x4000
54 UNDNAME_NO_THISTYPE = 0x0060
55 UNDNAME_NO_THROW_SIGNATURES = 0x0100
56
57
58
59 SYMOPT_ALLOW_ABSOLUTE_SYMBOLS = 0x00000800
60 SYMOPT_ALLOW_ZERO_ADDRESS = 0x01000000
61 SYMOPT_AUTO_PUBLICS = 0x00010000
62 SYMOPT_CASE_INSENSITIVE = 0x00000001
63 SYMOPT_DEBUG = 0x80000000
64 SYMOPT_DEFERRED_LOADS = 0x00000004
65 SYMOPT_DISABLE_SYMSRV_AUTODETECT = 0x02000000
66 SYMOPT_EXACT_SYMBOLS = 0x00000400
67 SYMOPT_FAIL_CRITICAL_ERRORS = 0x00000200
68 SYMOPT_FAVOR_COMPRESSED = 0x00800000
69 SYMOPT_FLAT_DIRECTORY = 0x00400000
70 SYMOPT_IGNORE_CVREC = 0x00000080
71 SYMOPT_IGNORE_IMAGEDIR = 0x00200000
72 SYMOPT_IGNORE_NT_SYMPATH = 0x00001000
73 SYMOPT_INCLUDE_32BIT_MODULES = 0x00002000
74 SYMOPT_LOAD_ANYTHING = 0x00000040
75 SYMOPT_LOAD_LINES = 0x00000010
76 SYMOPT_NO_CPP = 0x00000008
77 SYMOPT_NO_IMAGE_SEARCH = 0x00020000
78 SYMOPT_NO_PROMPTS = 0x00080000
79 SYMOPT_NO_PUBLICS = 0x00008000
80 SYMOPT_NO_UNQUALIFIED_LOADS = 0x00000100
81 SYMOPT_OVERWRITE = 0x00100000
82 SYMOPT_PUBLICS_ONLY = 0x00004000
83 SYMOPT_SECURE = 0x00040000
84 SYMOPT_UNDNAME = 0x00000002
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 SymNone = 0
119 SymCoff = 1
120 SymCv = 2
121 SymPdb = 3
122 SymExport = 4
123 SymDeferred = 5
124 SymSym = 6
125 SymDia = 7
126 SymVirtual = 8
127 NumSymTypes = 9
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
156 _fields_ = [
157 ("SizeOfStruct", DWORD),
158 ("BaseOfImage", DWORD),
159 ("ImageSize", DWORD),
160 ("TimeDateStamp", DWORD),
161 ("CheckSum", DWORD),
162 ("NumSyms", DWORD),
163 ("SymType", DWORD),
164 ("ModuleName", CHAR * 32),
165 ("ImageName", CHAR * 256),
166 ("LoadedImageName", CHAR * 256),
167 ]
168 PIMAGEHLP_MODULE = POINTER(IMAGEHLP_MODULE)
169
171 _fields_ = [
172 ("SizeOfStruct", DWORD),
173 ("BaseOfImage", DWORD64),
174 ("ImageSize", DWORD),
175 ("TimeDateStamp", DWORD),
176 ("CheckSum", DWORD),
177 ("NumSyms", DWORD),
178 ("SymType", DWORD),
179 ("ModuleName", CHAR * 32),
180 ("ImageName", CHAR * 256),
181 ("LoadedImageName", CHAR * 256),
182 ("LoadedPdbName", CHAR * 256),
183 ("CVSig", DWORD),
184 ("CVData", CHAR * (MAX_PATH * 3)),
185 ("PdbSig", DWORD),
186 ("PdbSig70", GUID),
187 ("PdbAge", DWORD),
188 ("PdbUnmatched", BOOL),
189 ("DbgUnmatched", BOOL),
190 ("LineNumbers", BOOL),
191 ("GlobalSymbols", BOOL),
192 ("TypeInfo", BOOL),
193 ("SourceIndexed", BOOL),
194 ("Publics", BOOL),
195 ]
196 PIMAGEHLP_MODULE64 = POINTER(IMAGEHLP_MODULE64)
197
199 _fields_ = [
200 ("SizeOfStruct", DWORD),
201 ("BaseOfImage", DWORD),
202 ("ImageSize", DWORD),
203 ("TimeDateStamp", DWORD),
204 ("CheckSum", DWORD),
205 ("NumSyms", DWORD),
206 ("SymType", DWORD),
207 ("ModuleName", WCHAR * 32),
208 ("ImageName", WCHAR * 256),
209 ("LoadedImageName", WCHAR * 256),
210 ]
211 PIMAGEHLP_MODULEW = POINTER(IMAGEHLP_MODULEW)
212
214 _fields_ = [
215 ("SizeOfStruct", DWORD),
216 ("BaseOfImage", DWORD64),
217 ("ImageSize", DWORD),
218 ("TimeDateStamp", DWORD),
219 ("CheckSum", DWORD),
220 ("NumSyms", DWORD),
221 ("SymType", DWORD),
222 ("ModuleName", WCHAR * 32),
223 ("ImageName", WCHAR * 256),
224 ("LoadedImageName", WCHAR * 256),
225 ("LoadedPdbName", WCHAR * 256),
226 ("CVSig", DWORD),
227 ("CVData", WCHAR * (MAX_PATH * 3)),
228 ("PdbSig", DWORD),
229 ("PdbSig70", GUID),
230 ("PdbAge", DWORD),
231 ("PdbUnmatched", BOOL),
232 ("DbgUnmatched", BOOL),
233 ("LineNumbers", BOOL),
234 ("GlobalSymbols", BOOL),
235 ("TypeInfo", BOOL),
236 ("SourceIndexed", BOOL),
237 ("Publics", BOOL),
238 ]
239 PIMAGEHLP_MODULEW64 = POINTER(IMAGEHLP_MODULEW64)
240
241
242
243
244
245
246
247
248
249
250 -def SymInitialize(hProcess, UserSearchPath = None, fInvadeProcess = False):
251 _SymInitialize = windll.dbghelp.SymInitialize
252 _SymInitialize.argtypes = [HANDLE, LPSTR, BOOL]
253 _SymInitialize.restype = bool
254 _SymInitialize.errcheck = RaiseIfZero
255 if not UserSearchPath:
256 UserSearchPath = None
257 _SymInitialize(hProcess, UserSearchPath, fInvadeProcess)
258
259
260
261
268
269
270
271
278
279
280
281
283 _SymSetParentWindow = windll.dbghelp.SymSetParentWindow
284 _SymSetParentWindow.argtypes = [HWND]
285 _SymSetParentWindow.restype = bool
286 _SymSetParentWindow.errcheck = RaiseIfZero
287 _SymSetParentWindow(hwnd)
288
289
290
291
293 _SymSetOptions = windll.dbghelp.SymSetOptions
294 _SymSetOptions.argtypes = [DWORD]
295 _SymSetOptions.restype = DWORD
296 _SymSetOptions.errcheck = RaiseIfZero
297 _SymSetOptions(SymOptions)
298
299
301 _SymGetOptions = windll.dbghelp.SymGetOptions
302 _SymGetOptions.argtypes = []
303 _SymGetOptions.restype = DWORD
304 return _SymGetOptions()
305
306
307
308
309
310
311
312
313
314 -def SymLoadModule(hProcess, hFile = None, ImageName = None, ModuleName = None, BaseOfDll = None, SizeOfDll = None):
315 _SymLoadModule = windll.dbghelp.SymLoadModule
316 _SymLoadModule.argtypes = [HANDLE, HANDLE, LPSTR, LPSTR, DWORD, DWORD]
317 _SymLoadModule.restype = DWORD
318
319 if not ImageName:
320 ImageName = None
321 if not ModuleName:
322 ModuleName = None
323 if not BaseOfDll:
324 BaseOfDll = 0
325 if not SizeOfDll:
326 SizeOfDll = 0
327 lpBaseAddress = _SymLoadModule(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll)
328 if lpBaseAddress == NULL:
329 dwErrorCode = GetLastError()
330 if dwErrorCode != ERROR_SUCCESS:
331 raise ctypes.WinError(dwErrorCode)
332 return lpBaseAddress
333
334
335
336
337
338
339
340
341
342 -def SymLoadModule64(hProcess, hFile = None, ImageName = None, ModuleName = None, BaseOfDll = None, SizeOfDll = None):
343 _SymLoadModule64 = windll.dbghelp.SymLoadModule64
344 _SymLoadModule64.argtypes = [HANDLE, HANDLE, LPSTR, LPSTR, DWORD64, DWORD]
345 _SymLoadModule64.restype = DWORD64
346
347 if not ImageName:
348 ImageName = None
349 if not ModuleName:
350 ModuleName = None
351 if not BaseOfDll:
352 BaseOfDll = 0
353 if not SizeOfDll:
354 SizeOfDll = 0
355 lpBaseAddress = _SymLoadModule64(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll)
356 if lpBaseAddress == NULL:
357 dwErrorCode = GetLastError()
358 if dwErrorCode != ERROR_SUCCESS:
359 raise ctypes.WinError(dwErrorCode)
360 return lpBaseAddress
361
362
363
364
365
372
373
374
375
376
383
384
385
386
387
388
399
410
411 SymGetModuleInfo = GuessStringType(SymGetModuleInfoA, SymGetModuleInfoW)
412
413
414
415
416
417
428
430 _SymGetModuleInfo64W = windll.dbghelp.SymGetModuleInfo64W
431 _SymGetModuleInfo64W.argtypes = [HANDLE, DWORD64, PIMAGEHLP_MODULE64W]
432 _SymGetModuleInfo64W.restype = bool
433 _SymGetModuleInfo64W.errcheck = RaiseIfZero
434
435 ModuleInfo = IMAGEHLP_MODULE64W()
436 ModuleInfo.SizeOfStruct = sizeof(ModuleInfo)
437 _SymGetModuleInfo64W(hProcess, dwAddr, ctypes.byref(ModuleInfo))
438 return ModuleInfo
439
440 SymGetModuleInfo64 = GuessStringType(SymGetModuleInfo64A, SymGetModuleInfo64W)
441
442
443
444
445
446
447 PSYM_ENUMMODULES_CALLBACK = WINFUNCTYPE(BOOL, LPSTR, DWORD, PVOID)
448 PSYM_ENUMMODULES_CALLBACKW = WINFUNCTYPE(BOOL, LPWSTR, DWORD, PVOID)
449
450
451
452
453
454
455 PSYM_ENUMMODULES_CALLBACK64 = WINFUNCTYPE(BOOL, LPSTR, DWORD64, PVOID)
456 PSYM_ENUMMODULES_CALLBACKW64 = WINFUNCTYPE(BOOL, LPWSTR, DWORD64, PVOID)
457
458
459
460
461
462
464 _SymEnumerateModules = windll.dbghelp.SymEnumerateModules
465 _SymEnumerateModules.argtypes = [HANDLE, PSYM_ENUMMODULES_CALLBACK, PVOID]
466 _SymEnumerateModules.restype = bool
467 _SymEnumerateModules.errcheck = RaiseIfZero
468
469 EnumModulesCallback = PSYM_ENUMMODULES_CALLBACK(EnumModulesCallback)
470 if UserContext:
471 UserContext = ctypes.pointer(UserContext)
472 else:
473 UserContext = LPVOID(NULL)
474 _SymEnumerateModules(hProcess, EnumModulesCallback, UserContext)
475
477 _SymEnumerateModulesW = windll.dbghelp.SymEnumerateModulesW
478 _SymEnumerateModulesW.argtypes = [HANDLE, PSYM_ENUMMODULES_CALLBACKW, PVOID]
479 _SymEnumerateModulesW.restype = bool
480 _SymEnumerateModulesW.errcheck = RaiseIfZero
481
482 EnumModulesCallback = PSYM_ENUMMODULES_CALLBACKW(EnumModulesCallback)
483 if UserContext:
484 UserContext = ctypes.pointer(UserContext)
485 else:
486 UserContext = LPVOID(NULL)
487 _SymEnumerateModulesW(hProcess, EnumModulesCallback, UserContext)
488
489 SymEnumerateModules = GuessStringType(SymEnumerateModulesA, SymEnumerateModulesW)
490
491
492
493
494
495
497 _SymEnumerateModules64 = windll.dbghelp.SymEnumerateModules64
498 _SymEnumerateModules64.argtypes = [HANDLE, PSYM_ENUMMODULES_CALLBACK64, PVOID]
499 _SymEnumerateModules64.restype = bool
500 _SymEnumerateModules64.errcheck = RaiseIfZero
501
502 EnumModulesCallback = PSYM_ENUMMODULES_CALLBACK64(EnumModulesCallback)
503 if UserContext:
504 UserContext = ctypes.pointer(UserContext)
505 else:
506 UserContext = LPVOID(NULL)
507 _SymEnumerateModules64(hProcess, EnumModulesCallback, UserContext)
508
510 _SymEnumerateModules64W = windll.dbghelp.SymEnumerateModules64W
511 _SymEnumerateModules64W.argtypes = [HANDLE, PSYM_ENUMMODULES_CALLBACK64W, PVOID]
512 _SymEnumerateModules64W.restype = bool
513 _SymEnumerateModules64W.errcheck = RaiseIfZero
514
515 EnumModulesCallback = PSYM_ENUMMODULES_CALLBACK64W(EnumModulesCallback)
516 if UserContext:
517 UserContext = ctypes.pointer(UserContext)
518 else:
519 UserContext = LPVOID(NULL)
520 _SymEnumerateModules64W(hProcess, EnumModulesCallback, UserContext)
521
522 SymEnumerateModules64 = GuessStringType(SymEnumerateModules64A, SymEnumerateModules64W)
523
524
525
526
527
528
529
530 PSYM_ENUMSYMBOLS_CALLBACK = WINFUNCTYPE(BOOL, LPSTR, DWORD, ULONG, PVOID)
531 PSYM_ENUMSYMBOLS_CALLBACKW = WINFUNCTYPE(BOOL, LPWSTR, DWORD, ULONG, PVOID)
532
533
534
535
536
537
538
539 PSYM_ENUMSYMBOLS_CALLBACK64 = WINFUNCTYPE(BOOL, LPSTR, DWORD64, ULONG, PVOID)
540 PSYM_ENUMSYMBOLS_CALLBACKW64 = WINFUNCTYPE(BOOL, LPWSTR, DWORD64, ULONG, PVOID)
541
542
543
544
545
546
547
549 _SymEnumerateSymbols = windll.dbghelp.SymEnumerateSymbols
550 _SymEnumerateSymbols.argtypes = [HANDLE, ULONG, PSYM_ENUMSYMBOLS_CALLBACK, PVOID]
551 _SymEnumerateSymbols.restype = bool
552 _SymEnumerateSymbols.errcheck = RaiseIfZero
553
554 EnumSymbolsCallback = PSYM_ENUMSYMBOLS_CALLBACK(EnumSymbolsCallback)
555 if UserContext:
556 UserContext = ctypes.pointer(UserContext)
557 else:
558 UserContext = LPVOID(NULL)
559 _SymEnumerateSymbols(hProcess, BaseOfDll, EnumSymbolsCallback, UserContext)
560
562 _SymEnumerateSymbolsW = windll.dbghelp.SymEnumerateSymbolsW
563 _SymEnumerateSymbolsW.argtypes = [HANDLE, ULONG, PSYM_ENUMSYMBOLS_CALLBACKW, PVOID]
564 _SymEnumerateSymbolsW.restype = bool
565 _SymEnumerateSymbolsW.errcheck = RaiseIfZero
566
567 EnumSymbolsCallback = PSYM_ENUMSYMBOLS_CALLBACKW(EnumSymbolsCallback)
568 if UserContext:
569 UserContext = ctypes.pointer(UserContext)
570 else:
571 UserContext = LPVOID(NULL)
572 _SymEnumerateSymbolsW(hProcess, BaseOfDll, EnumSymbolsCallback, UserContext)
573
574 SymEnumerateSymbols = GuessStringType(SymEnumerateSymbolsA, SymEnumerateSymbolsW)
575
576
577
578
579
580
581
583 _SymEnumerateSymbols64 = windll.dbghelp.SymEnumerateSymbols64
584 _SymEnumerateSymbols64.argtypes = [HANDLE, ULONG64, PSYM_ENUMSYMBOLS_CALLBACK64, PVOID]
585 _SymEnumerateSymbols64.restype = bool
586 _SymEnumerateSymbols64.errcheck = RaiseIfZero
587
588 EnumSymbolsCallback = PSYM_ENUMSYMBOLS_CALLBACK64(EnumSymbolsCallback)
589 if UserContext:
590 UserContext = ctypes.pointer(UserContext)
591 else:
592 UserContext = LPVOID(NULL)
593 _SymEnumerateSymbols64(hProcess, BaseOfDll, EnumSymbolsCallback, UserContext)
594
596 _SymEnumerateSymbols64W = windll.dbghelp.SymEnumerateSymbols64W
597 _SymEnumerateSymbols64W.argtypes = [HANDLE, ULONG64, PSYM_ENUMSYMBOLS_CALLBACK64W, PVOID]
598 _SymEnumerateSymbols64W.restype = bool
599 _SymEnumerateSymbols64W.errcheck = RaiseIfZero
600
601 EnumSymbolsCallback = PSYM_ENUMSYMBOLS_CALLBACK64W(EnumSymbolsCallback)
602 if UserContext:
603 UserContext = ctypes.pointer(UserContext)
604 else:
605 UserContext = LPVOID(NULL)
606 _SymEnumerateSymbols64W(hProcess, BaseOfDll, EnumSymbolsCallback, UserContext)
607
608 SymEnumerateSymbols64 = GuessStringType(SymEnumerateSymbols64A, SymEnumerateSymbols64W)
609
610
611
612
613
614
615
617 _UnDecorateSymbolNameA = windll.dbghelp.UnDecorateSymbolName
618 _UnDecorateSymbolNameA.argtypes = [LPSTR, LPSTR, DWORD, DWORD]
619 _UnDecorateSymbolNameA.restype = DWORD
620 _UnDecorateSymbolNameA.errcheck = RaiseIfZero
621
622 UndecoratedLength = _UnDecorateSymbolNameA(DecoratedName, None, 0, Flags)
623 UnDecoratedName = ctypes.create_string_buffer('', UndecoratedLength + 1)
624 _UnDecorateSymbolNameA(DecoratedName, UnDecoratedName, UndecoratedLength, Flags)
625 return UnDecoratedName.value
626
628 _UnDecorateSymbolNameW = windll.dbghelp.UnDecorateSymbolNameW
629 _UnDecorateSymbolNameW.argtypes = [LPWSTR, LPWSTR, DWORD, DWORD]
630 _UnDecorateSymbolNameW.restype = DWORD
631 _UnDecorateSymbolNameW.errcheck = RaiseIfZero
632
633 UndecoratedLength = _UnDecorateSymbolNameW(DecoratedName, None, 0, Flags)
634 UnDecoratedName = ctypes.create_unicode_buffer(u'', UndecoratedLength + 1)
635 _UnDecorateSymbolNameW(DecoratedName, UnDecoratedName, UndecoratedLength, Flags)
636 return UnDecoratedName.value
637
638 UnDecorateSymbolName = GuessStringType(UnDecorateSymbolNameA, UnDecorateSymbolNameW)
639
640
641
642
643
644
655
666
667 SymGetSearchPath = GuessStringType(SymGetSearchPathA, SymGetSearchPathW)
668
669
670
671
672
681
690
691 SymSetSearchPath = GuessStringType(SymSetSearchPathA, SymSetSearchPathW)
692