【valgrind】软件调试工具-valgrind内存调试工具
valgrind工具安装
Valgrind是一套Linux下的仿真调试工具工具的集合。由内核core以及基于内核的其他调试工具组成。内核类似与一个框架(framework),它模拟了一个CPU环境并提供服务给其他工具,而其他公户类似于插件(plug-in),利用内核提供各种特定的内存调试任务。valgrind体系结构:
Memcheck:内存检查器,能够发现绝大多数内存错误使用情况
Callgrind:用于检查程序中函数调用过程中出现的问题
Cachegrind:主要用于检查程序中缓存使用出现的问题
Helgrind:主要用于检查多线程程序中出现的竞争问题
Massif:主要用于检查程序中堆栈使用中的出现的问题
Extension:可以利用core提供的功能,编写特定的内存调试工具
Ubuntu环境安装
sudo apt install valgrind
源码编译
1.源码下载
http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2
2.valgrind编译安装
tar -jxvf valgrind-3.12.0.tar.bz2
cd valgrind-3.12.0
./configure
make
sudo make install
aarch64架构可使用rpm包
搜索valgrind-3.13.0-29.oe1.aarch64.rpm下载,并安装:
rpm -vhi --nodeps ./valgrind-3.13.0-29.oe1.aarch64.rpm
valgrind运行分析程序
valgrind - memcheck
使用示例
编写测试代码(内存泄漏)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *array = malloc(sizeof(int)); // 申请后不释放,内存泄漏4个字节
return 0;
}
编译前参数设置
如果想要调试工具输出具体问题出现在源码中的行号最好使用debug编译,编译等级建议-O2以下,属性设置为unstripped。
stripped和not stripped的区别:
stripped:将程序中的符号表的信息剔除掉了,这样子编译出来的可执行文件体积比较小;
not stripped:则反之,但是就是因为其保留了这些信息,所以便于调试。
debug + unstripped
有debug信息和无stripped是最理想情况,能详细列出问题函数及第几行。
Invalid write of size 1
at 0x80483BF: really (malloc1.c:20)
by 0x8048370: main (malloc1.c:9)
no debug + unstripped
无stripped但没有debug信息的情况,能指出哪个文件的哪个函数,但需要自行addr2line换算行数。
Invalid write of size 1
at 0x80483BF: really (in /auto/homes/njn25/grind/head5/a.out)
by 0x8048370: main (in /auto/homes/njn25/grind/head5/a.out)
no debug + stripped
除特殊场景之外,这差不多是最差的情况了。只有文件跟虚拟地址。
Invalid write of size 1
at 0x80483BF: (within /auto/homes/njn25/grind/head5/a.out)
by 0x8048370: (within /auto/homes/njn25/grind/head5/a.out)
by 0x42015703: __libc_start_main (in /lib/tls/libc-2.3.2.so)
by 0x80482CC: (within /auto/homes/njn25/grind/head5/a.out)
编译
gcc main.c -g -o test # -g表示增加gdb调试信息,能详细打印出来栈信息
执行valgrind分析
valgrind --tool=memcheck --leak-check=full ./test
输出信息
实际调试遇到问题
[root@xxx ~]# valgrind --tool=memcheck --leak-check=full /xxx/xxx/xxx/xxx/bin/xxxx /xxx/xx/xxxx/xxxxxx/xxx/xxxxxx.txt
==1259== Memcheck, a memory error detector
==1259== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1259== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==1259== Command: /xxx/xxx/xxx/xxx/bin/xxxx /xxx/xx/xxxx/xxxxxx/xxx/xxxxxx.txt
==1259==
valgrind: Fatal error at startup: a function redirection
valgrind: which is mandatory for this platform-tool combination
valgrind: cannot be set up. Details of the redirection are:
valgrind:
valgrind: A must-be-redirected function
valgrind: whose name matches the pattern: strlen
valgrind: in an object with soname matching: ld-linux-aarch64.so.1
valgrind: was not found whilst processing
valgrind: symbols from the object with soname: ld-linux-aarch64.so.1
valgrind:
valgrind: Possible fixes: (1, short term): install glibc's debuginfo
valgrind: package on this machine. (2, longer term): ask the packagers
valgrind: for your Linux distribution to please in future ship a non-
valgrind: stripped ld.so (or whatever the dynamic linker .so is called)
valgrind: that exports the above-named function using the standard
valgrind: calling conventions for this platform. The package you need
valgrind: to install for fix (1) is called
valgrind:
valgrind: On Debian, Ubuntu: libc6-dbg
valgrind: On SuSE, openSuSE, Fedora, RHEL: glibc-debuginfo
valgrind:
valgrind: Note that if you are debugging a 32 bit process on a
valgrind: 64 bit system, you will need a corresponding 32 bit debuginfo
valgrind: package (e.g. libc6-dbg:i386).
valgrind:
valgrind: Cannot continue -- exiting now. Sorry.
看log是需要安装libc6-dbg库,但是看了一部分帖子,说是因为链接库ld-linux-aarch64.so.1是stripped属性的缘故,正在解决中...
valgrind - massif
massif工具简介
massif工具使用
运行massif,分析数据会都写入massif.out.
valgrind --tool=massif /xxx/xxx/xxx/xxx/bin/xxxx /xxx/xx/xxxx/xxxxxx/xxx/xxxxxx.txt # 运行带参数解析(.txt)的程序
valgrind --tool=massif ./test # 运行可执行程序
程序运行一段时间后,发送信号停掉程序(ctrl+c),然后使用ms_print将调试信息输出到massif.log文件中,方便观看
ms_print massif.out.*** >massif.log
使用可视化工具查看:
安装massif-visualizer:
sudo apt install massif-visualizer
massif-visualizer massif.out.xxx # 可视化
实际调试过程
板端安装valgrind 的rpm包(依赖安装不全,不支持在板端ms_print),抓出massif.out.xxx后,传到pc端使用Ubuntu下ms_print输出log。也可以使用massif-visualizer直接可视化分析程序堆占用情况。
参考:
https://valgrind.org/docs/manual/ms-manual.html
https://www.cnblogs.com/gmpy/p/14778243.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!