Toolkit: Debugging & Inspection
The tools for finding out what a Make or Ninja build is doing, collected in one place. Most come with Make and Ninja themselves and were introduced in earlier lessons; the others are small extras worth installing.
Built into GNU Make
| Command | Use it for | Lesson |
|---|---|---|
make -n / make -nB |
print the commands of an incremental / a full build without running them | Debugging Makefiles |
make --trace |
why each recipe runs, with file:line | same |
make --debug=b / =v / =i / =j / =m |
basic reasoning / every target / implicit-rule search / jobs / remade makefiles | same |
make -p -q, make -p -f /dev/null |
the database: final variable values with origins, all rules, built-ins | same |
make -q |
exit status 0 when up to date; good for scripts | same |
make --warn-undefined-variables |
catch misspelled variables | same |
$(info), $(warning), $(error) |
print or stop while the Makefile is read | Variables |
print-%: ; @echo '$*=$($*)' |
a target that prints any variable: make print-CFLAGS |
Debugging Makefiles |
Built into Ninja
| Command | Use it for | Lesson |
|---|---|---|
ninja -d explain |
why each edge runs | The Build Log |
ninja -n -v |
print the commands without running them | below |
ninja -d keepdepfile |
keep the compiler's .d files to inspect them |
below |
ninja -d stats |
how long Ninja itself spent loading and checking (not the commands) | |
ninja -t query/commands/inputs/targets/deps/graph |
explore the graph | Ninja's Built-in Tools |
ninja -t missingdeps |
find outputs that use a generated file without a declared dependency path | Header Dependencies |
ninja -t browse |
a small local web page to click through the graph (needs Python) | |
.ninja_log + ninjatracing |
turn the build log into a Chrome/Perfetto timeline | The Build Log |
-n -v and keepdepfile in action:
bash · try it yourself
mkdir -p nd && cd nd
printf '#include "a.h"\nint main(void) { return A; }\n' > main.c
printf '#define A 0\n' > a.h
printf 'rule cc\n command = gcc -MMD -MF $out.d -c $in -o $out\n depfile = $out.d\n deps = gcc\n description = CC $out\nbuild main.o: cc main.c\n' > build.ninja
ninja -n -v
ls
ninja -j1 -d keepdepfile
cat main.o.d[1/1] gcc -MMD -MF main.o.d -c main.c -o main.o
a.h
build.ninja
main.c
[1/1] CC main.o
main.o: main.c a.h
The dry run created nothing (not even .ninja_log), and with keepdepfile the dependency file survived Ninja reading it.
Watching the processes: strace
When a build does something you can't explain (picks up the wrong compiler, reads a header from an unexpected directory), ask the kernel what really ran. strace -f -e trace=execve logs every program started by the build and its children:
bash · try it yourself
mkdir -p st && cd st
printf 'int main(void) { return 0; }\n' > main.c
printf 'app: main.c\n\t@$(CC) -O2 lt; -o $@\n' > Makefile
strace -f -q -e trace=execve -o trace.txt make -j1
grep ' = 0