valgrind基础

本文详细介绍了Valgrind工具的工作原理及其主要组件的功能。Valgrind通过提供虚拟处理器来运行应用程序,并利用各种工具(如Memcheck、Helgrind和Callgrind)进行内存泄漏检测、线程同步错误检查及性能剖析等。文章还提供了具体的安装步骤和使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值