2

我正在尝试SymSrvStoreFileW从 DBGHELP.DLL 调用(在之前尝试SymSrvGetFileIndexInfoW开始工作失败之后)。重要功能在 MSDN 中定义如下:

BOOL WINAPI SymInitialize(
  _In_      HANDLE hProcess,
  _In_opt_  PCTSTR UserSearchPath, // null is documented as fine
  _In_      BOOL fInvadeProcess // false
);

PCTSTR WINAPI SymSrvStoreFile(
  _In_      HANDLE hProcess,
  _In_opt_  PCTSTR SrvPath, // e.g. "srv*C:\symbols"
  _In_      PCTSTR File, // e.g. "C:\myapp.pdb"
  _In_      DWORD Flags // I am using SYMSTOREOPT_RETURNINDEX (0x04)
);

BOOL WINAPI SymCleanup(
  _In_  HANDLE hProcess
);

hProcess有点奇怪,从我收集的文档中可以看出,只要您保持一致,您传递给它的内容实际上并不重要。我同时使用了当前进程 ID 和“任何旧值”ID。都没有奏效。

我做了以下内容externs

[DllImport("dbghelp.dll", EntryPoint = "SymInitializeW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
public static extern bool SymInitialize(
    IntPtr process,
    [param: MarshalAs(UnmanagedType.LPTStr)]
    string searchPath, 
    bool invadeProcess);

[DllImport("dbghelp.dll", EntryPoint = "SymCleanup", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
public static extern bool SymCleanup(IntPtr process);

[DllImport("dbghelp.dll", EntryPoint = "SymSrvStoreFileW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.LPTStr)]
public static extern string SymSrvStoreFile(
    IntPtr process,
    [param: MarshalAs(UnmanagedType.LPTStr)]
    string srvPath,
    [param: MarshalAs(UnmanagedType.LPTStr)]
    string file,
    SymStoreOpt options);

enum SymStoreOpt : uint
{
    None = 0x00,
    Compress = 0x01,
    Overwrite = 0x02,
    Pointer = 0x08,
    ReturnIndex = 0x04,
    PassIfExists = 0x40,
}

并尝试打电话:

var processId = IntPtr.Zero;
try
{
    // This succeeds.
    processId = new IntPtr(Process.GetCurrentProcess().Id);
    if (!NativeMethods.SymInitialize(processId, null, false))
    {
        processId = IntPtr.Zero;
        throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    // This fails.
    var storageLocation = NativeMethods.SymSrvStoreFile(processId, "srv*C:\\vssym", "C:\\test\\mscorlib.pdb", SymStoreOpt.ReturnIndex);
    if (storageLocation == null)
    {
        // Errors under various circumstances.
        // - The operation completed successfully (but storageLocation is still null)
        // - The specified module could not be found
        throw new Win32Exception(Marshal.GetLastWin32Error());
    }
}
finally
{
    if (processId != IntPtr.Zero)
        NativeMethods.SymCleanup(processId);
}

extern我的声明有错误吗?或者,任何人都知道如何获取 PDB/PE(而不仅仅是 .Net 的)的符号存储位置?我也尝试了其他SymStoreOpt标志,但一无所获。

在某些时候(我不记得我做了什么)文件出现在符号存储位置(即使ReturnIndex已使用),但那时我的进程会崩溃(其中一个调试器甚至没有捕获它) ,再次运行它会导致“操作成功”,没有返回值。

编辑: 我在 C++ 中尝试过这个并且仍然得到相同的行为 - 显然我不想走那条路,因为 P/Invoke 具有架构中立的优势。

4

0 回答 0