forked from ufrisk/MemProcFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdb.c
1829 lines (1748 loc) · 72.7 KB
/
pdb.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
119
120
121
122
123
124
125
126
127
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// pdb.c : implementation related to parsing of program databases (PDB) files
// used for debug symbols and automatic retrieval from the Microsoft
// Symbol Server.
//
// Microsoft Symbol Server:
// Parsing may take place by using the Microsoft debug libraries
// 'dbghelp.dll' and 'symsrv.dll' on Windows if these .dll files
// exists alongside 'vmm.dll'. This is the default behavior.
//
// PDBCRust wrapper around Rust PDB crate:
// Parsing may also take place using the pdbcrust wrapper library
// which wraps the Rust PDB crate in a convenient c-library. This
// is the fallback method on Windows and the primary method on Linux.
// The file 'pdbcrust.[so|dll]' must exist alongside [vmm.so|dll]
// for this to work.
//
// Fallback to INFODB:
// A fallback of select functionality of cached symbols towards
// the info.db sqlite database may also take place.
//
// (c) Ulf Frisk, 2019-2024
// Author: Ulf Frisk, [email protected]
//
#include "pdb.h"
#include "pe.h"
#include "infodb.h"
#include "charutil.h"
#include "util.h"
#include "vmmwindef.h"
#include "vmmwininit.h"
#include "vmmuserconfig.h"
#include "ob/ob.h"
#include "ob/ob_tag.h"
#include <libpdbcrust.h>
VOID PDB_PrintError(_In_ VMM_HANDLE H, _In_ LPSTR szErrorMessage1, _In_opt_ LPSTR szErrorMessage2)
{
BOOL fInfoDB_Ntos;
DWORD dwTimeDateStamp = 0;
PVMM_PROCESS pObSystemProcess;
InfoDB_IsValidSymbols(H, &fInfoDB_Ntos, NULL);
if(fInfoDB_Ntos) {
VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "Functionality may be limited. Extended debug information disabled.");
VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "Partial offline fallback symbols in use.");
VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "For additional information use startup option: -loglevel symbol:4");
if(szErrorMessage2) { VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "%s", szErrorMessage2); }
VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "%s\n", szErrorMessage1);
return;
}
if(InfoDB_IsInitialized(H)) {
if((pObSystemProcess = VmmProcessGet(H, 4))) {
PE_GetTimeDateStampCheckSum(H, pObSystemProcess, H->vmm.kernel.vaBase, &dwTimeDateStamp, NULL);
Ob_DECREF_NULL(&pObSystemProcess);
}
VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "Functionality may be limited. Extended debug information disabled.");
VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "Offline symbols unavailable - ID: %08X%X", dwTimeDateStamp, (DWORD)H->vmm.kernel.cbSize);
if(szErrorMessage2) { VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "%s", szErrorMessage2); }
VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "%s\n", szErrorMessage1);
} else {
VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "Functionality may be limited. Extended debug information disabled.");
VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "Offline symbols unavailable - file 'info.db' not found.");
if(szErrorMessage2) { VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "%s", szErrorMessage2); }
VmmLog(H, MID_SYMBOL, LOGLEVEL_WARNING, "%s\n", szErrorMessage1);
}
}
/*
* Read memory pointed to at the PDB acquired symbol offset.
* -- H
* -- hPDB
* -- szSymbolName
* -- pProcess
* -- pqw
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolQWORD(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_ PQWORD pqw)
{
return PDB_GetSymbolPBYTE(H, hPDB, szSymbolName, pProcess, (PBYTE)pqw, sizeof(QWORD));
}
/*
* Read memory pointed to at the PDB acquired symbol offset.
* -- H
* -- hPDB
* -- szSymbolName
* -- pProcess
* -- pdw
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolDWORD(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_ PDWORD pdw)
{
return PDB_GetSymbolPBYTE(H, hPDB, szSymbolName, pProcess, (PBYTE)pdw, sizeof(DWORD));
}
/*
* Read memory pointed to at the PDB acquired symbol offset.
* -- H
* -- hPDB
* -- szSymbolName
* -- pProcess
* -- pv = PDWORD on 32-bit and PQWORD on 64-bit _operating_system_ architecture.
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolPTR(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_ PVOID pv)
{
return PDB_GetSymbolPBYTE(H, hPDB, szSymbolName, pProcess, (PBYTE)pv, (H->vmm.f32 ? sizeof(DWORD) : sizeof(QWORD)));
}
/*
* Read memory at the PDB acquired symbol offset and virtual address base.
* -- H
* -- hPDB
* -- vaBase
* -- szSymbolName = wildcard symbol name
* -- pProcess
* -- pb
* -- cb
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolPBYTE2(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ QWORD vaBase, _In_ LPCSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_writes_(cb) PBYTE pb, _In_ DWORD cb)
{
DWORD dwSymbolOffset;
if(!PDB_GetSymbolOffset(H, hPDB, szSymbolName, &dwSymbolOffset)) { return FALSE; }
return VmmRead(H, pProcess, vaBase + dwSymbolOffset, pb, cb);
}
/*
* Read memory pointed to at the PDB acquired symbol offset and virtual address base.
* -- H
* -- hPDB
* -- vaBase
* -- szSymbolName
* -- pProcess
* -- pqw
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolQWORD2(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ QWORD vaBase, _In_ LPCSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_ PQWORD pqw)
{
return PDB_GetSymbolPBYTE2(H, hPDB, vaBase, szSymbolName, pProcess, (PBYTE)pqw, sizeof(QWORD));
}
/*
* Read memory pointed to at the PDB acquired symbol offset and virtual address base.
* -- H
* -- hPDB
* -- vaBase
* -- szSymbolName
* -- pProcess
* -- pdw
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolDWORD2(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ QWORD vaBase, _In_ LPCSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_ PDWORD pdw)
{
return PDB_GetSymbolPBYTE2(H, hPDB, vaBase, szSymbolName, pProcess, (PBYTE)pdw, sizeof(DWORD));
}
/*
* Read memory pointed to at the PDB acquired symbol offset and virtual address base.
* -- H
* -- hPDB
* -- vaBase
* -- szSymbolName
* -- pProcess
* -- pv = PDWORD on 32-bit and PQWORD on 64-bit _operating_system_ architecture.
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolPTR2(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ QWORD vaBase, _In_ LPCSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_ PVOID pv)
{
return PDB_GetSymbolPBYTE2(H, hPDB, vaBase, szSymbolName, pProcess, (PBYTE)pv, (H->vmm.f32 ? sizeof(DWORD) : sizeof(QWORD)));
}
/*
* Retrieve a module name given a magic hPDB. If no match is made NULL is returned.
* -- hPDB = magic PDB handle.
* -- return
*/
_Success_(return != NULL)
LPSTR PDB_ModuleNameFromHandleMagic(_In_ PDB_HANDLE hPDB)
{
switch(hPDB) {
case PDB_HANDLE_KERNEL: return "nt";
case PDB_HANDLE_TCPIP: return "tcpip";
case PDB_HANDLE_NTDLL: return "ntdll";
case PDB_HANDLE_NTDLL_WOW64: return "wntdll";
default: return NULL;
}
}
/*
* Wrapper for Query the InfoDB for the offset of a symbol.
* NB! Query must be done with magic PDB handle to succeed.
* -- H
* -- hPDB = magic PDB handle.
* -- szSymbolName
* -- pdwSymbolOffset
* -- return
*/
_Success_(return)
BOOL PDB_InfoDB_SymbolOffset(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szSymbolName, _Out_ PDWORD pdwSymbolOffset)
{
LPSTR szModule = PDB_ModuleNameFromHandleMagic(hPDB);
if(!szModule) { return FALSE; }
return InfoDB_SymbolOffset(H, szModule, szSymbolName, pdwSymbolOffset);
}
/*
* Wrapper for Query the InfoDB for the size of a type.
* NB! Query must be done with magic PDB handle to succeed.
*
* -- hPDB = magic PDB handle.
* -- szTypeName
* -- pdwTypeSize
* -- fDynamic = dynamic(TRUE) or static(FALSE) type InfoDB lookup.
* -- return
*/
_Success_(return)
BOOL PDB_InfoDB_TypeSize(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szTypeName, _Out_ PDWORD pdwTypeSize, _In_ BOOL fDynamic)
{
LPSTR szModule = PDB_ModuleNameFromHandleMagic(hPDB);
if(!szModule) { return FALSE; }
if(fDynamic) {
return InfoDB_TypeSize_Dynamic(H, szModule, szTypeName, pdwTypeSize);
} else {
return InfoDB_TypeSize_Static(H, szModule, szTypeName, pdwTypeSize);
}
}
/*
* Wrapper for Query the InfoDB for the offset of a child inside a type - often inside a struct.
* NB! Query must be done with magic PDB handle to succeed.
* -- H
* -- hPDB = magic PDB handle.
* -- szTypeName
* -- uszTypeChildName
* -- pdwTypeOffset = offset relative to type base.
* -- fDynamic = dynamic(TRUE) or static(FALSE) type InfoDB lookup.
* -- return
*/
_Success_(return)
BOOL PDB_InfoDB_TypeChildOffset(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szTypeName, _In_ LPCSTR uszTypeChildName, _Out_ PDWORD pdwTypeOffset, _In_ BOOL fDynamic)
{
LPSTR szModule = PDB_ModuleNameFromHandleMagic(hPDB);
if(!szModule) { return FALSE; }
if(fDynamic) {
return InfoDB_TypeChildOffset_Dynamic(H, szModule, szTypeName, uszTypeChildName, pdwTypeOffset);
} else {
return InfoDB_TypeChildOffset_Static(H, szModule, szTypeName, uszTypeChildName, pdwTypeOffset);
}
}
#define VMMWIN_PDB_LOAD_ADDRESS_STEP 0x10000000
#define VMMWIN_PDB_LOAD_ADDRESS_BASE 0x0000511f00000000
QWORD g_PDB_qwLoadAddressNext = VMMWIN_PDB_LOAD_ADDRESS_BASE;
typedef struct tdPDB_ENTRY {
OB ObHdr;
QWORD qwHash;
QWORD vaModuleBase;
LPSTR szModuleName;
LPSTR szName;
BYTE pbGUID[16];
DWORD dwAge;
DWORD cbModuleSize;
// load data below
BOOL fLoadFailed;
LPSTR szPath;
QWORD qwLoadAddress; // MSPDB fake load address or CRUST handle
// CRUST cleanup function (since no access to pdb context exist at cleanup)
void(*pfn_pdbcrust_close_opt)(_In_ size_t hnd);
} PDB_ENTRY, *PPDB_ENTRY;
#ifdef _WIN32
#include <io.h>
#define _NO_CVCONST_H
#include <dbghelp.h>
const LPSTR szMSPDB_PDB_FUNCTIONS[] = {
"SymGetOptions",
"SymSetOptions",
"SymInitialize",
"SymCleanup",
"SymFindFileInPath",
"SymLoadModuleEx",
"SymUnloadModule64",
"SymEnumSymbols",
"SymEnumTypesByName",
"SymGetTypeFromName",
"SymGetTypeInfo",
"SymGetTypeInfoEx",
"SymFromAddr",
};
typedef struct tdMSPDB_PDB_FUNCTIONS {
DWORD(WINAPI *SymGetOptions)(VOID);
DWORD(WINAPI *SymSetOptions)(_In_ DWORD SymOptions);
BOOL(WINAPI *SymInitialize)(_In_ HANDLE hProcess, _In_opt_ PCSTR UserSearchPath, _In_ BOOL fInvadeProcess);
BOOL(WINAPI *SymCleanup)(_In_ HANDLE hProcess);
BOOL(WINAPI *SymFindFileInPath)(_In_ HANDLE hprocess, _In_opt_ PCSTR SearchPath, _In_ PCSTR FileName, _In_opt_ PVOID id, _In_ DWORD two, _In_ DWORD three, _In_ DWORD flags, _Out_writes_(MAX_PATH + 1) PSTR FoundFile, _In_opt_ PFINDFILEINPATHCALLBACK callback, _In_opt_ PVOID context);
DWORD64(WINAPI *SymLoadModuleEx)(_In_ HANDLE hProcess, _In_opt_ HANDLE hFile, _In_opt_ PCSTR ImageName, _In_opt_ PCSTR ModuleName, _In_ DWORD64 BaseOfDll, _In_ DWORD DllSize, _In_opt_ PMODLOAD_DATA Data, _In_opt_ DWORD Flags);
BOOL(WINAPI *SymUnloadModule64)(_In_ HANDLE hProcess, _In_ DWORD64 BaseOfDll);
BOOL(WINAPI *SymEnumSymbols)(_In_ HANDLE hProcess, _In_ ULONG64 BaseOfDll, _In_opt_ PCSTR Mask, _In_ PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback, _In_opt_ PVOID UserContext);
BOOL(WINAPI *SymEnumTypesByName)(_In_ HANDLE hProcess, _In_ ULONG64 BaseOfDll, _In_opt_ PCSTR mask, _In_ PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback, _In_opt_ PVOID UserContext);
BOOL(WINAPI *SymGetTypeFromName)(_In_ HANDLE hProcess, _In_ ULONG64 BaseOfDll, _In_ PCSTR Name, _Inout_ PSYMBOL_INFO);
BOOL(WINAPI *SymGetTypeInfo)(_In_ HANDLE hProcess, _In_ DWORD64 ModBase, _In_ ULONG TypeId, _In_ IMAGEHLP_SYMBOL_TYPE_INFO GetType, _Out_ PVOID pInfo);
BOOL(WINAPI *SymGetTypeInfoEx)(_In_ HANDLE hProcess, _In_ DWORD64 ModBase, _Inout_ PIMAGEHLP_GET_TYPE_INFO_PARAMS Params);
BOOL(WINAPI *SymFromAddr)(_In_ HANDLE hProcess, _In_ DWORD64 Address, _Out_ PDWORD64 Displacement, _Out_ PSYMBOL_INFO Symbol);
} MSPDB_PDB_FUNCTIONS, *PMSPDB_PDB_FUNCTIONS;
#endif /* _WIN32 */
const LPSTR szCRUST_PDB_FUNCTIONS[] = {
"pdbcrust_pdb_download_ensure",
"pdbcrust_open",
"pdbcrust_close",
"pdbcrust_symbol_offset",
"pdbcrust_symbol_name_from_offset",
"pdbcrust_type_size",
"pdbcrust_type_child_offset",
};
typedef struct tdCRUST_PDB_FUNCTIONS {
bool(*pdbcrust_pdb_download_ensure)(_In_ const char *szPdbBasePath, _In_ char *szPdbGuidAge, _In_ const char *szPdbName, _In_ bool fDownload, _In_ size_t cbFullPdbPath, _Out_writes_bytes_(cbFullPdbPath) char *szFullPdbPath);
size_t(*pdbcrust_open)(_In_ const char *szFullPdbPath);
void(*pdbcrust_close)(_In_ size_t hnd);
unsigned int(*pdbcrust_symbol_offset)(_In_ size_t hnd, _In_ const char *szSymbol);
bool(*pdbcrust_symbol_name_from_offset)(_In_ size_t hnd, _In_ unsigned int symbol_offset, _In_ size_t len_symbol_name, _Out_writes_bytes_(len_symbol_name) char *sz_symbol_name, _Out_ unsigned int *displacement);
unsigned int(*pdbcrust_type_size)(_In_ size_t hnd, _In_ const char *szTypeName);
bool(*pdbcrust_type_child_offset)(_In_ size_t hnd, _In_ const char *szTypeName, _In_ const char *szChildName, _In_ unsigned int *offset_type_child);
} CRUST_PDB_FUNCTIONS, *PCRUST_PDB_FUNCTIONS;
typedef struct tdOB_PDB_CONTEXT {
OB ObHdr;
BOOL fDisabled;
CRITICAL_SECTION Lock;
POB_MAP pmPdbByHash;
POB_MAP pmPdbByModule;
struct {
BOOL fValid;
HMODULE hModule;
union {
CRUST_PDB_FUNCTIONS pfn;
PVOID vafn[sizeof(CRUST_PDB_FUNCTIONS) / sizeof(PVOID)];
};
} crust;
#ifdef _WIN32
struct {
BOOL fValid;
HANDLE hSym;
HMODULE hModuleSymSrv;
HMODULE hModuleDbgHelp;
union {
MSPDB_PDB_FUNCTIONS pfn;
PVOID vafn[sizeof(MSPDB_PDB_FUNCTIONS) / sizeof(PVOID)];
};
} mspdb;
#endif /* _WIN32 */
} OB_PDB_CONTEXT, *POB_PDB_CONTEXT;
typedef struct tdOB_PDB_INITIALIZE_KERNEL_PARAMETERS {
OB ObHdr;
PHANDLE phEventThreadStarted;
BOOL fPdbInfo;
PE_CODEVIEW_INFO PdbInfo;
} OB_PDB_INITIALIZE_KERNEL_PARAMETERS, *POB_PDB_INITIALIZE_KERNEL_PARAMETERS;
QWORD PDB_HashPdb(_In_ LPSTR szPdbName, _In_reads_(16) PBYTE pbPdbGUID, _In_ DWORD dwPdbAge)
{
QWORD qwHash = 0;
qwHash = CharUtil_Hash32A(szPdbName, TRUE);
qwHash = dwPdbAge + ((qwHash >> 13) | (qwHash << 51));
qwHash = *(PQWORD)pbPdbGUID + ((qwHash >> 13) | (qwHash << 51));
qwHash = *(PQWORD)(pbPdbGUID + 8) + ((qwHash >> 13) | (qwHash << 51));
return qwHash;
}
DWORD PDB_HashModuleName(_In_ LPCSTR uszModuleName)
{
return CharUtil_HashNameFsU(uszModuleName, 0);
}
VOID PDB_CallbackCleanup_ObPdbEntry(PPDB_ENTRY pOb)
{
if(pOb->pfn_pdbcrust_close_opt && ((pOb->qwLoadAddress & 0xffff) == 0)) {
pOb->pfn_pdbcrust_close_opt((SIZE_T)pOb->qwLoadAddress);
}
LocalFree(pOb->szModuleName);
LocalFree(pOb->szName);
LocalFree(pOb->szPath);
}
/*
* Cleanup callback to clean up PDB context.
*/
VOID PDB_CallbackCleanup_ObPdbContext(POB_PDB_CONTEXT ctx)
{
Ob_DECREF(ctx->pmPdbByHash);
Ob_DECREF(ctx->pmPdbByModule);
if(ctx->crust.hModule) { FreeLibrary(ctx->crust.hModule); }
#ifdef _WIN32
if(ctx->mspdb.hSym) { ctx->mspdb.pfn.SymCleanup(ctx->mspdb.hSym); }
if(ctx->mspdb.hModuleSymSrv) { FreeLibrary(ctx->mspdb.hModuleSymSrv); }
if(ctx->mspdb.hModuleDbgHelp) { FreeLibrary(ctx->mspdb.hModuleDbgHelp); }
#endif /* _WIN32 */
DeleteCriticalSection(&ctx->Lock);
}
/*
* Retrieve the current PDB context
* CALLER DECREF: return
* -- H
* -- return
*/
POB_PDB_CONTEXT PDB_GetContext(_In_ VMM_HANDLE H)
{
return Ob_INCREF(H->vmm.pObPdbContext);
}
/*
* Add a module to the PDB database and return its handle.
* NB! The PDB for the added module won't be loaded until required.
* -- H
* -- vaModuleBase
* -- cbModuleSize = optional size of the module (required if using GetSymbolFromAddress functionality).
* -- szModuleName
* -- szPdbName
* -- pbPdbGUID
* -- dwPdbAge
* -- return = The PDB handle on success (no need to close handle); or zero on fail.
*/
PDB_HANDLE PDB_AddModuleEntry(_In_ VMM_HANDLE H, _In_ QWORD vaModuleBase, _In_opt_ DWORD cbModuleSize, _In_ LPSTR szModuleName, _In_ LPSTR szPdbName, _In_reads_(16) PBYTE pbPdbGUID, _In_ DWORD dwPdbAge)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
PPDB_ENTRY pObPdbEntry;
QWORD qwPdbHash = 0;
if(!ctxOb) { return 0; }
qwPdbHash = PDB_HashPdb(szPdbName, pbPdbGUID, dwPdbAge);
EnterCriticalSection(&ctxOb->Lock);
if(!ObMap_ExistsKey(ctxOb->pmPdbByHash, qwPdbHash)) {
pObPdbEntry = Ob_AllocEx(H, OB_TAG_PDB_ENTRY, LMEM_ZEROINIT, sizeof(PDB_ENTRY), (OB_CLEANUP_CB)PDB_CallbackCleanup_ObPdbEntry, NULL);
if(!pObPdbEntry) { goto fail; }
pObPdbEntry->dwAge = dwPdbAge;
pObPdbEntry->qwHash = qwPdbHash;
memcpy(pObPdbEntry->pbGUID, pbPdbGUID, 16);
pObPdbEntry->szName = Util_StrDupA(szPdbName);
pObPdbEntry->szModuleName = Util_StrDupA(szModuleName);
pObPdbEntry->vaModuleBase = vaModuleBase;
pObPdbEntry->cbModuleSize = cbModuleSize;
ObMap_Push(ctxOb->pmPdbByHash, qwPdbHash, pObPdbEntry);
ObMap_Push(ctxOb->pmPdbByModule, PDB_HashModuleName(szModuleName), pObPdbEntry);
Ob_DECREF(pObPdbEntry);
}
fail:
LeaveCriticalSection(&ctxOb->Lock);
Ob_DECREF(ctxOb);
return qwPdbHash;
}
/*
* Retrieve a PDB handle given a process and module base address. If the handle
* is not found in the database an attempt to automatically add it is performed.
* NB! Only one PDB with the same base address may exist regardless of process.
* NB! The PDB for the added module won't be loaded until required.
* -- pProcess
* -- vaModuleBase
* -- return = The PDB handle on success (no need to close handle); or zero on fail.
*/
PDB_HANDLE PDB_GetHandleFromModuleAddress(_In_ VMM_HANDLE H, _In_ PVMM_PROCESS pProcess, _In_ QWORD vaModuleBase)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
PPDB_ENTRY pObPdbEntry = 0;
PE_CODEVIEW_INFO CodeViewInfo = { 0 };
DWORD i, iMax;
QWORD qwPdbHash, cbModuleSize;
CHAR szModuleName[MAX_PATH], *szPdbText;
if(!ctxOb) { return 0; }
// find: module base address already in .pdb database.
for(i = 0, iMax = ObMap_Size(ctxOb->pmPdbByHash); i < iMax; i++) {
if((pObPdbEntry = ObMap_GetByIndex(ctxOb->pmPdbByHash, i))) {
if(vaModuleBase == pObPdbEntry->vaModuleBase) {
qwPdbHash = pObPdbEntry->qwHash;
Ob_DECREF_NULL(&pObPdbEntry);
Ob_DECREF_NULL(&ctxOb);
return qwPdbHash;
}
Ob_DECREF_NULL(&pObPdbEntry);
}
}
Ob_DECREF_NULL(&ctxOb);
// retrieve codeview and add to .pdb database.
if(!(cbModuleSize = PE_GetSize(H, pProcess, vaModuleBase)) || (cbModuleSize > 0x04000000)) { return 0; }
if(!PE_GetCodeViewInfo(H, pProcess, vaModuleBase, NULL, &CodeViewInfo)) { return 0; }
strcpy_s(szModuleName, MAX_PATH, CodeViewInfo.CodeView.PdbFileName);
if((szPdbText = strstr(szModuleName, ".pdb"))) {
szPdbText[0] = 0;
}
return PDB_AddModuleEntry(
H,
vaModuleBase,
(DWORD)cbModuleSize,
szModuleName,
CodeViewInfo.CodeView.PdbFileName,
CodeViewInfo.CodeView.Guid,
CodeViewInfo.CodeView.Age
);
}
/*
* Retrieve a PDB handle from an already added module.
* NB! If multiple modules exists with the same name the 1st module to be added
* is returned.
* -- H
* -- szModuleName
* -- return = The PDB handle on success (no need to close handle); or zero on fail.
*/
PDB_HANDLE PDB_GetHandleFromModuleName(_In_ VMM_HANDLE H, _In_ LPCSTR szModuleName)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
PPDB_ENTRY pObPdbEntry = NULL;
QWORD qwHashPdb = 0;
DWORD dwHashModule;
if(!ctxOb || ctxOb->fDisabled) { goto fail; }
if(!szModuleName || !strcmp("nt", szModuleName)) {
szModuleName = "ntoskrnl";
}
dwHashModule = PDB_HashModuleName(szModuleName);
if(!(pObPdbEntry = ObMap_GetByKey(ctxOb->pmPdbByModule, dwHashModule))) { goto fail; }
qwHashPdb = pObPdbEntry->fLoadFailed ? 0 : pObPdbEntry->qwHash;
fail:
Ob_DECREF(pObPdbEntry);
Ob_DECREF(ctxOb);
return qwHashPdb;
}
/*
* Get a valid PDB handle given an ordinary or magic PDB handle.
* -- H
* -- hPDB = ordinary or magic PDB handle.
* -- return = ordinary PDB handle (or 0 on fail).
*/
PDB_HANDLE PDB_GetHandleFromHandleMagic(_In_ VMM_HANDLE H, _In_ PDB_HANDLE hPDB)
{
LPSTR szModule = PDB_ModuleNameFromHandleMagic(hPDB);
return szModule ? PDB_GetHandleFromModuleName(H, szModule) : hPDB;
}
/*
* Ensure that the PDB_ENTRY have its symbols loaded into memory.
* NB! this function must be called in a single-threaded context!
* -- H
* -- ctx
* -- pPdbEntry
* -- return
*/
_Success_(return)
BOOL PDB_LoadEnsureEx(_In_ VMM_HANDLE H, _In_ POB_PDB_CONTEXT ctx, _In_ PPDB_ENTRY pPdbEntry)
{
BOOL fLoadResult = FALSE;
QWORD qwLoadAddress;
CHAR szPdbGuidAge[66];
CHAR szPdbPath[MAX_PATH + 1];
if(!ctx || pPdbEntry->fLoadFailed) { return FALSE; }
if(pPdbEntry->qwLoadAddress) { return TRUE; }
#ifdef _WIN32
if(ctx->mspdb.fValid) {
fLoadResult = ctx->mspdb.pfn.SymFindFileInPath(ctx->mspdb.hSym, NULL, pPdbEntry->szName, pPdbEntry->pbGUID, pPdbEntry->dwAge, 0, SSRVOPT_GUIDPTR, szPdbPath, NULL, NULL);
}
#endif /* _WIN32 */
if(!fLoadResult) {
_snprintf_s(szPdbGuidAge, _countof(szPdbGuidAge), _TRUNCATE, "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X%i",
*(PDWORD)(pPdbEntry->pbGUID + 0), *(PWORD)(pPdbEntry->pbGUID + 4), *(PWORD)(pPdbEntry->pbGUID + 6),
pPdbEntry->pbGUID[8], pPdbEntry->pbGUID[9], pPdbEntry->pbGUID[10], pPdbEntry->pbGUID[11],
pPdbEntry->pbGUID[12], pPdbEntry->pbGUID[13], pPdbEntry->pbGUID[14], pPdbEntry->pbGUID[15],
pPdbEntry->dwAge
);
fLoadResult = ctx->crust.fValid && ctx->crust.pfn.pdbcrust_pdb_download_ensure(H->pdb.szLocal, szPdbGuidAge, pPdbEntry->szName, TRUE, sizeof(szPdbPath), szPdbPath);
}
if(!fLoadResult) {
if(VmmLogIsActive(H, MID_SYMBOL, LOGLEVEL_4_VERBOSE)) {
if(CharUtil_StrCmpAny(CharUtil_StrEquals, pPdbEntry->szName, FALSE, 8, "ntkrnlmp.pdb", "ntkrnlpa.pdb", "ntkrpamp.pdb", "ntmarta.pdb", "ntoskrnl.pdb", "tcpip.pdb", "ntdll.pdb", "wntdll.pdb")) {
VmmLog(H, MID_SYMBOL, LOGLEVEL_4_VERBOSE, "Unable to download required debug symbols %s - manual download possible.", pPdbEntry->szName);
VmmLog(H, MID_SYMBOL, LOGLEVEL_4_VERBOSE, "Download from:");
VmmLog(H, MID_SYMBOL, LOGLEVEL_4_VERBOSE, " https://msdl.microsoft.com/download/symbols/%s/%s/%s", pPdbEntry->szName, szPdbGuidAge, pPdbEntry->szName);
VmmLog(H, MID_SYMBOL, LOGLEVEL_4_VERBOSE, "Download to:");
VmmLog(H, MID_SYMBOL, LOGLEVEL_4_VERBOSE, " %s\\%s\\%s\\%s", H->pdb.szLocal, pPdbEntry->szName, szPdbGuidAge, pPdbEntry->szName);
}
}
goto fail;
}
pPdbEntry->szPath = Util_StrDupA(szPdbPath);
#ifdef _WIN32
if(ctx->mspdb.fValid) {
qwLoadAddress = InterlockedAdd64(&g_PDB_qwLoadAddressNext, VMMWIN_PDB_LOAD_ADDRESS_STEP);
pPdbEntry->qwLoadAddress = ctx->mspdb.pfn.SymLoadModuleEx(ctx->mspdb.hSym, NULL, szPdbPath, NULL, qwLoadAddress, pPdbEntry->cbModuleSize, NULL, 0);
}
#endif /* _WIN32 */
if(ctx->crust.fValid) {
pPdbEntry->qwLoadAddress = (QWORD)ctx->crust.pfn.pdbcrust_open(szPdbPath);
}
if(!pPdbEntry->szPath || !pPdbEntry->qwLoadAddress) { goto fail; }
return TRUE;
fail:
pPdbEntry->fLoadFailed = TRUE;
return FALSE;
}
/*
* Ensure that the PDB_HANDLE have its symbols loaded into memory.
* -- H
* -- hPDB
* -- return
*/
_Success_(return)
BOOL PDB_LoadEnsure(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
PPDB_ENTRY pObPdbEntry = NULL;
BOOL fResult = FALSE;
if(!ctxOb || ctxOb->fDisabled) { goto fail; }
if(!(hPDB = PDB_GetHandleFromHandleMagic(H, hPDB))) { goto fail; }
if(!(pObPdbEntry = ObMap_GetByKey(ctxOb->pmPdbByHash, hPDB))) { goto fail; }
EnterCriticalSection(&ctxOb->Lock);
fResult = PDB_LoadEnsureEx(H, ctxOb, pObPdbEntry);
LeaveCriticalSection(&ctxOb->Lock);
fail:
Ob_DECREF(pObPdbEntry);
Ob_DECREF(ctxOb);
return fResult;
}
/*
* Return module information given a PDB handle.
* -- H
* -- hPDB
* -- szModuleName = buffer to receive module name upon success.
* -- pvaModuleBase
* -- pcbModuleSize
* -- return
*/
_Success_(return)
BOOL PDB_GetModuleInfo(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _Out_writes_opt_(MAX_PATH) LPSTR szModuleName, _Out_opt_ PQWORD pvaModuleBase, _Out_opt_ PDWORD pcbModuleSize)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
PPDB_ENTRY pObPdbEntry = NULL;
BOOL fResult = FALSE;
if(!ctxOb || ctxOb->fDisabled) { goto fail; }
if(!(hPDB = PDB_GetHandleFromHandleMagic(H, hPDB))) { goto fail; }
if(!(pObPdbEntry = ObMap_GetByKey(ctxOb->pmPdbByHash, hPDB))) { goto fail; }
if(szModuleName) {
strncpy_s(szModuleName, MAX_PATH, pObPdbEntry->szModuleName, MAX_PATH - 1);
}
if(pvaModuleBase) {
*pvaModuleBase = pObPdbEntry->vaModuleBase;
}
if(pcbModuleSize) {
*pcbModuleSize = pObPdbEntry->cbModuleSize;
}
fResult = TRUE;
fail:
Ob_DECREF(pObPdbEntry);
Ob_DECREF(ctxOb);
return fResult;
}
#ifdef _WIN32
/*
* Callback function for PDB_GetSymbolOffset() / SymEnumSymbols()
*/
BOOL PDB_GetSymbolOffset_Callback(_In_ PSYMBOL_INFO pSymInfo, _In_ ULONG SymbolSize, _In_ PDWORD pdwSymbolOffset)
{
if(pSymInfo->Address - pSymInfo->ModBase < 0x10000000) {
*pdwSymbolOffset = (DWORD)(pSymInfo->Address - pSymInfo->ModBase);
}
return FALSE;
}
#endif /* _WIN32 */
/*
* Query the PDB for the offset of a symbol.
* -- H
* -- hPDB
* -- szSymbolName
* -- pdwSymbolOffset
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolOffset(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szSymbolName, _Out_ PDWORD pdwSymbolOffset)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
PPDB_ENTRY pObPdbEntry = NULL;
*pdwSymbolOffset = 0;
if(PDB_InfoDB_SymbolOffset(H, hPDB, szSymbolName, pdwSymbolOffset)) { Ob_DECREF(ctxOb); return TRUE; }
if(!ctxOb || ctxOb->fDisabled) { goto fail; }
if(!(hPDB = PDB_GetHandleFromHandleMagic(H, hPDB))) { goto fail; }
if(!(pObPdbEntry = ObMap_GetByKey(ctxOb->pmPdbByHash, hPDB))) { goto fail; }
EnterCriticalSection(&ctxOb->Lock);
if(PDB_LoadEnsureEx(H, ctxOb, pObPdbEntry)) {
#ifdef _WIN32
SYMBOL_INFO SymbolInfo = { 0 };
if(ctxOb->mspdb.fValid) {
if(!H->vmm.f32) {
// 64-bit: use faster algo
SymbolInfo.SizeOfStruct = sizeof(SYMBOL_INFO);
if(ctxOb->mspdb.pfn.SymGetTypeFromName(ctxOb->mspdb.hSym, pObPdbEntry->qwLoadAddress, szSymbolName, &SymbolInfo)) {
*pdwSymbolOffset = (DWORD)(SymbolInfo.Address - SymbolInfo.ModBase);
}
}
if(0 == *pdwSymbolOffset) {
// 32-bit & 64-bit fallback: use slower algo since it's working ...
ctxOb->mspdb.pfn.SymEnumSymbols(ctxOb->mspdb.hSym, pObPdbEntry->qwLoadAddress, szSymbolName, (PSYM_ENUMERATESYMBOLS_CALLBACK)PDB_GetSymbolOffset_Callback, pdwSymbolOffset);
}
}
#endif /* _WIN32 */
if(ctxOb->crust.fValid) {
*pdwSymbolOffset = (DWORD)ctxOb->crust.pfn.pdbcrust_symbol_offset((SIZE_T)pObPdbEntry->qwLoadAddress, szSymbolName);
}
}
LeaveCriticalSection(&ctxOb->Lock);
fail:
Ob_DECREF(pObPdbEntry);
Ob_DECREF(ctxOb);
return *pdwSymbolOffset ? TRUE : FALSE;
}
/*
* Query the PDB for the offset of a symbol and return its virtual address. If
* szSymbolName contains wildcard '?*' characters and matches multiple symbols
* the virtual address of the 1st symbol is returned.
* -- H
* -- hPDB
* -- szSymbolName = wildcard symbol name
* -- pvaSymbolAddress
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolAddress(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szSymbolName, _Out_ PQWORD pvaSymbolAddress)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
PPDB_ENTRY pObPdbEntry = NULL;
DWORD cbSymbolOffset;
BOOL fResult = FALSE;
if(!PDB_GetSymbolOffset(H, hPDB, szSymbolName, &cbSymbolOffset)) { goto fail; }
if(hPDB == PDB_HANDLE_KERNEL) {
*pvaSymbolAddress = H->vmm.kernel.vaBase + cbSymbolOffset;
} else {
if(!ctxOb || ctxOb->fDisabled) { goto fail; }
if(!(hPDB = PDB_GetHandleFromHandleMagic(H, hPDB))) { goto fail; }
if(!(pObPdbEntry = ObMap_GetByKey(ctxOb->pmPdbByHash, hPDB))) { goto fail; }
*pvaSymbolAddress = pObPdbEntry->vaModuleBase + cbSymbolOffset;
}
fResult = TRUE;
fail:
Ob_DECREF(pObPdbEntry);
Ob_DECREF(ctxOb);
return fResult;
}
/*
* Query the PDB for the closest symbol name given an offset from the module
* base address.
* -- H
* -- hPDB
* -- dwSymbolOffset = the offset from the module base to query.
* -- szSymbolName = buffer to receive the name of the symbol.
* -- pdwSymbolDisplacement = displacement from the beginning of the symbol.
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolFromOffset(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ DWORD dwSymbolOffset, _Out_writes_opt_(MAX_PATH) LPSTR szSymbolName, _Out_opt_ PDWORD pdwSymbolDisplacement)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
QWORD cch, qwDisplacement;
PPDB_ENTRY pObPdbEntry = NULL;
BOOL fResult = FALSE;
if(!ctxOb || ctxOb->fDisabled) { goto fail; }
if(!(hPDB = PDB_GetHandleFromHandleMagic(H, hPDB))) { goto fail; }
if(!(pObPdbEntry = ObMap_GetByKey(ctxOb->pmPdbByHash, hPDB))) { goto fail; }
EnterCriticalSection(&ctxOb->Lock);
if(PDB_LoadEnsureEx(H, ctxOb, pObPdbEntry)) {
#ifdef _WIN32
if(ctxOb->mspdb.fValid) {
SYMBOL_INFO_PACKAGE SymbolInfo = { 0 };
SymbolInfo.si.SizeOfStruct = sizeof(SYMBOL_INFO);
SymbolInfo.si.MaxNameLen = MAX_SYM_NAME;
if(ctxOb->mspdb.pfn.SymFromAddr(ctxOb->mspdb.hSym, pObPdbEntry->qwLoadAddress + dwSymbolOffset, &qwDisplacement, &SymbolInfo.si)) {
if(szSymbolName) {
cch = min(MAX_PATH - 1, SymbolInfo.si.NameLen);
memcpy(szSymbolName, SymbolInfo.si.Name, (SIZE_T)cch);
szSymbolName[cch] = 0;
}
if(pdwSymbolDisplacement) {
*pdwSymbolDisplacement = (DWORD)qwDisplacement;
}
fResult = TRUE;
}
}
#endif /* _WIN32 */
if(!fResult && ctxOb->crust.fValid) {
fResult = ctxOb->crust.pfn.pdbcrust_symbol_name_from_offset((SIZE_T)pObPdbEntry->qwLoadAddress, dwSymbolOffset, MAX_PATH, szSymbolName, pdwSymbolDisplacement);
}
}
LeaveCriticalSection(&ctxOb->Lock);
fail:
Ob_DECREF(pObPdbEntry);
Ob_DECREF(ctxOb);
return fResult;
}
/*
* Read memory at the PDB acquired symbol offset. If szSymbolName contains
* wildcard '?*' characters and matches multiple symbols the offset of the
* 1st symbol is used to read the memory.
* -- H
* -- hPDB
* -- szSymbolName = wildcard symbol name
* -- pProcess
* -- pb
* -- cb
* -- return
*/
_Success_(return)
BOOL PDB_GetSymbolPBYTE(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szSymbolName, _In_ PVMM_PROCESS pProcess, _Out_writes_(cb) PBYTE pb, _In_ DWORD cb)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
PPDB_ENTRY pObPdbEntry = NULL;
DWORD dwSymbolOffset;
QWORD va;
BOOL fResult = FALSE;
if(!PDB_GetSymbolOffset(H, hPDB, szSymbolName, &dwSymbolOffset)) { goto fail; }
if(hPDB == PDB_HANDLE_KERNEL) {
va = H->vmm.kernel.vaBase + dwSymbolOffset;
} else {
if(!ctxOb || ctxOb->fDisabled) { goto fail; }
if(!(hPDB = PDB_GetHandleFromHandleMagic(H, hPDB))) { goto fail; }
if(!(pObPdbEntry = ObMap_GetByKey(ctxOb->pmPdbByHash, hPDB))) { goto fail; }
va = pObPdbEntry->vaModuleBase + dwSymbolOffset;
}
fResult = VmmRead(H, pProcess, va, pb, cb);
fail:
Ob_DECREF(pObPdbEntry);
Ob_DECREF(ctxOb);
return fResult;
}
/*
* Query the PDB for the size of a type. If szTypeName contains wildcard '?*'
* characters and matches multiple types the size of the 1st type is returned.
* -- H
* -- hPDB
* -- szTypeName = wildcard type name
* -- pdwTypeSize
* -- return
*/
_Success_(return)
BOOL PDB_GetTypeSize_Internal(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szTypeName, _Out_ PDWORD pdwTypeSize)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
PPDB_ENTRY pObPdbEntry = NULL;
BOOL fResult = FALSE;
if(!ctxOb || ctxOb->fDisabled) { goto fail; }
if(!(hPDB = PDB_GetHandleFromHandleMagic(H, hPDB))) { goto fail; }
if(!(pObPdbEntry = ObMap_GetByKey(ctxOb->pmPdbByHash, hPDB))) { goto fail; }
EnterCriticalSection(&ctxOb->Lock);
if(PDB_LoadEnsureEx(H, ctxOb, pObPdbEntry)) {
#ifdef _WIN32
if(ctxOb->mspdb.fValid) {
SYMBOL_INFO SymbolInfo = { 0 };
SymbolInfo.SizeOfStruct = sizeof(SYMBOL_INFO);
fResult = ctxOb->mspdb.pfn.SymGetTypeFromName(ctxOb->mspdb.hSym, pObPdbEntry->qwLoadAddress, szTypeName, &SymbolInfo) && SymbolInfo.Size;
*pdwTypeSize = SymbolInfo.Size;
}
#endif /* _WIN32 */
if(!fResult && ctxOb->crust.fValid) {
*pdwTypeSize = ctxOb->crust.pfn.pdbcrust_type_size((DWORD)pObPdbEntry->qwLoadAddress, szTypeName);
fResult = *pdwTypeSize != 0;
}
}
LeaveCriticalSection(&ctxOb->Lock);
fail:
Ob_DECREF(pObPdbEntry);
Ob_DECREF(ctxOb);
return fResult;
}
/*
* Query the PDB for the size of a type. If szTypeName contains wildcard '?*'
* characters and matches multiple types the size of the 1st type is returned.
* -- H
* -- hPDB
* -- szTypeName = wildcard type name
* -- pdwTypeSize
* -- return
*/
_Success_(return)
BOOL PDB_GetTypeSize(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szTypeName, _Out_ PDWORD pdwTypeSize)
{
// lookup hierarchy (1): InfoDB dynamic (cached pdb), (2): Full PDB, (3): InfoDB static (build# based).
return
PDB_InfoDB_TypeSize(H, hPDB, szTypeName, pdwTypeSize, TRUE) ||
PDB_GetTypeSize_Internal(H, hPDB, szTypeName, pdwTypeSize) ||
PDB_InfoDB_TypeSize(H, hPDB, szTypeName, pdwTypeSize, FALSE);
}
_Success_(return)
BOOL PDB_GetTypeSizeShort(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szTypeName, _Out_ PWORD pwTypeSize)
{
DWORD dwTypeSize;
if(!PDB_GetTypeSize(H, hPDB, szTypeName, &dwTypeSize) || (dwTypeSize > 0xffff)) { return FALSE; }
if(pwTypeSize) { *pwTypeSize = (WORD)dwTypeSize; }
return TRUE;
}
#ifdef _WIN32
/*
* Callback function for PDB_GetTypeChildOffset()
*/
BOOL WINAPI PDB_GetTypeChildOffset_MSPDB_Callback(_In_ PSYMBOL_INFO pSymInfo, _In_ ULONG SymbolSize, _In_ PDWORD pdwTypeId)
{
*pdwTypeId = pSymInfo->Index;
pSymInfo->Index;
return FALSE;
}
_Success_(return)
BOOL PDB_GetTypeChildOffset_MSPDB_Internal(_In_ VMM_HANDLE H, _In_ POB_PDB_CONTEXT ctx, _In_ PPDB_ENTRY pPdbEntry, _In_ LPCSTR szTypeName, _In_ LPCSTR uszTypeChildName, _Out_ PDWORD pdwTypeOffset)
{
BOOL fResult = FALSE;
LPWSTR wszTypeChildSymName;
LPSTR uszTypeChildSymName;
BYTE pbBuffer[MAX_PATH];
DWORD dwTypeId, cTypeChildren, iTypeChild;
TI_FINDCHILDREN_PARAMS *pFindChildren = NULL;
if(!ctx->mspdb.fValid) { goto fail; }
if(!ctx->mspdb.pfn.SymEnumTypesByName(ctx->mspdb.hSym, pPdbEntry->qwLoadAddress, szTypeName, (PSYM_ENUMERATESYMBOLS_CALLBACK)PDB_GetTypeChildOffset_MSPDB_Callback, &dwTypeId) || !dwTypeId) { goto fail; }
if(!ctx->mspdb.pfn.SymGetTypeInfo(ctx->mspdb.hSym, pPdbEntry->qwLoadAddress, dwTypeId, TI_GET_CHILDRENCOUNT, &cTypeChildren) || !cTypeChildren) { goto fail; }
if(!(pFindChildren = LocalAlloc(LMEM_ZEROINIT, sizeof(TI_FINDCHILDREN_PARAMS) + cTypeChildren * sizeof(ULONG)))) { goto fail; }
pFindChildren->Count = cTypeChildren;
if(!ctx->mspdb.pfn.SymGetTypeInfo(ctx->mspdb.hSym, pPdbEntry->qwLoadAddress, dwTypeId, TI_FINDCHILDREN, pFindChildren)) { goto fail; }
for(iTypeChild = 0; iTypeChild < cTypeChildren; iTypeChild++) {
if(!ctx->mspdb.pfn.SymGetTypeInfo(ctx->mspdb.hSym, pPdbEntry->qwLoadAddress, pFindChildren->ChildId[iTypeChild], TI_GET_SYMNAME, &wszTypeChildSymName)) { continue; }
if(CharUtil_WtoU(wszTypeChildSymName, -1, pbBuffer, sizeof(pbBuffer), &uszTypeChildSymName, NULL, 0) && !strcmp(uszTypeChildName, uszTypeChildSymName)) {
if(ctx->mspdb.pfn.SymGetTypeInfo(ctx->mspdb.hSym, pPdbEntry->qwLoadAddress, pFindChildren->ChildId[iTypeChild], TI_GET_OFFSET, pdwTypeOffset)) {
LocalFree(wszTypeChildSymName);
fResult = TRUE;
break;
}
}
LocalFree(wszTypeChildSymName);
}
fail:
LocalFree(pFindChildren);
return fResult;
}
#else
_Success_(return)
BOOL PDB_GetTypeChildOffset_MSPDB_Internal(_In_ VMM_HANDLE H, _In_ POB_PDB_CONTEXT ctx, _In_ PPDB_ENTRY pPdbEntry, _In_ LPCSTR szTypeName, _In_ LPCSTR uszTypeChildName, _Out_ PDWORD pdwTypeOffset)
{
return FALSE;
}
#endif /* _WIN32 */
/*
* Query the PDB for the offset of a child inside a type - often inside a struct.
* -- H
* -- hPDB
* -- szTypeName = wildcard type name.
* -- uszTypeChildName = exact match of child name.
* -- pdwTypeOffset = offset relative to type base.
* -- return
*/
_Success_(return)
BOOL PDB_GetTypeChildOffset_Internal(_In_ VMM_HANDLE H, _In_opt_ PDB_HANDLE hPDB, _In_ LPCSTR szTypeName, _In_ LPCSTR uszTypeChildName, _Out_ PDWORD pdwTypeOffset)
{
POB_PDB_CONTEXT ctxOb = PDB_GetContext(H);
BOOL fResult = FALSE;
PPDB_ENTRY pObPdbEntry = NULL;
if(!ctxOb || ctxOb->fDisabled) { goto fail; }
if(!(hPDB = PDB_GetHandleFromHandleMagic(H, hPDB))) { goto fail; }
if(!(pObPdbEntry = ObMap_GetByKey(ctxOb->pmPdbByHash, hPDB))) { goto fail; }
EnterCriticalSection(&ctxOb->Lock);
if(!PDB_LoadEnsureEx(H, ctxOb, pObPdbEntry)) { goto fail_lock; }
fResult = PDB_GetTypeChildOffset_MSPDB_Internal(H, ctxOb, pObPdbEntry, szTypeName, uszTypeChildName, pdwTypeOffset);
if(!fResult && ctxOb->crust.fValid) {
fResult = ctxOb->crust.pfn.pdbcrust_type_child_offset((DWORD)pObPdbEntry->qwLoadAddress, szTypeName, uszTypeChildName, pdwTypeOffset);
}
fail_lock:
LeaveCriticalSection(&ctxOb->Lock);
fail:
Ob_DECREF(pObPdbEntry);
Ob_DECREF(ctxOb);