3

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?

llahteinen
  • 113
  • 1
  • 6

1 Answers1

3

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.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • You are right. I had tried using -ix to load a GDB command file, but the result was the same as with gdbinit. With -x the commands are indeed run after loading the program file and this solves my problem. [Here](https://sourceware.org/gdb/onlinedocs/gdb/Startup.html) is some relevant info. – llahteinen Mar 19 '19 at 09:49