Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 23, 2025

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.

@CLAassistant
Copy link

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.

1 similar comment
@CLAassistant
Copy link

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.

@Copilot Copilot AI changed the title [WIP] Debugger should register the Python dbg object Register Python dbg object as magic variable in debugger scripting console Sep 23, 2025
@Copilot Copilot AI requested a review from xusheng6 September 23, 2025 09:11
Copilot finished work on behalf of xusheng6 September 23, 2025 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Debugger should register the Python dbg object
3 participants