4 This page details various ways to debug LLDB itself and other LLDB tools. If
5 you want to know how to use LLDB in general, please refer to
8 As LLDB is generally split into 2 tools, ``lldb`` and ``lldb-server``
9 (``debugserver`` on Mac OS), the techniques shown here will not always apply to
10 both. With some knowledge of them all, you can mix and match as needed.
12 In this document we refer to the initial ``lldb`` as the "debugger" and the
13 program being debugged as the "inferior".
15 Building For Debugging
16 ----------------------
18 To build LLDB with debugging information add the following to your CMake
23 -DCMAKE_BUILD_TYPE=Debug \
24 -DLLDB_EXPORT_ALL_SYMBOLS=ON
26 Note that the ``lldb`` you will use to do the debugging does not itself need to
27 have debug information.
29 Then build as you normally would according to :doc:`/resources/build`.
31 If you are going to debug in a way that doesn't need debug info (printf, strace,
32 etc.) we recommend adding ``LLVM_ENABLE_ASSERTIONS=ON`` to Release build
33 configurations. This will make LLDB fail earlier instead of continuing with
34 invalid state (assertions are enabled by default for Debug builds).
39 The simplest scenario is where we want to debug a local execution of ``lldb``
44 ./bin/lldb test_program
46 LLDB is like any other program, so you can use the same approach.
50 ./bin/lldb -- ./bin/lldb /tmp/test.o
52 That's it. At least, that's the minimum. There's nothing special about LLDB
53 being a debugger that means you can't attach another debugger to it like any
56 What can be an issue is that both debuggers have command line interfaces which
57 makes it very confusing which one is which:
63 Process 1741640 launched: '<...>/bin/lldb' (aarch64)
64 Process 1741640 stopped and restarted: thread 1 received signal: SIGCHLD
67 (lldb) target create "/tmp/test.o"
68 Current executable set to '/tmp/test.o' (aarch64).
70 Another issue is that when you resume the inferior, it will not print the
71 ``(lldb)`` prompt because as far as it knows it hasn't changed state. A quick
72 way around that is to type something that is clearly not a command and hit
77 (lldb) Process 1742266 stopped and restarted: thread 1 received signal: SIGCHLD
78 Process 1742266 stopped
79 * thread #1, name = 'lldb', stop reason = signal SIGSTOP
80 frame #0: 0x0000ffffed5bfbf0 libc.so.6`__GI___libc_read at read.c:26:10
82 Process 1742266 resuming
84 error: 'notacommand' is not a valid command.
87 You could just remember whether you are in the debugger or the inferior but
88 it's more for you to remember, and for interrupt based events you simply may not
91 Here are some better approaches. First, you could use another debugger like GDB
92 to debug LLDB. Perhaps an IDE like Xcode or Visual Studio Code. Something which
93 runs LLDB under the hood so you don't have to type in commands to the debugger
96 Or you could change the prompt text for the debugger and/or inferior.
100 $ ./bin/lldb -o "settings set prompt \"(lldb debugger) \"" -- \
101 ./bin/lldb -o "settings set prompt \"(lldb inferior) \"" /tmp/test.o
103 (lldb) settings set prompt "(lldb debugger) "
106 (lldb) settings set prompt "(lldb inferior) "
109 If you want spacial separation you can run the inferior in one terminal then
110 attach to it in another. Remember that while paused in the debugger, the inferior
111 will not respond to input so you will have to ``continue`` in the debugger
117 $ ./bin/lldb /tmp/test.o
120 $ ./bin/lldb ./bin/lldb --attach-pid $(pidof lldb)
125 Generally you will want to hit some breakpoint in the inferior ``lldb``. To place
126 that breakpoint you must first stop the inferior.
128 If you're debugging from another window this is done with ``process interrupt``.
129 The inferior will stop, you place the breakpoint and then ``continue``. Go back
130 to the inferior and input the command that should trigger the breakpoint.
132 If you are running debugger and inferior in the same window, input ``ctrl+c``
133 instead of ``process interrupt`` and then folllow the rest of the steps.
135 If you are doing this with ``lldb-server`` and find your breakpoint is never
136 hit, check that you are breaking in code that is actually run by
137 ``lldb-server``. There are cases where code only used by ``lldb`` ends up
138 linked into ``lldb-server``, so the debugger can break there but the breakpoint
141 Debugging ``lldb-server``
142 -------------------------
144 Note: If you are on MacOS you are likely using ``debugserver`` instead of
145 ``lldb-server``. The spirit of these instructions applies but the specifics will
148 We suggest you read :doc:`/use/remote` before attempting to debug ``lldb-server``
149 as working out exactly what you want to debug requires that you understand its
150 various modes and behaviour. While you may not be literally debugging on a
151 remote target, think of your host machine as the "remote" in this scenario.
153 The ``lldb-server`` options for your situation will depend on what part of it
154 or mode you are interested in. To work out what those are, recreate the scenario
155 first without any extra debugging layers. Let's say we want to debug
156 ``lldb-server`` during the following command:
160 $ ./bin/lldb /tmp/test.o
162 We can treat ``lldb-server`` as we treated ``lldb`` before, running it under
163 ``lldb``. The equivalent to having ``lldb`` launch the ``lldb-server`` for us is
164 to start ``lldb-server`` in the ``gdbserver`` mode.
166 The following commands recreate that, while debugging ``lldb-server``:
170 $ ./bin/lldb -- ./bin/lldb-server gdbserver :1234 /tmp/test.o
171 (lldb) target create "./bin/lldb-server"
172 Current executable set to '<...>/bin/lldb-server' (aarch64).
174 Process 1742485 launched: '<...>/bin/lldb-server' (aarch64)
175 Launched '/tmp/test.o' as process 1742586...
177 (in another terminal)
178 $ ./bin/lldb /tmp/test.o -o "gdb-remote 1234"
180 Note that the first ``lldb`` is the one debugging ``lldb-server``. The second
181 ``lldb`` is debugging ``/tmp/test.o`` and is only used to trigger the
182 interesting code path in ``lldb-server``.
184 This is another case where you may want to layout your terminals in a
185 predictable way, or change the prompt of one or both copies of ``lldb``.
187 If you are debugging a scenario where the ``lldb-server`` starts in ``platform``
188 mode, but you want to debug the ``gdbserver`` mode you'll have to work out what
189 subprocess it's starting for the ``gdbserver`` part. One way is to look at the
190 list of runninng processes and take the command line from there.
192 In theory it should be possible to use LLDB's
193 ``target.process.follow-fork-mode`` or GDB's ``follow-fork-mode`` to
194 automatically debug the ``gdbserver`` process as it's created. However this
195 author has not been able to get either to work in this scenario so we suggest
196 making a more specific command wherever possible instead.
198 Another option is to let ``lldb-server`` start up, then attach to the process
199 that's interesting to you. It's less automated and won't work if the bug occurs
200 during startup. However it is a good way to know you've found the right one,
201 then you can take its command line and run that directly.
203 Output From ``lldb-server``
204 ***************************
206 As ``lldb-server`` often launches subprocesses, output messages may be hidden
207 if they are emitted from the child processes.
209 You can tell it to enable logging using the ``--log-channels`` option. For
210 example ``--log-channels "posix ptrace"``. However that is not passed on to the
213 The same goes for ``printf``. If it's called in a child process you won't see
216 In these cases consider interactive debugging ``lldb-server`` or
217 working out a more specific command such that it does not have to spawn a
218 subprocess. For example if you start with ``platform`` mode, work out what
219 ``gdbserver`` mode process it spawns and run that command instead.
221 Another option if you have ``strace`` available is to trace the whole process
222 tree and inspect the logs after the session has ended. ::
224 $ strace -ff -o log -p $(pidof lldb-server)
226 This will log all syscalls made by ``lldb-server`` and processes that it forks.
227 ``-ff`` tells ``strace`` to trace child processes and write the results to a
228 separate file for each process, named using the prefix given by ``-o``.
230 Search the log files for specific terms to find the process you're interested
231 in. For example, to find a process that acted as a ``gdbserver`` instance::
233 $ grep "gdbserver" log.*
234 log.<N>:execve("<...>/lldb-server", [<...> "gdbserver", <...>) = 0
239 If you want to debug part of LLDB running on a remote machine, the principals
240 are the same but we will have to start debug servers, then attach debuggers to
243 In the example below we're debugging an ``lldb-server`` ``gdbserver`` mode
244 command running on a remote machine.
246 For simplicity we'll use the same ``lldb-server`` as the debug server
247 and the inferior, but it doesn't need to be that way. You can use ``gdbserver``
248 (as in, GDB's debug server program) or a system installed ``lldb-server`` if you
249 suspect your local copy is not stable. As is the case in many of these
254 $ <...>/bin/lldb-server gdbserver 0.0.0.0:54322 -- \
255 <...>/bin/lldb-server gdbserver 0.0.0.0:54321 -- /tmp/test.o
257 Now we have a debug server listening on port 54322 of our remote (``0.0.0.0``
258 means it's listening for external connections). This is where we will connect
259 ``lldb`` to, to debug the second ``lldb-server``.
261 To trigger behaviour in the second ``lldb-server``, we will connect a second
262 ``lldb`` to port 54321 of the remote.
264 This is the final configuration:
269 --------------------------------------------|--------------------
270 lldb A debugs lldb-server on port 54322 -> | lldb-server A
272 lldb B debugs /tmp/test.o on port 54321 -> | lldb-server B
276 You would use ``lldb A`` to place a breakpoint in the code you're interested in,
277 then ``lldb B`` to trigger ``lldb-server B`` to go into that code and hit the
278 breakpoint. ``lldb-server A`` is only here to let us debug ``lldb-server B``
281 Debugging The Remote Protocol
282 -----------------------------
284 LLDB mostly follows the `GDB Remote Protocol <https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html>`_
285 . Where there are differences it tries to handle both LLDB and GDB behaviour.
287 LLDB does have extensions to the protocol which are documented in
288 `lldb-gdb-remote.txt <https://github.com/llvm/llvm-project/blob/main/lldb/docs/lldb-gdb-remote.txt>`_
289 and `lldb/docs/lldb-platform-packets.txt <https://github.com/llvm/llvm-project/blob/main/lldb/docs/lldb-platform-packets.txt>`_.
294 If you just want to observe packets, you can enable the ``gdb-remote packets``
299 (lldb) log enable gdb-remote packets
301 lldb < 1> send packet: +
302 lldb history[1] tid=0x264bfd < 1> send packet: +
303 lldb < 19> send packet: $QStartNoAckMode#b0
304 lldb < 1> read packet: +
306 You can do this on the ``lldb-server`` end as well by passing the option
307 ``--log-channels "gdb-remote packets"``. Then you'll see both sides of the
310 Some packets may be printed in a nicer way than others. For example XML packets
311 will print the literal XML, some binary packets may be decoded. Others will just
312 be printed unmodified. So do check what format you expect, a common one is hex
315 You can enable this logging even when you are connecting to an ``lldb-server``
316 in platform mode, this protocol is used for that too.
318 Debugging Packet Exchanges
319 **************************
321 Say you want to make ``lldb`` send a packet to ``lldb-server``, then debug
322 how the latter builds its response. Maybe even see how ``lldb`` handles it once
325 That all takes time, so LLDB will likely time out and think the remote has gone
326 away. You can change the ``plugin.process.gdb-remote.packet-timeout`` setting
329 Here's an example, first we'll start an ``lldb-server`` being debugged by
330 ``lldb``. Placing a breakpoint on a packet handler we know will be hit once
331 another ``lldb`` connects.
335 $ lldb -- lldb-server gdbserver :1234 -- /tmp/test.o
337 (lldb) b GDBRemoteCommunicationServerCommon::Handle_qSupported
338 Breakpoint 1: where = <...>
342 Next we connect another ``lldb`` to this, with a timeout of 5 minutes:
348 (lldb) settings set plugin.process.gdb-remote.packet-timeout 300
349 (lldb) gdb-remote 1234
351 Doing so triggers the breakpoint in ``lldb-server``, bringing us back into
352 ``lldb``. Now we've got 5 minutes to do whatever we need before LLDB decides
353 the connection has failed.
357 * thread #1, name = 'lldb-server', stop reason = breakpoint 1.1
358 frame #0: 0x0000aaaaaacc6848 lldb-server<...>
359 lldb-server`lldb_private::process_gdb_remote::GDBRemoteCommunicationServerCommon::Handle_qSupported:
360 -> 0xaaaaaacc6848 <+0>: sub sp, sp, #0xc0
364 Once you're done simply ``continue`` the ``lldb-server``. Back in the other
365 ``lldb``, the connection process will continue as normal.
369 Process 2510266 stopped
370 * thread #1, name = 'test.o', stop reason = signal SIGSTOP
371 frame #0: 0x0000fffff7fcd100 ld-2.31.so`_start
373 -> 0xfffff7fcd100 <+0>: mov x0, sp
380 This section covers reducing a bug that happens in LLDB itself, or where you
381 suspect that LLDB causes something else to behave abnormally.
383 Since bugs vary wildly, the advice here is general and incomplete. Let your
384 instincts guide you and don't feel the need to try everything before reporting
385 an issue or asking for help. This is simply inspiration.
390 The first step is to reduce uneeded compexity where it is cheap to do so. If
391 something is easily removed or frozen to a cerain value, do so. The goal is to
392 keep the failure mode the same, with fewer dependencies.
394 This includes, but is not limited to:
396 * Removing test cases that don't crash.
397 * Replacing dynamic lookups with constant values.
398 * Replace supporting functions with stubs that do nothing.
399 * Moving the test case to less unqiue system. If your machine has an exotic
400 extension, try it on a readily available commodity machine.
401 * Removing irrelevant parts of the test program.
402 * Reproducing the issue without using the LLDB test runner.
403 * Converting a remote debuging scenario into a local one.
405 Now we hopefully have a smaller reproducer than we started with. Next we need to
406 find out what components of the software stack might be failing.
408 Some examples are listed below with suggestions for how to investigate them.
412 * Use a `released version of LLDB <https://github.com/llvm/llvm-project/releases>`_.
414 * If on MacOS, try the system ``lldb``.
416 * Try GDB or any other system debugger you might have e.g. Microsoft Visual
421 * Start a virtual machine running a different version. ``qemu-system`` is
424 * Try a different physical system running a different version.
426 * Remember that for most kernels, userspace crashing the kernel is always a
427 kernel bug. Even if the userspace program is doing something unconventional.
428 So it could be a bug in the application and the kernel.
430 * Compiler and compiler options
432 * Try other versions of the same compiler or your system compiler.
434 * Emit older versions of DWARF info, particularly DWARFv4 to v5, some tools
435 did/do not understand the new constructs.
437 * Reduce optimisation options as much as possible.
439 * Try all the language modes e.g. C++17/20 for C++.
441 * Link against LLVM's libcxx if you suspect a bug involving the system C++
444 * For languages other than C/C++ e.g. Rust, try making an equivalent program
445 in C/C++. LLDB tends to try to fit other languages into a C/C++ mould, so
446 porting the program can make triage and reporting much easier.
450 * Use docker to try various versions of Linux.
452 * Use ``qemu-system`` to emulate other operating systems e.g. FreeBSD.
456 * Use `QEMU user space emulation <https://www.qemu.org/docs/master/user/main.html>`_
457 to quickly test other architectures. Note that ``lldb-server`` cannot be used
458 with this as the ptrace APIs are not emulated.
460 * If you need to test a big endian system use QEMU to emulate s390x (user
461 space emulation for just ``lldb``, ``qemu-system`` for testing
464 .. note:: When using QEMU you may need to use the built in GDB stub, instead of
465 ``lldb-server``. For example if you wanted to debug ``lldb`` running
466 inside ``qemu-user-s390x`` you would connect to the GDB stub provided
469 The same applies if you want to see how ``lldb`` would debug a test
470 program that is running on s390x. It's not totally accurate because
471 you're not using ``lldb-server``, but this is fine for features that
472 are mostly implemented in ``lldb``.
474 If you are running a full system using ``qemu-system``, you likely
475 want to connect to the ``lldb-server`` running within the userspace
478 If your test program is bare metal (meaning it requires no supporting
479 operating system) then connect to the built in GDB stub. This can be
480 useful when testing embedded systems or kernel debugging.
482 Reducing Ptrace Related Bugs
483 ****************************
485 This section is written Linux specific but the same can likely be done on
486 other Unix or Unix like operating systems.
488 Sometimes you will find ``lldb-server`` doing something with ptrace that causes
489 a problem. Your reproducer involves running ``lldb`` as well, this is not going
490 to go over well with kernel and is generally more difficult to explain if you
491 want to get help with it.
493 If you think you can get your point across without this, no need. If you're
494 pretty sure you have for example found a Linux Kernel bug, doing this greatly
495 increases the chances it'll get fixed.
497 We'll remove the LLDB dependency by making a smaller standalone program that
498 does the same actions. Starting with a skeleton program that forks and debugs
499 the inferior process.
501 The program presented `here <https://eli.thegreenplace.net/2011/01/23/how-debuggers-work-part-1>`_
502 (`source <https://github.com/eliben/code-for-blog/blob/master/2011/simple_tracer.c>`_)
503 is a great starting point. There is also an AArch64 specific example in
504 `the LLDB examples folder <https://github.com/llvm/llvm-project/tree/main/lldb/examples/ptrace_example.c>`_.
506 For either, you'll need to modify that to fit your architecture. A tip for this
507 is to take any constants used in it, find in which function(s) they are used in
508 LLDB and then you'll find the equivalent constants in the same LLDB functions
509 for your architecture.
511 Once that is running as expected we can convert ``lldb-server``'s into calls in
512 this program. To get a log of those, run ``lldb-server`` with
513 ``--log-channels "posix ptrace"``. You'll see output like:
517 $ lldb-server gdbserver :1234 --log-channels "posix ptrace" -- /tmp/test.o
518 1694099878.829990864 <...> ptrace(16896, 2659963, 0x0000000000000000, 0x000000000000007E, 0)=0x0
519 1694099878.830722332 <...> ptrace(16900, 2659963, 0x0000FFFFD14BF7CC, 0x0000FFFFD14BF7D0, 16)=0x0
520 1694099878.831967115 <...> ptrace(16900, 2659963, 0x0000FFFFD14BF66C, 0x0000FFFFD14BF630, 16)=0xffffffffffffffff
521 1694099878.831982136 <...> ptrace() failed: Invalid argument
522 Launched '/tmp/test.o' as process 2659963...
524 Each call is logged with its parameters and its result as the ``=`` on the end.
526 From here you will need to use a combination of the `ptrace documentation <https://man7.org/linux/man-pages/man2/ptrace.2.html>`_
527 and Linux Kernel headers (``uapi/linux/ptrace.h`` mainly) to figure out what
530 The most important parameter is the first, which is the request number. In the
531 example above ``16896``, which is hex ``0x4200``, is ``PTRACE_SETOPTIONS``.
533 Luckily, you don't usually have to figure out all those early calls. Our
534 skeleton program will be doing all that, successfully we hope.
536 What you should do is record just the interesting bit to you. Let's say
537 something odd is happening when you read the ``tpidr`` register (this is an
538 AArch64 register, just for example purposes).
540 First, go to the ``lldb-server`` terminal and press enter a few times to put
541 some blank lines after the last logging output.
543 Then go to your ``lldb`` and:
547 (lldb) register read tpidr
548 tpidr = 0x0000fffff7fef320
550 You'll see this from ``lldb-server``:
554 <...> ptrace(16900, 2659963, 0x0000FFFFD14BF6CC, 0x0000FFFFD14BF710, 8)=0x0
556 If you don't see that, it may be because ``lldb`` has cached it. The easiest way
557 to clear that cache is to step. Remember that some registers are read every
558 step, so you'll have to adjust depending on the situation.
560 Assuming you've got that line, you would look up what ``116900`` is. This is
561 ``0x4204`` in hex, which is ``PTRACE_GETREGSET``. As we expected.
563 The following parameters are not as we might expect because what we log is a bit
564 different from the literal ptrace call. See your platform's definition of
565 ``PtraceWrapper`` for the exact form.
567 The point of all this is that by doing a single action you can get a few
568 isolated ptrace calls and you can then fill in the blanks and write
569 equivalent calls in the skeleton program.
571 The final piece of this is likely breakpoints. Assuming your bug does not
572 require a hardware breakpoint, you can get software breakpoints by inserting
573 a break instruction into the inferior's code at compile time. Usually by using
574 an architecture specific assembly statement, as you will need to know exactly
575 how many instructions to overwrite later.
577 Doing it this way instead of exactly copying what LLDB does will save a few
578 ptrace calls. The AArch64 example program shows how to do this.
580 * The inferior contains ``BRK #0`` then ``NOP``.
581 * 2 4 byte instructins means 8 bytes of data to replace, which matches the
582 minimum size you can write with ``PTRACE_POKETEXT``.
583 * The inferior runs to the ``BRK``, which brings us into the debugger.
584 * The debugger reads ``PC`` and writes ``NOP`` then ``NOP`` to the location
585 pointed to by ``PC``.
586 * The debugger then single steps the inferior to the next instruction
587 (this is not required in this specific scenario, you could just continue but
588 it is included because this more cloesly matches what ``lldb`` does).
589 * The debugger then continues the inferior.
590 * The inferior exits, and the whole program exits.
592 Using this technique you can emulate the usual "run to main, do a thing" type
595 Finally, that "thing" is the ptrace calls you got from the ``lldb-server`` logs.
596 Add those to the debugger function and you now have a reproducer that doesn't
597 need any part of LLDB.
602 See :doc:`/resources/test`.