Defrag Tools #109 - Writing a CLR Debugger Extension Part 1

Sign in to queue

Description

In this episode of Defrag Tools, Andrew Richards starts a two part series on writing a Debugger Extension in C# for the Debugging Tools for Windows. The series follows on from the recent 9 part series on writing a Debugger Extension in C++.

The C# code utilizes the Microsoft.Diagnostics.Runtime (CLRMD) and DllExports packages, available on NuGet.

Resources:
.NET Framework Blog - .NET Crash Dump and Live Process Inspection
NuGet - CLR Memory Diagnostics (CLRMD)
NuGet - Unmanaged Exports (DllExport for .Net)
OneDrive - Code Sample (DebuggerExtensionCSharp.zip)

Timeline:
[00:00] - Writing a Debugger Extension in C#
[01:27] - .NET Framework Blog
[02:37] - Native compilation is required to support exports
[03:31] - Visual Studio 2003 - C# Class Library Project
[04:10] - PM> Install-Package UnmanagedExports
[04:52] - Configuration Manager - Add x64 and x86; Remove Any CPU
[06:30] - PM> Install-Package Microsoft.Diagnostics.Runtime -Pre
[07:05] - Redistribute Microsoft.Diagnostics.Runtime.dll
[09:11] - DllExport attribute (RGiesecke.DllExport)
[11:50] - Marshalling IDebugClient via Microsoft.Diagnostics.Runtime.Interop
[13:45] - DbgEngStream -- Redirect Console output to IDebugControl::ControlledOutput
[17:03] - Hello World!
[18:04] - Debugging - Command line arguments
[18:51] - Lets run it...
[19:48] - Recap
[20:54] - Next week we add commands that use this (infrastructure) code
[21:36] - Email us your issues at defragtools@microsoft.com

Embed

Download

The Discussion

  • User profile image
    imak

    Thanks for the show. I am getting following errors when compiling classLibrary solution. I have installed the nugget packages. Any ideas?


    Error 2 The "DllExportAppDomainIsolatedTask" task could not be instantiated from "C:\Download\DefragTools\ClassLibrary1\packages\UnmanagedExports.1.2.6\tools\RGiesecke.DllExport.MSBuild.dll". Could not load file or assembly 'RGiesecke.DllExport.MSBuild, Version=1.2.6.36228, Culture=neutral, PublicKeyToken=ad5f9f4a55b5020b' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515) MyExt
    Error 3 The "DllExportAppDomainIsolatedTask" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name. MyExt

  • User profile image
    john

    hi imak, were you able to resolve this issue? I am encountering same error after compiling Andrew's class library.

  • User profile image
    windev

    Instead of using my project, maybe try the nuget package import yourself. That might fix it.

  • User profile image
    imak

    Hi Andrew, I already tried that without any luck.
    Hi John. No luck so far. Let me know in case you find someway of make it work.

  • User profile image
    paul

    I can compile and load the extension fine, however, get following exception when run the command. Any idea what I am doing wrong

    0:000> .chain
    Extension DLL search Path:
    C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\WINXP;C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\winext;C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\winext\arcade;C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\pri;C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64;C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\winext\arcade;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\
    Extension DLL chain:
    G:\Download\DefragTools\ClassLibrary1\MyExt\bin\x64\Release\myext.dll: image 1.0.0.0, API 0.1.0, built Mon Oct 20 14:22:34 2014
    [path: G:\Download\DefragTools\ClassLibrary1\MyExt\bin\x64\Release\myext.dll]
    dbghelp: image 6.3.9600.17029, API 6.3.6, built Thu Feb 20 04:02:36 2014
    [path: C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\dbghelp.dll]
    ext: image 6.3.9600.17029, API 1.0.0, built Thu Feb 20 04:23:58 2014
    [path: C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\winext\ext.dll]
    exts: image 6.3.9600.16384, API 1.0.0, built Thu Aug 22 07:32:48 2013
    [path: C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\WINXP\exts.dll]
    uext: image 6.3.9600.16384, API 1.0.0, built Thu Aug 22 07:32:44 2013
    [path: C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\winext\uext.dll]
    ntsdexts: image 6.3.9600.16384, API 1.0.0, built Thu Aug 22 07:33:09 2013
    [path: C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64\WINXP\ntsdexts.dll]
    0:000> !mycommand
    e0434352 Exception in G:\Download\DefragTools\ClassLibrary1\MyExt\bin\x64\Release\myext.dll.mycommand debugger extension.
    PC: 00007ffe`3ece43c8 VA: 00000000`00000000 R/W: 80070002 Parameter: 00000000`00000000
    0:000> !mycommand hello
    e0434352 Exception in G:\Download\DefragTools\ClassLibrary1\MyExt\bin\x64\Release\myext.dll.mycommand debugger extension.
    PC: 00007ffe`3ece43c8 VA: 00000000`00000000 R/W: 80070002 Parameter: 00000000`00000000

  • User profile image
    paul

    actually don't worry about it. That was my bad, didn't copy diagnostics file in the windbg folder. After copying, it worked like a charm :)

Add Your 2 Cents