LLDB script to get stack trace after crash

I am trying to add the ability to generate a stack trace from a core dump on a Mac automatically when one of our tests fails.

I was able to do this quite easily on linux using

gdb --batch --quiet -ex "thread apply all bt" -ex "quit" <binary> <core file> 2> /dev/null

But I am having problems with the same as on mac (OSX 10.8) with lldb. For starters, the lldb version I'm using is lldb-310.2.37.

My initial approach was to use a parameter -sand pass the script file as follows:

target create -c <core file> <binary>
thread backtrace all
quit

Initially, I had some problems that, in my opinion, were caused by the absence of a new line at the end of the script file, due to which lldb did not exit, but after that it was fixed: I get the following: Executing commands in 'lldbSource'.

(lldb)  target create -c <core file> <binary>
Core file '<core file>' (x86_64) was loaded.
(lldb)  thread backtrace all
error: Aborting reading of commands after command #1: 'thread backtrace all' failed with error: invalid thread
Aborting after_file command execution, command file: 'lldbSource' failed.

, lldb, "thread backtrace all" .

, № 2 python script API- python ( , , , - ).

script:

import lldb
debugger = lldb.SBDebugger.Create()
target = debugger.CreateTarget('<binary>')
if target:
 process = target.LoadCore('<core file>')
 if process:
  print process.exit_description
  for thread in process:
   print 'Thread %s:' % str(thread.GetThreadID())
   print '\n'.join(str(frame) for frame in thread)

, , , process.exit_description None ( , , LLDB python API- ).

, , :

Process 0 stopped
* thread #1: tid = 0x0000, 0x00007fff8aca4670 libsystem_c.dylib`strlen + 16, stop reason = signal SIGSTOP
    frame #0: 0x00007fff8aca4670 libsystem_c.dylib`strlen + 16
libsystem_c.dylib`strlen + 16:
-> 0x7fff8aca4670:  pcmpeqb (%rdi), %xmm0
   0x7fff8aca4674:  andl   $0xf, %ecx
   0x7fff8aca4677:  shll   %cl, %eax
   0x7fff8aca4679:  pmovmskb %xmm0, %ecx

LLDB . , , , , .

, , , , , . LLDB, , , , ​​ .

. perl script.

+4
2

TOT lldb lldb.llvm.org "--batch", , gdb, , - . lldb, , Xcode.

exit_description None, , . , OS X , , , . . , lldb , , GetStatus.

stream = lldb.SBStream()
process.threads[0].GetStatus(stream)
print stream.GetData()

, , .

+1

--batch lldb, Xcode 7.2 (lldb-340.4.119) , , .

, lldb --help:

   -b 
   --batch 
        Tells the debugger to running the commands from -s, -S, -o & -O,
        and then quit.  However if any run command stopped due to a signal
        or crash, the debugger will return to the interactive prompt at the
        place of the crash.

lldb:

   -o 
   --one-line 
        Tells the debugger to execute this one-line lldb command after any
        file provided on the command line has been loaded.

   -k 
   --one-line-on-crash 
        When in batch mode, tells the debugger to execute this one-line
        lldb command if the target crashes.

, :

ulimit -c unlimited && (<binary> || (lldb -c `ls -t /cores/* | head -n1` \
  --batch -o 'thread backtrace all' -o 'quit' && exit 1))

:

  • <binary>
  • , lldb /cores (, )
  • , ( CI, Makefiles ..).

lldb <binary>, . lldb - - GDB -return-child-result quit 1 - , . , ​​.

+3

All Articles