Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




22.2. User-defined command hooks

You may define hooks, which are a special kind of user-defined command. Whenever you run the command foo, if the user-defined command hook-foo exists, it is executed (with no arguments) before that command.

A hook may also be defined which is run after the command you executed. Whenever you run the command foo, if the user-defined command hookpost-foo exists, it is executed (with no arguments) after that command. Post-execution hooks may exist simultaneously with pre-execution hooks, for the same command.

It is valid for a hook to call the command which it hooks. If this occurs, the hook is not re-executed, thereby avoiding infinte recursion.

In addition, a pseudo-command, stop exists. Defining (hook-stop) makes the associated commands execute every time execution stops in your program: before breakpoint commands are run, displays are printed, or the stack frame is printed.

For example, to ignore SIGALRM signals while single-stepping, but treat them normally during normal execution, you could define:

define hook-stop
handle SIGALRM nopass

define hook-run
handle SIGALRM pass

define hook-continue
handle SIGLARM pass

As a further example, to hook at the begining and end of the echo command, and to add extra text to the beginning and end of the message, you could define:

define hook-echo
echo <<<---

define hookpost-echo
echo --->>>\n

(gdb) echo Hello World
<<<---Hello World--->>>

You can define a hook for any single-word command in gdb, but not for command aliases; you should define a hook for the basic command name, e.g. backtrace rather than bt. If an error occurs during the execution of your hook, execution of gdb commands stops and gdb issues a prompt (before the command that you actually typed had a chance to run).

If you try to define a hook which does not match any known command, you get a warning from the define command.

  Published under the terms of the GNU General Public License Design by Interspire