admin管理员组

文章数量:1531533

Valgrind memcheck介绍以及在海思himix410平台的安装使用

    • 概要
    • Valgrind 介绍
    • Memcheck 工具简介
      • 内存泄漏类型细分
      • 内存泄漏举例: definitely lost
      • 内存泄漏举例: indirectly lost
      • 内存泄漏举例: possibly lost
      • 内存泄漏举例: still reachable
      • 内存泄漏类型总结
    • 在海思himix410上交叉编译安装Valgrind
      • 安装交叉编译工具链
      • 修改 Valgrind 3.19.0 源码
      • 编译安装 Valgrind 3.19.0
      • 在开发板上运行Valgrind: 报 debuginfo 错误
      • 解决 glibc debuginfo 报错:不用编译glibc

概要

Valgrind 的 memcheck 是一个强大的内存检测和分析工具. 本文对 memcheck 的几种类型做介绍并举例; 然后给出在海思 himix410 工具链对应的编译步骤, 包括在开发板上运行时提示缺少 glibc debuginfo 报错的解决方法.

Valgrind 介绍

Valgrind 是一个内存检测工具框架,包括核心的 coregrind 库,独立的 IR 系统 libVEX, 以及基于 coregrind 和 libVEX 的一系列内存检测分析工具,最常用的是 memcheck, 其他还有 callgrind, cachegrind 等.

Valgrind 主要是在 Linux 系统上运行.通过 sudo apt install valgrind 进行安装.

在 ADAS 相关项目中,使用到海思的开发板,对应的交叉编译工具链为 himix410.即:使用 himix410 编译器交叉编译出 valgrind 后,能够运行和检测开发板上运行的程序中是否有内存泄漏,泄漏的位置是哪里,等等.

Memcheck 工具简介

Valgrind 中最常用的工具是 Memcheck, 它会报告出被代理运行的程序中的内存问题,具体又可以分成如下5种:

内存泄漏类型细分

内存泄漏细分类型 含义
definitely lost 确信无疑的内存泄漏
indirectly lost 间接的内存泄漏
possibly lost 可能有内存泄漏
still reachable 程序退出时仍然可以访问的内存,也是泄漏
suppressed 被抑制的

内存泄漏举例: definitely lost

definitely lost意思是:确信无疑的内存泄漏,缺少free/detelete/delete[]操作。
举例:

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

int main()
{
    float* data = (float*)malloc(sizeof(float)*5);

    return 0;
}
gcc example2.c -g 
valgrind --leak-check=yes ./a.out

23257 Memcheck, a memory error detector
23257 Copyright © 2002-2015, and GNU GPL’d, by Julian Seward et al.
23257 Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
23257 Command: ./a.out
23257
23257
23257 HEAP SUMMARY:
23257 in use at exit: 20 bytes in 1 blocks
23257 total heap usage: 1 allocs, 0 frees, 20 bytes allocated
23257
23257 20 bytes in 1 blocks are definitely lost in loss record 1 of 1
23257 at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
23257 by 0x400577: main (example2.c:6)
23257
23257 LEAK SUMMARY:
23257 definitely lost: 20 bytes in 1 blocks
23257 indirectly lost: 0 bytes in 0 blocks
23257 possibly lost: 0 bytes in 0 blocks
23257 still reachable: 0 bytes in 0 blocks
23257 suppressed: 0 bytes in 0 blocks
23257
23257 For counts of detected and suppressed errors, rerun with: -v
23257 ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

内存泄漏举例: indirectly lost

indirectly lost,说的很玄乎,还是看例子比较直观:

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

typedef struct Image {
    int h, w, c;
    float* data;
} Image;

int main()
{
    Image* im = (Image*)malloc(sizeof(Image));
    int h = 224, w = 224, c = 3;
    im->h = h;
    im->w = w;
    im->c = c;
    im->data = (float*)malloc(sizeof(float)*h*w*c);

    return 0;
}
gcc example2.c -g
valgrind --leak-check=yes ./a.out

23580 Memcheck, a memory error detector
23580 Copyright © 2002-2015, and GNU GPL’d, by Julian Seward et al.
23580 Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
23580 Command: ./a.out
23580
23580
23580 HEAP SUMMARY:
23580 in use at exit: 602,136 bytes in 2 blocks
23580 total heap usage: 2 allocs, 0 frees, 602,136 bytes allocated
23580
23580 602,136 (24 direct, 602,112 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
23580 at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
23580 by 0x400537: main (example2.c:11)
23580
23580 LEAK SUMMARY:
23580 definitely lost: 24 bytes in 1 blocks
23580 indirectly lost: 602,112 bytes in 1 blocks
23580 possibly lost: 0 bytes in 0 blocks
23580

本文标签: 平台valgrindMemcheck