Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get source/line number for IL instruction using Mono.Cecil

I'm using Mono.Cecil to write a simple utility that looks for type/method usage within .NET assemblies (ex. calling ToString on enums).

I am able to get find the method, but it would be cool to display the source/line information to the user. Is this possible with Mono.Cecil?

like image 671
Filip Frącz Avatar asked Sep 21 '11 15:09

Filip Frącz


People also ask

What is Cecil mono?

Mono.Cecil. Cecil is a library written by Jb Evain to generate and inspect programs and libraries in the ECMA CIL format. With Cecil, you can load existing managed assemblies, browse all the contained types, modify them on the fly and save back to the disk the modified assembly. Today it is used by the Mono Debugger,...

Is it possible to read in an assembly with Cecil?

Emphasizing this simple piece of code is because reading in an assembly was achieved with the AssemblyFactory class before Cecil’s 0.9 version which was completely removed yet most of the existing few tutorials still use this class and other deprecated ones.

What is Cecil used for?

With Cecil, you can load existing managed assemblies, browse all the contained types, modify them on the fly and save back to the disk the modified assembly. Today it is used by the Mono Debugger, the bug-finding and compliance checking tool Gendarme and many other tools. There is a Google Group to discuss everything Cecil related: mono-cecil

Is IL code executed atomically or not?

IL code is not executed, it is compiled during the JIT process and the compiled code, let's just say assembly instructions, are the ones that are executed. And one IL instruction can be compiled into many assembly instructions e.g. a function call, meaning the original IL instruction is not executed atomically. Hey wait!) Re: Hey wait!)


1 Answers

It is possible. First you should read the guide from the Mono.Cecil wiki about debugging symbols.

Make sure you have Mono.Cecil.Pdb.dll near Mono.Cecil.dll, set the ReaderParameters to read the symbols as indicated in the guide, and then, instructions who have a sequence point in the pdb file will have their SequencePoint property non null, with line information available. The Document property of the SequencePoint holds the name of the source file.

like image 158
Jb Evain Avatar answered Nov 15 '22 17:11

Jb Evain