Why did the process stop?

I am experimenting with the LLDB SB API to add debug capability to a compiler.

Right now I’d like to print out a backtrace when a program “crashes”. Let’s define “crash” as “receive SIGILL or SIGSEGV”.

I have a small routine that forks, runs the compiled program as a child, and has the parent attach to the child. I get an SBProcess handle, and then:

m_Process.Continue();

state = m_Process.GetState();
while (state != eStateExited) {
if (state == eStateStopped) {
dumpBackTrace();
m_Process.Detach(false);
break;
}
m_Process.Continue();
state = m_Process.GetState();
}

However, a process may stop for many reasons: it may hit a breakpoint, or it may get a signal other than one that indicates a crash. In particular, to synchronize parent and child, I have the child pause(), and the parent sends SIGUSR1 once it’s attached. I do not want the receipt of SIGUSR1 to cause a backtrace.

So:

  1. How do I determine that a process has entered eStateStopped because it received a signal?

  2. How do I determine which signal was received?

The Doxygen docs don’t have any detail, and none of the C/Python examples seem to go into this much detail.

1. How do I determine that a process has entered eStateStopped because it
received a signal?
2. How do I determine which signal was received?

Hi,

stop reasons are not a property of the whole process, but of a
particular thread (after all, you can have multiple threads stopping
at the same time for different (or same) reasons). So, to get the stop
reason, you first need to enumerate all the threads of the process
(SBProcess::GetThreadAtIndex) and then ask each thread for it's stop
reason (SBThread::GetStopReason). If the stop reason is
eStopReasonSignaled, then SBThread::GetStopReasonDataAtIndex(0) will
give you the signal number.

References: <http://lldb.llvm.org/python_reference/lldb.SBThread-class.html&gt;,
<http://lldb.llvm.org/python_reference/lldb.SBProcess-class.html&gt;

hope that helps,
pl

Pavel was correct in that you iterate through the SBThreads in the SBProcess and ask each thread for its stop reason.

For your code below to work, you must be running in synchronous mode. Did you enable synchronous mode using the following line?:

debugger.SetAsync(false);

If you didn't then you need to consume the events manually. The bad thing about synchronous mode is that there is no way to interrupt the process when it is running since the:

m_Process.Continue()

line won't return until the process stops.

If you want to run in async mode, it isn't much harder and we have a python example that can help you out:

svn cat http://llvm.org/svn/llvm-project/lldb/trunk/examples/python/process_events.py

The main event loop (where we wait for process state change events) is on line 179 - 245.

Greg Clayton