我正在尝试编写WinDbg扩展命令并遇到一些问题。我从this项目开始,并尝试对其进行修改以在调试器中提供自定义命令。
但是,当我运行TestCommand
时,出现以下错误。
The command was: !TestCommand this is a test0:000> !TestCommand this is a test
No export TestCommand found
我的完整代码如下。我试图将[Export]
修饰符添加到TestCommand函数,但是这种情况无法纠正。我如何让WinDbg识别该命令?
完整的扩展名:
using System.ComponentModel.Composition;
using DbgX.Interfaces;
using DbgX.Interfaces.Enums;
using DbgX.Interfaces.Listeners;
using DbgX.Interfaces.Services;
using DbgX.Util;
namespace WinDbgExt.LoadSos
{
[Export(typeof(IDbgCommandExecutionListener))]
[Export(typeof(IDbgStartupListener))]
public class ToggleSosViewModel : IDbgCommandExecutionListener, IDbgStartupListener
{
private bool _engineLoaded;
[Import]
private IDbgConsole _console;
[Import]
private IDbgEngineState _engineState;
public void OnCommandExecuted(string command)
{
if (command.StartsWith("!TestCommand"))
{
TstCommand(command);
}
}
public void TestCommand(string command)
{
_console.PrintTextToConsole("The command was: " + command);
}
public void OnStartup()
{
}
}
}
作为一个奖励问题,我需要了解WinDbg所附加的过程。有没有简单的方法可以将其添加到上述扩展中?
我认为存在误解。 OnCommandExecuted()
是事件侦听器,当您在WinDbg中输入命令时调用。首先,将向您通知有关您已编译的给定UI扩展的命令,然后WinDbg将尝试并实际运行该命令。由于该命令不存在,因此当未加载UI扩展时,它将以与您得到的相同错误消息进行响应。
如果我正确理解了您想要的内容,那么您正在尝试实现一个WinDbg扩展(不是UI扩展),该扩展实现了命令!TestCommand
。通常这是完全不同的方法。
在给定的源代码中,您在WinDbgScriptRunner.x64和WinDbgScriptRunner.x86项目中找到了“常规” WinDbg扩展名。在那里,您可以看到您需要的
[DllExport("compileandrun")]
将方法设为!
-命令的方法。
但是,不幸的是,它们失败,但有一个例外:
0:000> !compileandrun
e0434352 Exception in C:\Users\T\AppData\Local\dbg\UIExtensions\CsharpScriptRunner-x64\WindbgScriptRunner.dll.compileandrun debugger extension.
PC: 00007ff9`c183a799 VA: 00000000`00000000 R/W: 8013150c Parameter: 00000000`00000000
我已经在其Github存储库中将其添加为issue #4。
所以,是的,我知道如果您想用C#来实现扩展会有些棘手。那么,您如何滥用给定的功能并实现命令呢?
唯一没有副作用的命令可能是comment命令:
* this is a comment
您可以将其用于]
*!TestCommand
和它的代码是
if (command.StartsWith("*!TestCommand")) { _console.PrintTextToConsole("My command was invoked! Yay!\r\n"); return; }
输出也有点尴尬,因为您的代码是在将实际命令写入窗口之前执行的:
(4b34.1954): Break instruction exception - code 80000003 (first chance) ntdll!LdrpDoDebuggerBreak+0x30: 00007ff9`c3dd119c cc int 3 My command was invoked! Yay! 0:000> *!TestCommand
我不熟悉为WinDbg Preview编写UI扩展,所以我不确定使用这种方法可以达到什么程度以及是否可以固定输出顺序。