深入理解valgrind在armlinux平台中的应用 (valgrind armlinux)

深入理解Valgrind在ARM Linux平台中的应用

Valgrind是一个强大的开发工具,它可以帮助开发者识别程序中的各种错误和问题。本文将深入探讨Valgrind在ARM Linux平台中的应用。我们将介绍Valgrind的基本原理、它在ARM Linux平台中的特性和优势,以及它如何帮助我们开发高质量的应用程序。

Valgrind基本原理

Valgrind是基于动态二进制翻译技术的虚拟机。它运行在用户空间,同时修改CPU硬件的行为,使得CPU可以在它的上下文中执行二进制代码。在这个目的下,Valgrind对二进制代码进行解析,并进行诸如内存读/写访问检测、指针跟踪、内存泄漏检测、调用堆栈跟踪等工作,以识别程序中的各种错误和问题。

Valgrind适用于多种平台,如x86、ARM、PowerPC等。它实现了一个通用的框架,可以支持各种不同的硬件和操作系统平台。在ARM Linux平台中,Valgrind的特性和优势有许多方面。

Valgrind在ARM Linux平台中的特性和优势

Valgrind在ARM Linux平台上的特性和优势有许多方面。Valgrind可以进行高效的指令仿真,这对于大规模程序的调试和测试非常有利。Valgrind可以对ARM指令体系结构的每个操作模拟,包括所有指令的寄存器读/写、内存读/写、控制流等。这使得Valgrind可以非常精确地检查程序的行为。此外,Valgrind还提供了强大的内存调试功能,包括内存泄漏检测、无效指针跟踪、堆栈溢出检测等等。

Valgrind在ARM Linux平台上的应用场景也很丰富。它可以用于调试多线程程序、嵌入式程序、驱动程序和系统软件等复杂的软件系统。同时,Valgrind可以与其他工具一起使用,如GDB、objdump等,以便更好地理解程序的行为。在开发嵌入式系统时,Valgrind可以用于在开发板上进行快速原型开发和调试,提高团队的开发效率。

Valgrind的使用方法

在ARM Linux平台上使用Valgrind非常简单。需要安装Valgrind的ARM版本到开发板上。然后,在Valgrind的环境下运行需要测试的程序即可。可以使用类似如下的命令行:

$ valgrind –leak-check=full

此命令将会使用Valgrind分析程序,进行内存泄漏检测并输出相关信息。此外,还可以指定其他选项,如–track-origins=yes选项可以跟踪指针的来源,帮助识别错误。

结论

相关问题拓展阅读:

linux数组越界漏洞怎么利用

Linux c/c++上常用内存泄露检测工具有valgrind, Rational purify。Valgrind免费。Valgrind可以在 32位或64位 PowerPC/Linux内核上工作。

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)

Valgrind不检查静态分配数组的使用情况。

Valgrind占用了更多的内存–可达两倍于你程序的正常使用量。如果你用Valgrind来检测使用大量内存的程序就会遇到问题,它可能会用很长的时间来运行测试

2.1. 下载安装

安装

./configure;make;make install

2.2. 编译程序

被检测程序加入–g -fno-inline 编译选项保留调试信息。

2.3. 内存泄露检测

$ valgrind –tool=memcheck –log-file=/root/valgrind_log_all –leak-check=full –error-limit=no –show-reachable=yes –trace-children=yes /usr/local/sdata/in/rpcbakupsvr

其中–leak-check=full 指的是完全检查内存泄漏,–show-reachable=yes是显示内存泄漏的地点,–trace-children=yes是跟入子进程。当程序正常退出的时候valgrind自然会输出内存泄漏的信知扮息。

1.内存泄露:

#include void function()

{ int *p = (int*)malloc(10*sizeof(int)); p = 0;

}int main()

{ function(); return 0;

}

相关日志:

==20230== Memcheck, a memory error detector

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

==20230== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info

==20230== Command: ./test

==20230== Parent PID: 20230

==20230==

==20230== Invalid write of size 4

==20230== at 0x80483FF: function (in /mnt/Documents/Training/valgrind/test)

==20230== by 0x: main (in /mnt/Documents/Training/valgrind/test)

==20230== Address 0x41be050 is 0 bytes after a block of size 40 alloc’d

==20230== at 0x: malloc (vg_replace_malloc.c:236)

==20230== by 0x80483F5: function (in /mnt/Documents/Training/valgrind/test)

==20230== by 0x: main (in /mnt/Documents/Training/valgrind/test)

==20230==

==20230==

==20230== HEAP SUMMARY:

==20230== in use at exit: 40 bytes in 1 blocks

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

==20230==

==20230== LEAK SUMMARY:

==20230== definitely lost: 40 bytes in 1 blocks

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

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

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

==20230==suppressed: 0 bytes in 0 blocks

==20230== Rerun with –leak-check=full to see details of leaked memory

==20230==

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

==20230== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)

2.使用未初始化的内存

#include int main()

{ int a; if (a==1)

{printf(“a==%d\n”,a);

} return 0;

}

日志分析:

==20235== Memcheck, a memory error detector

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

==20235== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info

==20235== Command: ./test

==20235==

==20235== Conditional jump or move depends on uninitialised value(s)

==20235== at 0x80483F2: main (in /mnt/Documents/Training/valgrind/test)

==20235==

==20235==

==20235== HEAP SUMMARY:

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

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

==20235==

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

==20235==

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

==20235== Use –track-origins=yes to see where uninitialised values come from

==20235== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)

可以使用–track-origins=yes 得到更多的信息

3.内存读写越界

#include int main()

{ int *a = (int*)malloc(5*sizeof(int)); a = 1; return 0;

}

==20238== Memcheck, a memory error detector

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

==20238== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info

==20238== Command: ./test

==20238==

==20238== Invalid write of size 4

==20238== at 0x: main (in /mnt/Documents/Training/valgrind/test)

==20238== Address 0x41be03c is 0 bytes after a block of size 20 alloc’d

==20238== at 0x: malloc (vg_replace_malloc.c:236)

==20238== by 0x80483F8: main (in /mnt/Documents/Training/valgrind/test)

==20238==

==20238==

==20238== HEAP SUMMARY:

==20238== in use at exit: 20 bytes in 1 blocks

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

==20238==

==20238== LEAK SUMMARY:

==20238== definitely lost: 20 bytes in 1 blocks

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

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

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

==20238==suppressed: 0 bytes in 0 blocks

==20238== Rerun with –leak-check=full to see details of leaked memory

==20238==

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

==20238== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)

4.内存申请释放管理错误

#include int main()

{ int *a = new int; /*free(a);*/ delete a; return 0;

关于valgrind armlinux的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » 深入理解valgrind在armlinux平台中的应用 (valgrind armlinux)