1

I try to start a program with gdb from commands line, then immediately add a breakpoint with commands, then run:

gdb -q -ex 'set pagination off' -ex 'break XOpenDisplay' -ex 'commands' -ex 'silent' -ex 'info locals' -ex 'bt full' -ex 'cont' -ex 'end' -ex 'r' ./myprogram

The program gets stuck after the "commands" prompting me to enter commands via keyboard then enter "end".

Did I forget something?

Regards

Update:

I added a .gdbinit with the following content:

gdb -q -ex breakXOpenDisplayRun


define breakXOpenDisplayRun
set pagination off
break XOpenDisplay
commands
  silent
  info locals
  bt full
  cont
end
run
end

gdb -q -ex breakXOpenDisplayRun ./myapp

When the program encounters the breakpoint the first time it stops there prompting a user input which should not happen. After the first cont it works as expected.

Desperado17
  • 835
  • 6
  • 12

2 Answers2

1

The -ex expects a complete command, and in case of commands the complete command is

commands
  silent
  info locals
  bt full
  cont
end

While you can enter multi-line command at the shell prompt, doing so is exceedingly awkward, and you'll be better off putting all of the desired commands into a temporary command file. Something like this should work:

cat > /tmp/gdb.$$ <<"EOF" && gdb -x /tmp/gdb.$$ ./myprogram && rm -f /tmp/gdb.$$
set pagination off
break XOpenDisplay'
commands
  silent
  info locals
  bt full
  cont
end
run
EOF
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Update in the original question. I put the commands into a function in .gdbinit but it gets stuck when it first encounters the breakpoint. – Desperado17 Feb 02 '22 at 08:37
  • With "stuck" I mean it stops there and prompts me for input although it should just execute info locals and bt full and continue. When I enter cont then it continues as it should and further encounters with the breakpoint are silent. – Desperado17 Feb 02 '22 at 09:21
  • I don't think `commands` on `-ex` works even if you inject newlines, e.g.: `gdb -nh -batch -ex 'b f' -ex "$(printf 'commands\nbt\ncontinue\nend')" -ex r hello.out` does not hit function `f` multiple times as expected. Same `printf` to file + `-x` works however. GDB 12.0.90. I would also make the temporary file with `mktemp`: https://unix.stackexchange.com/questions/181937/how-create-a-temporary-file-in-shell-script – Ciro Santilli OurBigBook.com Dec 11 '22 at 15:59
0

Thanks to gdb mailing list Andrew Burgess. This is a bug that has been fixed in gdb 11. For versions before that, using a separate source worked for me:

gdb -q -ex 'source breakXOpenDisplayRun.gdb' ./myapp

Desperado17
  • 835
  • 6
  • 12