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

4

7

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?

Katzenjammer answered 16/2, 2010 at 0:11 Comment(0)
D
3

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.

Dreg answered 21/2, 2010 at 2:25 Comment(2)
I already use Cecil, and I'm looking for an unmanaged alternative.Katzenjammer
When I said "probably" I was answer the question "do I need to write my own....".Dreg
B
2

I wrote a simple one for OpenCover https://github.com/sawilde/opencover which you or anyone else for that matter may find useful

Burns answered 22/6, 2011 at 6:38 Comment(0)
S
0

The actual bytes has to come from somewhere and if you are just using the Profiling API then you must provide them yourself. This article goes in-depth on how to do it (probably one of those you have read): http://msdn.microsoft.com/en-us/magazine/cc188743.aspx

A more 'common' technique is to actually write whatever code you need in whatever language you prefer and then compile it to IL. You can then either extract the OpCodes at design-time and store them where you can reach them or extract them from your compiled IL at run-time and stuff it where you need it.

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

Steve answered 18/2, 2010 at 22:53 Comment(0)
N
-1

I assume you want to do this because you want to see what is taking time so that you can make it go faster (as opposed to just getting various timing info). IMHO, you don't need the profiling API, if you can run your app under an IDE and pause it at random. Here's why.

Notarize answered 18/2, 2010 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.