1

Trying to set a conditional breakpoint to stop at a specific value of a string.

Following set of commands I have tried:

Set 1:

(gdb) b file.C:97 if strcmp(pName, "abc") == 0

set 2:

(gdb) b file.C:97
(gdb) cond 1 !strcmp(pName, "abc")

set 3:

(gdb) b file.C:97
(gdb) set $str = "abc"
(gdb) cond 1 strcmp (pName, $str) == 0

Both of these are ending with a segmentation fault at STL of strcmp:

Continuing.
Detaching after fork from child process 12742.
Detaching after fork from child process 12818.
Detaching after fork from child process 12819.
FUSION: detected debugger running, not catching non-fusion exceptions

Program received signal SIGSEGV, Segmentation fault.
__strcmp_sse42 () at ../sysdeps/x86_64/multiarch/strcmp.S:260
Error in testing breakpoint condition:
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(__strcmp_sse42) will be abandoned.
When the function is done executing, GDB will silently stop.

Breakpoint 1, __strcmp_sse42 () at ../sysdeps/x86_64/multiarch/strcmp.S:260
/usr/src/debug/glibc-2.12-2-gc4ccff1/sysdeps/x86_64/multiarch/strcmp.S:260:7925:beg:0x32f912871a

Using find() is not ending up with segmentation fault, but the breakpoint is stopping without applying the condition:

(gdb) b file.C:97
(gdb) set $str = "abc"
(gdb) cond 1 pName.find($str) == 0

These above commands I have tried on GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-47.el6

If I try GNU gdb (GDB) 8.1.90.20180726-git, it's ending with a new issue.

Though pName is defined as string,

I am getting the following after stopping debugger at breakpoint(without the condition using strcmp()):

(gdb) print pName
4: (string)pName = {static npos = <optimized out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x2aebca8 "xyz"}}

On GDB 7.6.1-47.el6, after applying simple breakpoint(without any condition), print pName only gives 'xyz'.

Is it possible to do the same in GDB 8.1.90.20180726?

Anyway, please suggest me a way to put the condition breakpoint on string successfully!

Foobar-naut
  • 111
  • 3
  • 11

2 Answers2

0

This one has worked on GDB 7.6.1-47.el6!

(gdb) b file.C:97
(gdb) cond 1 ((int)strcmp(pName.c_str(), "abc")) == 0

Please let me know if anyone has a better answer!

Foobar-naut
  • 111
  • 3
  • 11
  • This used to work for me too. But after updating to gcc 12 and gdb 12, I get the error `Error in testing breakpoint condition: Invalid type combination in equality test`. Somehow gdb thinks strcmp returns void... Very weird. ```(gdb) p strcmp(pName.c_str(), "abc") $7 = void``` – David Faure Oct 17 '22 at 12:40
0

You can try the .compare method, such as

condition 1 pName.compare("abc") == 0

I succeed with GNU gdb (GDB) 14.0.50.20230225-git

Absoler
  • 1
  • 1