GDB breakpoints types: conditional breakpoints, regex

Written by Dr Greg Law | Last updated 31st May 2020

You probably know that you can also set the breakpoint based on the function name. 

But what if you have multiple functions with the same name, which can happen with statically defined functions in C and with overloaded functions in C++. 

Let's look at an example in which there are two functions called my_func. One takes a string and the other takes a long.

#include <iostream>
using namespace std;

static void
my_func(std::string str)
  cout << "String is " << str << endl;

static void
my_func(long l)
  cout << "Long is " << l << endl;

  my_func("The meaning of life is");
  return 0;

We compile this with:

g++ funcs.cpp -g

Loading this into GDB and then TUI (Text User Interface) mode, I can then create a breakpoint with:

(gdb) break my_func
Breakpoint 2 at 0x5555555555551f1: my_func. (2 locations)

And with `info break` I can inspect the 2 breakpoint locations which shows the 2 locations under 2.1 and 2.2:


So if I run my program, I hit the first breakpoint location first and if I continue running, I hit the second.

command 2

Which results in:


... which helps me see which breakpoint was hit.

But suppose I want to have a breakpoint on just one of the locations. Starting again by deleting the breakpoints with `delete`

break my_func(long)

An extra hint is that typing tab will show completions. So hitting tab-tab at break my_func( shows the two variants of the method:


Back to my new breakpoint (break my_func(long)) I now have one breakpoint with one location. So this technique can be used for situations where you have statically defined functions in C and with overloaded functions in C++ (or any other situation with functions of the same name).

Don't miss my next GDB tutorial: sign up to the gdbWatchPoint mailing list below.

Conditional breakpoints

In the video, I also show how to create conditional breakpoints which allow you to determine when a breakpoint should be triggered rather than having it triggered every time.

For example, if we only want breakpoint 3 to be triggered if the variable l is 41 we do:

condition 3 l==41

In the example above, this would obviously never be triggered (since l always equals 42).

Another syntax which does the same thing and combined conditionals with our previous tip about identically named methods is:

break my_func(long) if l == 42

Regex breakpoints

Another cool feature of GDB is the ability to use regex to specify breakpoints using `rbreak`. In the video, I show an example program which can be compiled, loaded into GDB and then using `rbreak`:

rbreak myfunc[12]

...which creates 2 breakpoints. This creates 2 breakpoints I can connect different commands and enable/disable them independently.

Get tutorials straight to your inbox
Become a GDB Power User. Get Greg's debugging tips directly in your inbox every 2 weeks.
Related content
  • Concurrency Defects Resolution
    New techniques to help quickly find and resolve defects in multithreaded and multiprocess applications
    Download the technical paper

A dedicated resource to learn about debugging in GDB by industry leader in debugging and founder of Undo

Meet Greg