debugging Valgrind与GDB的连接工作不正确

rbl8hiat  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(138)

我想把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

(the 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
可以做些什么来解决这个问题?

13z8s7eq

13z8s7eq1#

Valgrind-3.15.0有一个错误,这个输出建议给予GDB是不正确的:vgdb位于/usr/bin/vgdb,而不是输出所建议的位置。
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=942532
建议使用最新版本的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

相关问题