valgrind

官网

原理

官网解释
Your program is then run on a synthetic CPU provided by the Valgrind core. As new code is executed for the first time, the core hands the code to the selected tool. The tool adds its own instrumentation code to this and hands the result back to the core, which coordinates the continued execution of this instrumented code.

stackoverflow解释
valgrind provides a virtual processor that executes your application. However, before your application instructions are processed, they are passed to tools (such as memcheck). These tools are kind of like plugins, and they are able to modify your application before it is run on the processor.
The great thing about this approach is that you don't have to modify or relink your program at all to run it in valgrind. It does cause your program to run slower, however valgrind isn't meant to measure performance or run during normal execution of your application, so this isn't really an issue.

安装

源码地址

使用如下命令更简单些

yum install valgrind 

参数

# When enabled, Valgrind will trace into sub-processes initiated via the exec system call.
# This is necessary for multi-process programs.
# Note that Valgrind does trace into the child of a fork (it would be difficult not to,
# since fork makes an identical copy of a process), so this option is arguably badly named.
# However, most children of fork calls immediately call exec anyway.--trace-children=<yes|no> [default: no]

Memcheck

功能

  1. Memory leaks.
  2. Using undefined values, i.e. values that have not been initialised, or that have been derived from other undefined values.
  3. Accessing memory you shouldn't, e.g. overrunning and underrunning heap blocks, overrunning the top of the stack, and accessing memory after it has been freed.
  4. Incorrect freeing of heap memory, such as double-freeing heap blocks, or mismatched use of malloc/new/new[] versus free/delete/delete[]
  5. Overlapping src and dst pointers in memcpy and related functions.
  6. Passing a fishy (presumably negative) value to the size parameter of a memory allocation function.

使用

#编译文件,加参数 -g
clang++ -g -Wall memcheck_test.cc -o memcheck_test
#--tool=memcheck 参数缺省
#--leak-check=full,内存泄的具体信息
#--log-file=log_file.txt,输出信息重定向到文件中
valgrind --leak-check=full --log-file=log_file.txt memcheck_test

Helgrind

功能

  1. Misuses of the POSIX pthreads API
  2. Potential deadlocks arising from lock ordering problems
  3. Data races -- accessing memory without adequate locking or synchronisation

使用

valgrind --tool=helgrind memcheck_test

如何高效实用Helgrind

链接

callgrind

功能

Callgrind is a profiling tool that records the call history among functions in a program's run as a call-graph.

使用

valgrind --tool=callgrind --separate-threads=yes ./callgrind_test
#函数调用情况总览
callgrind_annotate callgrind.out.<pid>#结合源文件
#注意.cc文件目录格式要和上面命令产生的路径格式一致
callgrind_annotate callgrind.out.<pid> callgrind_test.cc

使用kcachegrind工具

yum install kcachegrind
yum install graphviz
kcachegrind callgrind.out.<pid>

QA

链接

valgrind基础相关推荐

  1. valgrind基本功能介绍、基础使用方法说明

    1.Valgrind概述 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合. Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个框架(f ...

  2. 使用valgrind检测ATS插件中的内存泄露

    一.内存错误出现的场景 这几天在重构ATS插件代码的过程中遇到了烦人的内存泄露问题, 周五周六连续两天通过走查代码的方法,未能看出明显的导致内存错误的代码, 同时也觉得C和C++混合编程得到一个动态库 ...

  3. 使用valgrind分析C程序调用线路图

    Valgrind可以检测内存泄漏和内存违例,但是用Valgrind生成的日志信息结合kcachegrind就可以查看C程序的执行线路图,调用时间,是做性能优化和查看代码的非常好的工具. 1.下载安装 ...

  4. Ubuntu上通过android toolchain交叉编译Valgrind操作步骤

    关于Valgrind的介绍可以参考:https://blog.csdn.net/fengbingchun/article/details/50196189. 这里介绍下在Ubuntu 16.04上通过 ...

  5. 【linux】Valgrind工具集详解(一):简介

    一.Valgrind概述 Valgrind是用于构建动态分析工具的仪器框架.它附带了一组工具,每个工具都执行某种调试,分析或类似任务,可帮助您改进程序.Valgrind的架构采用模块化设计,因此可以轻 ...

  6. 动态内存检测工具Valgrind

    1. Valgrind查找内存泄露利器 Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析.你可以在它的环境中运行你的程序来 ...

  7. c linux new使内存耗尽_C/C++的内存泄漏检测工具Valgrind memcheck的使用经历

    Linux下的Valgrind真是利器啊(不知道Valgrind的请自觉查看参考文献(1)(2)),帮我找出了不少C++中的内存管理错误,前一阵子还在纠结为什么VS 2013下运行良好的程序到了Lin ...

  8. linux c 内存泄漏调试工具 《valgrind用户手册》 2. 使用和理解Valgrind核心

    valgrind 用户手册原版地址:https://valgrind.org/docs/manual/manual.html 此原文地址:https://valgrind.org/docs/manua ...

  9. C++面试八股文快问快答の基础篇

    文章目录 基础篇 变量的声明和定义有什么区别 简述#ifdef.#else.#endif和#ifndef的作用 写出int .bool. float .指针变量与 "零值"比较的i ...

最新文章

  1. Mac 安装多个版本jdk
  2. Java程序中Timer的用法
  3. Java实现算法导论中线性规划单纯形算法
  4. API Hook完全手册
  5. 面试精选:链表问题集锦
  6. 飞畅科技-国内老牌工业以太网交换机品牌
  7. 第二部分_搭建Java Web开发环境与配置Tomcat服务器JSP详解
  8. [机器学习-实践]支持向量机(SVM)从例子代码中学习
  9. 如何用 Python 给女友准备甜蜜的七夕礼物?
  10. matplotlib.pyplot库解析
  11. php生成GIF动态验证码图片(代码家园)
  12. 统一社会信用代码解析登记管理部门和机构类别
  13. axure html图标 图片大小,Axure 图标解决方案_html/css_WEB-ITnose
  14. 高数篇(三)-- 最小二乘法、正则化
  15. 选择中医 - 虚寒性体质的人吃什么好(萝卜、生姜、地瓜、大蒜)
  16. 自定义报表制作的注意事项——思迈特软件Smartbi报表工具
  17. tesseract的使用
  18. 搜狗输入法怎样使用自定义短语
  19. golang 通过docker 搭建 ocr识别
  20. 鼠标悬停出现遮罩或图片放大效果

热门文章

  1. Effective Java读书笔记四:通用程序设计
  2. 涨姿势!北京地铁原来是16条旅游专线
  3. 【OpenCV3】双线性插值
  4. tp3.2.3运用phpexcel将excel文件导入mysql数据库
  5. 大数据分析助精准医疗迅猛发展
  6. ui-grid下拉过滤
  7. 思科超融合:主推HyperFlex,押注HCI
  8. StartSSL免费SSL证书申请和账户注册完整过程
  9. linux c:关联变量的双for循环
  10. 《Build your own AngularJS》笔记分享