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

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

问题描述

  gdb $ print $ x 
$ 1 = 0x1001009b0hello

但与


的比较

 如果$ x ==hello

不起作用。

解决方案

 (gdb)p strcmp ,你好)== 0 
$ 1 = 1

如果你不幸没有程序运行(执行一​​个核心文件或某些东西),如果您的gdb足够新建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 $ xhello
0


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.

解决方案

(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

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

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