windows 下,使用 visual studio 调试程序时,如果 pdb 文件与可执行文件不匹配,visual studio 会提示“无法加载符号文件”。程序运行发生崩溃,落地 dump 文件,如果是不匹配的 pdb 文件,visual studio 也无法顺利的进入崩溃现场。
什么是 pdb 文件
PDB 文件是微软的一种调试信息文件,用于调试程序。PDB 文件包含了程序的符号表、源代码文件名、行号等信息。在程序编译时,可以生成 PDB 文件,用于调试程序。
WinDbg 调试
WinDbg 是微软的一款调试工具,可以用于调试 windows 程序。WinDbg 可以加载不匹配的 pdb 文件,但是需要手动加载。.reload /f /i
命令可以强制加载不匹配的 pdb 文件。
但是 WinDbg 使用起来不如 visual studio 方便,所以我们希望 visual studio 也能加载不匹配的 pdb 文件。
visual studio 加载不匹配的 pdb 文件
源码现在一般都是通过 git 管理,完全能找到对应版本的代码,重新编译,生成对应的 pdb 文件。为什么无法加载呢?主要是一些元数据无法匹配。
有个小工具,可以修改元数据,基于 exe 文件信息,生成一个新的 pdb 文件,使得 visual studio 能够加载。
chkmatch 下载地址:https://www.debuginfo.com/tools/chkmatch.html
站点缓存地址:chkmatch.zip
ChkMatch utility can be used to check whether an executable and debug information file match. It can also be used to enforce matching between an executable and debug information file, if they are compatible.
For more information about debug information matching and related issues, see this article.
Supported debug information formats: DBG, PDB 2.0, PDB 7.0.
chkmatch [-c ExeFile DebugInfoFile ] |
[-m ExeFile DebugInfoFile]
-c
Check matching between the executable and the debug information file.
-m
Make the executable and the debug information file match.
ExeFile
The name of the executable file.
DebugInfoFile
The name of the debug information file.
使用 chkmatch
先执行检查操作,分析不匹配的原因,提示签名不匹配。
C:\Users\tianlong.xiang\Downloads\chkmatch>ChkMatch.exe -c "D:\Program Files\Rolan\trade\UAT_YinStrade\YinTrade.Main.exe" E:\YinTech\ykcz_securities_trading_client\Sec_Trade\YinTrade.Main\bin\Release\YinTrade.Main.pdb
ChkMatch - version 1.0
Copyright (C) 2004 Oleg Starodumov
http://www.debuginfo.com/
Executable: D:\Program Files\Rolan\trade\UAT_YinStrade\YinTrade.Main.exe
Debug info file: E:\YinTech\ykcz_securities_trading_client\Sec_Trade\YinTrade.Main\bin\Release\YinTrade.Main.pdb
Executable:
TimeDateStamp: c26d9be3
Debug info: 2 ( CodeView )
TimeStamp: f86b0a4f Characteristics: 0 MajorVer: 0 MinorVer: 0
Size: 122 RVA: 001cdc44 FileOffset: 001cbe44
CodeView format: RSDS
Signature: {428c9b95-39a3-4a8d-a8e5-7be453684757} Age: 1
PdbFile: D:\stock_UAT\ykcz_securities_trading_client\Sec_Trade\YinTrade.Main\obj\Release\YinTrade.Main.pdb
Debug info: 16 ( Unknown )
TimeStamp: 00000000 Characteristics: 0 MajorVer: 0 MinorVer: 0
Size: 0 RVA: 00000000 FileOffset: 00000000
Debug information file:
Format: PDB 7.00
Signature: {06fae08e-c0a2-4f3d-9c7c-dfc684445dd1} Age: 1
Result: Unmatched (reason: Signature mismatch)
然后执行修改操作,使得 pdb 文件与 exe 文件匹配。
C:\Users\tianlong.xiang\Downloads\chkmatch>ChkMatch.exe -m "D:\Program Files\Rolan\trade\UAT_YinStrade\YinTrade.Main.exe" E:\YinTech\ykcz_securities_trading_client\Sec_Trade\YinTrade.Main\bin\Release\YinTrade.Main.pdb
ChkMatch - version 1.0
Copyright (C) 2004 Oleg Starodumov
http://www.debuginfo.com/
Executable: D:\Program Files\Rolan\trade\UAT_YinStrade\YinTrade.Main.exe
Debug info file: E:\YinTech\ykcz_securities_trading_client\Sec_Trade\YinTrade.Main\bin\Release\YinTrade.Main.pdb
Executable:
TimeDateStamp: c26d9be3
Debug info: 2 ( CodeView )
TimeStamp: f86b0a4f Characteristics: 0 MajorVer: 0 MinorVer: 0
Size: 122 RVA: 001cdc44 FileOffset: 001cbe44
CodeView format: RSDS
Signature: {428c9b95-39a3-4a8d-a8e5-7be453684757} Age: 1
PdbFile: D:\stock_UAT\ykcz_securities_trading_client\Sec_Trade\YinTrade.Main\obj\Release\YinTrade.Main.pdb
Debug info: 16 ( Unknown )
TimeStamp: 00000000 Characteristics: 0 MajorVer: 0 MinorVer: 0
Size: 0 RVA: 00000000 FileOffset: 00000000
Debug information file:
Format: PDB 7.00
Signature: {06fae08e-c0a2-4f3d-9c7c-dfc684445dd1} Age: 1
Writing to the debug information file...
Result: Success.