【问题标题】:WinDbg extension not recognizing commandWinDbg 扩展无法识别命令
【发布时间】:2020-05-24 20:13:26
【问题描述】:

我正在尝试编写 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 所附加的进程的句柄。有没有一种简单的方法可以将它添加到上述扩展中?

【问题讨论】:

    标签: c# windbg


    【解决方案1】:

    我认为这是一个误解。 OnCommandExecuted() 是一个事件侦听器,当您在 WinDbg 中输入命令时会调用它。您编译的给定 UI 扩展将首先收到有关该命令的通知,然后 WinDbg 将尝试并实际运行该命令。由于该命令不存在,因此它会以与未加载 UI 扩展时相同的错误消息进行响应。

    如果我正确理解你想要什么,那么你正在尝试实现一个 WinDbg 扩展(不是 UI 扩展),它实现了命令 !TestCommand。这通常是一种完全不同的方法。

    在给定的源代码中,您可以在 WinDbgScriptRunner.x64 和 WinDbgScriptRunner.x86 项目中找到“常规”WinDbg 扩展。在那里你可以看到你需要

    [DllExport("compileandrun")]
    

    关于将方法设为!-command 的方法。

    但是,不幸的是,它们因异常而失败:

    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 扩展,所以我不确定你可以用这种方法走多远,以及你是否可以修复输出顺序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      相关资源
      最近更新 更多