Stopping and Continuing

Posted by Beetle B. on Thu 10 October 2019

To break conditionally, do:

break ... if COND

… could be a function, location, address, etc.

You can set a breakpoint such that it breaks only once.

You can break on any function that matches a regular expression! Use this to set a breakpoint on all functions!

info break can show you how many times a breakpoint is hit. You can tell gdb to ignore the first \(N\) times. This is useful when you want to step through a function only near where the bug appears, but that function is called very often by unrelated operations.

It has some information about breakpoints in shared libraries - read for details.

See “Setting Watchpoints” for information on how to set a watchpoint (i.e. a breakpoint on the value of a variable).

You can break when a variable is read.

You can break when a variable is written.

You can watch a particular address.

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. Keep in mind that support of this may not exist all that well.

You can catch system calls and calls to fork. Or the delivery of any signal.

Delete breakpoints with delete. You can also use clear to delete breakpoints in certain places (e.g. a particular function or line).

You disable and enable breakpoints, watchpoints, and catchpoints with the ‘enable’ and ‘disable’ commands, optionally specifying one or more breakpoint numbers as arguments (found via info breakpoints). You can enable breakpoints with a count.

You can specify conditions for breakpoints and watchpoints. Your condition can also call functions in your code (useful for logging).

Break conditions can be specified when a breakpoint is set, by using ‘if’ in the arguments to the ‘break’ command. They can also be changed at any time with the ‘condition’ command. You can also use the ‘if’ keyword with the ‘watch’ command.

You can give any breakpoint (or watchpoint or catchpoint) a series of commands to execute when your program stops due to that breakpoint. For example, you might want to print the values of certain expressions, or enable other breakpoints.

If the first command you specify in a command list is ‘silent’, the usual message about stopping at a breakpoint is not printed. This may be desirable for breakpoints that are to print a specific message and then continue. If none of the remaining commands print anything, you see no sign that the breakpoint was reached. ‘silent’ is meaningful only at the beginning of a breakpoint command list.

The commands ‘echo’, ‘output’, and ‘printf’ allow you to print precisely controlled output, and are often useful in silent breakpoints.

The dynamic printf command ‘dprintf’ combines a breakpoint with formatted printing of your program’s data to give you the effect of inserting ‘printf’ calls into your program on-the-fly, without having to recompile it. Read for more details.

You can save breakpoints to a file using save breakpoints [FILENAME].

Continuing and Stepping

Type continue or c to continue. If you specify a numeric argument, it means to ignore the breakpoint \(N\) times at this location.

return will resume execution in the calling function. jump allows you to resume at a different location in the program. You can give it a count argument to step \(N\) lines.

step will step into (I think). The counterpart is next.

finish: I think this says to continue until the function in the stack frame returns.

until is useful for stepping out of a loop. You can optionally specify a location.

advance is similar to until. Useful for advancing to a particular location.

Skipping Over Functions and Files

Use skip to tell GDB to skip a function or set of functions while stepping (i.e. don’t stop in those functions while stepping).

Signals

Information on catching a signal (like SIGINT).

You can also use the ‘signal’ command to prevent your program from seeing a signal, or cause it to see a signal it normally would not see, or to give it any signal at any time.

Stopping and Starting Multi-threaded Programs

All-stop mode: If you hit a breakpoint, all threads stop. Non-stop mode allows other threads to continue execution.

In all stop mode, even step will cause other threads to start executing. However, synchronization is not guaranteed. Other threads may execute more than one instruction each time.

Read the rest of the section if debugging multithreaded applications.

tags : gdb