GDB Init Files Save Time At Startup

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:

b main

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

You can also add any arguments that you repeatedly type in:

set args param1 param2
b main

And any configuration settings that you might want:

set prompt debug-->
set args param1 param2
b main

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!

5 thoughts on “GDB Init Files Save Time At Startup”

  1. Hi Faye,
    Congrats on the new addition.
    Suggestion for a future article:
    Using gdb via jtag (via say openocd) to debug remote bare metal embedded cpus that don’t have the memory or networking for hosting gdb client.

    • Thanks Paul! Took me a while to see these comments – as you can imagine it’s pretty chaotic at home right now (and the lack of sleep is v tiring). GDB via jtag: I do love a challenge 🙂 If I get the chance I’ll see what I can do!

  2. Can I specify this file through a Sourcery CodeBench workspace? If so, where? In the project(s) where I am debugging?

  3. Hi,
    Thank you for your GDB posts 😉

    FYI: I had to put in my ~/.gdbinit
    “set auto-load safe-path /”
    to enable loading of current directory .gdbinit file.
    Maybe it’s related to the GDB version (GNU gdb (GDB) 7.5.1 – 2012 in my case).
    No big deal at all, as GDB kindly indicates that in boot logs.

    • Thanks Laurent – you are absolutely right. This has been in place since August 2012 (version 7.5 onwards).
      I was aware of it, but hadn’t updated the post – I’ve added some more info and linked back to it.

Comments are closed.