简体   繁体   English

.NET:如何使用exe和pdb文件查找一行代码的源代码

[英].NET : How to find the source code for a line of code using the exe and pdb file

I need to find the source code for a line of code using the exe and pdb file. 我需要使用exe和pdb文件查找一行代码的源代码。 The exe and pdb files are written in c#. exe和pdb文件用c#编写。 An error was generated and the Stacktrace states a line number that does not sync with my source code line numbers since the exe was compiled a while ago. 生成了一个错误,并且Stacktrace声明了一个行号,该行号与该源代码行号不同步,因为该exe是在不久前编译的。 I am trying to figure out the line of code that is causing the issue as it relates to the exe. 我试图找出导致问题的代码行,因为它与exe有关。

Short answer: you can't get the original source code from the EXE and PDB. 简短的答案:您无法从EXE和PDB获取原始源代码。

Neither the PDB nor the executable contain the original source code. PDB和可执行文件都不包含原始源代码。 PDBs contain a mapping between IL and source file line numbers. PDB包含IL和源文件行号之间的映射。 So if your source file has changed (it has in this context), the PDB will be mapping IL to (possibly) incorrect lines in the source file. 因此,如果您的源文件已更改(在此上下文中已更改),则PDB会将IL映射到(可能)源文件中的错误行。

To get the exact source code of the line throwing the exception... you need the original source code. 要获得引发异常的行的确切源代码,您需要原始源代码。 If you are using version control, you need to find the commit from which the .exe was built. 如果使用版本控制,则需要查找生成.exe的提交。 The exe may (but won't necessarily) have useful version information that could help you get back to the exact version of source your exe was compiled from. 该exe可能(但不一定)具有有用的版本信息,可以帮助您返回到exe编译源的确切版本。

Concrete example: at my company, we use continuous integration that creates a unique version / build number every time code is compiled through it. 具体示例:在我公司,我们使用持续集成,每次通过它编译代码时,都会创建一个唯一的版本/内部版本号。 It also lets you see, for a given version number, the code that was used to build this, through a link to the Github commit that triggered the CI build. 它还可以让您通过给定触发了CI构建的Github提交的链接,查看给定版本号所使用的代码。 So in my case, I'd look at the .exe file and get the version / build number (you should be able to easily find how to do this). 因此,就我而言,我将查看.exe文件并获取版本/内部版本号(您应该能够轻松找到执行此操作的方法)。 I'd then go to CI, find that build, and the navigate to Github in my case to get the commit hash I'd need to check out to debug, I'd check out that version of the code, and voila, the PDB line numbers should reliably match up to the code I'm looking at. 然后,我将转到CI,找到该版本,然后导航至Github以获取提交哈希(我需要签出进行调试),然后签出该版本的代码,瞧, PDB行号应该可靠地匹配我正在查看的代码。

If you don't use CI, you could try less reliable methods for figuring out what the exe was built from. 如果不使用CI,则可以尝试使用不太可靠的方法来确定exe的基础。 For example, if you know the date the EXE was compiled on, that may give you a clue to what version of your source you should be looking at, but this wouldn't be necessarily correct. 例如,如果您知道EXE的编译日期,则可能会为您提供您应该查看源代码版本的线索,但这不一定是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VS Code中的“无法找到或打开PDB文件” - “Cannot find or open the PDB file” in VS Code C#预处理程序指令中的源代码和PDB文件中的行号 - Source code in C# preprocessor directives and Line numbers from the PDB file VS代码“无法找到或打开PDB文件”,即使pdb存在并且加载了其他csproj pdb文件 - VS Code “Cannot find or open the PDB file” even though the pdb exists and it loads other csproj pdb files 在 Visual Studio 中使用 Pdb 文件和源代码文件进行调试 - Debugging with Pdb file and Source Code File in Visual Studio 如何找到.NET的外部源代码? - How to find extern source code for .NET? 如何读取崩溃转储文件(我有源代码和.pdb文件) - How to read a crash dump file (I have source code and .pdb files) 如何反编译pdb得到C#源代码? - How to decompile pdb to get C# source code? 找不到或打开PDB文件,调试本机代码 - cannot find or open PDB file, debugging native code 如何在.NET程序集中包含源代码位置信息(文件/行/列)? - How to include source code location information (file/line/column) in .NET assembly? 当我将源代码编译到.net框架中的exe文件时会发生什么 - what happens when i compile source code to exe file in .net framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM