Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to watch all calls in a .NET application (profiling / instrumentation)

I'm trying to write a .NET application that can profile other .NET processes and list all the calls that have been made by them including the values of the passed parameters.

I understand that writing my own profiler with something like "ICorProfilerCallback2" can help on this but it seems like a quite challenging task. Before undertaking it I want to be sure that's the only way to do this.

I looked into open source .NET profilers and alike such as part-cover, CLR Profiler v4.0 etc. but none of them seems like provide a managed API to do this, and they do much more than what I need (memory profiling etc.).

Is there any other way to do this? An easier way to do this kind of profiling? What are my options in here?

like image 331
dr. evil Avatar asked Nov 14 '22 00:11

dr. evil


1 Answers

We once wrote such a tool using Mono Cecil.

With Cecil you can emit IL code into existing assemblies. This way you can add calls to a managed DLL of yours at the beginning of each method.

However the IL-injection is quite tricky as there are many cases to consider, but it is feasible. On the bright side: Once your managed DLL was called all your code can be written in a high-level way (e.g. C#), so the "only" tricky part is weaving the calls to your API.

A workflow using Cecil instrumentation could look like this:

  1. Get your binaries (either build them or download)
  2. Instrument them with your own post-compiler that uses Cecil
  3. Run the modified assemblies which in turn call your managed DLL
  4. Your DLL stores or analyses the data
like image 56
NobodysNightmare Avatar answered Dec 27 '22 18:12

NobodysNightmare