Next: Delete Breaks, Previous: Set Watchpoints, Up: Breakpoints

#### 5.1.3 Setting Catchpoints

You can use catchpoints to cause the debugger to stop for certain kinds of program events, such as C`++` exceptions or the loading of a shared library. Use the `catch` command to set a catchpoint.

`catch` event
  Stop when event occurs. The event can be any of the following:
  `throw` [regexp]
  `rethrow` [regexp]
  `catch` [regexp]
    The throwing, re-throwing, or catching of a C`++` exception.

    If regexp is given, then only exceptions whose type matches the regular expression will be caught.

    The convenience variable `$_exception` is available at an exception-related catchpoint, on some systems. This holds the exception being thrown.

    There are currently some limitations to C`++` exception handling in gdb:

    - The support for these commands is system-dependent. Currently, only systems using the ‘`gnu-v3`’ C`++` ABI (see ABI) are supported.
    - The regular expression feature and the `$_exception` convenience variable rely on the presence of some SDT probes in `libstdc++`. If these probes are not present, then these features cannot be used. These probes were first available in the GCC 4.8 release, but whether or not they are available in your GCC also depends on how it was built.
    - The `$_exception` convenience variable is only valid at the instruction at which an exception-related catchpoint is set.
    - When an exception-related catchpoint is hit, gdb stops at a location in the system library which implements runtime exception support for C`++`, usually `libstdc++`. You can use `up` (see Selection) to get to your code.
    - If you call a function interactively, gdb normally returns control to you when the function has finished executing. If the call raises an exception, however, the call may bypass the mechanism that returns control to you and cause your program either to abort or to simply continue running until it hits a breakpoint, catches a signal that gdb is listening for, or exits. This is the case even if you set a catchpoint for the exception; catchpoints on exceptions are disabled within interactive calls. See Calling, for information on controlling this with `set unwind-on-terminating-exception`.
    - You cannot raise an exception interactively.
    - You cannot install an exception handler interactively.
  `exception`
    An Ada exception being raised. If an exception name is specified at the end of the command (eg `catch exception Program_Error`), the debugger will stop only when this specific exception is raised. Otherwise, the debugger stops execution when any Ada exception is raised.

    When inserting an exception catchpoint on a user-defined exception whose name is identical to one of the exceptions defined by the language, the fully qualified name must be used as the exception name. Otherwise, gdb will assume that it should stop on the pre-defined exception rather than the user-defined one. For instance, assuming an exception called `Constraint_Error` is defined in package `Pck`, then the command to use to catch such exceptions is `catch exception Pck.Constraint_Error`.

  `exception unhandled`
    An exception that was raised but is not handled by the program.
  `assert`
    A failed Ada assertion.
  `exec`
    A call to `exec`. This is currently only available for HP-UX and gnu/Linux.
  `syscall`
  `syscall` [name | number]`...`
    A call to or return from a system call, a.k.a. syscall. A syscall is a mechanism for application programs to request a service from the operating system (OS) or one of the OS system services. gdb can catch some or all of the syscalls issued by the debuggee, and show the related information for each syscall. If no argument is specified, calls to and returns from all system calls will be caught.

    name can be any system call name that is valid for the underlying OS. Just what syscalls are valid depends on the OS. On GNU and Unix systems, you can find the full list of valid syscall names on `/usr/include/asm/unistd.h`.

    Normally, gdb knows in advance which syscalls are valid for each OS, so you can use the gdb command-line completion facilities (see command completion) to list the available choices.

    You may also specify the system call numerically. A syscall's number is the value passed to the OS's syscall dispatcher to identify the requested service. When you specify the syscall by its name, gdb uses its database of syscalls to convert the name into the corresponding numeric code, but using the number directly may be useful if gdb's database does not have the complete list of syscalls on your system (e.g., because gdb lags behind the OS upgrades).

    The example below illustrates how this command works if you don't provide arguments to it:

                       (gdb) catch syscall
                       Catchpoint 1 (syscall)
                       (gdb) r
                       Starting program: /tmp/catch-syscall

                       Catchpoint 1 (call to syscall 'close'), \
                       	   0xffffe424 in __kernel_vsyscall ()
                       (gdb) c
                       Continuing.

                       Catchpoint 1 (returned from syscall 'close'), \
                       	0xffffe424 in __kernel_vsyscall ()
                       (gdb)

    Here is an example of catching a system call by name:

                       (gdb) catch syscall chroot
                       Catchpoint 1 (syscall 'chroot' [61])
                       (gdb) r
                       Starting program: /tmp/catch-syscall

                       Catchpoint 1 (call to syscall 'chroot'), \
                       		   0xffffe424 in __kernel_vsyscall ()
                       (gdb) c
                       Continuing.

                       Catchpoint 1 (returned from syscall 'chroot'), \
                       	0xffffe424 in __kernel_vsyscall ()
                       (gdb)

    An example of specifying a system call numerically. In the case below, the syscall number has a corresponding entry in the XML file, so gdb finds its name and prints it:

                       (gdb) catch syscall 252
                       Catchpoint 1 (syscall(s) 'exit_group')
                       (gdb) r
                       Starting program: /tmp/catch-syscall

                       Catchpoint 1 (call to syscall 'exit_group'), \
                       		   0xffffe424 in __kernel_vsyscall ()
                       (gdb) c
                       Continuing.

                       Program exited normally.
                       (gdb)

    However, there can be situations when there is no corresponding name in XML file for that syscall number. In this case, gdb prints a warning message saying that it was not able to find the syscall name, but the catchpoint will be set anyway. See the example below:

                       (gdb) catch syscall 764
                       warning: The number '764' does not represent a known syscall.
                       Catchpoint 2 (syscall 764)
                       (gdb)

    If you configure gdb using the ‘`--without-expat`’ option, it will not be able to display syscall names. Also, if your architecture does not have an XML file describing its system calls, you will not be able to see the syscall names. It is important to notice that these two features are used for accessing the syscall name database. In either case, you will see a warning like this:

                       (gdb) catch syscall
                       warning: Could not open "syscalls/i386-linux.xml"
                       warning: Could not load the syscall XML file 'syscalls/i386-linux.xml'.
                       GDB will not be able to display syscall names.
                       Catchpoint 1 (syscall)
                       (gdb)

    Of course, the file name will change depending on your architecture and system.

    Still using the example above, you can also try to catch a syscall by its number. In this case, you would see something like:

                       (gdb) catch syscall 252
                       Catchpoint 1 (syscall(s) 252)

    Again, in this case gdb would not be able to display syscall's names.

  `fork`
    A call to `fork`. This is currently only available for HP-UX and gnu/Linux.
  `vfork`
    A call to `vfork`. This is currently only available for HP-UX and gnu/Linux.
  `load` [`regexp`]
  `unload` [`regexp`]
    The loading or unloading of a shared library. If regexp is given, then the catchpoint will stop only if the regular expression matches one of the affected libraries.
  `signal` [signal`...` |`‘``all``’`]
    The delivery of a signal.

    With no arguments, this catchpoint will catch any signal that is not used internally by gdb, specifically, all signals except ‘`SIGTRAP`’ and ‘`SIGINT`’.

    With the argument ‘`all`’, all signals, including those used by gdb, will be caught. This argument cannot be used with other signal names.

    Otherwise, the arguments are a list of signal names as given to `handle` (see Signals). Only signals specified in this list will be caught.

    One reason that `catch signal` can be more useful than `handle` is that you can attach commands and conditions to the catchpoint.

    When a signal is caught by a catchpoint, the signal's `stop` and `print` settings, as specified by `handle`, are ignored. However, whether the signal is still delivered to the inferior depends on the `pass` setting; this can be changed in the catchpoint's commands.

`tcatch` event
  Set a catchpoint that is enabled only for one stop. The catchpoint is automatically deleted after the first time the event is caught.

Use the `info break` command to list the current catchpoints.