从Valgrind源代码访问用户变量

时间:2017-11-22 01:50:41

标签: c profiling valgrind memory-profiling memcheck

我正在尝试使用valgrind源代码进行一些实验。我使用以下代码作为我的测试代码:

#include <stdio.h>
int g_int = 12;
int main()
{
  int y = 10;
  int x;
  printf("%d\n",x);
  return x;
}

我构建了一个名为“test.out”的可执行文件。然后我执行了以下命令:

$./valgrind --tool=memcheck ./test.out

在我的测试代码中,我有一个未初始化的错误和valgrind报告来自“ mc_errors.c ”的错误,给我一些消息,如:

../build/bin$ ./valgrind --tool=memcheck --track-origins=yes --read-var-info=yes ./test >> outpur.txt
==24255== Memcheck, a memory error detector
==24255== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==24255== Using Valgrind-3.14.0.GIT and LibVEX; rerun with -h for copyright info
==24255== Command: ./test
==24255== 
==24255== Conditional jump or move depends on uninitialised value(s)
==24255== I want to print my local variable here!
==24255==    at 0x4E87B83: vfprintf (vfprintf.c:1631)
==24255==    by 0x4E8F898: printf (printf.c:33)
==24255==    by 0x400548: main (test.c:10)
==24255==  Uninitialised value was created by a stack allocation
==24255==    at 0x400526: main (test.c:6)
==24255== 
==24255== Use of uninitialised value of size 8
==24255== I want to print my local variable here!
==24255==    at 0x4E8476B: _itoa_word (_itoa.c:179)
==24255==    by 0x4E8812C: vfprintf (vfprintf.c:1631)
==24255==    by 0x4E8F898: printf (printf.c:33)
==24255==    by 0x400548: main (test.c:10)
==24255==  Uninitialised value was created by a stack allocation
==24255==    at 0x400526: main (test.c:6)
==24255== 
==24255== Conditional jump or move depends on uninitialised value(s)
==24255== I want to print my local variable here!
==24255==    at 0x4E84775: _itoa_word (_itoa.c:179)
==24255==    by 0x4E8812C: vfprintf (vfprintf.c:1631)
==24255==    by 0x4E8F898: printf (printf.c:33)
==24255==    by 0x400548: main (test.c:10)
==24255==  Uninitialised value was created by a stack allocation
==24255==    at 0x400526: main (test.c:6)
==24255== 
==24255== Conditional jump or move depends on uninitialised value(s)
==24255== I want to print my local variable here!
==24255==    at 0x4E881AF: vfprintf (vfprintf.c:1631)
==24255==    by 0x4E8F898: printf (printf.c:33)
==24255==    by 0x400548: main (test.c:10)
==24255==  Uninitialised value was created by a stack allocation
==24255==    at 0x400526: main (test.c:6)
==24255== 
==24255== Conditional jump or move depends on uninitialised value(s)
==24255== I want to print my local variable here!
==24255==    at 0x4E87C59: vfprintf (vfprintf.c:1631)
==24255==    by 0x4E8F898: printf (printf.c:33)
==24255==    by 0x400548: main (test.c:10)
==24255==  Uninitialised value was created by a stack allocation
==24255==    at 0x400526: main (test.c:6)
==24255== 
==24255== Conditional jump or move depends on uninitialised value(s)
==24255== I want to print my local variable here!
==24255==    at 0x4E8841A: vfprintf (vfprintf.c:1631)
==24255==    by 0x4E8F898: printf (printf.c:33)
==24255==    by 0x400548: main (test.c:10)
==24255==  Uninitialised value was created by a stack allocation
==24255==    at 0x400526: main (test.c:6)
==24255== 
==24255== Conditional jump or move depends on uninitialised value(s)
==24255== I want to print my local variable here!
==24255==    at 0x4E87CAB: vfprintf (vfprintf.c:1631)
==24255==    by 0x4E8F898: printf (printf.c:33)
==24255==    by 0x400548: main (test.c:10)
==24255==  Uninitialised value was created by a stack allocation
==24255==    at 0x400526: main (test.c:6)
==24255== 
==24255== Conditional jump or move depends on uninitialised value(s)
==24255== I want to print my local variable here!
==24255==    at 0x4E87CE2: vfprintf (vfprintf.c:1631)
==24255==    by 0x4E8F898: printf (printf.c:33)
==24255==    by 0x400548: main (test.c:10)
==24255==  Uninitialised value was created by a stack allocation
==24255==    at 0x400526: main (test.c:6)
==24255== 
==24255== 
==24255== HEAP SUMMARY:
==24255==     in use at exit: 0 bytes in 0 blocks
==24255==   total heap usage: 1 allocs, 1 frees, 4,096 bytes allocated
==24255== 
==24255== All heap blocks were freed -- no leaks are possible
==24255== 
==24255== For counts of detected and suppressed errors, rerun with: -v
==24255== ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 0 from 0)

现在我想用valgrind的消息(错误报告)打印“ g_int ”和“ y ”的值。我已经在valgrind源打印中添加了一个打印输出:“我想在这里打印我的局部变量!” 有没有办法使用任何内部api从valgrind源的用户源代码中读取变量的值? 如果我可以从用户代码中获取所有变量名称,那将是一个加号。

2 个答案:

答案 0 :(得分:0)

尝试使用选项

  

- 轨迹 - 起源= YES

这将提供有关动态内存的更多信息。

此外,请尝试

  

- 读VAR-信息= YES

这应该与调试版本一起使用(请参阅有关使用-g进行编译的注释)。这将提供有关自动变量的更多信息。

答案 1 :(得分:0)

使用valgrind选项--vgdb-error = 1

有了这个,当valgrind报告错误时,它将等待一个gdb 附上。使用gdb,您可以查看所有本地或全局变量。

有关详细信息,请参阅http://www.valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver