Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decompile pdb to get C# source code?

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?

like image 338
Bero Avatar asked Jul 04 '11 21:07

Bero


People also ask

Do PDB files contain source code?

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.

What is PDB C#?

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.

Can C# be decompiled?

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.

How do I use PDB debugging?

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


1 Answers

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.

like image 108
Ben Voigt Avatar answered Sep 21 '22 11:09

Ben Voigt