如何比较 GDB 中存储的字符串变量? [英] How to compare a stored string variable in GDB?

查看:16
本文介绍了如何比较 GDB 中存储的字符串变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 GDB 中有一个名为 x 的变量,我想将它与字符串进行比较.

I have a variable called x in GDB which I want to compare against a string.

gdb $ print $x
$1 = 0x1001009b0 "hello"

但与

if $x == "hello"

没用.

推荐答案

正如@tlwhitec 指出的那样:你也可以使用内置的 $_streq(str1, str2) 函数:

As @tlwhitec points out: You could also use the built-in $_streq(str1, str2) function:

(gdb) p $_streq($x, "hello")

此功能不需要为 GDB 配置 Python 支持,这意味着它们始终可用.

This function does not require GDB to be configured with Python support, which means that they are always available.

更方便的功能可以在https://sourceware.org/找到gdb/onlinedocs/gdb/Convenience-Funs.html.或者使用

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.

对于缺少内置 $_streq 函数的旧 gdb,您可以定义自己的比较

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

如果你不幸没有让程序运行(执行核心文件或其他东西),如果你的 gdb 足够新,可以有 python,你可以做一些事情来达到以下效果:

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

或:

(gdb) define strcmp
>py print cmp(gdb.execute("output $arg0", to_string=True).strip('"'), $arg1)
>end
(gdb) strcmp $x "hello"
0

这篇关于如何比较 GDB 中存储的字符串变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆