Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to write my own unmanaged IL library to rewrite IL with the CLR Profiling API?

I've been looking at some of the articles for the CLR Profiling API, and many of those articles talk about calling SetILFunctionBody() to do the actual IL rewriting; however, none of those articles actually explain exactly what you could use to rewrite the actual method IL bytes. Is there an unmanaged library out there that allows me to write IL, or would I have to write one myself?

like image 297
plaureano Avatar asked Feb 16 '10 00:02

plaureano


1 Answers

Probably. It depends.

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

http://mono-project.com/Cecil

However it's managed code, which you can't call while profiling. You may a have a few options though:

  1. Use IPC. You could spawn a new process, doing the rewriting using cecil in that process and then pass the bytes back to your profiler using named pipes.
  2. Port CECIL to C++. The code's distributed under the MIT / X11 license, so you could do this without having to share your changes.
  3. Just write your own stuff from scratch.

#1 introduces a bunch of extra complexity. Your profiler would end up having more moving parts than it really needed. Also, the IPC introduces a bunch of extra overhead.

#2 would take a long time. Given that Cecil is still only at version 0.6, it might not be worth the time to do it, vs writing your own implementation.

#3 would give you the greatest degree of control, and would probably be the most performant. However it would take substantially more effort than #1 would.

like image 77
Scott Wisniewski Avatar answered Sep 28 '22 10:09

Scott Wisniewski