I can’t leave the subject of GDB alone for too long, so today I thought I’d talk about GDB init files.
Each time GDB is run, it checks first your home directory and then the current directory for the existence of a file called .gdbinit. If it finds this file, it reads the contents and runs any commands it finds there.
Note: as of version 7.5 GDB needs permission to load files from anywhere other than your home directory. See this update for further details.
This is extremely handy if you are repeatedly debugging an executable and don’t want to keep typing in the same old commands at startup.
For example, if you always put a breakpoint in a certain method because you like it as a starting point, you can specify this in the .gdbinit file, exactly as you would on the command line for GDB:
And if you just can’t be bothered to keep typing ‘r’ to run GDB once it’s loaded your program, you can specify that too:
b main r
You can also add any arguments that you repeatedly type in:
set args param1 param2 b main r
And any configuration settings that you might want:
set prompt debug--> set args param1 param2 b main r
Now all you have to do is run GDB on your exe as usual, but now it will set up a custom prompt, pass in your arguments, set a breakpoint in main, and even start the program for you.
Next thing you know, you’ve got a GDB prompt at the top of the main function and you’re ready to go!