0

I want to use LLDB as a python library as described here https://lldb.llvm.org/use/python-reference.html#using-the-lldb-py-module-in-python.

After creating a target with

debugger = lldb.SBDebugger.Create()
target = debugger.CreateTargetWithFileAndArch(path_to_executable, lldb.LLDB_ARCH_DEFAULT)

and getting the command interpreter with

interpreter = debugger.GetCommandInterpreter()

I try to start the target with

ret = llldb.SBCommandReturnObject()
interpreter.HandleCommand('/r', ret)

I get error: invalid target, create a target using the 'target create' command. I also tried to set the selected target with debugger.SetSelectedTarget(target), but it didn't work either. Running file <path_to_target> in the command handler works as expected.

Is there a way to create a target in python and run commands in the interpreter against it?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Dimitri Vorona
  • 450
  • 3
  • 13

2 Answers2

0

Apparently, debugger.CreateTargetWithFileAndArch doesn't return valid targets, which you can see by doing bool(target). Use

target = debugger.CreateTargetWithFileAndArch(path_to_executable, lldb.LLDB_ARCH_DEFAULT)
debugger.SetSelectedTarget(target)
Dimitri Vorona
  • 450
  • 3
  • 13
0

Apparently, lldb.LLDB_ARCH_DEFAULT does not work by some reason on MacOS. So I've replaced it with an explicit name:

target = debugger.CreateTargetWithFileAndArch(exe, "x86_64-apple-macosx10.15.0")

followed by:

assert target

which should help to avoid unexpectedly empty target.

Dmitry Mikushin
  • 1,478
  • 15
  • 16