Thursday, April 20, 2006

Dynamic binary weaving using mdbg works!

I took one of the C# samples (see attached typefinder.cs) from the Microsoft SSCLI 2.0 package and built it using the regular C# compiler. I then launched it using Wicca and wove a Logging aspect at runtime:
mdbg.exe -verbosity 0 -nologo !load wicca.dll !wtype binary !wrun typefinder.exe html !weave LogAspect.dll !wgo !quit

Here's an explanation of the command-line:

mdbg.exe The Microsoft Managed Debugger.

-verbosity 0 Keep debugger messages to a minimum.

-nologo Don't display the Managed Debugger logo.

!load wicca.dll Load our plugin.

!wtype binary Use binary weaving. We also support breakpoint
weaving using the Debugger's Breakpoint API.

!wrun typefinder.exe html Launch the typefinder.exe
client program in suspended mode. Typefinder
is a sample program that searches for classes
and interfaces that include the string specified
on the command-line (we just specify "html").

!weave LogAspect.dll Weave in the Logging aspect (see attached
LogAspect.cs). The aspect causes all Reflection
calls made by typefinder.exe to be logged. We
use Phx.Morph to weave in the aspect. Our
AssemblyDiff component diffs the original and
woven executables and creates the delta files
(.dil and .dmeta). Wicca then calls the
Debugger's Edit-and-Continue API to patch the
running program using the delta files.

!wgo Resume execution of the newly woven Typefinder.
The output is attached (dotest.txt).

!quit Exit the Managed Debugger when Typefinder exits.

This is our first successful nontrivial test, but there are still numerous issues to work out. The goal is for dynamic (binary and breakpoint) weaving to be at parity with static weaving (using Phx.Morph). The only difference is that dynamic weaving is currently much slower.

No comments: