是否可以在部分代码上运行 valgrind?

Is it possible to run valgrind on a portion of the code?

我在相当大的系统的已知部分中发生了内存泄漏。我想全速运行直到该部分代码,在该部分运行 valgrind memcheck,并在该部分末尾收集 valgrind memcheck 报告而不进一步完成运行。可以吗?

memcheck 不行,不行。根据 valgrind FAQ,callgrind 是目前唯一支持此功能的 valgrind 组件:

5.4. Is it possible to attach Valgrind to a program that is already running?

No. The environment that Valgrind provides for running programs is significantly different to that for normal programs, e.g. due to different layout of memory. Therefore Valgrind has to have full control from the very start.

It is possible to achieve something like this by running your program without any instrumentation (which involves a slow-down of about 5x, less than that of most tools), and then adding instrumentation once you get to a point of interest. Support for this must be provided by the tool, however, and Callgrind is the only tool that currently has such support. See the instructions on the callgrind_control program for details.