cpp icon indicating copy to clipboard operation
cpp copied to clipboard

Docs on using lldb

Open springmeyer opened this issue 9 years ago • 3 comments
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
  • How to use lldb to interactively debug
  • How to use lldb from within XCode gui (refs #8)

springmeyer avatar Oct 12 '16 15:10 springmeyer

/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?

springmeyer avatar Oct 12 '16 15:10 springmeyer

Sure - I've rarely used the interactive debugging in much depth, my usual workflow looks something like:

  1. 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), or tape is great for this, as tests can be run directly with the node binary instead of a requiring a test runner like mocha.
  2. Run the test script inside the debugger with lldb node test.js - enter r to run, and it will break automatically when it hits the segfault.
  3. Check the state of the current thread with bt or all threads with bt all to look for clues.
  4. ^ is usually sufficient to find a problematic spot to start iterating on a fix, but sometimes fr v (in conjunction with up and down to select different frames in the stack) is helpful for showing the values of "frame variables" to spot unexpected values or null pointers.
  5. The gdb to llldb command map is the most amazingly helpful resource ever for quickly discovering the more in-depth debugging commands available.

mikemorris avatar Oct 12 '16 16:10 mikemorris

Per chat with @springmeyer , also add docs on "Snapshot" profiling using lldb.

GretaCB avatar Oct 12 '16 16:10 GretaCB