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...
#!/usr/bin/python
#----------------------------------------------------------------------
# Be sure to add the python path that points to the LLDB shared library.
# On MacOSX csh, tcsh:
# setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
# On MacOSX sh, bash:
# export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
#----------------------------------------------------------------------
import lldb
import os
import sys
import time
# Create a new debugger instance
debugger = lldb.SBDebugger.Create()
# When we step or continue, don't return from the function until the process
# stops. We do this by setting the async mode to false.
debugger.SetAsync (False)
# Create a target from a file and arch
print "Creating a target for '%s'" % sys.argv[1]
target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT)
if target.IsValid():
# Launch the process. Since we specified synchronous mode, we won't return
# from this function until we hit the breakpoint at main
error = lldb.SBError()
for i in range (0, 1000):
# If you're using current LLDB TOT, you should use this:
#process = target.Launch (debugger.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
# otherwise use this:
process = target.Launch (None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
# Make sure the launch went ok
if process.IsValid():
# Print some simple process info
state = process.GetState ()
print "Run: ", i, " Process: ", process
if state == lldb.eStateStopped:
print "Process stopped: ", process
res = lldb.SBCommandReturnObject()
debugger.GetCommandInterpreter().HandleCommand ("thread backtrace all", res)
print res
elif state == lldb.eStateExited:
print "Exited."
else:
print "Unexpected process state: %s, killing process..." % debugger.StateAsCString (state)
process.Kill()
lldb.SBDebugger.Terminate()
This one works for me. See if it works for you...
Jim
On Mar 31, 2011, at 6:15 PM, William Knop wrote:
> 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()
>> dbg.SetAsync(False)
>> for i in range(1, 1000):
>> ci.HandleCommand("process launch", res)
>> while (not res.Succeeded()) : time.sleep(0.1)
>> res.Clear()
>
> 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!
>
> Will
> _______________________________________________
> lldb-dev mailing list
> [email protected]
> http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev
_______________________________________________
lldb-dev mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev