Created
August 8, 2021 17:20
-
-
Save aconz2/fc52f191f32bff2a8fa7416be5c25190 to your computer and use it in GitHub Desktop.
Example using the lldb Python bindings to create breakpoints, run a program, and step through programatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lldb | |
import os | |
import sys | |
symbol_types = {} | |
for x in dir(lldb): | |
if x.startswith('eSymbolType'): | |
symbol_types[getattr(lldb, x)] = x[len('eSymbolType'):] | |
dbg = lldb.SBDebugger.Create() | |
dbg.SetAsync(False) | |
target = dbg.CreateTarget(sys.argv[1]) | |
for m in target.module_iter(): | |
print(m, [x.name for x in m.sections]) | |
for symbol in m: | |
if symbol.type not in (lldb.eSymbolTypeCode, lldb.eSymbolTypeTrampoline) or symbol.name is None or symbol.name.startswith('.'): | |
print('Skipping {:<40} {:<8}'.format(symbol.name or '', symbol_types[symbol.type])) | |
continue | |
print('{:<12} {:<40} {:<8}'.format( | |
symbol.mangled or '', | |
symbol.name or '', | |
symbol_types[symbol.type], | |
)) | |
target.BreakpointCreateByName(symbol.name) | |
# no args, no env vars | |
process = target.LaunchSimple(None, None, os.getcwd()) | |
print(process) | |
thread = process.GetThreadAtIndex(0) | |
while process.GetState() != lldb.eStateExited: | |
frame = thread.GetFrameAtIndex(0) | |
print(frame.GetSymbol().name) | |
function = frame.GetFunction() | |
if function: | |
typ = function.type | |
ret_t = typ.GetFunctionReturnType() | |
args_t = typ.GetFunctionArgumentTypes() | |
print(function.name, ret_t, [x.name for x in args_t]) | |
arg_names = [function.GetArgumentName(i) for i in range(len(args_t))] | |
arg_values = [frame.FindVariable(n).value for n in arg_names] | |
print(' ', arg_values) | |
else: | |
print(frame.GetSymbol().name) | |
process.Continue() | |
print('exited') | |
for bp in target.breakpoint_iter(): | |
print(bp.id, bp.locations[0].GetAddress().GetSymbol().name, bp.GetHitCount()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment