Register Python `dbg` object as magic variable in debugger scripting console
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:
-
_get_debuggerfunction: Creates aDebuggerControllerinstance for the currently active binary view -
Magic variable registration: Registers
"dbg"usingbinaryninja.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
Nonewhen no active debugger context is available - Provides the same functionality as manually created
DebuggerControllerinstances
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.
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.
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.