One day I noticed that my GDB autocomplete stopped working. I had a hunch my recent update had broken something in GDB, so I decided to investigate the internals of GDB autocomplete.
Introducing readline
Readline (https://en.wikipedia.org/wiki/GNU_Readline) is an open source library that provides utilities for interactive terminal programs. Readline provides functionality like history, autocomplete, line editing, vi/emacs modes, etc.
Readline in GDB
The code shown above (gdb/completer.c
) is where GDB initializes readline and sets up the completion function. All of the rl_*
variables are global variables that exist inside the readline shared library. This means that if multiple different systems in the same program try to use readline at the same time, they will override each other.
Debugging GDB
How do you debug GDB? With another instance GDB of course!
Using gdb -ex 'set follow-fork-mode parent' -ex 'b _initialize_completer' --args /usr/bin/gdb
to debug GDB with GDB. We can see that _initialize_completer
is called successfully, so autocomplete is initialized properly. That means somewhere after autocomplete initialization something else is overriding GDB’s autocomplete.
Printing out the value of rl_attempt_completion_function
shows that it has been overriden:
WTF is flex_complete
and where is it from?
So turns out that it is an issue with python. Some random module is loading the python readline module and overriding the GDB autocomplete. It was at this point that I stumbled across a recent pwndbg
github issue discussing the exact problem I had been facing.
https://github.com/pwndbg/pwndbg/issues/2232
Turns out that GDB already has a mechanism to disable python readline (here: https://github.com/bminor/binutils-gdb/blob/62e4d4d3ad68fe17113069b99d80a9ee9df87cb1/gdb/python/py-gdb-readline.c#L98-L111), but updating to python 3.13 broke this mechanism and messed up autocomplete.
To restore proper GDB autocomplete I have added a line to the top of my .gdbinit
that sources a script to disable readline again: