VSDebugHelper - Capture memory snapshot of a debugged process to a file
Introduction
At times I need to look into a processing step of a pipeline, either to identify a performance improvement or as part of impact analysis. If the step is covered by a unit test, then it is a bit easier to analyze, but alas this is not always the case.
In such a case, a general approach is to isolate a buffer of data being processed and extract it to a data file. Then the data can be analyzed separately or the data can be used to create a unit test. In the latter case, the unit test can be used to test the performance improvement.
There are generally two ways of capturing the buffer: either modify the source code to add the data dump procedure (e.g., fopen
/fwrite
/fclose
) or restart the debug session with WinDBG and use the writemem
extension.
Both of the above methods are robust but inefficient as both require restarting of the debug session and one of them requires recompilation of the sources, which may be time consuming.
Hence the VsDebugHelper add-in was born. The VsDebugHelper add-in allows to capture a buffer from the program being debugged into a data file and back.
Using the VsDebugHelper Add-in
The steps to use the add-in are rather simple:
- Make sure VsDebugHelper is loaded by checking Tools\Addin manager...
- Start debugging the application and
break
on the desired location - Use one or more of the commands from VsDebugHelper
Once loaded, the plug-in adds two commands to the Visual Studio environment, which can be executed from the Visual Studio Command Window.
VsDebugHelper Commands
The following commands are inspired by the WinDBG extension and provide the following functionality:
writemem
- Write memory to file- filename - destination file name.
- address - starting address of the buffer.
- length - length of the data to be copied.
readmem
- Read memory from File- Attention: To see the effect of
readmem
in Visual Studio, you need to 'step' afterreadmem
is called. - filename - source file name.
- address - starting address of the buffer.
- length - length of the data to be copied.
Usage: writemem <filename> <address> <length>
Usage: readmem <filename> <address> <length>
Both the address
and length
arguments can be variable names or an expression.
VsDebugHelper example usage
Here is an example of how VsDebugHelper can capture a buffer of pixels:
std::auto_ptr<lum8> grayPixels(new lum8[width*height]);
...
std::auto_ptr<rgba> colorPixels(new rgba[width*height]);
...
Note that the arguments are variables and expressions of expressions, which are evaluated by the debugger. If you run into any issue, first double check that all expressions can be resolved in the Watch window.
And here is how the raw data is displayed by an external viewer (IrfranView):
Installation
Download and install the file at the top of the article. After the installation, confirm the add-in is installed and set to be loaded at startup, as shown below:
Add-in internals
Visual Studio has a number of extensibility technologies, but useful documentation is very difficult to find. Additionally, there is a significant disconnect as to what is available in managed extensions vs. unmanaged extensions. It is rather clear that Visual Studio is an unmanaged application, yet there is no information on how to interact from an unmanaged add-in. And interestingly enough, extensibility functionality for 'Visualizer' has been clearly cut off.
Besides being a nuisance, the above boils down to an issue that there is no way to work through Visual Studio to modify a region of memory. Hence when the readmem
command is used (to load a buffer from file), the Watch and Memory window displays will be out of sync with the actual memory contents.
The overall sequence of execution is pretty simple. Visual Studio calls the CommandTarget
interface and its implementation will dispatch the call to the ICommand
implementation based on the command string. The ICommand
implementation is responsible for parsing command parameters etc. A simplified diagram is shown below:
发表评论
eslyCJ Thanks for the article.Much thanks again. Keep writing.
UrfljM Very neat article post.Much thanks again. Cool.