Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In GDB, how do I execute a command automatically when program stops? (like display)

Tags:

command

gdb

I want some commands to be automatically executed each time the program stops, just like what display does with x. How do I do that?

like image 451
neuron Avatar asked Sep 20 '11 06:09

neuron


People also ask

When debugging using GDB which command should we use if we would like to proceed to next breakpoint directly?

Just press c. It will continue execution until the next breakpoint.

Which GDB command will display the next instruction to execute that is controlled by the program counter?

It is often useful to do ' display/i $pc ' when stepping by machine instructions. This makes GDB automatically display the next instruction to be executed, each time your program stops.

Which command is used to continue the program until the next breakpoint occurs?

continue (shorthand: c ) will resume execution until the next breakpoint is hit or the program exits. finish will run until the current function call completes and stop there.

Which command is used to continue the execution until the end of current function?

You can use the finish command. finish : Continue running until just after function in the selected stack frame returns.


2 Answers

Another "new" way to do it is with the Python Event interface:

 def stop_handler (event):
     print "event type: stop"

 gdb.events.stop.connect (stop_handler)

which will trigger the stop_handler function each the the inferior stops.

There are two other similar events type:

events.cont
events.exited

respectively triggered when the inferior is continued or exists.

like image 104
Kevin Avatar answered Oct 04 '22 23:10

Kevin


Here's the easy way I found out:

define hook-stop
  ...commands to be executed when execution stops
end

Refer to this page for details: http://sourceware.org/gdb/current/onlinedocs/gdb/Hooks.html#Hooks

like image 22
neuron Avatar answered Oct 05 '22 01:10

neuron