My scenario: I want to write in log file part of code where exception has happened (for example 5 lines before and 5 lines after line where exception happened - or at least all the code of that method).
My idea is to decompile pdb file in C# code and from that decompiled file find a method that went in exception in catch block.
Pbd file exists and my app is build as debug version. I know that there are tools that allows through its GUI (for example Reflector) to do decompiling but I want to have that functionality from my code.
How to do it?
pdb files contain the following information: The names of all local variables. The names of all source code files and the mapping from IL instructions onto lines within those files.
A program database file (extension . pdb) is a binary file that contains type and symbolic debugging information gathered over the course of compiling and linking the project. A PDB file is created when you compile a C/C++ program with /ZI or /Zi or a Visual Basic, Visual C#, or JScript program with the /debug option.
You can now use Visual Studio to decompile managed code even if you don't have the symbols, allowing you to look at code, inspect variables and set breakpoints.
To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().
The PDB contains the mapping between MSIL and source filename / line number. This is most useful when you can go back and look at the original source files, because decompilation typically doesn't preserve line numbers (although it could if it also used the PDB file). It certainly doesn't recover the original code exactly as written, although with symbol names (also stored in the PDB) it often comes close.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With