'gdb run program in a loop until a breakpoint is reached then display stacktrace

I am trying to debug a very sporadic issue in my application. If ran ~1000 times my application surely hits a certain line it shouldn't and I would like to view the stack. I tried using a gdb script cmd.gdb for this:

set logging overwrite on
set pagination off
set $n = 1000
break file.c:496
while $n-- > 0
  ignore 1 9
  condition 1 global_var == 10
  run
end

How should I modify this script in order to print the stack when the breakpoint is reached? I tried adding this after "run":

if $_siginfo
bt
loop_break
end

but it doesn't seem to work.



Solution 1:[1]

Actually, I have a Github repo with a Python-GDB extension, which does exactly the same thing as You have described, but with some more functionality.

You can just clone the repo:

git clone https://github.com/Viaceslavus/gdb-debug-until.git

and feed the python script to GDB with the following command inside GDB:

source <python script path>

Then, according to your example, you should run the next command:

debug-until file.c:496 --args="" --var-eq="global_var:10" -r=1000

*some remarks:

  • file.c:496 here is a starting breakpoint
  • "--args" parameter contains the arguments for your program
  • "--var-eq" is a debugging event, where 'global_var' is a variable name and '10' is a value
  • and finally the "-r" option specifies the number of times the program will be ran.

So all together this command will run your program 1000 times and will immediately notify You when the 'global_var' will be equal to 10.

Any additional information about the project could be found here:
https://github.com/Viaceslavus/gdb-debug-until.git in the README file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1