Click here to Skip to main content
15,880,905 members
Articles / Programming Languages / C++

VSDebugHelper - Capture memory snapshot of a debugged process to a file

Rate me:
Please Sign up or sign in to vote.
4.76/5 (14 votes)
15 Apr 2011CPOL3 min read 62.7K   1.1K   23   33
The VsDebugHelper add-in allows to capture a buffer from the program being debugged into a data file and back.

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 instances, 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 using 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
  • Usage: writemem <filename> <address> <length>

    • filename - destination file name.
    • address - starting address of the buffer.
    • length - length of the data to be copied.
  • readmem - Read memory from file
  • Usage: readmem <filename> <address> <length>

    • filename - source file name.
    • address - starting address of the buffer.
    • length - length of the data to be copied.

    Note: To see the effect of readmem in Visual Studio, you need to 'step' after readmem is called.

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:

C++
std::auto_ptr<lum8> grayPixels(new lum8[width*height]);
...
std::auto_ptr<rgba> colorPixels(new rgba[width*height]);
...

alt text

Note that the arguments are variables and expressions of expressions, which are evaluated by the debugger. If you run into any issues, 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):

alt text

alt text

Installation

Installation by Direct Download

Proceed with the download and install 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:

alt text

Installation Through Extension Manager

Alternately, the add-in can be installed from within Visual Studio through the use of the Extension Manager as shown below:

Image 5

Add-in Internals

Visual Studio has a number of extensibility technologies, but useful documentation is very difficult to find. Additionally, there is significant disconnect as to what is available to 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.

Overall, the 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 the command parameters and etc. A simplified diagram is shown below:

alt text

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionwritemem Failed Pin
Member 954640017-May-13 7:18
Member 954640017-May-13 7:18 
AnswerRe: writemem Failed Pin
Igor Okulist18-May-13 10:33
Igor Okulist18-May-13 10:33 
GeneralRe: writemem Failed Pin
Member 954640020-May-13 5:35
Member 954640020-May-13 5:35 
GeneralRe: writemem Failed Pin
Igor Okulist20-May-13 6:39
Igor Okulist20-May-13 6:39 
GeneralRe: writemem Failed Pin
Member 954640020-May-13 6:56
Member 954640020-May-13 6:56 
GeneralRe: writemem Failed Pin
Igor Okulist20-May-13 7:02
Igor Okulist20-May-13 7:02 
GeneralRe: writemem Failed Pin
Member 954640021-Aug-13 8:13
Member 954640021-Aug-13 8:13 
GeneralRe: writemem Failed Pin
Igor Okulist22-Aug-13 18:52
Igor Okulist22-Aug-13 18:52 
GeneralRe: writemem Failed Pin
chk20-Nov-13 0:47
chk20-Nov-13 0:47 
GeneralRe: writemem Failed Pin
Igor Okulist20-Nov-13 11:14
Igor Okulist20-Nov-13 11:14 
GeneralRe: writemem Failed Pin
Igor Okulist20-May-13 7:05
Igor Okulist20-May-13 7:05 
GeneralRe: writemem Failed Pin
professor90023-Sep-15 1:13
professor90023-Sep-15 1:13 
BugVSDebugHelper locks up when saving memory from a crash dump Pin
Member 41995799-May-12 4:53
Member 41995799-May-12 4:53 
GeneralRe: VSDebugHelper locks up when saving memory from a crash dump Pin
Igor Okulist9-May-12 16:52
Igor Okulist9-May-12 16:52 
GeneralRe: VSDebugHelper locks up when saving memory from a crash dump Pin
Member 419957910-May-12 4:49
Member 419957910-May-12 4:49 
GeneralRe: VSDebugHelper locks up when saving memory from a crash dump Pin
Igor Okulist16-May-12 21:15
Igor Okulist16-May-12 21:15 
GeneralRe: VSDebugHelper locks up when saving memory from a crash dump Pin
jmenter2-Aug-12 5:36
jmenter2-Aug-12 5:36 
GeneralRe: VSDebugHelper locks up when saving memory from a crash dump Pin
Igor Okulist25-Aug-12 16:10
Igor Okulist25-Aug-12 16:10 
QuestionVery Helpful Pin
tharaka_t26-Apr-12 18:58
tharaka_t26-Apr-12 18:58 
AnswerRe: Very Helpful Pin
Igor Okulist27-Apr-12 14:41
Igor Okulist27-Apr-12 14:41 
Thanks for vote.

Regarding your question -- it's a bit too generic, but there have
been a few articles posted on codeproject recently (run a search).

Try them out, but do post a questions if there are specific issues.

Regards,
Igor
GeneralRe: Very Helpful Pin
tharaka_t30-Apr-12 22:53
tharaka_t30-Apr-12 22:53 
GeneralRe: Very Helpful Pin
Igor Okulist3-May-12 12:19
Igor Okulist3-May-12 12:19 
GeneralRe: Very Helpful Pin
tharaka_t7-May-12 6:40
tharaka_t7-May-12 6:40 
GeneralRe: Very Helpful Pin
Igor Okulist9-May-12 16:57
Igor Okulist9-May-12 16:57 
GeneralRe: Very Helpful Pin
tharaka_t23-May-12 22:08
tharaka_t23-May-12 22:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.