简体   繁体   中英

.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. The exe and pdb files are written in 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. I am trying to figure out the line of code that is causing the issue as it relates to the exe.

Short answer: you can't get the original source code from the EXE and PDB.

Neither the PDB nor the executable contain the original source code. PDBs contain a mapping between IL and source file line numbers. 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.

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. 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.

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. 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). 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.

If you don't use CI, you could try less reliable methods for figuring out what the exe was built from. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM