views:

193

answers:

5

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.

A: 

I know PostSharp allows you to add 'aspects' to existing methods via attributing so you can add entry/exit traces onto your methods

zebrabox
See my comment for Mitch Wheat, please.
Iravanchi
+2  A: 

You are referring to Aspect Oriented Programming (AOP).

Have a look at PostSharp.

Alo: Open Source Aspect-Oriented Frameworks in C#

Mitch Wheat
I am not referring to AOP, but I want to use the runtime instrumentation for AOP. I know a few good frameworks for AOP, and I've written one myself (will be published as open source soon), and I was wondering if I can use the same thing as runtime instrumentation to intercept calls.PostSharp is the closest thing to what I had in mind, but it operates in build time. Is there a run-time version (which actually adds code to the same method, similar to PostSharp)?
Iravanchi
+1  A: 

As others have answered, such Cross-Cutting Concerns are often addressed with Aspect Oriented Programming (AOP).

One way to do AOP is with code instrumentation with tools such as PostSharp, but an alternative that requires no extra tools is by utilizing Dependency Injection (DI) and the Decorator design pattern.

Imagine that your code consumes the IFoo interface:

public interface IFoo
{
    string GetStuff(string request);
}

You may have a concrete implmentation MyFoo of IFoo, but you can also write one or several Decorators that handle different aspects:

public class AdministratorGuardingFoo : IFoo
{
    private readonly IFoo foo;

    public AdministratorGuardingFoo(IFoo foo)
    {
        if (foo == null)
        {
            throw new ArgumentNullException("foo");
        }

        this.foo = foo;
    }

    public string GetStuff(string request)
    {
        new PrincipalPermission(null, "Administrator").Demand();

        return this.foo.GetStuff(request);            
    }
}

You can now (have your DI Container) wrap MyFoo in AdministratorGuardingFoo. All consumers that consume IFoo will not notice the difference.

Mark Seemann
A: 

The CLR allows method interception via message sinks.

Patrik
I think the message-based approach is good for remote calls, not local ones. Am I right?
Iravanchi
+2  A: 

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.

Ohad Horesh