Linux下安装Valgrind工具

2024-03-25 17:08
文章标签 工具 linux 安装 valgrind

本文主要是介绍Linux下安装Valgrind工具,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Linux下安装Valgrind工具

valgrind是一款很优秀的内存泄漏检测工具。
下面介绍如何在Linux下安装Valgrind。
首先下载安装包到/root目录下(或者其他目录);
[root@localhost ~]# ll
total 7200
-rw-------. 1 root root    2692 Apr 10 13:52 anaconda-ks.cfg
-rw-r--r--. 1 root root   39935 Apr 10 13:52 install.log
-rw-r--r--. 1 root root    9154 Apr 10 13:51 install.log.syslog
-rw-r--r--. 1 root root  303586 Apr 11 22:50 lua-5.3.4.tar.gz
-rw-r--r--. 1 root root 6998648 Apr 11 19:34 ntp-4.2.8p10.tar.gz
drwxr-xr-x. 2 root root    4096 Apr 12 18:57 sort
[root@localhost ~]# which valgrind
/usr/bin/which: no valgrind in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# ll /root/
total 19692
-rw-------. 1 root root     2692 Apr 10 13:52 anaconda-ks.cfg
-rw-r--r--. 1 root root    39935 Apr 10 13:52 install.log
-rw-r--r--. 1 root root     9154 Apr 10 13:51 install.log.syslog
-rw-r--r--. 1 root root   303586 Apr 11 22:50 lua-5.3.4.tar.gz
-rw-r--r--. 1 root root  6998648 Apr 11 19:34 ntp-4.2.8p10.tar.gz
drwxr-xr-x. 2 root root     4096 Apr 12 18:57 sort
-rw-r--r--. 1 root root 12788556 Apr 14 17:56 valgrind-3.12.0.tar.bz2
[root@localhost src]# tar jxf /root/valgrind-3.12.0.tar.bz2 
[root@localhost src]# ll
total 12
drwxr-xr-x.  4 1026 1000 4096 Apr 11 23:02 lua-5.3.4
drwxrwsr-x. 23  427 6011 4096 Mar 21 07:47 ntp-4.2.8p10
drwxrwxr-x. 24 1000 1000 4096 Oct 21 04:03 valgrind-3.12.0
[root@localhost src]# cd valgrind-3.12.0/
[root@localhost valgrind-3.12.0]# ll
total 1996
-rw-rw-r--.  1 1000 1000  44566 Oct 21 03:42 aclocal.m4
-rw-rw-r--.  1 1000 1000   3267 Oct 21 03:37 AUTHORS
-rwxrwxr-x.  1 1000 1000    191 Oct 21 03:37 autogen.sh
drwxrwxr-x.  3 1000 1000   4096 Oct 21 04:01 auxprogs


将源码解压缩到/usr/local/src下。

阅读下valgrind中的README文件,可以看到如下信息:

Building and installing it
~~~~~~~~~~~~~~~~~~~~~~~~~~
To install from the Subversion repository :

  0. Check out the code from SVN, following the instructions at
     http://www.valgrind.org/downloads/repository.html.

  1. cd into the source directory.

  2. Run ./autogen.sh to setup the environment (you need the standard
     autoconf tools to do so). 

  3. Continue with the following instructions...

To install from a tar.bz2 distribution:

  4. Run ./configure, with some options if you wish.  The only interesting
     one is the usual --prefix=/where/you/want/it/installed.

  5. Run "make". 

  6. Run "make install", possibly as root if the destination permissions
     require that.

  7. See if it works.  Try "valgrind ls -l".  Either this works, or it
     bombs out with some complaint.  In that case, please let us know
     (see www.valgrind.org).

Important!  Do not move the valgrind installation into a place
different from that specified by --prefix at build time.  This will
cause things to break in subtle ways, mostly when Valgrind handles
fork/exec calls.

和通常的安装步骤一样:configure;make;make install
过程如下:

[root@localhost valgrind-3.12.0]# ./configure --prefix=/usr/local/valgrind
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
……
config.status: executing depfiles commands

         Maximum build arch: x86
         Primary build arch: x86
       Secondary build arch: 
                   Build OS: linux
       Primary build target: X86_LINUX
     Secondary build target: 
           Platform variant: vanilla
      Primary -DVGPV string: -DVGPV_x86_linux_vanilla=1
         Default supp files: exp-sgcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.X.supp 

[root@localhost valgrind-3.12.0]# make
echo "# This is a generated file, composed of the following suppression rules:" > default.supp
echo "# " exp-sgcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.X.supp  >> default.supp
cat exp-sgcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.X.supp >> default.supp
make  all-recursive
make[1]: Entering directory `/usr/local/src/valgrind-3.12.0'
Making all in include
make[2]: Entering directory `/usr/local/src/valgrind-3.12.0/include'
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/usr/local/src/valgrind-3.12.0/include'
Making all in VEX
……
make[1]: Leaving directory `/usr/local/src/valgrind-3.12.0'
[root@localhost valgrind-3.12.0]# make install
make  install-recursive
make[1]: Entering directory `/usr/local/src/valgrind-3.12.0'
Making install in include
make[2]: Entering directory `/usr/local/src/valgrind-3.12.0/include'
make[3]: Entering directory `/usr/local/src/valgrind-3.12.0/include'
make[3]: Nothing to be done for `install-exec-am'.
……
make[1]: Leaving directory `/usr/local/src/valgrind-3.12.0'
[root@localhost valgrind-3.12.0]# cd ..
[root@localhost src]# cd ..
[root@localhost local]# ll
total 40
drwxr-xr-x. 2 root root 4096 Sep 23  2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23  2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23  2011 games
drwxr-xr-x. 2 root root 4096 Sep 23  2011 include
drwxr-xr-x. 2 root root 4096 Sep 23  2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23  2011 libexec
drwxr-xr-x. 2 root root 4096 Sep 23  2011 sbin
drwxr-xr-x. 5 root root 4096 Apr 10 13:42 share
drwxr-xr-x. 5 root root 4096 Apr 14 17:57 src
drwxr-xr-x. 6 root root 4096 Apr 14 18:04 valgrind
[root@localhost local]# which valgrind
/usr/bin/which: no valgrind in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)


make install后发现还是没有(没有找到)valgrind可执行文件。
其实这个时候已经有了,只是没有找到罢了。
我们可以将valgrind做一个软连接到/usr/local/bin下:
[root@localhost local]# cd valgrind/
[root@localhost valgrind]# ll
total 16
drwxr-xr-x. 2 root root 4096 Apr 14 18:04 bin
drwxr-xr-x. 3 root root 4096 Apr 14 18:04 include
drwxr-xr-x. 4 root root 4096 Apr 14 18:04 lib
drwxr-xr-x. 4 root root 4096 Apr 14 18:04 share
[root@localhost valgrind]# cd bin/
[root@localhost bin]# ll
total 392
-rwxr-xr-x. 1 root root 41112 Apr 14 18:04 callgrind_annotate
-rwxr-xr-x. 1 root root 12016 Apr 14 18:04 callgrind_control
-rwxr-xr-x. 1 root root 32170 Apr 14 18:04 cg_annotate
-rwxr-xr-x. 1 root root 10418 Apr 14 18:04 cg_diff
-rwxr-xr-x. 1 root root 49307 Apr 14 18:04 cg_merge
-rwxr-xr-x. 1 root root 24398 Apr 14 18:04 ms_print
-rwxr-xr-x. 1 root root 39093 Apr 14 18:04 valgrind
-rwxr-xr-x. 1 root root 65697 Apr 14 18:04 valgrind-di-server
-rwxr-xr-x. 1 root root 21037 Apr 14 18:04 valgrind-listener
-rwxr-xr-x. 1 root root 85247 Apr 14 18:04 vgdb
[root@localhost bin]# which valgrind
/usr/bin/which: no valgrind in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost bin]# cd /usr/local/bin/
[root@localhost bin]# ln -s /usr/local/valgrind/bin/valgrind valgrind
[root@localhost bin]# which valgrind
/usr/local/bin/valgrind

测试下:
[root@localhost bin]# valgrind ls -l
==16148== Memcheck, a memory error detector
==16148== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==16148== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==16148== Command: ls -l
==16148== 
total 0
lrwxrwxrwx. 1 root root 32 Apr 14 18:06 valgrind -> /usr/local/valgrind/bin/valgrind
==16148== 
==16148== HEAP SUMMARY:
==16148==     in use at exit: 13,069 bytes in 11 blocks
==16148==   total heap usage: 206 allocs, 195 frees, 62,575 bytes allocated
==16148== 
==16148== LEAK SUMMARY:
==16148==    definitely lost: 0 bytes in 0 blocks
==16148==    indirectly lost: 0 bytes in 0 blocks
==16148==      possibly lost: 0 bytes in 0 blocks
==16148==    still reachable: 13,069 bytes in 11 blocks
==16148==         suppressed: 0 bytes in 0 blocks
==16148== Rerun with --leak-check=full to see details of leaked memory
==16148== 
==16148== For counts of detected and suppressed errors, rerun with: -v
==16148== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from 10)

OK,安装完成。
回想刚刚的安装步骤:
1.解压源代码到/usr/local/src下;
2.configure --prefix=/usr/local/valgrind;
3.make && make install;
4.ln做一个软连接到/usr/local/bin中;
大功告成。

这篇关于Linux下安装Valgrind工具的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/845663

相关文章

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#