TExcelTAD
Home › GNU Make & Ninja › Toolkit: Debugging & Inspection

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 trace.txt | grep -o 'execve("[^"]*"' | sed 's/execve(//' | sort | uniq -c
      1 "/usr/bin/as"
      1 "/usr/bin/cc"
      1 "/usr/bin/ld"
      1 "/usr/bin/make"
      1 "/usr/libexec/gcc/x86_64-linux-gnu/13/cc1"
      1 "/usr/libexec/gcc/x86_64-linux-gnu/13/collect2"

make ran cc directly (a recipe without shell syntax is executed without /bin/sh), which ran the real compiler cc1, the assembler and, via collect2, the linker. Change trace=execve to trace=openat and filter for .h to see every header the compiler opened. (strace is Linux-only; on macOS use dtruss or Instruments.) The Build Fundamentals course has more on these tools.

remake: a debugger for Makefiles

remake is a fork of GNU Make with better error reporting and an interactive debugger: breakpoints on targets, stepping through recipe lines, printing and changing variables mid-build, and a stack trace of the target chain when something fails. It's packaged in Debian and Ubuntu (apt install remake) and on Homebrew; it isn't installed in this course's image, so here are the commands without a recorded session:

bash
remake --trace            # like make --trace, plus the full target chain on errors
remake -X                 # start the debugger before reading the Makefile
remake -X app             # debug the build of target "app"
# Inside the debugger:
#   break main.o          stop when main.o is about to be considered
#   continue / step / next
#   print CFLAGS          show a variable's value and origin
#   target main.o         show a target's prerequisites, recipe and state
#   where                 the chain of targets that led here
#   quit

When an error happens deep in a recursive build, remake's "target stack" (which target needed which) often explains it faster than any amount of --debug output.

Other helpers

Tool What it does
make2graph (makefile2graph) turns make -Bnd output into a Graphviz graph of the real dependency tree
Graphviz dot renders ninja -t graph and make2graph output: dot -Tsvg graph.dot > graph.svg (run in Ninja Tools)
ninjatracing .ninja_log → Chrome trace JSON
ccache -s shows cache hits when a build is wrapped with ccache (see Build Performance)
bear, compiledb produce compile_commands.json for Make builds (next lesson)

Test yourself

Which command prints Ninja's commands without running them?

  1. ninja -d explain
  2. ninja -n -v
  3. ninja -t clean
  4. ninja -d stats

What does strace -f -e trace=execve make show?

  1. Make's variables
  2. Every program the build starts, including compiler subprocesses
  3. Only failed recipes
  4. The build log

What does remake add to GNU Make?

  1. A faster scheduler
  2. An interactive debugger with breakpoints on targets and better error traces
  3. Ninja output
  4. Support for Windows