首页 > 系统相关 >交叉编译内存分析工具 valgrind3.21.0 (aarch64-linux-gnu-gcc)

交叉编译内存分析工具 valgrind3.21.0 (aarch64-linux-gnu-gcc)

时间:2023-05-26 16:12:39浏览次数:64  
标签:gcc aarch64 valgrind3.21 ld valgrind -- so linux

交叉编译工具

编译机器: ubuntu Server 22 LTS
编译目标: ARM64 开发板

https://releases.linaro.org/components/toolchain/binaries/7.5-2019.12/aarch64-linux-gnu/
: 如果使用7.5以上的GCC,请到ARM官网下载:https://developer.arm.com/downloads/-/gnu-a

gcc版本(7.5)需对应板子的glibc版本【这点非常重要,具体可以查gcc对应的glibc版本】
架构(aarch64-linux-gnu)需对应操作系统和SOC核架构

源码下载地址

valgrind3.21.0 https://valgrind.org/downloads/current.html
依赖:

sudo apt install automake
sudo apt install libc6-dbg

我的编译脚本

查看 valgrind3.21.0 项目里的 README.aarch64 文件可得到编译说明,然后自己再根据它写成脚本
build_valgrind3.sh

#!/bin/sh


# lopment so far was however done by cross compiling, viz:

export GCC_PATH=/your_path_for_linaro/bin

#  wrote by yucloud.cnblogs.com
  export CC=${GCC_PATH}/aarch64-linux-gnu-gcc
  export LD=${GCC_PATH}/aarch64-linux-gnu-ld
  export AR=${GCC_PATH}/aarch64-linux-gnu-ar

  ./autogen.sh
  ./configure --prefix=`pwd`/Inst --host=aarch64-unknown-linux \
              --enable-only64bit
  make -j4
  make -j4 install

 tar -czf ../valgrind_3.21.0.yucloudbuild.tgz  Inst/
 echo "build and tar successful at: ../valgrind_3.21.0.yucloudbuild.tgz"


把上面的GCC_PATH改成你自己的交叉编译工具链路径即可,也就是 aarch64-linux-gnu-gcc 位于的目录路径
编译后打包生成在 ``../valgrind_3.21.0.yucloudbuild.tgz`

部署

  1. 丢到板子上(个人习惯用scp) 然后解压
tar -xvf valgrind_3.21.0.yucloudbuild.tgz
chmod -R +x Inst/ && cp Inst/* / -rfv
 export VALGRIND_LIB=/libexec/valgrind/

然后再把带有debug信息的 ld-<版本号>.so 放入板子并chmod +x ,如果不知道怎么操作,请见下文的 [### cannot be set up 报错]

使用

运行

# valgrind --tool=memcheck --leak-check=full  <目标程序执行命令_可带参数>
valgrind --tool=memcheck --leak-check=full  ./test

如果这里你的程序携带了完整的调试信息,那么至此就完成了。它会告诉你你的程序内存泄漏情况。

错误解决

memcheck 报错

报错信息:
valgrind: failed to start tool 'memcheck' for platform 'arm64-linux': No such file or directory
这个需要声明运行库的变量

# vi ~/.bashrc
export VALGRIND_LIB=/libexec/valgrind/

cannot be set up 报错

这个问题是程序本身没有包含所有调试信息的原因。
要么程序完全加入调试信息(包括动静态库),要不程序编译的时候加gcc参数-g调试信息,并且也把带调试信息的libc放入板子。

报错信息:

root@plnx:/tmp#  export VALGRIND_LIB=/libexec/valgrind/ && valgrind --tool=memcheck --leak-check=full  /run/my_exec



root@plnx:/tmp#  export VALGRIND_LIB=/libexec/valgrind/ && valgrind --tool=memcheck --leak-check=full  /run/my_exec
==7643== Memcheck, a memory error detector
==7643== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==7643== Using Valgrind-3.21.0 and LibVEX; rerun with -h for copyright info
==7643== Command: /run/my_exec
==7643==

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.


==7643== Memcheck, a memory error detector
==7643== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==7643== Using Valgrind-3.21.0 and LibVEX; rerun with -h for copyright info
==7643== Command: /run/my_exec
==7643==

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.

在x86主机的arm交叉编译工具链路径搜索

$ find . -name "*ld*.so"
./aarch64-linux-gnu/libc/lib/ld-2.25.so
$ file ./aarch64-linux-gnu/libc/lib/ld-
ld-2.25.so             ld-linux-aarch64.so.1
$ file ./aarch64-linux-gnu/libc/lib/ld-2.25.so
./aarch64-linux-gnu/libc/lib/ld-2.25.so: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, BuildID[sha1]=73b23c16ff9fe3eab046636cd9dd6db9d3309f27, with debug_info, not stripped

然后把它放到板子上即可

chmod +x ./tmp/ld-2.25.so
export VALGRIND_LIB=/libexec/valgrind/ && valgrind --tool=memcheck --leak-check=full /tmp/ld-2.25.so /run/my_exec

效果

export VALGRIND_LIB=/libexec/valgrind && valgrind --tool=memcheck --leak-check=full /tmp/ld-2.28.so  /run/my_exec

输出信息如下:

root@plnx:/tmp#  export VALGRIND_LIB=/libexec/valgrind && valgrind --tool=memcheck --leak-check=full /tmp/ld-2.28.so  /run/my_exec
==27913== Memcheck, a memory error detector
==27913== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==27913== Using Valgrind-3.21.0 and LibVEX; rerun with -h for copyright info
==27913== Command: /tmp/ld-2.28.so /run/my_exec
==27913==


Hell World!
***************************************************************************

==27913==
==27913== HEAP SUMMARY:
==27913==     in use at exit: 0 bytes in 0 blocks
==27913==   total heap usage: 33 allocs, 33 frees, 90,118 bytes allocated
==27913==
==27913== All heap blocks were freed -- no leaks are possible
==27913==
==27913== For lists of detected and suppressed errors, rerun with: -s
==27913== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

可见没有泄露!

更便捷的使用

上面的命令确实参数太长了,我们可以做成脚本:
cat run_valgrind3.sh

#!/bin/sh

#   tar -xvf valgrind_3.21.0.yucloudbuild.tgz && chmod -R +x Inst/ && cp Inst/* / -rfv
#   chmod +x /tmp/ld-*.so

export VALGRIND_LIB=/libexec/valgrind && valgrind --tool=memcheck --leak-check=full /tmp/ld-2.28.so  $@

至于为什么放在/tmp目录,因为调试是只是临时使用,真正的产品是不能带调试信息的。

运行示例

./run_valgrind3.sh your_exec your_args0 your_args1
如果需要运行程序并检测内存泄露

  • 普通运行
    ./my_test 1 0
  • 检测内存运行
    ./run_valgrind3.sh /my_test 1 0

附录

附上$@的含义

  • $* 所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
  • $@ 所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。

另外还有其他放置运行库的方案

标签:gcc,aarch64,valgrind3.21,ld,valgrind,--,so,linux
From: https://www.cnblogs.com/yucloud/p/armbuild_valgrind3.html

相关文章

  • 查看gcc/g++版本以及对应默认的的C++标准
    通过如下命令查看_cplusplus的值:g++-dM-E-xc++/dev/null|grep-F__cplusplus对应查表C++标准_cplusplusC++11201103LC++14201402LC++17201703L ......
  • buildCentral配置aarch64
    一、测试环境二、基础软件安装三、aarch64环境配置四、buildCentral环境配置五、HelloWorld应用一、测试环境虚拟机版本:VirtualBox7.0系统版本:Ubuntu18.04 二、基础软件安装通用软件安装$sudoaptinstallgit$sudoaptinstallopenssh-server$sudoapt-get......
  • yum安装gcc时降级安装包的问题
    安装gccgcc-c++make等编译包,提示glibc错误依赖-->FinishedDependencyResolutionError:Package:glibc-headers-2.17-317.el7.x86_64(centos7)Requires:glibc=2.17-317.el7Installed:glibc-2.17-326.el7_9.x86_64(@anaconda)......
  • - python=3.7 -> libgcc-ng[version='>=11.2.0'] -> __glibc[version='>=2.17
     001、利用conda安装软件时遇到如下问题:Outputinformat:Requestedpackage->AvailableversionsThefollowingspecificationswerefoundtobeincompatiblewithyoursystem:-feature:/linux-64::__glibc==2.17=0-python=3.7->libgcc-ng[version='>=1......
  • centos上使用makefile编译sliver时 提示gcc 错误,cannot find -ldl cannot find -lpth
    github.com/bishopfox/sliver/server/usr/local/go/pkg/tool/linux_amd64/link:runninggccfailed:exitstatus1/usr/bin/ld:cannotfind-ldl/usr/bin/ld:cannotfind-lpthread/usr/bin/ld:cannotfind-lccollect2:error:ldreturned1exitstatus这个错误通常......
  • Linux gcc升级
    源码编译1.查看当前gcc版本#gcc--versiongcc(GCC)4.8.520150623(RedHat4.8.5-44)Copyright(C)2015FreeSoftwareFoundation,Inc.Thisisfreesoftware;seethesourceforcopyingconditions.ThereisNOwarranty;notevenforMERCHANTABILITYorFITNES......
  • docker 安装 ha supervisor [aarch64]
    克隆镜像下载hassio-supervisor,homeassistant镜像打开网址下面两个网址,获取最新版本镜像,使用dockerpull拉取镜像https://github.com/home-assistant/supervisor/pkgs/container/aarch64-hassio-supervisorhttps://github.com/home-assistant/core/pkgs/container/aarch64......
  • 双链表和队列-->gcc编译
    双链表队列doublueList.h#include<stdlib.h>#include<stdio.h>#include<assert.h>#include<stdbool.h>typedefintLTDataType;typedefstructDList{ LTDataTypedata; structDList*next; structDList*prev;}LTNode;LTNode*init();......
  • GCC、gcc、g++与gdb
     GCC:GNUCompilerCollection(GUN编译器集合),它可以编译C、C++、JAV、Fortran、Pascal、Object-C、Ada等语言。gcc是GCC中的GUNCCompiler(C编译器)g++是GCC中的GUNC++Compiler(C++编译器)GDB全称“GNUsymbolicdebugger”,从名称上不难看出,它诞生于GNU计划(同时诞生的还......
  • windows安装gcc
    1.安装MinGW的方法跟官网走,很清晰,不会有错漏:MSYS2注意上文中第5步打开的是ucrt64终端,而不是自动弹出的msys,否则gcc指令无效。 2.ucrt64终端那里,之前看vscode的官网教程没更新卡了好久,后来解决之后,专门去查了一下:以下内容来自知乎:https://www.zhihu.com/question/463666011/an......