Skip to content

Commit 42c3247

Browse files
Restore calls to SymInitialize and SymCleanup (#105)
1 parent 71a01eb commit 42c3247

File tree

4 files changed

+20
-27
lines changed

4 files changed

+20
-27
lines changed

Engine/SafeNativeMethods.cs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ internal class SafeNativeMethods {
99

1010
private const uint ACTCTX_FLAG_RESOURCE_NAME_VALID = 0x008;
1111
private const UInt16 ISOLATIONAWARE_MANIFEST_RESOURCE_ID = 2;
12-
[DllImport("Kernel32.dll")]
13-
private extern static IntPtr CreateActCtx(ref ACTCTX actctx);
14-
[DllImport("Kernel32.dll")]
15-
private extern static bool ActivateActCtx(IntPtr hActCtx, out IntPtr lpCookie);
16-
[DllImport("Kernel32.dll")]
17-
private extern static bool DeactivateActCtx(uint dwFlags, IntPtr lpCookie);
18-
[DllImport("Kernel32.dll")]
19-
private extern static bool ReleaseActCtx(IntPtr hActCtx);
12+
[DllImport("Kernel32.dll")] private extern static IntPtr CreateActCtx(ref ACTCTX actctx);
13+
[DllImport("Kernel32.dll")] private extern static bool ActivateActCtx(IntPtr hActCtx, out IntPtr lpCookie);
14+
[DllImport("Kernel32.dll")] private extern static bool DeactivateActCtx(uint dwFlags, IntPtr lpCookie);
15+
[DllImport("Kernel32.dll")] private extern static bool ReleaseActCtx(IntPtr hActCtx);
2016

2117
private struct ACTCTX {
2218
public int cbSize;
@@ -30,10 +26,8 @@ private struct ACTCTX {
3026
public IntPtr hModule;
3127
}
3228

33-
[ThreadStatic]
34-
private static IntPtr m_cookie;
35-
[ThreadStatic]
36-
private static IntPtr m_hActCtx;
29+
[ThreadStatic] private static IntPtr m_cookie;
30+
[ThreadStatic] private static IntPtr m_hActCtx;
3731
internal static bool DestroyActivationContext() {
3832
if (m_cookie != IntPtr.Zero) {
3933
if (!DeactivateActCtx(0, m_cookie))
@@ -61,16 +55,12 @@ internal static bool EstablishActivationContext() {
6155
return true;
6256
}
6357

64-
[DllImport("dbghelp.dll", CharSet = CharSet.Unicode)]
65-
public static extern bool SymFindFileInPath(IntPtr hProcess,
66-
[MarshalAs(UnmanagedType.LPWStr)] string SearchPath,
67-
[MarshalAs(UnmanagedType.LPWStr)] string FileName,
68-
IntPtr id,
69-
Int32 two,
70-
Int32 three,
71-
Int32 flags,
72-
[Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder filePath,
73-
IntPtr callback,
74-
IntPtr context);
58+
[DllImport("dbghelp.dll", CharSet = CharSet.Unicode)] public static extern bool SymFindFileInPath(IntPtr hProcess,
59+
[MarshalAs(UnmanagedType.LPWStr)] string SearchPath, [MarshalAs(UnmanagedType.LPWStr)] string FileName, IntPtr id,
60+
Int32 two, Int32 three, Int32 flags, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder filePath, IntPtr callback, IntPtr context);
61+
62+
[DllImport("dbghelp.dll")] public static extern bool SymCleanup(IntPtr hProcess);
63+
64+
[DllImport("dbghelp.dll", CharSet = CharSet.Unicode)] public static extern bool SymInitialize(IntPtr hProcess, [MarshalAs(UnmanagedType.LPWStr)] string UserSearchPath, bool fInvadeProcess);
7565
}
7666
}

Engine/SymSrvHelpers.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private static string GetLocalSymbolFolderForModule(string pdbFilename, string p
1010
int rawsize = Marshal.SizeOf(guid);
1111
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
1212
Marshal.StructureToPtr(guid, buffer, false);
13-
bool success = SafeNativeMethods.SymFindFileInPath(IntPtr.Zero, symPath, pdbFilename, buffer, pdbAge, 0, 8, outPath, IntPtr.Zero, IntPtr.Zero);
13+
bool success = SafeNativeMethods.SymFindFileInPath((IntPtr)(-1), null, pdbFilename, buffer, pdbAge, 0, 8, outPath, IntPtr.Zero, IntPtr.Zero);
1414
if (!success) return String.Empty;
1515
return outPath.ToString();
1616
}
@@ -22,6 +22,7 @@ public static List<string> GetFolderPathsForPDBs(StackResolver parent, string sy
2222
var retval = new List<string>();
2323
Contract.Requires(null != syms);
2424
Contract.Requires(null != parent);
25+
if (!SafeNativeMethods.SymInitialize((IntPtr)(-1), symPath, false)) return retval;
2526
int progress = 0;
2627
foreach (var sym in syms) {
2728
parent.StatusMessage = string.Format(CultureInfo.CurrentCulture, $"Finding local PDB path for {sym.PDBName}");
@@ -35,6 +36,7 @@ public static List<string> GetFolderPathsForPDBs(StackResolver parent, string sy
3536
progress++;
3637
parent.PercentComplete = (int)((double)progress / syms.Count * 100.0);
3738
}
39+
SafeNativeMethods.SymCleanup((IntPtr)(-1));
3840
return retval;
3941
}
4042
}

Tests/Tests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,17 +477,18 @@ private string PrepareLargeXEventInput() {
477477
[TestMethod][TestCategory("Unit")] public async Task E2ESymSrv() {
478478
using var csr = new StackResolver();
479479
using var cts = new CancellationTokenSource();
480-
var pdbPath = @"srv*https://msdl.microsoft.com/download/symbols";
481480
var input = "ntdll+0x9F7E4\r\nKERNELBASE+0x38973\r\nVCRUNTIME140+0xB8F0\r\nntdll+0xA479F\r\nntdll+0x4BEF\r\nntdll+0x89E6\r\nKERNELBASE+0x396C9" +
482481
"\r\n\"ntdll.dll\",\"10.0.17763.1490\",2019328,462107166,2009368,\"ntdll.pdb\",\"{C374E059-5793-9B92-6525-386A66A2D3F5}\",0,1\r\n" +
483482
"\"KERNELBASE.dll\",\"10.0.17763.1518\",2707456,4281343292,2763414,\"kernelbase.pdb\",\"{E77E26E7-D1C4-72BB-2C05-DD17624A9E58}\",0,1\r\n" +
484483
"\"VCRUNTIME140.dll\",\"14.16.27033.0\",86016,1563486943,105788,\"vcruntime140.amd64.pdb\",\"{AF138C3F-2933-4097-8883-C1071B13375E}\",0,1";
485484

486-
var ret = await csr.ResolveCallstacksAsync(await csr.GetListofCallStacksAsync(input, false, cts), pdbPath, false, null, false, true, false, true, false, false, null, cts);
487485
var expected = "ntdll!NtWaitForSingleObject+20\r\nKERNELBASE!WaitForSingleObjectEx+147\r\nVCRUNTIME140!__C_specific_handler+160 (d:\\agent\\_work\\2\\s\\src\\vctools\\crt\\vcruntime\\src\\eh\\riscchandler.cpp:290)\r\nntdll!RtlpExecuteHandlerForException+15\r\nntdll!RtlDispatchException+1039\r\nntdll!RtlRaiseException+790\r\nKERNELBASE!RaiseException+105" +
488486
"\r\n\"ntdll.dll\",\"10.0.17763.1490\",2019328,462107166,2009368,\"ntdll.pdb\",\"{C374E059-5793-9B92-6525-386A66A2D3F5}\",0,1\r\n" +
489487
"\"KERNELBASE.dll\",\"10.0.17763.1518\",2707456,4281343292,2763414,\"kernelbase.pdb\",\"{E77E26E7-D1C4-72BB-2C05-DD17624A9E58}\",0,1\r\n" +
490488
"\"VCRUNTIME140.dll\",\"14.16.27033.0\",86016,1563486943,105788,\"vcruntime140.amd64.pdb\",\"{AF138C3F-2933-4097-8883-C1071B13375E}\",0,1";
489+
var ret = await csr.ResolveCallstacksAsync(await csr.GetListofCallStacksAsync(input, false, cts), @"https://msdl.microsoft.com/download/symbols", false, null, false, true, false, true, false, false, null, cts);
490+
Assert.AreEqual(expected.Trim(), ret.Trim());
491+
ret = await csr.ResolveCallstacksAsync(await csr.GetListofCallStacksAsync(input, false, cts), @"srv*https://msdl.microsoft.com/download/symbols", false, null, false, true, false, true, false, false, null, cts);
491492
Assert.AreEqual(expected.Trim(), ret.Trim());
492493
}
493494

latestrelease.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2022-11-05 00:00
1+
2022-11-09 00:00

0 commit comments

Comments
 (0)