我需要使用GDB来监视具有相同名称的两个不同变量,其中一个是全局变量,而另一个是局部变量。任何解决方案?
答案 0 :(得分:3)
考虑以下示例:
int foo;
void bar()
{
foo++;
}
int main()
{
int foo;
for (foo = 0; foo < 5; foo++) {
bar();
}
return 0;
}
gcc -g t.c
gdb -q ./a.out
(gdb) start
Temporary breakpoint 1 at 0x40050a: file t.c, line 11.
Starting program: /tmp/a.out
Temporary breakpoint 1, main () at t.c:11
11 for (foo = 0; foo < 5; foo++) {
(gdb) p &foo
$1 = (int *) 0x7fffffffdccc
(gdb) watch -l foo
Hardware watchpoint 2: -location foo
注意:以上观察设置在本地foo
。
(gdb) p &::foo
$2 = (int *) 0x60103c <foo>
(gdb) watch -l ::foo
Hardware watchpoint 3: -location ::foo
注意:上面的观看是在全局foo
上设置的。
(gdb) c
Continuing.
Hardware watchpoint 3: -location ::foo
全局foo
已在bar()
内进行了调整。
Old value = 0
New value = 1
bar () at t.c:6
6 }
(gdb) c
Continuing.
Hardware watchpoint 2: -location foo
本地foo
已在main
内修改:
Old value = 0
New value = 1
0x0000000000400521 in main () at t.c:11
11 for (foo = 0; foo < 5; foo++) {
(gdb) c
Continuing.
Hardware watchpoint 3: -location ::foo
Old value = 1
New value = 2
bar () at t.c:6
6 }
(gdb) c
Continuing.
Hardware watchpoint 2: -location foo
Old value = 1
New value = 2
0x0000000000400521 in main () at t.c:11
11 for (foo = 0; foo < 5; foo++) {
......等等。