内存调试——valgrind工具对数组访问错误和内存泄漏的检测

下面的 C 程序分配了1024字节的内存,然后从分配的内存以外的区域读取数据,在分配内存尾部之后写数据,最后将该内存区域变得不可访问。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char *ptr = (char *)malloc( 1024 );
    char ch;
    //Uninitialized read
    ch = ptr[1024];
    //Write beyond the block
    ptr[1024] = 0;
    //Orphan the block
    ptr = 0;
    return 0;
}

下面是运行valgrind工具检测的结果:3处内存管理方面的错误全部查出。

[[email protected] document]$ valgrind --leak-check=yes -v ./checker
==19044== Memcheck, a memory error detector
==19044== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==19044== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==19044== Command: ./checker
==19044==
--19044-- Valgrind options:
--19044--    --leak-check=yes
--19044--    -v
--19044-- Contents of /proc/version:
--19044--   Linux version 2.6.32-504.3.3.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) ) #1 SMP Wed Dec 17 01:55:02 UTC 2014
--19044-- Arch and hwcaps: AMD64, LittleEndian, amd64-cx16-rdtscp-sse3-avx
--19044-- Page sizes: currently 4096, max supported 4096
--19044-- Valgrind library directory: /usr/local/lib/valgrind
--19044-- Reading syms from /home/zhang/document/checker
--19044-- Reading syms from /usr/local/lib/valgrind/memcheck-amd64-linux
--19044--    object doesn't have a dynamic symbol table
--19044-- Reading syms from /lib64/ld-2.12.so
--19044-- Scheduler: using generic scheduler lock implementation.
--19044-- Reading suppressions file: /usr/local/lib/valgrind/default.supp
==19044== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-19044-by-zhang-on-localhost.localdomain
==19044== embedded gdbserver: writing to   /tmp/vgdb-pipe-to-vgdb-from-19044-by-zhang-on-localhost.localdomain
==19044== embedded gdbserver: shared mem   /tmp/vgdb-pipe-shared-mem-vgdb-19044-by-zhang-on-localhost.localdomain
==19044==
==19044== TO CONTROL THIS PROCESS USING vgdb (which you probably
==19044== don't want to do, unless you know exactly what you're doing,
==19044== or are doing some strange experiment):
==19044==   /usr/local/lib/valgrind/../../bin/vgdb --pid=19044 ...command...
==19044==
==19044== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==19044==   /path/to/gdb ./checker
==19044== and then give GDB the following command
==19044==   target remote | /usr/local/lib/valgrind/../../bin/vgdb --pid=19044
==19044== --pid is optional if only one valgrind process is running
==19044==
--19044-- REDIR: 0x3283e17610 (ld-linux-x86-64.so.2:strlen) redirected to 0x38051201 (vgPlain_amd64_linux_REDIR_FOR_strlen)
--19044-- Reading syms from /usr/local/lib/valgrind/vgpreload_core-amd64-linux.so
--19044-- Reading syms from /usr/local/lib/valgrind/vgpreload_memcheck-amd64-linux.so
==19044== WARNING: new redirection conflicts with existing -- ignoring it
--19044--     old: 0x3283e17610 (strlen              ) R-> (0000.0) 0x38051201 vgPlain_amd64_linux_REDIR_FOR_strlen
--19044--     new: 0x3283e17610 (strlen              ) R-> (2007.0) 0x04a08960 strlen
--19044-- REDIR: 0x3283e17480 (ld-linux-x86-64.so.2:index) redirected to 0x4a08540 (index)
--19044-- REDIR: 0x3283e17500 (ld-linux-x86-64.so.2:strcmp) redirected to 0x4a09320 (strcmp)
--19044-- REDIR: 0x3283e183f0 (ld-linux-x86-64.so.2:mempcpy) redirected to 0x4a0bd80 (mempcpy)
--19044-- Reading syms from /lib64/libc-2.12.so
--19044-- REDIR: 0x3284284cd0 (libc.so.6:strcasecmp) redirected to 0x480155c (_vgnU_ifunc_wrapper)
--19044-- REDIR: 0x3284286f90 (libc.so.6:strncasecmp) redirected to 0x480155c (_vgnU_ifunc_wrapper)
--19044-- REDIR: 0x3284282c40 (libc.so.6:__GI_strrchr) redirected to 0x4a082d0 (__GI_strrchr)
--19044-- REDIR: 0x328427a640 (libc.so.6:malloc) redirected to 0x4a07183 (malloc)
==19044== Invalid read of size 1
==19044==    at 0x4004E4: main (in /home/zhang/document/checker)
==19044==  Address 0x4c2b440 is 0 bytes after a block of size 1,024 alloc'd
==19044==    at 0x4A0720A: malloc (vg_replace_malloc.c:296)
==19044==    by 0x4004D5: main (in /home/zhang/document/checker)
==19044==
==19044== Invalid write of size 1
==19044==    at 0x4004F4: main (in /home/zhang/document/checker)
==19044==  Address 0x4c2b440 is 0 bytes after a block of size 1,024 alloc'd
==19044==    at 0x4A0720A: malloc (vg_replace_malloc.c:296)
==19044==    by 0x4004D5: main (in /home/zhang/document/checker)
==19044==
--19044-- REDIR: 0x328427b520 (libc.so.6:free) redirected to 0x4a06b5d (free)
==19044==
==19044== HEAP SUMMARY:
==19044==     in use at exit: 1,024 bytes in 1 blocks
==19044==   total heap usage: 1 allocs, 0 frees, 1,024 bytes allocated
==19044==
==19044== Searching for pointers to 1 not-freed blocks
==19044== Checked 64,184 bytes
==19044==
==19044== 1,024 bytes in 1 blocks are definitely lost in loss record 1 of 1
==19044==    at 0x4A0720A: malloc (vg_replace_malloc.c:296)
==19044==    by 0x4004D5: main (in /home/zhang/document/checker)
==19044==
==19044== LEAK SUMMARY:
==19044==    definitely lost: 1,024 bytes in 1 blocks
==19044==    indirectly lost: 0 bytes in 0 blocks
==19044==      possibly lost: 0 bytes in 0 blocks
==19044==    still reachable: 0 bytes in 0 blocks
==19044==         suppressed: 0 bytes in 0 blocks
==19044==
==19044== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 4 from 4)
==19044==
==19044== 1 errors in context 1 of 3:
==19044== Invalid write of size 1
==19044==    at 0x4004F4: main (in /home/zhang/document/checker)
==19044==  Address 0x4c2b440 is 0 bytes after a block of size 1,024 alloc'd
==19044==    at 0x4A0720A: malloc (vg_replace_malloc.c:296)
==19044==    by 0x4004D5: main (in /home/zhang/document/checker)
==19044==
==19044==
==19044== 1 errors in context 2 of 3:
==19044== Invalid read of size 1
==19044==    at 0x4004E4: main (in /home/zhang/document/checker)
==19044==  Address 0x4c2b440 is 0 bytes after a block of size 1,024 alloc'd
==19044==    at 0x4A0720A: malloc (vg_replace_malloc.c:296)
==19044==    by 0x4004D5: main (in /home/zhang/document/checker)
==19044==
--19044--
--19044-- used_suppression:      4 U1004-ARM-_dl_relocate_object /usr/local/lib/valgrind/default.supp:1401
==19044==
==19044== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 4 from 4)

其中,valgrind工具可以在http://valgrind.org上找到它。

时间: 2024-11-03 05:32:22

内存调试——valgrind工具对数组访问错误和内存泄漏的检测的相关文章

内存问题排查工具 --- valgrind

1. 概述 2. Valgrind 3. 内存泄漏监测 3.1. 示例代码 3.2. 编译它 3.3. 用Valgrind监测进程的内存泄漏 4. 悬挂指针 4.1. 示例代码 4.2. Valgrind运行结果 5. 多次释放同一个指针 5.1. 示例代码 5.2. Valgrind 监测 6. Valgrind的优缺点 6.1. Advantages 6.2. Disadvantages 7. Valgrind的其他工具 7.1. Cachegrind 7.2. Callgrind 7.3.

Linux下利用Valgrind工具进行内存泄露检测和性能分析

from http://www.linuxidc.com/Linux/2012-06/63754.htm Valgrind通常用来成分析程序性能及程序中的内存泄露错误 一 Valgrind工具集简绍 Valgrind包含下列工具: 1.memcheck:检查程序中的内存问题,如泄漏.越界.非法指针等. 2.callgrind:检测程序代码的运行时间和调用过程,以及分析程序性能. 3.cachegrind:分析CPU的cache命中率.丢失率,用于进行代码优化. 4.helgrind:用于检查多线

内存使用分析工具Valgrind简单用法

转载自 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html 暂时还未使用过,记录下,记录下,记录下 Valgrind的主要作者Julian Seward刚获得了今年的Google-O'Reilly开源大奖之一──Best Tool Maker.让我们一起来看一下他的作品.Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,它包含一个内核──一个软件合成 的CPU,和一系列的小工具,每个工具都可以完成一

Linux下检测内存泄露的工具 valgrind

参考:http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html 几乎是照抄参考过来的,只不过后面自己调试一下代码. 这里主要介绍Valgrind的一些简单用法.更多详细的使用方法可以访问valgrind的主页:http://www.valgrind.org Valgrind是Julian Seward的作品.Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,它包含一个内核,一个软件合成的CPU,和一系列的

valgrind 工具介绍和简单的使用

最近老是遇上各种奇奇怪怪的core dump,不太会分析的情况下看到了这款工具.在这记录分享下. Valgrind 是个开源的工具,功能很多.例如检查内存泄漏工具---memcheck. Valgrind 安装: 去官网下载: http://valgrind.org/downloads/current.html#current 安装过程:(可以直接查看README文档来确认安装过程) tools/valgrind-3.12.0> pwd    /proj/MPS_DEV_REPO/xchonxu

内存排查 valgrind

内存问题排查工具 --- valgrind 1. 概述 2. Valgrind 3. 内存泄漏监测 3.1. 示例代码 3.2. 编译它 3.3. 用Valgrind监测进程的内存泄漏 4. 悬挂指针 4.1. 示例代码 4.2. Valgrind运行结果 5. 多次释放同一个指针 5.1. 示例代码 5.2. Valgrind 监测 6. Valgrind的优缺点 6.1. Advantages 6.2. Disadvantages 7. Valgrind的其他工具 7.1. Cachegri

C++内存分配及变长数组的动态分配

//------------------------------------------------------------------------------------------------ 第一部分 C++内存分配 //------------------------------------------------------------------------------------------------ 一.关于内存 1.内存分配方式 内存分配方式有三种: (1)从静态存储区域分配

C语言内存调试技巧—C语言最大难点揭秘

本文将带您了解一些良好的和内存相关的编码实践,以将内存错误保持在控制范围内.内存错误是 C 和 C++ 编程的祸根:它们很普遍,认识其严重性已有二十多年,但始终没有彻底解决,它们可能严重影响应用程序,并且很少有开发团队对其制定明确的管理计划.但好消息是,它们并不怎么神秘.引言C 和 C++ 程序中的内存错误非常有害:它们很常见,并且可能导致严重的后果.来自计算机应急响应小组(请参见参考资料)和供应商的许多最严重的安全公告都是由简单的内存错误造成的.自从 70 年代末期以来,C 程序员就一直讨论此

嵌入式开发/调试辅助工具

开发辅助工具 开发环境组成 通常开发环境由三部分组成:构建环境:包括代码编写,程序编译,版本控制等功能.调试环境:用于定位问题的辅助工具集测试环境:用于验证目标程序是否满足用户要求的显性需求和隐性需求嵌入式开发中,通常有20%的时间用于目标构建,80%的时间用于测试.调试和bug修复,工欲善其事,必先利其器,提高开发和调试效率十分重要.GNU为GCC编译器提供了配套的辅助工具集(Binutils),网址:http://www.gnu.org/software/binutils/ 开发工具集 ad