[英]Hardware breakpoint on C++ reference modification in gdb
C++ standard says that it is unspecified whether or not a reference requires storage (3.7). C ++标准表示, 尚不确定引用是否需要存储(3.7)。 .
。 However, as far as I understand, gcc implements C++ references as pointers and as such they can be corrupted.
但是,据我了解,gcc将C ++引用实现为指针,因此它们可能会损坏。
Is it possible to get an address of a reference in gdb and put a hardware breakpoint on that address in order to find out what corrupts the memory where the reference resides? 是否有可能在gdb中获取引用的地址,并在该地址上放置一个硬件断点,以便找出导致引用所在的内存损坏的原因? How can one set such a breakpoint?
如何设置这样的断点?
GDB may does hardware watchpointing. GDB可能会进行硬件监视。 You can use command
watch
for this. 您可以为此使用命令
watch
。 Example: main.cpp: 示例: main.cpp:
int main(int argc, char **argv)
{
int a = 0;
int& b = a;
int* c = &a;
*c = 1;
return 0;
}
Start debugging and set breakpoint on start main function and end main function: 开始调试并在开始主要功能和结束主要功能上设置断点:
(gdb) b main
Breakpoint 1 at 0x401bc8: file /../main.cpp, line 60.
(gdb) b main.cpp:65
Breakpoint 2 at 0x401be9: file /../main.cpp, line 65.
(gdb) r
Get address of reference b
: 获取参考
b
地址:
Breakpoint 1, main (argc=1, argv=0x7fffffffddd8) at /../main.cpp:60
60 int a = 0;
(gdb) disas /m
Dump of assembler code for function main(int, char**):
59 {
... Something code
60 int a = 0;
=> 0x0000000000401bc8 <+11>: movl $0x0,-0x14(%rbp)
61 int& b = a;
0x0000000000401bcf <+18>: lea -0x14(%rbp),%rax
0x0000000000401bd3 <+22>: mov %rax,-0x10(%rbp)
62 int* c = &a;
0x0000000000401bd7 <+26>: lea -0x14(%rbp),%rax
0x0000000000401bdb <+30>: mov %rax,-0x8(%rbp)
63 *c = 1;
0x0000000000401bdf <+34>: mov -0x8(%rbp),%rax
0x0000000000401be3 <+38>: movl $0x1,(%rax)
64
65 return 0;
0x0000000000401be9 <+44>: mov $0x0,%eax
66 }
0x0000000000401bee <+49>: pop %rbp
0x0000000000401bef <+50>: retq
End of assembler dump.
(gdb) p $rbp-0x10
$1 = (void *) 0x7fffffffdce0
p $rbp-0x10
is printing address of reference b
. p $rbp-0x10
是参考b
打印地址。 It is 0x7fffffffdce0
. 它是
0x7fffffffdce0
。 Set this address for watching: 设置此地址进行观看:
(gdb) watch *0x7fffffffdce0
Hardware watchpoint 3: *0x7fffffffdce0
(gdb) c
GDB break only if value is changed: 只有更改值,GDB才会中断:
(gdb) c
Continuing.
Hardware watchpoint 3: *0x7fffffffdce0
Old value = -8752
New value = -8996
main (argc=1, argv=0x7fffffffddd8) at /../main.cpp:62
62 int* c = &a;
Sorry for my english! 对不起我的英语不好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.