Some might consider writing a managed debugger in VB.Net to be an oxymoron. But maybe not. Here’s a VB.Net snippet that serves as a highly-specialized debugger to launch an app and print all the modules that get loaded.
This is adapted from the C# sample snippet here that does the same thing. Although we know that VB.Net is theoretically powerful enough to do what C# does, I thought it would be nice to actually concretely see it in VB.
Writing a whole debugger in VB?
We believe we could have written most of MDbg in VB (instead of C#). However, just because it’s possible doesn’t mean that it’s easy to do. One concern is that although this is technically possible, I doubt MDbg’s usage conforms to the developer-paradigms / design rules that VB developers would expect. People don’t want to worry about things like programming with multiple threads, manipulating metadata, and apartments.
However, since MDbg takes care of most of these problems, building on top of MDbg (like the sample below shows) should be easier.
FWIW, It only took about 15 minutes for me to port it over, and I don’t actually know VB.Net. The IDE really helps you along, and I did a lot of it with Edit + Continue. The biggest problem I had was tracking down the “typeof” operator. It also initially left off the MTAThread attribute and so it hung at the “proc.Go.WaitOne()” line, and then I remembered that ICorDebug is MTA, and so added the attribute.
‘ Test harness to print all loaded modules
‘ Ported from C# version at:
Imports Microsoft.Samples.Debugging.MdbgEngine Module Module1
<MTAThread()> Sub Main()
‘ Get target filename from args
Dim filename As String
Dim args As String()
args = System.Environment.GetCommandLineArgs()
If (args.Length <> 2) Then
Console.WriteLine(“Usage: PrintMods <filename>”)
Console.WriteLine(” Will run <filename> and print all loaded modules.”)
filename = args(1)
Dim debugger As MDbgEngine = New MDbgEngine()
‘ Set options. Specifically need to request Module Loads
debugger.Options.CreateProcessWithNewConsole = True
debugger.Options.StopOnModuleLoad = True Dim proc As MDbgProcess = debugger.CreateProcess(filename, “”, DebugModeFlag.Default, Nothing)
‘ Consume all debug events, sniffing for LoadModules events.
Dim o As Object = proc.StopReason
If (TypeOf o Is ModuleLoadedStopReason) Then
Dim m As ModuleLoadedStopReason = CType(o, ModuleLoadedStopReason)
Console.WriteLine(“Module loaded:” & m.Module.CorModule.Name)
End While Console.WriteLine(“Done!”)