Debugging
There are two ways to debug a program in a machine. Machine’s own debugger
works from outside the machine: machine regs, mem, step, break
and the rest read and change a task while the machine is stopped, and they
work on any program, including one with no debugger on its disk. GDB works
from inside: install it on the disk, attach it with gdb -p, and use it as on
any Linux host.
Both are recorded. A replay repeats every step, every write and every stop, and a checkpoint taken before you start debugging lets you debug again from the same state.
Machine’s debugger
Each command takes the run and a task by its tid, which
machine tasks lists. A task’s tgid is its
process. The commands work while the machine is stopped, between two
machine run commands, on a task that is on a vCPU or off one.
Symbols, types and source lines come from the program files on the machine’s disk: the program’s symbol table, and its debug information when it has some.
Debug information
What the debugger shows depends on what the program’s files on the disk hold:
| On the disk | What you get |
|---|---|
| The symbol table alone | Function names in a backtrace, a stop at a function by its name, registers, memory, and a global at its symbol’s address |
| Debug information too | Source lines, a stop at FILE:LINE, line steps, arguments in a backtrace, print and locals by name, and a watch of a variable by its name |
Here is frame 1 of the same stop in pg_parse_query, first in PostgreSQL as
it ships, then with its debug package installed:
1 | 1 0x56365898a1af ?? |
A program built with -g has its debug information in itself. A
distribution ships it separately, in a package such as -dbg, -dbgsym or
debuginfo, which puts a debug file under /usr/lib/debug. Install the
package of the program’s exact build in the image the disk is built from:
the debugger takes a debug file only when its build id is the program’s, and
finds it in any layer of a container’s image.
A server ships built with optimization. Its arguments and locals are where
the compiler kept them at each instruction: on the stack, in registers, or in
pieces of both, as clang keeps a C++ std::string_view in two registers. A
variable the compiler kept nowhere shows as optimized out.
The debugger names a C++ function by its short name, such as
DB::executeQuery. machine --signatures names it by its whole signature.
Without debug information, read the arguments from the registers the ABI passes them in, and give a global a type with a cast:
1 | machine mem app 412 '$rsi' s # the second argument, a string |
The debug examples build ClickHouse and PostgreSQL with and without debug information, and debug each build.
Locations
A location names an address in the task’s process. Every command that takes a location accepts any of these forms:
| Location | What it names |
|---|---|
0x401136 |
an address |
*0x401136 |
the same address, written with a * |
main |
a function or a variable, by its name |
libc.so.6:malloc |
a name in one file of the process, by its path or its file name |
init.c:106 |
a source line, at its first code |
$rsp |
a register of the task, as it is now |
main+4, $rsp-8 |
any of these, moved by +N or -N |
When the program has debug information, a breakpoint at a function by its name is placed past its prologue. Without it, the breakpoint is at the function’s first instruction.
Registers and memory
1 | machine regs app 412 # the general registers of tid 412 |
machine mem shows memory in the format given
after the location: a count, a letter and a unit, such as 8xg, 4i or s:
1 | machine mem app 412 '$sp' # eight giant words at the stack |
A write is part of the run, and a replay makes it again. A page the tgid has
not written yet is still shared, so a write to it is refused. When two files
of the program define the same name, that name is refused, and the message
lists each file to qualify it with, such as /lib/libc.so.6:optind, by its
path in the container as machine symbols lists it.
^= flips the bits a mask sets, at the variable’s width or at a unit’s, such
as machine mem app 412 '$sp^=0x1' b. It reads, flips and writes the value
with the machine stopped, and prints the value before and after.
The stack, symbols and variables
1 | machine backtrace app 412 # the stack, the innermost frame first |
print takes a variable of the frame, a global or a $register, with .m,
->m, [i], *, & and casts, and prints it by its type. print and
locals need the program’s debug information.
Stepping
machine step steps one task. The machine runs
whichever task the machine’s kernel schedules, so other tasks run meanwhile:
1 | machine step app 412 # one instruction, as stepi |
A step of an instruction that faults, such as a write to a copy-on-write page, ends on the next instruction.
Breakpoints, watchpoints and catchpoints
A breakpoint stops the run when a task of its tgid reaches its location. A watchpoint stops it after an instruction writes the bytes it watches. A catchpoint stops it at an event of a task: an exec, an exit, a fork or vfork, a system call, a signal, a file mapped or unmapped, an injected fault, a disk request or a C++ exception.
1 | machine break app add 412 main # stop tgid of 412 at main |
watch add takes --on write, change, read or access, and --size for
a location that is not a variable. An exit catchpoint takes --in tgid=N,
since an exit is the end of a process. A throw, catch or rethrow
catchpoint needs --in tgid=N.
--in comm=NAME stops at the events of the tasks with that name, the same
tasks machine tasks app exist comm=NAME
lists. This catches a busybox applet, or a script, before its ids are known. An exec catchpoint sees the new program’s name. An exit catchpoint
sees the name of the process’s main thread, which ends last, so a worker
thread named cat inside a differently named process does not stop it. A
syscall catchpoint with --in comm=NAME makes the machine report every
task’s calls while it is set, which costs time on a busy machine.
Each stop has a number, and the same verbs change any kind:
1 | machine points app # every breakpoint, watchpoint and catchpoint |
With no verb, machine break, machine watch and machine catch each list
the stops of their own kind.
A run that reaches a stop ends there and says which. The run log keeps the stops: a branch has the ones its parent had at its fork point, and a replay applies the adds and deletes. The stops are armed while the run records, and not while it replays.
Running to a place
1 | machine run app --iters 0 # until the next hit, as continue |
tid=N:at=LOC and tgid=N:at=LOC take any location. write=LOC and
access=LOC take a variable or an expression, watched at its size of 1, 2, 4
or 8 bytes. A write the kernel makes into the program’s memory counts too,
such as the data the read system call copies into its buffer. The address is resolved when the run starts, and the stop
lasts for that run alone.
The run says the tid it stopped in and that task’s location. A watch also says the value the access left in the bytes:
1 | ✔ Written counter at main+116, watched.c:63, in tid 191 |
Going back
Going back puts you on a new branch, at an earlier point of the run, and leaves the run as it was:
1 | machine run app --back # onto a branch at the newest hit, as reverse-continue |
The branch keeps the run’s stops as they are now.
Calling, returning and signals
1 | machine call app 412 square 3 # call a function in the task, as call |
call puts the task’s registers back after the call. Each of these is part of
the run, and a replay makes it again.
At the control prompt
Every command here has a slash command at the
control prompt, with the run left
out: /regs 412, /mem 412 '$pc' 4i, /step 412 --line, /break add 412 main, /run until 'tid=412:at=main'. The prompt also has /display, which
prints an expression after each run and step, and /skip, which names a
function or file that every line step at the prompt steps out of:
1 | /display 412 counter # printed after every /step and /run |
/help lists them all.
From Python
The Python SDK has the same debugger: run.task(tid) gives a
task with registers(), read(), backtrace(), print(), step(),
breakpoint() and watch(), and run.breakpoints lists and changes the
stops.
How many stops a run holds
A run holds 64 breakpoints. The machine puts each one into the tgid’s own copy
of its code as an int3 instruction. Every thread of the tgid stops at it,
but another process that maps the same library does not. A read of the task’s memory, with
mem or the instruction a step shows, shows the program’s byte, not the
int3. A write over that byte is refused while the breakpoint is there.
The throw, catch and rethrow catchpoints are breakpoints too. The other
catchpoints are events the machine reports, and a run holds 8 of them.
A run has 3 debug registers. These take one each:
- a watchpoint of
machine watch add - a breakpoint added with
--hardware - the stop of
run --until tid=N:at=LOC,write=LOCoraccess=LOC, for that run - the stop of a step:
step --overandstep --outtake one, and two in a C++ program, where the second stops at a catch the exception reaches - a hardware breakpoint or watchpoint of GDB inside the machine,
hbreakorwatch
When the 3 are taken, the next is refused. A machine watch add refused while
GDB holds some says how many:
1 | ✗ No room: a run holds 3 hardware breakpoints and watchpoints. The debugger in the guest holds 2. |
A step of one instruction takes no debug register, even a step from a breakpoint’s hit. The one exception is a task that returns into a signal handler: the step stops where the handler returns, and takes a debug register for that stop.
A breakpoint in a library every process maps
A breakpoint at malloc stops one process, though every process of the
machine calls libc’s malloc. The other processes keep running, and the
breakpoint does not slow them:
1 | $ machine break app add 218 malloc |
A breakpoint in a library the program loads later is placed when it loads, and again each time it is loaded after being unloaded.
Many breakpoints at once
A pattern adds a breakpoint at each function that matches. For example, 38 of libc’s string functions:
1 | machine break app add 251 --regex '^str[a-n]' |
A pattern that matches more functions than the run has room for adds none, and says how many it matched and how many the run holds:
1 | ✗ 79 functions match: strcasecmp, strcasecmp_l, strcasestr, strcat, strchr and 74 more. The run holds 64 more breakpoints. |
Threads
Every thread of the tgid stops at a breakpoint, each with its own tid. For example, two worker threads that run the same function:
1 | $ machine break app add 251 worker_tick |
A child process that the process forks or vforks is another tgid and does not stop, though it shares the parent’s copy of the code until it writes.
When to use --hardware
A software breakpoint is the right one almost always. --hardware is for the
cases an int3 in the program cannot cover:
Code in memory the program allocated, as a JIT writes. The listing says the breakpoint is not placed, with the command that puts it in a register:
1
2
3
4$ machine break app
# KIND ADDRESS SIZE WHERE TGID HITS WHEN
1 breakpoint 0x7f35d1580000 1 *0x7f35d1580000 251 /usr/bin/bpprobe 0 not placed: the address maps no file
Put it in a debug register machine break app add 251 '*0x7f35d1580000' --hardwareCode the program reads as data. The program sees the
int3, so a checksum of its own code changes, for example. A hardware breakpoint changes no byte.An instruction the kernel does not let a breakpoint sit on, such as
syscall. The listing saysnot placed: the kernel refused the instruction.
A hardware breakpoint lists as hw breakpoint and takes one of the 3 debug
registers.
When the 3 debug registers are taken, a GDB hbreak or watch is refused
too, and GDB says Couldn't write debug register: No space left on device at
the continue that inserts it. An x86 processor has 4 debug registers, so a
GDB session that uses all 4 on a host gets only 3 in the machine.
GDB inside the machine
GDB runs in the machine as it does on any Linux host: breakpoints, stepi,
next, finish, hbreak, watch and detach.
Getting GDB onto the disk
Put GDB in the image the disk is built from. For a server in a container, put it in the server’s image, so GDB sees the server’s files:
1 | FROM clickhouse/clickhouse-server:26.8.31.2 |
GDB reads the program’s symbol table for function names. For arguments, locals and source lines it needs the program’s debug information too, in a format it reads. ClickHouse’s debug information is DWARF 5, which GDB 12 and later read. An older image’s GDB may not: GDB 9.2, Ubuntu 20.04’s, does not.
GDB holds what it reads in memory, and the machine needs room for it beside the
program. ClickHouse’s 1.7GB of debug information grows to about 3.8GB in GDB,
so that machine needs --mem 12288; with 4GB its out-of-memory killer ends
GDB.
The ClickHouse example builds both kinds of image for ClickHouse 26.8: one with the symbol table alone, and one with the debug information. Its server prints the commit it was built from as it starts, with a link to that source on GitHub, and the example shows how to put a source file where GDB finds it.
A batch of commands
The example’s README
builds the example’s server image. Build a disk from that image, and create
a run on the disk:
1 | machine disks build --image clickhouse-server:26.8.31.2 clickhouse-server |
Run the machine to the point you want to debug, and take a checkpoint there.
Then run GDB with machine exec. Here the
example’s server prints clickhouse: ready once it answers queries:
1 | machine run ch --until 'console~clickhouse: ready' |
1 | [New LWP 78] |
exec run runs the machine until GDB exits, and prints what GDB printed.
-batch makes GDB exit after its commands, and an attached process is
detached as GDB exits. A file of commands works the same way, with -x FILE.
-c gives the line to the container’s shell, which expands
$(pidof clickhouse-server) to the server’s process id.
ClickHouse signals its own threads with SIGUSR1 and SIGUSR2. Without the
handle line, continue stops at the first of those signals instead of at
the breakpoint, and bt shows whichever thread got it.
At the control prompt the line after -c needs no outer quotes:
1 | /run until 'console~clickhouse: ready' |
Use --user to run GDB as the user of the process it attaches to. A
container has no CAP_SYS_PTRACE by default, so root in it cannot attach to
another user’s process.
Keeping GDB attached
-batch runs a list of commands chosen in advance. To decide the next
command from what GDB just printed, keep GDB attached and give it its
commands through a named pipe in the container.
This works on the server’s own image with GDB added, the example’s server
image: ClickHouse 26.8 with no debug information. GDB finds functions by
name in the program’s symbol table, and attaches in about 16 seconds. With
the debug information of the debug image it takes minutes instead.
Make the pipe, keep it open, and start GDB with its input on it.
exec start returns at once and prints the id of the process it started:
1 | machine exec ch run --container app --user clickhouse -- mkfifo /tmp/gdb.in |
1 | ✔ Sent 95916p2 to ch (whvR8O5Y1otCgBTbNi3rc) |
The sleep holds the pipe open. Without it, GDB reads the end of its input
after the first command and exits.
Every step below is the same three commands. exec start queues a printf
that writes the step’s GDB commands to the pipe, the last of them an echo
of a marker. run --until runs the machine until GDB prints that marker,
which is when GDB has done every command before it. proc reads what GDB
printed, by the id exec start gave:
1 | machine exec ch start --container app --user clickhouse -c 'printf "%s\n" "handle SIGUSR1 SIGUSR2 nostop noprint" "echo step-1\n" > /tmp/gdb.in' |
1 | Attaching to process 7 |
The handle line keeps continue from stopping at ClickHouse’s own signals,
as in the batch session. This first step waits for GDB to read the program’s
symbols, 10 seconds here. The others take a second or two.
Four rules keep the steps reliable:
- Each step has a marker of its own.
run --untilmatches only text printed after that command starts, so thestep-1printed earlier never ends a later run. - Use
exec start, notexec run, to write to the pipe.exec runruns the machine until theprintfexits, and GDB may print the marker during it. Therun --untilafter it then waits for a marker that is already there. printf "%s\n"writes each argument as a line and leaves\nin it as it is, so GDB’sechoends the marker’s line. A$in a GDB command is written\$inside the double quotes, as"x/s \$rsi"below.--within 60sbounds a step whose marker never comes. Without it a mistyped marker runs the machine until it is cancelled.
Each read shows the whole session so far. The output below is what each step added.
A breakpoint. GDB writes an int3 into the program, as on any host, and
uses no debug register:
1 | machine exec ch start --container app --user clickhouse -c 'printf "%s\n" "break DB::executeQuery" "info breakpoints" "echo step-2\n" > /tmp/gdb.in' |
1 | (gdb) Breakpoint 1 at 0x1d386340 (3 locations) |
Continue to it. The example’s server sends itself a query every second,
and a thread that handles it stops. GDB reads the marker after c stops, so
the marker follows the hit:
1 | machine exec ch start --container app --user clickhouse -c 'printf "%s\n" c "echo step-3\n" > /tmp/gdb.in' |
1 | (gdb) Continuing. |
Every later continue works the same way. Each hit prints a new
hit Breakpoint line, so --until '/output/exec/95916p2/stdout~hit Breakpoint'
stops at the next hit too.
Read the query. executeQuery returns its result by value, so rdi
holds where to put it, and the query follows in rsi and rdx, a pointer
and a length:
1 | machine exec ch start --container app --user clickhouse -c 'printf "%s\n" "info registers rsi rdx" "x/s \$rsi" "echo step-4\n" > /tmp/gdb.in' |
1 | (gdb) rsi 0x7f8482a32cc0 140207104142528 |
Instructions and single steps:
1 | machine exec ch start --container app --user clickhouse -c 'printf "%s\n" "x/4i \$pc" stepi "nexti 3" "echo step-5\n" > /tmp/gdb.in' |
1 | (gdb) => 0x1d386340 <_ZN2DB12executeQuery...>: jmp 0x1d38634b <_ZN2DB12executeQuery...+11> |
The stack, and running to the return:
1 | machine exec ch start --container app --user clickhouse -c 'printf "%s\n" "bt 3" finish "echo step-6\n" > /tmp/gdb.in' |
1 | (gdb) #0 0x000000001d386350 in DB::executeQuery(...) () |
A hardware breakpoint. Delete the first breakpoint, and put one at the
address finish returned to. It takes one of the run’s 3 debug registers,
and stops where the next query returns:
1 | machine exec ch start --container app --user clickhouse -c 'printf "%s\n" "delete 1" "hbreak *0x1ed12ec3" c "echo step-7\n" > /tmp/gdb.in' |
1 | (gdb) (gdb) Hardware assisted breakpoint 2 at 0x1ed12ec3 |
delete prints nothing, so the output starts with two prompts.
A hardware watchpoint. It takes another debug register, and stops after the next call writes its return address below the stack pointer:
1 | machine exec ch start --container app --user clickhouse -c 'printf "%s\n" "watch -l *(long *)(\$rsp - 8)" c "echo step-8\n" > /tmp/gdb.in' |
1 | (gdb) Hardware watchpoint 3: -location *(long *)($rsp - 8) |
A system call. Delete every breakpoint and watchpoint, stop at the next
sendto, and read its socket, its length and the first bytes it sends.
GDB asks before it deletes them all, and answers itself, since its input is
not a terminal:
1 | machine exec ch start --container app --user clickhouse -c 'printf "%s\n" delete "catch syscall sendto" c "info registers rdi rdx" "x/16xb \$rsi" "echo step-9\n" > /tmp/gdb.in' |
1 | (gdb) Delete all breakpoints? (y or n) [answered Y; input not from terminal] |
That is the query’s result going back to the client, 34 bytes on socket 58.
Leave. Delete the catchpoint, detach and quit. GDB prints no marker after
quit, so the run waits for the process’s status file to say it exited:
1 | machine exec ch start --container app --user clickhouse -c 'printf "%s\n" delete detach quit > /tmp/gdb.in' |
1 | (gdb) Delete all breakpoints? (y or n) [answered Y; input not from terminal] |
GDB has exited, and the server carries on.
The session is recorded with the run. Rewind to the checkpoint taken before it, and the same commands give the same answers.
From the checkpoint
The checkpoint holds the machine as it was before GDB attached. Rewind to it, and the next session starts from the same instruction:
1 | machine rewind ch to ^ready |
Or fork a branch at it and keep both:
1 | machine fork ch --at ^ready --name again |
ClickHouse is a registered trademark of ClickHouse, Inc. See clickhouse.com.