gpt4 book ai didi

c - 使用 valgrind 和 gdb 跟踪变量更改

转载 作者:行者123 更新时间:2023-12-04 18:16:44 27 4
gpt4 key购买 nike

我有一个程序在执行 > 5 小时后发出 SIGABRT。这很可能是由 valgrind 检查后的内存泄漏引起的,但是我有问题根据 valgrind 报告(它只包含地址和???)来追踪究竟是哪个变量导致了这个问题。

我尝试使用 valgrind 和 gdb 来逐步完成。但是,由于需要 5 小时才能到达泄漏点(循环 428 轮之后),我想设置一个断点,比如说,当 loop=428 时,然后进入代码。我怎样才能做到这一点?

根据下面的一个简单程序,我可以知道,

a)如何跟踪变量'a'中的值变化?

b) 当loop = 428时如何设置断点?

typedef struct data_attr {
int a[2500];
}stdata;

typedef struct pcfg{
stdata *data;
}stConfig;

int funcA(stConfig* pt){

int loop = 0;

while (loop < NUM_NODE){
pt->data->a[0] = 1000;
pt->data->a[0] = 1001;
loop++;
}
return 0;
}

int main(){
stConfig *p;

p = (stConfig*) malloc(sizeof(stConfig));
p->data = (stdata*) malloc (sizeof(stdata));

funcA(p);

free(p->data);
free (p);

return 0;
}

我在 ubuntu 10.04 上使用 valgrind 3.7

@valgrind 终端,

valgrind -v --vgdb=yes --vgdb-error=0 --tool=memcheck --leak-check=full --leak-resolution=high --num-callers=40 --track-origins=yes --log-file=mr3m1n2500_valgrind_0717_1155.txt ./pt m >& mr3m1n2500_logcheck_0717_1155.txt



@gdb 终端
我试图获取'p'的地址,但它返回无效,为什么?
> gdb ./pt
(gdb) target remote | vgdb
Remote debugging using | vgdb
relaying data between gdb and process 12857
Reading symbols from /lib/ld-linux.so.2...Reading symbols from /usr/lib/debug/lib/ld-2.11.1.so...done.
done.
Loaded symbols for /lib/ld-linux.so.2
[Switching to Thread 12857]
0x04000850 in _start () from /lib/ld-linux.so.2
(gdb) p $p
$1 = void
(gdb) bt 10
#0 0x04000850 in _start () from /lib/ld-linux.so.2

最佳答案

  • 要跟踪变量值的变化,您可以在该变量上设置观察点。
    对于您的情况,请使用:watch p->data->a[index]
  • 要在所需条件下中断,您可以使用 break break if loop_counter==428
  • 关于c - 使用 valgrind 和 gdb 跟踪变量更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11517271/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com