cpp
cpp copied to clipboard
Docs on using lldb
trafficstars
Upcoming mason provides packages for lldb 3.9.x that are identical on os x and linux: https://github.com/mapbox/mason/pull/253. This means we can and should write consistent docs for how to debug C++ programs on both platforms.
Topics to cover:
- How to build your program in debug mode
- How to use lldb to generate backtraces
- Via a core file:
lldb -c $(ls -t /cores/* | head -n1) --batch -o 'thread backtrace all' -o 'quit'- How to ensure core files are created and found on linux vs osx
- Via re-running the program with lld:
lldb --batch -o 'run' -k 'thread backtrace all' -k 'quit 1' node /path/to/script.js
- Via a core file:
- How to use lldb to interactively debug
- How to use lldb from within XCode gui (refs #8)
/cc @mikemorris - you've been using lldb quite a bit: can you drop ideas on this ticket for things you've learned that can be documented for others?
Sure - I've rarely used the interactive debugging in much depth, my usual workflow looks something like:
- Find a way to consistently reproduce or write a unit test reproducing the segfault or other C++-type crash I'm seeing.
- Writing unit tests with
tap(preferred, built in JS test coverage is amazing), ortapeis great for this, as tests can be run directly with thenodebinary instead of a requiring a test runner likemocha.
- Writing unit tests with
- Run the test script inside the debugger with
lldb node test.js- enterrto run, and it will break automatically when it hits the segfault. - Check the state of the current thread with
btor all threads withbt allto look for clues. - ^ is usually sufficient to find a problematic spot to start iterating on a fix, but sometimes
fr v(in conjunction withupanddownto select different frames in the stack) is helpful for showing the values of "frame variables" to spot unexpected values or null pointers. - The gdb to llldb command map is the most amazingly helpful resource ever for quickly discovering the more in-depth debugging commands available.
Per chat with @springmeyer , also add docs on "Snapshot" profiling using lldb.