GDB:我可以为另一个范围内的变量添加 "watch" 吗?

GDB: Can I add a "watch" for a variable in another scope?

似乎 watch 仅在我 运行 进入函数并观察函数局部变量的值时才起作用。我的问题是,我可以观察函数的输入参数是否大于数字吗?例如。我有这个代码:

$cat testWatch.cpp
#include<stdio.h>
void f(int i){
    ++i;
    printf("%d\n",i);
}
int main(){
    int i=1;
    f(2);
    f(3);
    ++i;
    f(4);
    ++i;
    return 0;
}

我愿意

(1) 当程序在"main"函数时,我想在f()里面设置一个"watch"。可能吗?

(2)我想在f()函数的开头设置一个"watch"点,当输入"int i"大于2时,我想让gdb停止。可能吗?

1) 你真的需要 'watch' 吗?通过指定行号在 f() 内设置条件断点很简单。 (或者在不那么琐碎的程序中,fileName:lineNum)

2) 您描述的行为是一个条件断点。

(gdb) break 2 if (i > 2)
Breakpoint 5 at 0x400531: file test.c, line 2.
(gdb) run
Starting program: /tmp/test
3

Breakpoint 5, f (i=3) at test.c:3