gdb:特定对象被更改时中断 [英] gdb: break when a particular object is altered

查看:126
本文介绍了gdb:特定对象被更改时中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在c ++中定义的对象,指向它在整个项目中用于各种功能和文件中的指针。我正在更新数据的问题,所以我想调试它,看看发生了什么。理想情况下,我想要在每次访问对象时中断。但是, watch 需要一个特定的内存地址。所以,例如,如果我有:

I have an object defined in c++ with a pointer to it used in various functions and files throughout the project. I am having an issue with the data being updated, so I want to debug it to see what is happening. Ideally, I want to break every time the object is accessed. however, watch requires a specific memory address. So, for example, if I have:

class data{
public:
    int a;
    int b;
};

那么gdb只会在 a 因为指向数据的指针指向 a ,而是在 b 被更改时。

then gdb will only break when a is altered, since the pointer to data is pointed at a, but not when b is altered.

如果数据类覆盖的整个内存范围有所改变,是否有改变的方法?

Is there a way to break whenever the entire range of memory covered by the data class is altered?

推荐答案


数据类覆盖的整个内存范围是否有改变的方法?

Is there a way to break whenever the entire range of memory covered by the data class is altered?

可能

GDB硬件观察点在硬件中使用特殊的调试寄存器,通常有限制这些寄存器的工作原理。在 x86 上,您最多可以设置4个字大小的硬件观察点,例如您给予您可以在<$ c $上设置观察点c>& data-> a 和& data-> b ,这将覆盖$ $的整个内存c $ c> data 。

GDB hardware watchpoints use special debug registers in hardware, and there is usually a limit on how such registers work. On x86, you can set up to 4 word-sized hardware watch points, so for example you gave you can set watchpoints on &data->a and &data->b, and that will "cover" entire memory of the data.

我猜你实际的数据有很多更多的成员虽然,所以4个字大小的观察点是不够的。

I am guessing that your actual data has many more members though, and so 4 word-sized watch points will not suffice.

如果你在平台上有Valgrind支持,如果你的程序可以在Valgrind下执行,那么您可以使用Valgrind的内置 gdbserver 在任意内存区域设置观察点。

If you are on platform which has Valgrind support, and if your program can execute under Valgrind, then you can use Valgrind's built-in gdbserver to set watchpoints on arbitrary regions of memory.

更新:


我查看了您链接到的页面,找不到我正在寻找的内容

I looked through the page you linked to and couldn't find what I was looking for

我是不知道你在找什么以下是一个示例会话,显示其工作原理:

I am not sure what you were looking for. Here is a sample session showing how it works:

#include <stdlib.h>

void foo(char *p)
{
  *p = 'a';
}

typedef struct {
  char buf[1024];
} data;

int main()
{
  data *d = calloc(1, sizeof(data));
  foo(d->buf + 999);
}

gcc -g main.c

valgrind --vgdb-error=0 ./a.out
...
==10345== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==10345==   /path/to/gdb ./a.out
==10345== and then give GDB the following command
==10345==   target remote | vgdb --pid=10345

... Valgrind现在等待调试器附加。

... Valgrind now waits for debugger to attach.

在另一个窗口中:

gdb ./a.out
GNU gdb (GDB) 7.4
...
(gdb) target remote | vgdb --pid=10345
relaying data between gdb and process 10345
[Switching to Thread 10345]
0x0000000004000af0 in _start () from /lib64/ld-linux-x86-64.so.2
(gdb) b main
Breakpoint 1 at 0x40053d: file main.c, line 14.
(gdb) c

Breakpoint 1, main () at main.c:14
14        data *d = calloc(1, sizeof(data));
(gdb) n
15        foo(d->buf + 999);
(gdb) watch *d
Hardware watchpoint 2: *d

注意在整个 * d 中设置了硬件观察点。
这是一个硬件观察点,只是在Valgrind是硬件的意义上。

Note that a "hardware" watchpoint has been set on entire *d. It's a hardware watchpoint only in the sense that Valgrind is the hardware.

(gdb) p d.buf[999]
$1 = 0 '\000'
(gdb) c
Hardware watchpoint 2: *d

Old value = {buf = '\000' <repeats 1023 times>}
New value = {buf = '\000' <repeats 999 times>, "a", '\000' <repeats 23 times>}
foo (p=0x51b6457 "a") at main.c:6
6       }
(gdb) q

Voila:当第999个元素被修改时,调试器停止,证明该观察点覆盖整个结构。

Voila: the debugger stopped when 999th element was modified, proving that the watchpoint "covered" the entire structure.

这篇关于gdb:特定对象被更改时中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆