Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I know instrumentation is a technique to add trace code dynamically into the methods to enable tracing and debugging.

I was wondering if this is only a "Trace" option, hard coded into the CLR to add only trace code, or is there the ability to add any code to the methods?

For example, I want to check for a condition in the beginning of every single method call in a certain class (say for permissions). Can I do this via adding dynamic code to the beginning of the methods in execution time?

I'm not sure how this trace "instrumentation" thing works, but I'm wondering if this can be used for other goals too, or not.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
439 views
Welcome To Ask or Share your Answers For Others

1 Answer

Basically what you should do is write a CLR profiler and use the profiler API in c++
You need to implement the ICorProfilerCallback interface.
What you are looking for is in the JITCompilationStarted callback. This method is called each time a managed method gets called and before the jit compiler compiles the IL into machine code. Any code insertion work during run time should be done in JITCompilationStarted.
You can look at the open source coverage tool part cover as an example how to do it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...