-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolResolver.cs
747 lines (663 loc) · 26.6 KB
/
SymbolResolver.cs
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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace symbolresolver
{
using static NativeDefinitions;
using static TraceLogger;
public class SymbolResolver
{
private nint m_SymHandle; // NOT a process handle!
private string m_DebuggerToolsPath;
private string m_SymbolPath;
private Dictionary<ulong, string> m_SymbolCache;
private List<ulong> m_LoadedModules;
private List<LoadedDriver> m_LoadedDrivers;
public class LoadedDriver
{
public string ImagePath;
public ulong BaseAddress;
public uint Size;
public nint hModule;
}
public
SymbolResolver(string SymbolPath, string DebuggerToolsPath)
{
m_SymbolPath = SymbolPath;
m_SymbolCache = new Dictionary<ulong, string>();
m_DebuggerToolsPath = DebuggerToolsPath;
m_LoadedModules = new List<ulong>();
m_LoadedDrivers = new List<LoadedDriver>();
}
~SymbolResolver()
{
Trace(TraceLoggerType.Resolver,
TraceEventType.Verbose,
"Entered destructor");
foreach (var module in m_LoadedModules)
{
SymUnloadModule64(m_SymHandle, module);
}
foreach (var driver in m_LoadedDrivers)
{
FreeLibrary(driver.hModule);
}
if (m_SymHandle != nint.Zero)
{
Debug.Assert(SymCleanup(m_SymHandle));
}
}
public
void
Initialize()
{
if (m_SymHandle != nint.Zero)
{
return;
}
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"Initializing resolver");
//
// Preload driver modules using LoadLibraryEx to force sym API to recognize it.
//
LoadAllKernelModules();
//
// Use dbghelp.dll and friends from debugger tools
//
if (!SetDllDirectory(m_DebuggerToolsPath))
{
var err = $"Unable to set dll directory: " +
$"{Marshal.GetLastWin32Error():X}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new Exception(err);
}
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"Dll search path set to {m_DebuggerToolsPath}");
uint flags = SYMOPT_UNDNAME |
SYMOPT_DEBUG |
SYMOPT_NO_PROMPTS |
SYMOPT_CASE_INSENSITIVE |
SYMOPT_DEFERRED_LOADS |
SSYMOPT_INCLUDE_32BIT_MODULES;
if (SymSetOptions(flags) != flags)
{
var err = $"SymSetOptions failed: " +
$"0x{Marshal.GetLastWin32Error():X}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new Exception(err);
}
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"Sym options set to 0x{flags:X}");
//
// See MSDN for the hProcess argument to SymInitialize. It just needs to be
// a unique number. It seems to have nothing to do with a process at all.
//
var r = new Random();
m_SymHandle = new nint(r.NextInt64());
if (!SymInitialize(m_SymHandle, m_SymbolPath, false))
{
var err = $"SymInitialize failed: " +
$"0x{Marshal.GetLastWin32Error():X}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new Exception(err);
}
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"SymInitialize complete");
//
// Now SymLoadModuleEx on kernel modules.
//
foreach (var driver in m_LoadedDrivers)
{
LoadModule(driver.ImagePath, driver.BaseAddress, driver.Size);
}
//
// Log all sym noisy events to trace session
//
if (!SymRegisterCallback64(
m_SymHandle,
(process, action, data, context) =>
{
var message = "DBGHELP: ";
switch (action)
{
case DbgHelpCallbackActionCode.CBA_SYMBOLS_UNLOADED:
{
message += "Symbols unloaded";
break;
}
case DbgHelpCallbackActionCode.CBA_DEBUG_INFO:
{
message += Marshal.PtrToStringUni(data);
break;
}
case DbgHelpCallbackActionCode.CBA_DEFERRED_SYMBOL_LOAD_COMPLETE:
{
var info = (IMAGEHLP_DEFERRED_SYMBOL_LOAD)Marshal.PtrToStructure(
data, typeof(IMAGEHLP_DEFERRED_SYMBOL_LOAD))!;
message += $"Symbols loaded for {info.FileName} " +
$"(Checksum=0x{info.Checksum:X})" +
$": Base=0x{info.BaseOfImage:X}, Flags=0x{info.Flags:X}";
break;
}
case DbgHelpCallbackActionCode.CBA_DEFERRED_SYMBOL_LOAD_FAILURE:
{
var info = (IMAGEHLP_DEFERRED_SYMBOL_LOAD)Marshal.PtrToStructure(
data, typeof(IMAGEHLP_DEFERRED_SYMBOL_LOAD))!;
message += $"Symbol load failed for {info.FileName} " +
$"(Checksum=0x{info.Checksum:X})" +
$": Base=0x{info.BaseOfImage:X}, Flags=0x{info.Flags:X}";
break;
}
default:
{
return false;
}
}
Trace(TraceLoggerType.Dbghelp,
TraceEventType.Information,
message);
return true;
},
nint.Zero))
{
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
$"SymRegisterCallback64 failed: 0x{Marshal.GetLastWin32Error():X}");
}
}
public
void
InitializeForProcess(int ProcessId)
{
if (m_SymHandle == nint.Zero)
{
throw new Exception("SymbolResolver is not initialized");
}
LoadAllUserModules(ProcessId);
}
public
string?
GetFormattedSymbol(ulong Address)
{
if (m_SymHandle == nint.Zero)
{
throw new Exception("SymbolResolver is not initialized");
}
var moduleInfo = GetModuleInfo(Address);
string moduleName = "<unknown_module>";
if (!string.IsNullOrEmpty(moduleInfo.ModuleName))
{
moduleName = moduleInfo.ModuleName;
}
var formatted = moduleName;
var symbol = SymbolFromAddress(Address);
if (!string.IsNullOrEmpty(symbol))
{
formatted += $"!{symbol}";
}
else
{
formatted += $"!<unknown_0x{Address:X}>";
}
return formatted;
}
public
string?
SymbolFromAddress(
ulong Address
)
{
if (m_SymHandle == nint.Zero)
{
throw new Exception("SymbolResolver is not initialized");
}
if (m_SymbolCache.ContainsKey(Address))
{
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"Symbol 0x{Address:X} = {m_SymbolCache[Address]} (cached)");
return m_SymbolCache[Address];
}
nint buffer = nint.Zero;
string? resolved = null;
try
{
ulong displacement = 0;
var symbol = new SYMBOL_INFO();
symbol.MaxNameLen = MAX_SYM_NAME;
symbol.SizeOfStruct = (uint)Marshal.SizeOf(typeof(SYMBOL_INFO));
buffer = Marshal.AllocHGlobal((int)(symbol.SizeOfStruct + MAX_SYM_NAME));
if (buffer == nint.Zero)
{
throw new Exception("Out of memory");
}
Marshal.StructureToPtr(symbol, buffer, false);
if (!SymFromAddr(m_SymHandle, Address, ref displacement, buffer))
{
var code = Marshal.GetLastWin32Error();
Trace(TraceLoggerType.Resolver,
TraceEventType.Warning,
$"Unable to resolve symbol at address 0x{Address:X}: 0x{code:X}");
//
// This might not be catastrophic, so don't throw an exception
//
return null;
}
symbol = (SYMBOL_INFO)Marshal.PtrToStructure(buffer, typeof(SYMBOL_INFO))!;
var nameLenCharacters = (int)symbol.NameLen; // not incl. null-term
if (nameLenCharacters == 0)
{
throw new Exception("Symbol name length was 0!");
}
//
// Marshal the name buffer, which is just after the last field in SYMBOL_INFO.
// TODO: Add in displacement, if available.
//
var nameLenBytes = nameLenCharacters * 2; // Unicode
byte[] nameData = new byte[nameLenBytes];
var pointer = nint.Add(buffer, (int)Marshal.OffsetOf<SYMBOL_INFO>("Dummy"));
Marshal.Copy(pointer, nameData, 0, nameLenBytes);
resolved = Encoding.Unicode.GetString(nameData, 0, nameLenBytes);
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"Symbol 0x{Address:X} = {resolved}");
}
finally
{
if (buffer != nint.Zero)
{
Marshal.FreeHGlobal(buffer);
}
}
if (!string.IsNullOrEmpty(resolved))
{
m_SymbolCache.Add(Address, resolved);
}
return resolved;
}
public
List<LoadedDriver>
GetLoadedKernelDrivers()
{
return m_LoadedDrivers;
}
private
IMAGEHLP_MODULE64
GetModuleInfo(ulong Address)
{
if (m_SymHandle == nint.Zero)
{
throw new Exception("SymbolResolver is not initialized");
}
var moduleInfo = new IMAGEHLP_MODULE64();
moduleInfo.SizeOfStruct = (uint)Marshal.SizeOf(typeof(IMAGEHLP_MODULE64));
var buffer = Marshal.AllocHGlobal((int)(moduleInfo.SizeOfStruct));
if (buffer == nint.Zero)
{
throw new Exception("Out of memory");
}
try
{
Marshal.StructureToPtr(moduleInfo, buffer, false);
if (!SymGetModuleInfo(m_SymHandle, Address, buffer))
{
//
// Similar to SymFromAddr, this API can fail for a host of reasons.
// We'll trace an error, but no exception.
//
var err = $"SymGetModuleInfo failed: 0x{Marshal.GetLastWin32Error():X}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
return moduleInfo;
}
moduleInfo = (IMAGEHLP_MODULE64)Marshal.PtrToStructure(
buffer, typeof(IMAGEHLP_MODULE64))!;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
return moduleInfo;
}
private
void
LoadAllUserModules(int ProcessId)
{
if (m_SymHandle == nint.Zero)
{
throw new Exception("SymbolResolver is not initialized");
}
//
// We have to open a handle to the process to get its loaded module list.
// SymInitialize would also do the same thing with invasive mode.
//
nint handle;
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"Opening target process {ProcessId}");
try
{
handle = OpenProcess(PROCESS_ALL_ACCESS, false, (uint)ProcessId);
}
catch (Exception ex)
{
var err = $"Unable to open process ID {ProcessId}: {ex.Message}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new Exception(err);
}
if (ProcessId == nint.Zero)
{
var code = Marshal.GetLastWin32Error();
if (code == 0x5)
{
//
// Wrap this up separately so caller can handle it explicitly if desired.
//
var err = $"Unable to open process ID {ProcessId}: access is denied";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new AccessViolationException(err);
}
else if (code == 0x57)
{
//
// The process likely isn't running - this can happen if the source
// of our requestor is from an ETW containing a PID from a terminated
// process, as an example.
//
throw new InvalidOperationException($"Process {ProcessId} doesn't exist");
}
else
{
var err = $"Unable to open process ID {ProcessId}: 0x{code:X}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new Exception(err);
}
}
try
{
//
// Enumerate all modules in this process
//
if (!IsWow64Process(handle, out bool isWow64))
{
var err = $"IsWow64Process failed: " +
$"0x{Marshal.GetLastWin32Error():X}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new Exception(err);
}
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"Enumerating modules for process");
var modules = new nint[1024];
var size = modules.Length * nint.Size;
var modFilter = isWow64 ? EnumProcessModulesFilter.LIST_MODULES_32BIT : EnumProcessModulesFilter.LIST_MODULES_64BIT;
if (!EnumProcessModulesEx(handle, modules, size, out int numModules, modFilter))
{
var err = $"EnumProcessModulesEx failed: " +
$"0x{Marshal.GetLastWin32Error():X}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new Exception(err);
}
if (numModules == 0)
{
//
// Likely a frozen/suspended UWP process.
//
throw new InvalidOperationException($"Process {ProcessId} has no loaded modules");
}
foreach (var module in modules.Take(numModules / nint.Size))
{
var buffer = Marshal.AllocHGlobal(1024);
if (buffer == nint.Zero)
{
throw new Exception("Out of memory");
}
var fileNameSize = GetModuleFileNameEx(handle, module, buffer, 1024);
if (fileNameSize == 0)
{
Marshal.FreeHGlobal(buffer);
var err = $"GetModuleFileNameEx failed: " +
$"0x{Marshal.GetLastWin32Error():X}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new Exception(err);
}
var fileName = Marshal.PtrToStringUni(buffer);
Marshal.FreeHGlobal(buffer);
//
// Get load address and size.
//
size = Marshal.SizeOf(typeof(MODULE_INFO));
buffer = Marshal.AllocHGlobal(size);
if (buffer == nint.Zero)
{
throw new Exception("Out of memory");
}
if (!GetModuleInformation(handle, module, buffer, (uint)size))
{
Marshal.FreeHGlobal(buffer);
var err = $"GetModuleInformation failed for {fileName}: " +
$"0x{Marshal.GetLastWin32Error():X}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new Exception(err);
}
var modInfo = (MODULE_INFO)Marshal.PtrToStructure(
buffer, typeof(MODULE_INFO))!;
var baseAddress = modInfo.BaseOfDll;
var imageSize = modInfo.SizeOfImage;
Marshal.FreeHGlobal(buffer);
if (baseAddress == 0 || imageSize == 0)
{
//
// This happens with modules loaded as data/image files.
//
var err = $"The module {fileName} in the process has invalid " +
$"base address ({baseAddress} or size ({imageSize})";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
continue;
}
LoadModule(fileName, (ulong)baseAddress.ToInt64(), imageSize);
m_LoadedModules.Add((ulong)baseAddress.ToInt64());
}
}
finally
{
CloseHandle(handle);
}
}
private
void
LoadAllKernelModules()
{
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"Loading all kernel modules");
var result = IsWow64Process(Process.GetCurrentProcess().Handle, out bool isWow64);
Debug.Assert(result && !isWow64);
//
// Use Zw API to get full driver information including base and size.
// For simplicity, we'll allocate a large enough buffer for 1024 drivers.
//
var size = Marshal.SizeOf(typeof(SYSTEM_MODULE_INFORMATION)) * 1024;
var buffer = Marshal.AllocHGlobal(size);
if (buffer == nint.Zero)
{
throw new Exception("Out of memory");
}
var status = ZwQuerySystemInformation(
SYSTEM_INFORMATION_CLASS.SystemModuleInformation,
buffer,
(uint)size,
out uint _);
if (status != 0)
{
Marshal.FreeHGlobal(buffer);
throw new Exception($"ZwQuerySystemInformation failed: 0x{status:X}");
}
var numModules = Marshal.ReadInt32(buffer);
if (numModules == 0)
{
Marshal.FreeHGlobal(buffer);
throw new Exception("No driver modules found.");
}
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"There are {numModules} kernel modules");
var pointer = nint.Add(buffer, 8); // aligned on 8-byte boundary
try
{
for (int i = 0; i < numModules; i++)
{
var module = (SYSTEM_MODULE_INFORMATION)Marshal.PtrToStructure(
pointer, typeof(SYSTEM_MODULE_INFORMATION))!;
if (string.IsNullOrEmpty(module.ImageName))
{
Trace(TraceLoggerType.Resolver,
TraceEventType.Warning,
$"Skipping unnamed module at load address 0x{module.ImageBase:X}");
continue;
}
//
// Normalize driver path
//
var driverPath = module.ImageName.ToLower();
if (driverPath.StartsWith(@"\systemroot\"))
{
driverPath = driverPath.Replace(@"\systemroot",
Environment.GetEnvironmentVariable("SystemRoot"));
}
else if (driverPath.StartsWith(@"\??\"))
{
driverPath = driverPath.Remove(0, 4);
}
var driverName = Path.GetFileName(driverPath);
if (driverName.StartsWith("dump_"))
{
//
// These don't really exist on disk.
//
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"Skipping virtual dump driver at load address "+
$"0x{module.ImageBase:X}");
continue;
}
Debug.Assert(File.Exists(driverPath));
//
// Important: We use LoadLibraryEx ourselves instead of calling
// SymLoadModuleEx, which doesn't appear to work.
//
var handle = LoadLibraryEx(driverPath,
nint.Zero,
LOAD_LIBRARY_AS_IMAGE_RESOURCE | LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE);
if (handle == nint.Zero)
{
var err = $"LoadLibraryEx failed: 0x{Marshal.GetLastWin32Error():X}";
Trace(TraceLoggerType.Resolver, TraceEventType.Error, err);
throw new Exception(err);
}
m_LoadedDrivers.Add(new LoadedDriver
{
hModule = handle,
ImagePath = driverPath,
BaseAddress = (ulong)module.ImageBase.ToInt64(),
Size = module.Size
});
pointer = nint.Add(pointer,
Marshal.SizeOf(typeof(SYSTEM_MODULE_INFORMATION)));
}
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
private
void
LoadModule(string Path, ulong Base, uint Size)
{
if (m_SymHandle == nint.Zero)
{
throw new Exception("SymbolResolver is not initialized");
}
Trace(TraceLoggerType.Resolver,
TraceEventType.Information,
$"Loading module {Path} (Base=0x{Base:X}, size={Size})");
var baseAddress = SymLoadModuleEx(m_SymHandle,
nint.Zero,
Path,
null,
Base,
Size,
nint.Zero,
0);
if (baseAddress == 0)
{
var code = Marshal.GetLastWin32Error();
if (code != 0)
{
var err = $"SymLoadModuleEx failed: 0x{code:X}";
Trace(TraceLoggerType.Resolver,
TraceEventType.Error,
err);
throw new Exception(err);
}
//
// Module was already loaded.
//
return;
}
//
// Since we have deferred symbol load option set, force load now by
// referencing a symbol.
//
var moduleInfo = GetModuleInfo(Base + 1);
if (string.IsNullOrEmpty(moduleInfo.ModuleName))
{
throw new Exception($"Unable to resolve address 0x{(Base + 1):X} in module");
}
}
}
}