valgrind 是如何工作的? [英] How does valgrind work?

查看:25
本文介绍了valgrind 是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以快速解释一下 Valgrind 的工作原理吗?一个例子:它如何知道内存何时被分配和释放?

Can someone provide a quick top level explanation of how Valgrind works? An example: how does it know when memory is allocated and freed?

推荐答案

Valgrind 基本上是在沙箱"中运行您的应用程序.在此沙箱中运行时,它能够插入自己的指令以进行高级调试和分析.

Valgrind basically runs your application in a "sandbox." While running in this sandbox, it is able to insert its own instructions to do advanced debugging and profiling.

来自手册:

然后,您的程序将在 Valgrind 内核提供的合成 CPU 上运行.当新代码第一次执行时,内核将代码交给选定的工具.该工具向其中添加了自己的检测代码,并将结果返回给核心,后者协调此检测代码的继续执行.

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.

所以基本上,valgrind 提供了一个虚拟处理器来执行您的应用程序.但是,在处理您的应用程序指令之前,它们会被传递给工具(例如 memcheck).这些工具有点像插件,它们能够在您的应用程序在处理器上运行之前对其进行修改.

So basically, 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.

这种方法的优点在于,您根本无需修改或重新链接程序即可在 valgrind 中运行它.它确实会导致您的程序运行速度变慢,但是 valgrind 并不意味着在您的应用程序正常执行期间测量性能或运行,因此这不是真正的问题.

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.

这篇关于valgrind 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆