Sample Mdbg extension

Here is a template for playing around with an extension for the MDbg sample (Mdbg is the managed debuggeer written in C#). It adds an "Addition" command to MDbg that adds two numbers. Not very exciting, but it shows the plumbing.  You can download the MDbg sample here.  I'll likely update this if I think of a better sample command than just "addition".

These extensions target the MDbg sample, and not Mdbg in the SDK.

Please note that the MDbg sample is just a sample. We don't consider it a real production debugger (use VS2005 for that). The main goal for extensions is to let hobbyists play around with prototyping debugging ideas and exploring our APIs.


 
//-----------------------------------------------------------------------------
// Template for an MDbg extension 
//
// You can build this in Visual Studio 2005 by adding a new Class Library project
// to the Mdbg sample, and then adding the proper references. 
//
// More about Mdbg here:
//   https://blogs.msdn.com/jmstall/archive/2005/11/08/mdbg_linkfest.aspx 
// 
//
// HOW TO BUILD:
//    This must have a reference to the other Mdbg dlls (corapi, corapi2, mdbgeng, mdbgext)
//
//  Assuming this is caled "myfile.cs", compile like:
//    csc /t:library /debug+ myfile.cs /r:corapi.dll /r:corapi2.dll /r:mdbgeng.dll /r:mdbgext.dll
//
// HOW TO USE:
// You must then load this extension into Mdbg via the "load" command before
// you can use the commands in this extension. For example, at an Mdbg prompt, type:
//     load C:\fullpath\subdir\MyFile.dll
//-----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

using Microsoft.Samples.Tools.Mdbg;
using Microsoft.Samples.Debugging.MdbgEngine;
using Microsoft.Samples.Debugging.CorDebug;
using System.Globalization;

// extension class name must have [MDbgExtensionEntryPointClass] attribute on it 
// and implement a LoadExtension()
[MDbgExtensionEntryPointClass(
    Url = "https://blogs.msdn.com/jmstall", // put your URL here
    ShortDescription = "Eventing test extension." // your "help" description for group of commands
)]
public class MyMdbgExt : CommandBase
{
    // This is called when the extension is first loaded.
    // Extensions are loaded via MDbg's "Load" command.
    public static void LoadExtension()
    {
        // Add all the commands from this class
        MDbgAttributeDefinedCommand.AddCommandsFromType(Shell.Commands, typeof(MyMdbgExt));

        // You can do other initialization here, or write out to the MDbg console.
        // Use WriteOutput instead of "Console.WriteLine", because the Mdbg console may be redirected
        // (to a GUI or logfile, etc). 
        WriteOutput("My mdbg Extension loaded");
    }

    // Commands are identified by the "CommandDescription" attribute on a method.
    [
        CommandDescription(
        CommandName = "addition",  // name of your command in the shell.

        // How many characters does the shell need to match? This can be used 
        // as a "shortcut" to your command
        MinimumAbbrev = 3, 

        // Short help string that appears next to this command in the "Help" list.
        ShortHelp = "adds two numbers",
        
        // Longer help string for when we explicitly ask about this command.    
        // It can include sample usage.
        LongHelp = @"Adds two numbers.
Usage:
    add 2 4     // returns 6
"
        )
    ]
    public static void MyCommand(string argString)
    {   
        // This is a trivial command to add two numbers.
        // For more complex commands, see mdbgCommands.cs in the Mdbg project.
        ArgParser args = new ArgParser(argString);
        if (args.Count != 2)
        {
            throw new MDbgShellException("Expected 2 arguments");
        }
        int a = args.AsInt(0); // throws if not int
        int b = args.AsInt(1);
        WriteOutput(String.Format(CultureInfo.InvariantCulture, "Adding {0} + {1} = {2}", a, b, a+b));
    }

} // end class for my extensions.