Do I need to write my own unmanaged IL library to rewrite IL with the CLR Profiling API?
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:
- 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.
- Port CECIL to C++. The code's distributed under the MIT / X11 license, so you could do this without having to share your changes.
- 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.
I wrote a simple one for OpenCover https://github.com/sawilde/opencover which you or anyone else for that matter may find useful