Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[net]how to inject debugging code to an assembly?

Given an assembly with an entry point like:

int FooClass::doFoo(int x, double y)
{
   int ret;
   // Do some foo
   return ret;
}

Is it possible to use yet another assembly to simulate something like:

int FooClass::doFoo(int x, double y)
{
   int ret;
   TRACE_PARAM_INT(x)
   TRACE_PARAM_DOUBLE(y)
   // Do some foo
   TRACE_RETURN_INT(ret)
   return ret;
}

And only enable this code injection when DEBUG is present. If there is such way, how do you load the "debugging" assembly?

EDIT 1: #ifdef is not an option. Say, I don't want to modify the code base.

EDIT 2: My main question is "How to INJECT code to an already compiled assembly". I do have the base code but I'd rather not add the K of lines for tracing in that main code but have another assembly that do such. I do know how to use VS to debug, what I want to is add tracing mechanism of variables (among other things).

like image 597
Anzurio Avatar asked Dec 06 '25 17:12

Anzurio


2 Answers

You could try an AOP post-compiler like PostSharp. It works with all .net languages, but I have not tried it with C++.

like image 65
Jared314 Avatar answered Dec 08 '25 08:12

Jared314


For injecting code into an existing assembly, I would use the Cecil library, which lets you work with IL. This would let you rewrite the assembly if that's what you're after. I have to warn you: it's no small feat.

Oh, there's also an add-in for Reflector, called Reflexil, which lets you edit assemblies.

By the way, AOP-based tracing doesn't add code directly to your assembly. You can keep all the AOP stuff in a separate assembly (in fact, it's a very good idea), and then apply it with attributes. PostSharp will hard-wire code for you, but other AOP frameworks such as Spring or PIAB make things more flexible as they use dynamic proxies, so you can effectively 'turn off' your aspects when they are not needed.

like image 45
Dmitri Nesteruk Avatar answered Dec 08 '25 07:12

Dmitri Nesteruk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!