dbgeng-mcp

An MCP server that drives the Windows debugger engine (dbgeng) for dynamic analysis.
git clone https://dittus.dev/git/dbgeng-mcp.git
Log | Files | Refs | README

diag.py (1803B)


      1 """Low-level diagnostics: print the raw HRESULT of each engine call, no exceptions."""
      2 import ctypes
      3 import os
      4 import sys
      5 from ctypes import POINTER, byref, c_char_p, c_ulong, c_ulonglong, c_void_p
      6 
      7 sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
      8 from dbgeng_mcp import _engine as E  # noqa: E402
      9 
     10 
     11 def h(x):
     12     return f"0x{x & 0xFFFFFFFF:08X}"
     13 
     14 
     15 client = c_void_p()
     16 iid = E._guid(E._abi.INTERFACES["IDebugClient"]["iid"])
     17 print("DebugCreate:", h(E._DebugCreate(byref(iid), byref(client))))
     18 client = client.value
     19 control = E._query_interface(client, "IDebugControl")
     20 print("have control:", control is not None)
     21 
     22 sink = E._OutputSink()
     23 print("SetOutputCallbacks:", h(E._call(client, "IDebugClient", "SetOutputCallbacks",
     24       ctypes.c_long, [c_void_p], sink.pointer)))
     25 
     26 buf = ctypes.create_string_buffer(b"cmd.exe")
     27 hr = E._call(client, "IDebugClient", "CreateProcess", ctypes.c_long,
     28              [c_ulonglong, c_char_p, c_ulong], 0, ctypes.cast(buf, c_char_p),
     29              E.DEBUG_ONLY_THIS_PROCESS | E.CREATE_NO_WINDOW)
     30 print("CreateProcess:", h(hr))
     31 
     32 hr = E._call(control, "IDebugControl", "WaitForEvent", ctypes.c_long,
     33              [c_ulong, c_ulong], 0, E.INFINITE)
     34 print("WaitForEvent:", h(hr), "(S_OK means an event arrived, S_FALSE=0x1 means none)")
     35 
     36 status = c_ulong(0xDEAD)
     37 hr = E._call(control, "IDebugControl", "GetExecutionStatus", ctypes.c_long,
     38              [POINTER(c_ulong)], byref(status))
     39 print("GetExecutionStatus:", h(hr), "status=", h(status.value))
     40 
     41 for outctl in (E.DEBUG_OUTCTL_THIS_CLIENT, 0x1, 0x2):
     42     sink.take()
     43     hr = E._call(control, "IDebugControl", "Execute", ctypes.c_long,
     44                  [c_ulong, c_char_p, c_ulong], outctl, b".echo hi", 0)
     45     print(f"Execute(.echo, outctl={outctl}):", h(hr), "captured=", repr(sink.take()[:60]))