当 char* x 指向值等于“hello”的字符串时,如何在 gdb 中设置条件断点?
我可以指定当 char* x
指向值等于 "hello"
的字符串时,我希望 gdb 在第 x 行处中断吗?如果是,怎么办?
Can I specify that I want gdb to break at line x when char* x
points to a string whose value equals "hello"
? If yes, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
strcmp
:20
是行号,x
可以是任何文件名,y
可以是任何变量。You can use
strcmp
:20
is line number,x
can be any filename andy
can be any variable.使用 中断条件 和
$_streq
(以下之一GDB 自己的便利函数):或者,如果断点已存在,请向其添加条件:
自 GDB 7.5(很久以前)以来,您可以使用它和一些其他本机用于各种字符串匹配的便捷函数,包括支持的
$_regex
Python 正则表达式语法:这些问题比必须执行注入到进程堆栈中的常规
strcmp()
,因为这可能会产生不良副作用。遗憾的是,使用本机函数并不总是可行,因为它们依赖于使用 Python 支持编译的 GDB。这通常是默认设置,但某些受限环境可能没有它。当然,您可以通过在 GDB 中运行
show configuration
并搜索--with-python
来检查它。这个 shell oneliner 也能达到这个目的:Use a break condition with
$_streq
(one of GDB's own convenience functions):or, if your breakpoint already exists, add the condition to it:
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: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 可能不知道 strcmp 的返回类型。这意味着您必须进行强制转换,否则它的计算结果将始终为 true!
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!