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

gen_abi.py (2389B)


      1 """Generate src/dbgeng_mcp/_abi.py from a Windows SDK DbgEng.h.
      2 
      3     python3 tools/gen_abi.py "/mnt/c/Program Files (x86)/Windows Kits/10/Include/<ver>/um/DbgEng.h"
      4 
      5 The vtable order and interface IDs are read straight from the header, so the binding
      6 matches whatever dbgeng.dll ships with that SDK. Method declarations come in three forms,
      7 all of which take a vtable slot: STDMETHOD (HRESULT), STDMETHOD_ (other return type), and
      8 STDMETHODV (variadic, e.g. Output). Missing any of them shifts every later index.
      9 """
     10 
     11 import os
     12 import re
     13 import sys
     14 
     15 INTERFACES = ["IDebugClient", "IDebugControl", "IDebugRegisters", "IDebugDataSpaces",
     16               "IDebugSymbols", "IDebugSystemObjects", "IDebugOutputCallbacks",
     17               "IDebugOutputCallbacksWide"]
     18 
     19 # STDMETHOD(Name) / STDMETHOD_(ret, Name) / STDMETHODV(Name) / STDMETHODV_(ret, Name).
     20 METHOD = re.compile(r"STDMETHODV?(?:_)?\(\s*(?:[A-Za-z0-9_ \t\*]+,\s*)?(\w+)\s*\)")
     21 
     22 
     23 def guid_tuple(text, name):
     24     m = re.search(r"DEFINE_GUID\(\s*IID_%s\s*,(.*?)\);" % name, text, re.S)
     25     return [int(x, 16) for x in re.findall(r"0x[0-9A-Fa-f]+", m.group(1))]
     26 
     27 
     28 def guid_str(n):
     29     return "{%08x-%04x-%04x-%02x%02x-%s}" % (
     30         n[0], n[1], n[2], n[3], n[4], "".join("%02x" % b for b in n[5:11]))
     31 
     32 
     33 def methods(text, name):
     34     m = re.search(r"DECLARE_INTERFACE_\(\s*%s\s*,\s*\w+\s*\)(.*?)\n\s*\};" % name, text, re.S)
     35     return METHOD.findall(m.group(1))
     36 
     37 
     38 def main(header_path):
     39     text = open(header_path, encoding="utf-8", errors="replace").read()
     40     out = ['"""Generated by tools/gen_abi.py from the Windows SDK DbgEng.h. Do not edit by hand."""',
     41            "", "INTERFACES = {"]
     42     for iface in INTERFACES:
     43         ms = methods(text, iface)
     44         assert ms[:3] == ["QueryInterface", "AddRef", "Release"], (iface, ms[:4])
     45         g = guid_tuple(text, iface)
     46         assert len(g) == 11
     47         out += [f'    "{iface}": {{',
     48                 f'        "iid": {tuple(g)},  # {guid_str(g)}',
     49                 f'        "guid_str": "{guid_str(g)}",',
     50                 f'        "methods": {ms},',
     51                 "    },"]
     52     out.append("}")
     53     dest = os.path.join(os.path.dirname(__file__), "..", "src", "dbgeng_mcp", "_abi.py")
     54     with open(dest, "w") as f:
     55         f.write("\n".join(out) + "\n")
     56     print(f"wrote {os.path.normpath(dest)} ({len(INTERFACES)} interfaces)")
     57 
     58 
     59 if __name__ == "__main__":
     60     main(sys.argv[1])