gpt4 book ai didi

c - 从 Valgrind 源代码访问用户变量

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:55 27 4
gpt4 key购买 nike

我正在尝试用 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 源的用户源代码中读取变量的值?如果我能从用户代码中获取所有变量名,那将是一个加号。

最佳答案

您可以使用 VALGRIND_COUNT_ERRORSVALGRIND_PRINTF Valgrind Client Requests为此。

这里是如何在示例代码中使用它们的方法:

#include <stdio.h>
#include <valgrind/valgrind.h>

int g_int = 12;
int main()
{
int y = 10;
int x;
printf("%d\n",x);
if (VALGRIND_COUNT_ERRORS > 0)
{
VALGRIND_PRINTF("y=%d, g_int=%d\n", y, g_int);
}
return x;
}

Valgrind 输出:

==4030== Memcheck, a memory error detector
==4030== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4030== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4030== Command: ./a.out
==4030==
==4030== Conditional jump or move depends on uninitialised value(s)
==4030== at 0x4E90DDA: vfprintf (in /usr/lib64/libc-2.26.so)
==4030== by 0x4E99285: printf (in /usr/lib64/libc-2.26.so)
==4030== by 0x400719: main (in /home/ks/a.out)
==4030==
==4030== Use of uninitialised value of size 8
==4030== at 0x4E8CDAB: _itoa_word (in /usr/lib64/libc-2.26.so)
==4030== by 0x4E9046D: vfprintf (in /usr/lib64/libc-2.26.so)
==4030== by 0x4E99285: printf (in /usr/lib64/libc-2.26.so)
==4030== by 0x400719: main (in /home/ks/a.out)
==4030==
==4030== Conditional jump or move depends on uninitialised value(s)
==4030== at 0x4E8CDB5: _itoa_word (in /usr/lib64/libc-2.26.so)
==4030== by 0x4E9046D: vfprintf (in /usr/lib64/libc-2.26.so)
==4030== by 0x4E99285: printf (in /usr/lib64/libc-2.26.so)
==4030== by 0x400719: main (in /home/ks/a.out)
==4030==
==4030== Conditional jump or move depends on uninitialised value(s)
==4030== at 0x4E90572: vfprintf (in /usr/lib64/libc-2.26.so)
==4030== by 0x4E99285: printf (in /usr/lib64/libc-2.26.so)
==4030== by 0x400719: main (in /home/ks/a.out)
==4030==
==4030== Conditional jump or move depends on uninitialised value(s)
==4030== at 0x4E9104C: vfprintf (in /usr/lib64/libc-2.26.so)
==4030== by 0x4E99285: printf (in /usr/lib64/libc-2.26.so)
==4030== by 0x400719: main (in /home/ks/a.out)
==4030==
0
**4030** y=10, g_int=12
==4030== Syscall param exit_group(status) contains uninitialised byte(s)
==4030== at 0x4F1A478: _Exit (in /usr/lib64/libc-2.26.so)
==4030== by 0x4E77B3A: __run_exit_handlers (in /usr/lib64/libc-2.26.so)
==4030== by 0x4E77BD9: exit (in /usr/lib64/libc-2.26.so)
==4030== by 0x4E5D040: (below main) (in /usr/lib64/libc-2.26.so)
==4030==
==4030==
==4030== HEAP SUMMARY:
==4030== in use at exit: 0 bytes in 0 blocks
==4030== total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==4030==
==4030== All heap blocks were freed -- no leaks are possible
==4030==
==4030== For counts of detected and suppressed errors, rerun with: -v
==4030== Use --track-origins=yes to see where uninitialised values come from
==4030== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 0 from 0)

变量 g_inty 打印在这一行中:

**4030** y=10, g_int=12

关于c - 从 Valgrind 源代码访问用户变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47425612/

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