Search code examples
armgdbembeddedelfgdbinit

GDB automate commands after symbols have been loaded


I'm debugging a microcontroller with GDB remote. I have multiple build targets and I would like to have one generic .gdbinit file for flashing and/or debugging all the different targets.

I'm launching GDB with a BAT script where the debuggable .elf file is given as a parameter for GDB. This way GDB will load the symbols and also my .gdbinit is being run correctly.

My gdbinit:

define target hookpost-remote
    echo POST TARGET REMOTE\n
    # do stuff
    #load ./path/to/foo.elf # I don't want this
    load # This works if and only if the .elf has been loaded already
    monitor reset
    quit
end

target remote tcp:localhost:2331

The problem is that gdbinit is run and the "target remote" command is issued before the binary file is loaded and GDB will produce an error:

.gdbinit:15: Error in sourced command file: No executable file specified. Use the "file" or "exec-file" command.

And directly after that:

Reading symbols from path/to/foo.elf

Everything works if I remove "target remote" command from gdbinit and call it manually in GDB console, but I don't want to do that, I want to automate everything.

How can I automate commands after the symbols have been loaded? Is there some kind of hook that fires after GDB has finished initializing and after running gdbinit? Can I hook to post Reading symbols?


Solution

  • You are probably using .gdbinit for purposes including some which would be better accomplished with a command file passed by the -x [cmds_file] command line option.

    A little experimentation shows that the .gdbinit is run before the program file is loaded, while the -x file is run after.