Do I need to write my own unmanaged IL library to rewrite IL using the CLR profiling API?

I studied some articles for the CLR profiling APIs, and many of these articles talk about calling SetILFunctionBody () to actually rewrite IL; however, none of these articles actually explains what you could use to overwrite the actual byte of the IL method. Is there an unmanaged library that allows me to write IL, or do I need to write it myself?

+7
profiling cil rewriting clrprofiler
source share
5 answers

Maybe. It depends.

The Mono Project has a library called Cecil, which you can get here:

http://mono-project.com/Cecil

However, it controlled code that you cannot invoke when profiling. You may have several options:

  • Use IPC. You can create a new process by rewriting with cecil in the process, and then transfer the bytes back to your profiler using named pipes.
  • CECIL port in C ++. Code distributed under the MIT / X11 license so you can do this without sharing your changes.
  • Just write your own stuff from scratch.

# 1 is challenging. Your profiler will have more moving parts than it really needs. In addition, IPC is a set of additional overhead.

# 2 will take a long time. Given that Cecil is still only in version 0.6, it may not be worth the time to do this, instead of writing your own implementation.

# 3 will give you the maximum degree of control and will probably be the most productive. However, this will require significantly more effort than # 1.

+3
source share

This article may be what you are looking for http://www.codeproject.com/KB/cs/IL_Rewriting.aspx

+1
source share

I wrote a simple one for OpenCover https://github.com/sawilde/opencover , which you or someone else might find useful

+1
source share

Actual bytes must come from somewhere, and if you just use the Profiling API, you have to provide them yourself. This article details how to do this (perhaps one of those that you read): http://msdn.microsoft.com/en-us/magazine/cc188743.aspx

A more β€œgeneral” method is to actually write any code you need in any language you prefer, and then compile it into IL. You can then retrieve OpCodes at design time and store them where you can get them, or retrieve them from compiled IL at run time and fill it where you need it.

AFAIK there are no unmanaged libraries to help you with this.

0
source share

I suppose you want to do this because you want to see what takes time so you can speed it up (instead of just getting various time information). IMHO, you do not need a profiling API if you can run the application in the IDE and pause it at random. That's why.

-one
source share

All Articles