Reputation: 451
I have a variable called x in GDB which I want to compare against a string.
gdb $ print $x
$1 = 0x1001009b0 "hello"
but a comparsion with
if $x == "hello"
doesn't work.
Upvotes: 22
Views: 18509
Reputation: 5624
As @tlwhitec points out:
You could also use the built-in $_streq(str1, str2)
function:
(gdb) p $_streq($x, "hello")
This function does not require GDB to be configured with Python support, which means that they are always available.
More convenient functions can be found in https://sourceware.org/gdb/onlinedocs/gdb/Convenience-Funs.html. Or use
(gdb) help function
to print a list of all convenience functions.
For older gdb's that lack the built-in $_streq
function,
You can define your own comparison
(gdb) p strcmp($x, "hello") == 0
$1 = 1
If you are unfortunate enough to not have the program running (executing a core file or something), you can do something to the effect of the following if your gdb is new enough to have python:
(gdb) py print cmp(gdb.execute("output $x", to_string=True).strip('"'), "hello") == 0
True
or:
(gdb) define strcmp
>py print cmp(gdb.execute("output $arg0", to_string=True).strip('"'), $arg1)
>end
(gdb) strcmp $x "hello"
0
Upvotes: 30