Bootstrapping LLDB Scripting

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:

The ~/.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:

Happy debugger scripting!

Tags:  debugging

Home