To set a conditional breakpoint, activate the context menu in the source pane, on the line where you want the breakpoint, and select “Add Conditional Breakpoint”. You'll then see a textbox where you can enter the expression. Press Return to finish.
Breakpoints are set with the break command (abbreviated b ). The debugger convenience variable `$bpnum' records the number of the breakpoint you've set most recently; see section Convenience variables, for a discussion of what you can do with convenience variables.
c file listed in Example 7.1, “Compiling a C Program With Debugging Information” with debugging information, you can set a new breakpoint at line 7 by running the following command: (gdb) break 7 Breakpoint 2 at 0x4004e3: file fibonacci.
You can use strcmp
:
break x:20 if strcmp(y, "hello") == 0
20
is line number, x
can be any filename and y
can be any variable.
Use a break condition with $_streq
(one of GDB's own convenience functions):
break [where] if $_streq(x, "hello")
or, if your breakpoint already exists, add the condition to it:
condition <breakpoint number> $_streq(x, "hello")
Since GDB 7.5 (long ago) you can use that and a handful of other native convenience functions for various string matching, including $_regex
which supports the Python regex syntax:
$_memeq(buf1, buf2, length)
$_regex(str, regex)
$_streq(str1, str2)
$_strlen(str)
These are quite less problematic than having to execute the usual strcmp()
injected to the process' stack, because that can have undesired side effects.
Alas, using the native functions is not always possible, because they rely on GDB being compiled with Python support. This is usually the default, but some constrained environments might not have it. To be sure, you can check it by running show configuration
inside GDB and searching for --with-python
. This shell oneliner does the trick, too:
gdb -n -quiet -batch -ex 'show configuration' | grep 'with-python'
break x if ((int)strcmp(y, "hello")) == 0
On some implementations gdb might not know the return type of strcmp. That means you would have to cast, otherwise it would always evaluate to true!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With