2
votes

When debugging an unfamiliar program with gdb, the program often unexpectedly exits after executing next. When that happens I'll typically set a break point, re-run the program and execute step instead of next to trace what's happening. However, sometimes it is difficult to know where to set the break point. Is there a technique set the break automatically? Something like:

define hook-next
break
end

define hookpost-next
# delete the previous break if the program is still running
end
1

1 Answers

4
votes

I think you could do it with a combination of hook-next, convenience variables, and a breakpoint on exit. Something like:

define hook-next
set $saved_pc = $pc
end

break exit
commands
break *$saved_pc
end

You may prefer "tbreak" there.