Reading gdb like a map
The Python side of this visualizer was easy in one specific way: the interpreter will tell you anything you ask. Frames, locals, object types, what refers to what. You walk its own data structures and write them down.
C++ has no such thing. There is no interpreter to ask, no runtime registry of live objects, no way to say "give me everything reachable from this local". The only thing in the system that knows where the values are is the debugger, and the debugger knows it in the least friendly form possible: addresses, types, and the raw bytes at those addresses. So for C++ the tracer does not introspect the program. It runs inside gdb.
The shape of the thing
The worker is a Python script that gdb loads:
gdb -nx -batch -q -x cpp_gdb_worker.py
It steps the program one source line at a time with gdb's own step, and after every line it writes one snapshot: stack frames, the variables in each frame, and the objects reachable from them. That snapshot is the same JSON shape the Python tracer produces, so the browser does not care which language it is looking at.
The format is frames, locals and a heap, and a C++ program fits in it as well as a Python one does.
The cost of asking gdb instead of an interpreter shows up in the timings from the test report:
| Program | Events | Compile | Trace | Per event |
|---|---|---|---|---|
| loop, n=20 | 45 | 62 ms | 206 ms | 4.59 ms |
| loop, n=100 | 205 | 63 ms | 327 ms | 1.59 ms |
| loop, n=400 | 805 | 62 ms | 570 ms | 0.71 ms |
Fixed start-up is about 0.2 s of tracing plus 60 ms of compiling, and the marginal cost settles at roughly 0.3 to 0.4 ms per event. For comparison, stepping through a finished recording in the browser costs a fraction of a microsecond, which is the whole argument of the previous post: tracing is expensive, replay is cheap.
A map, not an answer
The useful way to think about gdb is that it hands you a map with coordinates on it. It tells you an address, a type name, a byte count. It does not tell you which objects are alive, which ones are the same object, or that this pointer used to point at something that has since been freed. Those are the parts the worker has to build, and each one is a small lesson in how C++ actually works.
Identity is an address, and an address has no type. Two pointers are the same object when their addresses match, so addresses become node ids. But the address says nothing about what lives there, and by the time you look, the typed pointer that created it may be gone. The worker keeps a table from allocation to the last type it saw through a typed pointer. A block it has never seen through a typed pointer cannot be read as anything, so it is left uncovered and only counted; the snapshot reports how much heap it never reached instead of inventing a shape for it.
Freed is observed, not inferred. You cannot ask a C++ program whether a block is still alive, because that question has no answer. Instead the worker sets breakpoints on operator new, operator delete, malloc and free after main starts, and marks a block freed when one of them is hit. That is why a use-after-free can be drawn at all: the node stays in the diagram, marked freed, with a note that a pointer to it is now dangling, and the read that follows points straight at it.
The same accounting has a second half. Live memory that no pointer leads to any more is still real memory, so a block whose type was seen earlier is read again and flagged unreachable: a leaked object shows up in the picture instead of quietly vanishing. Freed memory is never read, because reading it would be the very bug the tool is trying to show.
Types come from the standard library, where they exist. std::string and std::vector are rendered with libstdc++'s own pretty printers, which is the difference between a string showing "hello" and showing three pointers and a size. Everything else in the STL is opaque: a std::map, a std::list, a unique_ptr shows up as a box with a type name and nothing inside. That is a real limitation, not a bug being hidden, and it is listed in the project's C++ limits document next to the rest.
The lesson I did not expect
The single biggest tracing cost was not stepping, and it was not reading memory. It was os.path.realpath. Every time the worker considers a symbol it asks "is this symbol in the user's source file?", and the honest way to answer is to resolve both paths and compare. On a 278-event run that came to about 54,000 calls, and it was 65 percent of the tracing time. Caching the answer by file name took the run from unusable to normal.
The general lesson is worth more than the specific fix: in code that runs per symbol, per variable, per event, a syscall is not a detail, it is the whole cost. You cannot see this by reading the code, only by measuring it, and I only found it because the run felt slow enough to time.
What the map leaves out
Two things are worth being blunt about, because they are the kind of thing a screenshot of a debugger never tells you.
First, the program runs twice. Once under gdb for the recording, and once natively with AddressSanitizer, UndefinedBehaviorSanitizer and LeakSanitizer, in parallel, in separate empty folders, with the same input. The sanitizers catch memory errors that stepping cannot see, and stepping catches the shape of the run that the sanitizers never show you. The warning about the double run sits next to the Run button, because a program that writes to an absolute path or talks to the network is doing it twice.
Second, there are things neither run can tell you. Uninitialised reads are not detected. A wrong answer is not detected. A std::map is a box with a name on it. The visualizer shows you what happened, and it is honest about the boundary: 32 frames, 300 heap nodes, 40 items per node, 40 locals, 2,000 events, 10 seconds. A crash keeps the trace up to the crash, which is usually the most interesting part.
The lesson that transfers
When a language has no introspection, the debugger is the only map you get. Borrowing it works, but you inherit its worldview: addresses instead of objects, breakpoints instead of liveness, type names instead of structure. Building the missing meaning on top of that map is most of the work, and being explicit about what the map cannot show is the rest of it.
Numbers are from the project's C++ test report, on one Linux machine (Ubuntu 22.04, g++ 11.4, gdb 12.1), on synthetic programs. Windows, macOS and Apple Silicon are not verified; on Windows this needs WSL.
Comments
No comments yet. Start the discussion.