Valgrind 与 GDB 的联合工作不正确

问题描述 投票:0回答:1

我正在尝试将 Valgrind 连接到 GDB。 我写了一个测试代码:

#include <stdio.h>

int main()
{
    int x, i;
    for (i=0; i < 10; i++) {
        if (x < 10)
            printf("First\n");
        else
            printf("Second\n");
        x = i * i;
    }
    return 0;
}

我执行

valgrind --vgdb-error=0 ./badcode
并得到

==123460== Memcheck, a memory error detector
==123460== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==123460== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==123460== Command: ./badcode
==123460== 
==123460== (action at startup) vgdb me ... 
==123460== 
==123460== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==123460==   /path/to/gdb ./badcode
==123460== and then give GDB the following command
==123460==   target remote | /usr/lib/x86_64-linux-gnu/valgrind/../../bin/vgdb --pid=123460
==123460== --pid is optional if only one valgrind process is running
==123460==

然后我在第二个终端窗口中运行它

gdb ./badcode

我跟着它

target remote | vgdb --pid=123460

(valgrind 生成的指令给了我一个错误的 vgdb 路径)

所以我在 GDB 中的会话如下所示:

GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./badcode...
(gdb) target remote | vgdb --pid=123460
Remote debugging using | vgdb --pid=123460
relaying data between gdb and process 123460
warning: remote target does not support file transfer, attempting to access files from local filesystem.
Reading symbols from /lib64/ld-linux-x86-64.so.2...
(No debugging symbols found in /lib64/ld-linux-x86-64.so.2)
0x0000000004001100 in ?? () from /lib64/ld-linux-x86-64.so.2
(gdb) step
Cannot find bounds of current function
(gdb)

显然出了什么问题。所以我关闭 GDB 并再试一次:

GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./badcode...
(gdb) target remote | vgdb --pid=123460
Remote debugging using | vgdb --pid=123460
relaying data between gdb and process 123460
warning: remote target does not support file transfer, attempting to access files from local filesystem.
Reading symbols from /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_core-amd64-linux.so...
(No debugging symbols found in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_core-amd64-linux.so)
Reading symbols from /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so...
(No debugging symbols found in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...
Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.31.so...
Reading symbols from /lib64/ld-linux-x86-64.so.2...
(No debugging symbols found in /lib64/ld-linux-x86-64.so.2)
0x0000000000109162 in main () at badcode.c:7
7           if (x < 10)
(gdb) step
8               printf("First\n");
(gdb) step
__GI__IO_puts (str=0x10a004 "First") at ioputs.c:33
33  ioputs.c: There is no such file or directory.
(gdb)

起初看起来一切正常,但两步后 GDB 在

printf
函数处中断。 完整的 Valgrind 日志如下:

==123460== Memcheck, a memory error detector
==123460== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==123460== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==123460== Command: ./badcode
==123460== 
==123460== (action at startup) vgdb me ... 
==123460== 
==123460== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==123460==   /path/to/gdb ./badcode
==123460== and then give GDB the following command
==123460==   target remote | /usr/lib/x86_64-linux-gnu/valgrind/../../bin/vgdb --pid=123460
==123460== --pid is optional if only one valgrind process is running
==123460== 
==123460== 
==123460== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==123460==   /path/to/gdb ./badcode
==123460== and then give GDB the following command
==123460==   target remote | /usr/lib/x86_64-linux-gnu/valgrind/../../bin/vgdb --pid=123460
==123460== --pid is optional if only one valgrind process is running
==123460== 
==123460== Conditional jump or move depends on uninitialised value(s)
==123460==    at 0x109162: main (badcode.c:7)
==123460== 
==123460== (action on error) vgdb me ... 
==123460== Continuing ...
==123460== 
==123460== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==123460==   /path/to/gdb ./badcode
==123460== and then give GDB the following command
==123460==   target remote | /usr/lib/x86_64-linux-gnu/valgrind/../../bin/vgdb --pid=123460
==123460== --pid is optional if only one valgrind process is running
==123460== 
First
First
First
First
First
Second
Second
Second
Second
Second
==123460== 
==123460== HEAP SUMMARY:
==123460==     in use at exit: 0 bytes in 0 blocks
==123460==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==123460== 
==123460== All heap blocks were freed -- no leaks are possible
==123460== 
==123460== Use --track-origins=yes to see where uninitialised values come from
==123460== For lists of detected and suppressed errors, rerun with: -s
==123460== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Ubuntu 20.04.3 LTS Valgrind-3.15.0

可以采取什么措施来解决这个问题?

c linux debugging gdb valgrind
1个回答
0
投票

Valgrind-3.15.0 有错误,此输出建议提供给 GDB 是 不正确:vgdb 位于 /usr/bin/vgdb,而不是输出所示的位置。

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=942532

Debian 有一个蹩脚的 Valgrind 软件包,它使用的包装脚本无法正常工作。在更高版本中,他们使用硬编码路径修补二进制文件。

建议使用最新版本的Valgrind(现在是valgrind-3.21.0)。

https://valgrind.org/downloads/下载源代码(选择Valgrind最新版本V3.21.0)。然后编译并安装(./configure, make, make install)参见 https://valgrind.org/docs/manual/manual-intro.html

我用的是valgrind-3.21.0就可以了

见下图:

https://imgur.com/a/9aiWQ2M

© www.soinside.com 2019 - 2024. All rights reserved.