WinDbg scripting has gotten more sophisticated, especially after the last debugger revamp a few years ago.
LLDB also has a scripting mechanism based on Python, not unlike GDB. Today's post will touch on how to get started with this.
First, here's a simple command you can paste in zsh or bash to get going.
mkdir ~/lldb
echo "def lldbcanary(debugger, command, result, internal_dict):" > ~/lldb/lldbcanary.py
echo " print('hello from lldbcanary')" >> ~/lldb/lldbcanary.py
echo "" >> ~/lldb/lldbcanary.py
echo "def __lldb_init_module(debugger, internal_dict):" >> ~/lldb/lldbcanary.py
echo " debugger.HandleCommand('command script add -f lldbcanary.lldbcanary canary')" >> ~/lldb/lldbcanary.py
echo "" >> ~/lldb/lldbcanary.py
echo "command script import ~/lldb/lldbcanary.py" >> ~/.lldbinit
The actual contents are as follows:
~/lldb/lldbcanary.py
def lldbcanary(debugger, command, result, internal_dict):
print('hello from lldbcanary')"
def __lldb_init_module(debugger, internal_dict):"
debugger.HandleCommand('command script add -f lldbcanary.lldbcanary canary')
~/.lldbinit
command script import ~/lldb/lldbcanary.py
The ~/lldb/lldbcanary.py
file has two functions:
lldbcanary
, which takes some arguments that the debugger will pass in upon invocation, and simply prints out a 'hello world'-style message__lldb_init_module
, which also some arguments that the debugger will pass in when invoking this as part of module load, and in turn invokes the command script add
command to register a new command associated with the lldbcanary
function from the lldbcanary
moduleThe ~/.lldbinit
file has a debugger command to import the lldbcanary.py
file, and is invoked whenever the debugger initializes.
So, this provides a simple mechanism to bootstrap whatever commands you want automatically. To add new functionality:
__lldb_init_module
to provide an alias.Happy debugger scripting!
Tags: debugging