简体   繁体   中英

Hook into gdb or make gdb execute command on exception

Sometimes I am trying to debug a program in gdb and it takes it a while of running before the bug comes up.

While I'm waiting, I like to do other things. It would be nice if gdb could beep at me when an exception happens.

Is there a way to make gdb run an external command when an exception happens? Or a standard way to hook into gdb for this sort of thing?

You could make use of the define and shell , so:

(gdb) define nrun
run
shell notify-send "Go look at GDB now...."
end

This creates a new command nrun (for notify run) that does a run command, then when the run has finished uses shell to execute notify-send .

The notify-send is nothing to do with gdb , this is just a program in my $PATH that causes a popup notification dialogue to appear, you could replace notify-send with anything that you want.

Now instead of using run I use nrun , and once gdb stops I see a notification to go look at gdb.

You can add the define for nrun to your ~/.gdbinit file, and then it will always be available, if you're going to do that you might also want to add some documentation, like this:

documentation nrun
Perform a standard run command and notify the user when gdb stops.
end

Now in gdb if your forget what your command does you can do this:

(gdb) help nrun
Perform a standard run command and notify the user when gdb stops.

And if you forget what you called you command, but you know that it performs a notify when gdb stops you can do this:

(gdb) apropos notify
nrun -- Perform a standard run command and notify the user when gdb stops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM