debugger icon indicating copy to clipboard operation
debugger copied to clipboard

Register Python `dbg` object as magic variable in debugger scripting console

Open Copilot opened this issue 3 months ago • 2 comments

This PR implements automatic registration of the dbg magic variable in the Python scripting console, allowing users to access the debugger controller directly without manual instantiation.

Background

Previously, users had to manually create a DebuggerController instance in the Python console:

# Before this change
dbg = DebuggerController(bv)
dbg.launch_and_wait()

Changes

Following the pattern established in the BinaryNinja API (scriptingprovider.py lines 1532-1535) and the kernelcache plugin, this PR adds:

  1. _get_debugger function: Creates a DebuggerController instance for the currently active binary view
  2. Magic variable registration: Registers "dbg" using binaryninja.PythonScriptingProvider.register_magic_variable()

Usage

Users can now access the debugger directly in the Python console:

# After this change - dbg is automatically available
dbg.launch_and_wait()
dbg.step_into_and_wait()
dbg.regs['rax']

The dbg variable automatically:

  • Updates based on the current binary view context
  • Returns None when no active debugger context is available
  • Provides the same functionality as manually created DebuggerController instances

Implementation

The implementation adds 8 lines of code to api/python/debuggercontroller.py:

def _get_debugger(instance: binaryninja.PythonScriptingInstance):
    if instance.interpreter.active_view is None:
        return None
    return DebuggerController(instance.interpreter.active_view)

binaryninja.PythonScriptingProvider.register_magic_variable(
    "dbg",
    _get_debugger
)

This follows the exact same pattern as other Binary Ninja magic variables and integrates seamlessly with the existing scripting infrastructure.

Fixes #851.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Sep 23 '25 09:09 Copilot

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Sep 23 '25 09:09 CLAassistant

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Sep 23 '25 09:09 CLAassistant