Debugging a C++ Database Kernel with GDB Alone
Why use GDB directly
IDEs are often enough for debugging Python, Java, or Go. C++ database kernels are different: their debug toolchains can be elaborate, and an IDE must also parse GDB state into its UI. That extra layer can be slow or fragile. For C++ work, a command-line GDB session is often faster and more predictable. This article walks through one such debugging session and the commands that mattered.
Prerequisites
Build with debug symbols
Compile C++ in debug mode. Otherwise the compiler may optimize away the symbols needed by the debugger.
-DCMAKE_BUILD_TYPE=Debug # using cmake
g++ -g -O0 -o program main.cpp # without cmake
Set LD_LIBRARY_PATH
GDB must be able to locate the required shared libraries.
The shared libraries are loaded from the directories in LD_LIBRARY_PATH.
The failing test
While debugging a Milvus gtest case, I hit a failure in a CompareExpr. The expression compared values from two varchar columns and counted equal rows:
SELECT COUNT(*) FROM table t WHERE t.str_1 = t.str_2;
The test inserted 20,000 matching values into a segment, so it expected 20000 = 20000.
Problem 1: a crash
The first failure was a SEGV, the most direct kind of C++ debugging problem. Enable core dumps, load the core in GDB, and inspect the failing stack.
On some newer Ubuntu releases,
ulimitalone is not sufficient to produce a core file. Configurekernel.core_patternand disableapport.serviceas needed. Enabling core dumps on macOS requires a different setup; see Writing Core Dump Files on macOS.
ulimit -c unlimited # enable core dump file
gdb 'path/to/executablefile' 'core.file' # load core file
bt # print the stack trace at the crash
f 2 # switch to the second stack frame
info locals # print local variables in the current frame
print var1 # print a local variable
step # step into the next instruction
c # continue to the next breakpoint
The cause was simple: the gtest fixture marked the column as nullable=True but did not supply a null bitmap.
Problem 2: a misaligned bitmap and silent memory corruption
After adding a null bitmap to the Arrow string arrays, the crash disappeared, but the test still failed. Only 3,145 pairs compared equal.
Expected equality of these values:
chunk_num * test_data_count
Which is: 20000
final.count()
Which is: 3145
There was no obvious clue, so I followed the execution path and inspected runtime state. For a query such as SELECT COUNT(*) FROM table t WHERE t.str_1 = t.str_2;, two iterators scan the data block—either in memory or through mmap.
Two details matter here:
- The database kernel does not record offsets for null strings; rows with a null bit of
0are skipped. - A non-null value can be compared with any other value.
Both columns had all-valid null bitmaps, so the second rule should not have explained the mismatch. I set a conditional breakpoint at i = 1249, immediately before the 1,250-byte boundary of the bitmap I had created:
break /XXXXX/XXXXX/milvus/internal/core/src/exec/expression/CompareExpr.cpp:138 if i >=1249
At i = 1249, the values were valid.
Continuing to the next loop iteration showed invalid left_value and right_value at i = 1250.
The bitmap had been created as a 1,250-byte vector with every bit set, so the boundary failure pointed to my assumption about how Arrow consumed the input. Stepping into Apache Arrow showed that it treats the input uint8_t array as uncompressed: every non-zero uint8_t represents one valid bit. The input therefore needed one byte per row, not one packed bit per row. valid_bytes needed a length of 10,000 rather than 1,250.
The test ran at all because this was silent memory corruption. Without Address Sanitizer, that kind of error can easily remain hidden. For an ASan walkthrough, see Debugging Database Kernels with ASan.
Problem 3: an optimized-away bitmap
After correcting the input bitmap length, the gtest failed again: the null bitmap had become NULL.
Following Arrow's code led to AdjustNonNullable. When null_count is zero—which means no rows are null—Arrow replaces the null bitmap with nullptr to save storage. Handling that boundary case fixed the test.
Takeaway
Direct GDB is enough for the essential C++ debugging loop: inspect values, set targeted breakpoints, and control execution. In this case, that loop separated three problems that initially looked like one failing assertion: a missing bitmap, a byte-versus-bit representation error, and a null-bitmap optimization in Arrow.
— MrPresent-Han





