Using LLDB to catch a spurious segfault: scripting, etc.

Hello all,

I am attempting to use lldb to debug a segfault in my program that
happens sporadically. For instance, running `for ((i=0;i<1000;i++)) {
./myprogram; };` at a shell prompt may show one. It seems there is no
way to set lldb to run automatically and exit upon success from the
CLI, so I've been exploring lldb's python scripting.

The goal of the script is to loop, launching the process until
completion or error; if there's an error, I need the script to dump me
back into the lldb interpreter to investigate the bug. Here's what
I've come up with so far:

import time
dbg = lldb.SBDebugger.FindDebuggerWithID(lldb.debugger_unique_id)
ci = dbg.GetCommandInterpreter()
res = lldb.SBCommandReturnObject()
for i in range(1, 1000):
  ci.HandleCommand("process launch", res)
  while (not res.Succeeded()) : time.sleep(0.1)

Unfortunately, however, it seems the command does not run to
completion, no matter how long I wait. Then when I call
`HandleCommand` a second time, lldb deadlocks. I intended to
eventually check `res.GetError()` or `res.GetStatus()` and call
`quit()` when the error appeared, but I haven't made it that far. I
also initially explored calling `dbg.GetTargetAtIndex(0).Launch()`
rather than `HandleCommand`, but I wasn't entirely sure how to go
about it. Any help would be much appreciated!


Not sure why the CLI runner is not working, I generally use the SB API's when doing scripting as that is more powerful. There are plenty of examples in the tests directory in the sources that do this, however.

The following will just run any program you pass it over and over, and catch if it stops, and print a backtrace. Note I cheesed out and did use the CLI to get the backtrace but that's 'cause it is just a toy... (2.16 KB)

Oh, sorry, you're probably running this inside of lldb? I don't think that works yet. Running from python while inside lldb it tricky because lldb is already waiting on the debugger, and so the two fight over events, with Python losing...

Adding a --batch mode to the main lldb driver shouldn't be that hard, however.


Hi William,

Wonder if:

Add a Python script which launches a program from within lldb and loop until the
process stops for some reason. main.c (compiled into a.out) is used as an example in
the README-run-until-faulted file.

lldb/trunk/utils/test/ (with props)

is a good enough start for your need?


Brilliant! Between this example and Jim's, I think I'll be able to put
something nice together.

Thank you both,

Hi again,
I've gotten quite far with this script (attached), however I cannot
figure out if there is a way invoke the lldb interpreter (i.e. the
"(lldb)" prompt). It would seem that one could somehow activate the
debugger's EditLineInputReader, perhaps via PushInputReader.
Unfortunately, EditLineInputReader doesn't appear to be exposed. At
this point, I may write a small hackish python function that loops,
sending input via HandleCommand. If there's another way where I don't
have to hack out an interpreter loop, I'd be grateful to hear of it.


Oops, I forgot to attach the script; here it is.

Will (3.23 KB)