简体   繁体   English

当 char* x 指向值等于“hello”的字符串时,如何在 gdb 中设置条件断点?

[英]How do I set a conditional breakpoint in gdb, when char* x points to a string whose value equals “hello”?

Can I specify that I want gdb to break at line x when char* x points to a string whose value equals "hello" ?char* x指向值等于"hello"的字符串时,我可以指定我希望 gdb 在第 x 行中断吗? If yes, how?如果是,如何?

You can use strcmp :您可以使用strcmp

break x:20 if strcmp(y, "hello") == 0

20 is line number, x can be any filename and y can be any variable. 20是行号, x可以是任何文件名, y可以是任何变量。

break x if ((int)strcmp(y, "hello")) == 0

On some implementations gdb might not know the return type of strcmp.在某些实现中,gdb 可能不知道 strcmp 的返回类型。 That means you would have to cast, otherwise it would always evaluate to true!这意味着您必须强制转换,否则它将始终评估为 true!

Use a break condition with $_streq (one of GDB's own convenience functions ):使用带有$_streq中断条件(GDB 自己的便利函数之一):

break [where] if $_streq(x, "hello")

or, if your breakpoint already exists, add the condition to it:或者,如果您的断点已存在,则向其添加条件:

condition <breakpoint number> $_streq(x, "hello")

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 : 从 GDB 7.5(很久以前)开始,您可以使用它和一些其他原生便利函数来进行各种字符串匹配,包括支持Python 正则表达式语法的$_regex

$_memeq(buf1, buf2, length)
$_regex(str, regex)
$_streq(str1, str2)
$_strlen(str)

These are quite less problematic than having to execute the usual strcmp() injected to the process' stack, because that can have undesired side effects.与必须执行注入进程堆栈的通常strcmp()相比,这些问题要少得多,因为这可能会产生不希望的副作用。

Alas, using the native functions is not always possible, because they rely on GDB being compiled with Python support.唉,使用本机函数并不总是可行的,因为它们依赖于使用 Python 支持编译的 GDB。 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 .可以肯定的是,您可以通过在 GDB 中运行show configuration并搜索--with-python来检查它。 This shell oneliner does the trick, too:这个 shell oneliner 也可以解决这个问题:

gdb -n -quiet -batch -ex 'show configuration' | grep 'with-python'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM