嵌入式linux如何使用valgrind进行内存调试 (嵌入式linux valgrind)

嵌入式Linux如何使用valgrind进行内存调试

内存错误是嵌入式系统开发中最常见的问题之一。它可能导致不可预测的结果,甚至引起系统崩溃。在开发过程中,为了检测和修复内存错误,通常需要使用一些工具,其中一个很有用的工具是Valgrind。

Valgrind是一个免费的开源工具集,用于更好地调试嵌入式系统中的程序。它可以检测内存泄漏、使用未初始化的内存、越界访问和其他内存错误。本文将介绍如何在嵌入式Linux系统中使用Valgrind进行内存调试。

Step 1:安装Valgrind

要使用Valgrind进行内存调试,首先需要将其安装到开发机器上。 在Ubuntu中,可以使用以下命令来安装Valgrind:

`sudo apt-get install valgrind`

Step 2:交叉编译Valgrind

Valgrind是为x86架构编写的,因此需要交叉编译它以便在嵌入式系统上运行。 在此之前,需要安装交叉编译工具链。下面是一个例子:

`sudo apt-get install gcc-arm-linux-gnueabihf`

接下来,在Valgrind源代码目录中执行以下命令进行交叉编译:

“`

./configure –host=arm-linux

make

“`

编译完成后,将可执行文件拷贝到嵌入式系统的文件系统中。

Step 3:在嵌入式系统中使用Valgrind

将交叉编译的Valgrind可执行文件拷贝到嵌入式系统的文件系统中后,可以使用以下命令来运行程序:

`valgrind –tool=memcheck –verbose –leak-check=full my_program`

这个命令中,`–tool` 指定要使用的Valgrind工具(在本例中,我们使用memcheck来检查内存错误),`–verbose` 告诉Valgrind输出更详细的信息, `–leak-check` 开启内存泄漏检查。

运行后,Valgrind会输出有关内存错误的详细信息,例如未释放的内存块或访问越界的内存。修复这些错误将有助于提高程序的稳定性和可靠性。

Valgrind是一个非常实用的工具,可以帮助我们在嵌入式Linux系统中检测和修复内存错误。它可以检测未初始化的内存、内存泄漏、访问越界等错误。虽然它需要交叉编译才能在嵌入式系统上运行,但它的强大功能使它成为嵌入式Linux开发中不可或缺的工具之一。

相关问题拓展阅读:

如何使用Valgrind memcheck工具进行C/C++的内存泄漏检测

使用 Valgrind Memcheck

memcheck工具的使用方式如下:

valgrind –tool=memcheck ./a.out

从上面的命令可以清楚的看到, 主要的命令是valgrind,而我们想使用的工具是通过’-tool’选项来指定的. 上面的‘a.out’指的是做腔肆我们想使用memcheck运行的可执行文件.

该工具可以检测下列与内存相关的问题 :

未释放内存的使用

对释放后内存的读/写

对已分配内存圆租块尾部的读/写

内存泄露

不匹配的使用malloc/new/new 和 free/delete/delete

重复释放内存

注意: 上面列出的并不很全面,但却包含了能被该工具检测到的很多普遍的问题.

让我们一个一个地对上面的场景进行讨论:

注意: 下面讨论的所有测试代码都应该使用gcc并且加上-g选项(用来在memcheck的输出中生成行号)进行编译. 就想我们之前讨论过的 C程序被编译成可执行文件, 它需要经历四个不同的阶段.

ToB蓝波湾

翻译于 1 年 前

0人顶

顶 翻译的不错哦!

1. 使用未初始化的内存

Code :

#include

#include

int main(void)

{

char *p;

char c = *p;

printf(“\n \n”,c);

return 0;

}

在上面的代码中纯轿,我们尝试使用未初始化的指针 ‘p’.

让我们运行Memcheck来看下结果.

$ valgrind –tool=memcheck ./val

==2862== Memcheck, a memory error detector

==2862== Copyright (C), and GNU GPL’d, by Julian Seward et al.

==2862== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info

==2862== Command: ./val

==2862==

==2862== Use of uninitialised value of size 8

==2862== at 0x400530: main (valgrind.c:8)

==2862==

==2862==

==2862== HEAP SUMMARY:

==2862== in use at exit: 0 bytes in 0 blocks

==2862== total heap usage: 0 allocs, 0 frees, 0 bytes allocated

==2862==

==2862== All heap blocks were freed — no leaks are possible

==2862==

==2862== For counts of detected and suppressed errors, rerun with: -v

==2862== Use –track-origins=yes to see where uninitialized values come from

==2862== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

从上面的输出可以看到,Valgrind检测到了未初始化的变量,然后给出了警告(上面加粗的几行(译者注:貌似上面没有加粗的)).

2. 在内存被释放后进行读/写

Code :

#include

#include

int main(void)

{

char *p = malloc(1);

*p = ‘a’;

char c = *p;

printf(“\n \n”,c);

free(p);

c = *p;

return 0;

}

上面的代码中,我们有一个释放了内存的指针 ‘p’ 然后我们又尝试利用指针获取值.

让我们运行memcheck来看一下Valgrind对这种情况是如何反应的.

$ valgrind –tool=memcheck ./val

==2849== Memcheck, a memory error detector

==2849== Copyright (C), and GNU GPL’d, by Julian Seward et al.

==2849== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info

==2849== Command: ./val

==2849==

==2849== Invalid read of size 1

==2849== at 0x400603: main (valgrind.c:30)

==2849== Address 0x51b0040 is 0 bytes inside a block of size 1 free’d

==2849== at 0x4C270BD: free (vg_replace_malloc.c:366)

==2849== by 0x4005FE: main (valgrind.c:29)

==2849==

==2849==

==2849== HEAP SUMMARY:

==2849== in use at exit: 0 bytes in 0 blocks

==2849== total heap usage: 1 allocs, 1 frees, 1 bytes allocated

==2849==

==2849== All heap blocks were freed — no leaks are possible

==2849==

==2849== For counts of detected and suppressed errors, rerun with: -v

==2849== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

从上面的输出内容可以看到,Valgrind检测到了无效的读取操作然后输出了警告 ‘Invalid read of size 1′.

另注,使用gdb来调试c程序.

3. 从已分配内存块的尾部进行读/写

Code :

#include

#include

int main(void)

{

char *p = malloc(1);

*p = ‘a’;

char c = *(p+1);

printf(“\n \n”,c);

free(p);

return 0;

}

在上面的代码中,我们已经为‘p’分配了一个字节的内存,但我们在将值读取到 ‘c’中的时候使用的是地址p+1.

现在我们使用Valgrind运行上面的代码 :

$ valgrind –tool=memcheck ./val

==2835== Memcheck, a memory error detector

==2835== Copyright (C), and GNU GPL’d, by Julian Seward et al.

==2835== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info

==2835== Command: ./val

==2835==

==2835== Invalid read of size 1

==2835== at 0x4005D9: main (valgrind.c:25)

==2835== Address 0x51b0041 is 0 bytes after a block of size 1 alloc’d

==2835== at 0x4C274A8: malloc (vg_replace_malloc.c:236)

==2835== by 0x4005C5: main (valgrind.c:22)

==2835==

==2835==

==2835== HEAP SUMMARY:

==2835== in use at exit: 0 bytes in 0 blocks

==2835== total heap usage: 1 allocs, 1 frees, 1 bytes allocated

==2835==

==2835== All heap blocks were freed — no leaks are possible

==2835==

==2835== For counts of detected and suppressed errors, rerun with: -v

==2835== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

同样,该工具在这种情况下也检测到了无效的读取操作.

4. 内存泄露

Code:

#include

#include

int main(void)

{

char *p = malloc(1);

*p = ‘a’;

char c = *p;

printf(“\n \n”,c);

return 0;

}

在这次的代码中, 我们申请了一个字节但是没有将它释放.现在让我们运行Valgrind看看会发生什么:

$ valgrind –tool=memcheck –leak-check=full ./val

==2888== Memcheck, a memory error detector

==2888== Copyright (C), and GNU GPL’d, by Julian Seward et al.

==2888== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info

==2888== Command: ./val

==2888==

==2888==

==2888== HEAP SUMMARY:

==2888== in use at exit: 1 bytes in 1 blocks

==2888== total heap usage: 1 allocs, 0 frees, 1 bytes allocated

==2888==

==2888== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1

==2888== at 0x4C274A8: malloc (vg_replace_malloc.c:236)

==2888== by 0x400575: main (valgrind.c:6)

==2888==

==2888== LEAK SUMMARY:

==2888== definitely lost: 1 bytes in 1 blocks

==2888== indirectly lost: 0 bytes in 0 blocks

==2888== possibly lost: 0 bytes in 0 blocks

==2888== still reachable: 0 bytes in 0 blocks

==2888==suppressed: 0 bytes in 0 blocks

==2888==

==2888== For counts of detected and suppressed errors, rerun with: -v

==2888== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

输出行(上面加粗的部分)显示,该工具能够检测到内存的泄露.

1、用工具,linux下有免费的valgrind2、渗悉在代码里集成memcheck代码,vc理由,还有开枝衫源的猛喊腔代码库

2、如何用Valgrind 检查后台服务进程的内存使用情况

这个需要进进程管理查看

如何打开进程管理器

快此或捷键Ctrl+Alt+方向键下键(或者小键盘的Enter键)

另一种方法,就是鼠标移至屏幕下方的图标栏,右击鼠标,选择任务管理器

在任务管理器–进程里面就可以看到那些那芹扒基些程序正在运行

如果想嫌谨看CPU占用率排行,点击一下CPU就会把CPU占用率更高的按顺序排布下来

Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析。你可以在它的环境中运行你的程序来监视内存的使用情况,比烂闷磨如C 语言中的malloc和free或者 C++中的new和 delete。使用Valgrind的工具包,你可以自动的检测许多内存管理和线程的bug,避免罩闹花费太多的时间在bug寻找上,使得你的程序更加稳固。

Valgrind的主要功能

Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif。下面分别介绍个工具的作用:

Memcheck 工具主要检查下面的程序错误:

使用未初始化的内存 (Use of uninitialised memory)

使用饥斗已经释放了的内存 (Reading/writing memory after it has been free’d)

使用超过 malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)

对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)

申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)

malloc/free/new/delete申请和释放内存的匹配(Miatched use of malloc/new/new vs free/delete/delete )

src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)

返回列表

上一篇:redhat linux 5.5安装教程

嵌入式linux valgrind的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于嵌入式linux valgrind,嵌入式linux如何使用valgrind进行内存调试,如何使用Valgrind memcheck工具进行C/C++的内存泄漏检测的信息别忘了在本站进行查找喔。


数据运维技术 » 嵌入式linux如何使用valgrind进行内存调试 (嵌入式linux valgrind)