Scripting a repetitive gdb job


I want to do the same repetitive job in gdb across a bunch of different files. Namely,

bash$ gdb ./file1

... gdb starts up ...

gdb$ b *0xdeadbeef
gdb$ r < file2

... some output prints ...

gdb$ x/3a $esp

...  some addresses print ...

Is there some way I can script this? Starting up gdb via a script is easy, but passing commands to gdb and getting their output isn't obvious to me. Would I use redirection?

asked on Stack Overflow Oct 16, 2019 by peachykeen

1 Answer


You can just use a shell trick to pass multiple gdb commands in sequence, new-line separated to standard input of gdb, for it to consume. For e.g. for a single file just do

printf '%s\n' 'b *0xdeadbeef' 'r < file2' 'x/3a $esp' | gdb ./file1

For multiple input files, and feeding one file to gdb at a time, starting with name file*. Use an appropriate glob expression based on your actual filename

for file in file*; do
    printf '%s\n' 'b *0xdeadbeef' 'r < file2' 'x/3a $esp' | gdb "$file"
answered on Stack Overflow Oct 16, 2019 by Inian • edited Oct 16, 2019 by Inian

User contributions licensed under CC BY-SA 3.0