Skip to content

Commit

Permalink
Restore calls to SymInitialize and SymCleanup (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindshmicrosoft authored Nov 9, 2022
1 parent 71a01eb commit 42c3247
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
36 changes: 13 additions & 23 deletions Engine/SafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ internal class SafeNativeMethods {

private const uint ACTCTX_FLAG_RESOURCE_NAME_VALID = 0x008;
private const UInt16 ISOLATIONAWARE_MANIFEST_RESOURCE_ID = 2;
[DllImport("Kernel32.dll")]
private extern static IntPtr CreateActCtx(ref ACTCTX actctx);
[DllImport("Kernel32.dll")]
private extern static bool ActivateActCtx(IntPtr hActCtx, out IntPtr lpCookie);
[DllImport("Kernel32.dll")]
private extern static bool DeactivateActCtx(uint dwFlags, IntPtr lpCookie);
[DllImport("Kernel32.dll")]
private extern static bool ReleaseActCtx(IntPtr hActCtx);
[DllImport("Kernel32.dll")] private extern static IntPtr CreateActCtx(ref ACTCTX actctx);
[DllImport("Kernel32.dll")] private extern static bool ActivateActCtx(IntPtr hActCtx, out IntPtr lpCookie);
[DllImport("Kernel32.dll")] private extern static bool DeactivateActCtx(uint dwFlags, IntPtr lpCookie);
[DllImport("Kernel32.dll")] private extern static bool ReleaseActCtx(IntPtr hActCtx);

private struct ACTCTX {
public int cbSize;
Expand All @@ -30,10 +26,8 @@ private struct ACTCTX {
public IntPtr hModule;
}

[ThreadStatic]
private static IntPtr m_cookie;
[ThreadStatic]
private static IntPtr m_hActCtx;
[ThreadStatic] private static IntPtr m_cookie;
[ThreadStatic] private static IntPtr m_hActCtx;
internal static bool DestroyActivationContext() {
if (m_cookie != IntPtr.Zero) {
if (!DeactivateActCtx(0, m_cookie))
Expand Down Expand Up @@ -61,16 +55,12 @@ internal static bool EstablishActivationContext() {
return true;
}

[DllImport("dbghelp.dll", CharSet = CharSet.Unicode)]
public static extern bool SymFindFileInPath(IntPtr hProcess,
[MarshalAs(UnmanagedType.LPWStr)] string SearchPath,
[MarshalAs(UnmanagedType.LPWStr)] string FileName,
IntPtr id,
Int32 two,
Int32 three,
Int32 flags,
[Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder filePath,
IntPtr callback,
IntPtr context);
[DllImport("dbghelp.dll", CharSet = CharSet.Unicode)] public static extern bool SymFindFileInPath(IntPtr hProcess,
[MarshalAs(UnmanagedType.LPWStr)] string SearchPath, [MarshalAs(UnmanagedType.LPWStr)] string FileName, IntPtr id,
Int32 two, Int32 three, Int32 flags, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder filePath, IntPtr callback, IntPtr context);

[DllImport("dbghelp.dll")] public static extern bool SymCleanup(IntPtr hProcess);

[DllImport("dbghelp.dll", CharSet = CharSet.Unicode)] public static extern bool SymInitialize(IntPtr hProcess, [MarshalAs(UnmanagedType.LPWStr)] string UserSearchPath, bool fInvadeProcess);
}
}
4 changes: 3 additions & 1 deletion Engine/SymSrvHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private static string GetLocalSymbolFolderForModule(string pdbFilename, string p
int rawsize = Marshal.SizeOf(guid);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(guid, buffer, false);
bool success = SafeNativeMethods.SymFindFileInPath(IntPtr.Zero, symPath, pdbFilename, buffer, pdbAge, 0, 8, outPath, IntPtr.Zero, IntPtr.Zero);
bool success = SafeNativeMethods.SymFindFileInPath((IntPtr)(-1), null, pdbFilename, buffer, pdbAge, 0, 8, outPath, IntPtr.Zero, IntPtr.Zero);
if (!success) return String.Empty;
return outPath.ToString();
}
Expand All @@ -22,6 +22,7 @@ public static List<string> GetFolderPathsForPDBs(StackResolver parent, string sy
var retval = new List<string>();
Contract.Requires(null != syms);
Contract.Requires(null != parent);
if (!SafeNativeMethods.SymInitialize((IntPtr)(-1), symPath, false)) return retval;
int progress = 0;
foreach (var sym in syms) {
parent.StatusMessage = string.Format(CultureInfo.CurrentCulture, $"Finding local PDB path for {sym.PDBName}");
Expand All @@ -35,6 +36,7 @@ public static List<string> GetFolderPathsForPDBs(StackResolver parent, string sy
progress++;
parent.PercentComplete = (int)((double)progress / syms.Count * 100.0);
}
SafeNativeMethods.SymCleanup((IntPtr)(-1));
return retval;
}
}
Expand Down
5 changes: 3 additions & 2 deletions Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,17 +477,18 @@ private string PrepareLargeXEventInput() {
[TestMethod][TestCategory("Unit")] public async Task E2ESymSrv() {
using var csr = new StackResolver();
using var cts = new CancellationTokenSource();
var pdbPath = @"srv*https://msdl.microsoft.com/download/symbols";
var input = "ntdll+0x9F7E4\r\nKERNELBASE+0x38973\r\nVCRUNTIME140+0xB8F0\r\nntdll+0xA479F\r\nntdll+0x4BEF\r\nntdll+0x89E6\r\nKERNELBASE+0x396C9" +
"\r\n\"ntdll.dll\",\"10.0.17763.1490\",2019328,462107166,2009368,\"ntdll.pdb\",\"{C374E059-5793-9B92-6525-386A66A2D3F5}\",0,1\r\n" +
"\"KERNELBASE.dll\",\"10.0.17763.1518\",2707456,4281343292,2763414,\"kernelbase.pdb\",\"{E77E26E7-D1C4-72BB-2C05-DD17624A9E58}\",0,1\r\n" +
"\"VCRUNTIME140.dll\",\"14.16.27033.0\",86016,1563486943,105788,\"vcruntime140.amd64.pdb\",\"{AF138C3F-2933-4097-8883-C1071B13375E}\",0,1";

var ret = await csr.ResolveCallstacksAsync(await csr.GetListofCallStacksAsync(input, false, cts), pdbPath, false, null, false, true, false, true, false, false, null, cts);
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" +
"\r\n\"ntdll.dll\",\"10.0.17763.1490\",2019328,462107166,2009368,\"ntdll.pdb\",\"{C374E059-5793-9B92-6525-386A66A2D3F5}\",0,1\r\n" +
"\"KERNELBASE.dll\",\"10.0.17763.1518\",2707456,4281343292,2763414,\"kernelbase.pdb\",\"{E77E26E7-D1C4-72BB-2C05-DD17624A9E58}\",0,1\r\n" +
"\"VCRUNTIME140.dll\",\"14.16.27033.0\",86016,1563486943,105788,\"vcruntime140.amd64.pdb\",\"{AF138C3F-2933-4097-8883-C1071B13375E}\",0,1";
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);
Assert.AreEqual(expected.Trim(), ret.Trim());
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);
Assert.AreEqual(expected.Trim(), ret.Trim());
}

Expand Down
2 changes: 1 addition & 1 deletion latestrelease.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2022-11-05 00:00
2022-11-09 00:00

0 comments on commit 42c3247

Please sign in to comment.