天天看點

讓vs調試器顯示你想要的資料格式

Calvin Hsia

Microsoft Corporation

June 2006

Applies to:

   Visual Studio 2005

   Visual Studio .NET 2003

   Visual Studio 7.0

Summary: Illustrates ways to customize the Visual Studio 2005 debugger to get the most out of your debugging time. (6 printed pages)

As a software developer, I spend much of my time looking at code, learning how it works, and figuring out how to modify or fix it. A very good tool to help examine code is the Visual Studio debugger.

(Even if you're not a hard core programmer, the following tutorial shows some of the power of the Visual Studio components—for example, the project system, build system, and debugger—working together.)

At a breakpoint, I can examine local variables in the Watch, Auto, or Locals window to see their values and types. If it's a class or structure, the debugger will show a plus sign (<code>+</code>), indicating that it can be expanded, and the first

couple members of that structure. Structures' submembers or inherited values can be examined. These structures can getvery deep. Sometimes I need to inspect a value that's dozens of levels down in a hierarchy. That's a lot of complicated tree navigation

in the debugger. Other times, I need to take a local variable name (or a member of that variable if it's a structure/class), drag and drop it to a new line in the Watch window, and then typecast it to a value that's more meaningful. As I step through the code,

the variable might go out of scope, or it might have a different name in a subroutine, so I'd have to repeat the typecasting steps in the Watch window with the different variable name.

For example, suppose that one of the variables is called VBLine, and that it is an internal representation of a line of Visual Basic .NET code. It's much more meaningful to see<code>Dim MyVar As String</code> than a bunch of hex

numbers in the debugger. I drag and drop it to the Watch window, typecast it to aDIM statement, and expand/navigate the results to findMyVar. Then, I step into the next called function, withVBLine passed as

an argument. The receiving function names the parameterVBStatement, so my Watch window drilldown needs to be modified to use the different variable name.

This gets very cumbersome. Let's improve it!

Here's a simple demonstration of how you can control what the debugger displays.

Start Visual Studio 2005 or 2003. (It also works in Visual Studio 7, although the steps might be slightly different.)

Click File &gt; New &gt; Projects.

Select Visual C++, and then Win32 Console Application, and name itTest.

Click Finish in the wizard.

Paste in some sample code to debug.

To make things simple, let's use ANSI rather than Unicode characters (Visual Studio 2005 defaults to Unicode):

Click Project &gt; Properties &gt; Configuration Properties &gt;General &gt;Character Set.

Change the character set from Use Unicode Character Set to

Use Multi-Byte Character Set.

Let's also remove the check for 64-bit portability issues:

Click Project &gt; Properties &gt; Configuration Properties &gt;C++ &gt;General.

Clear Detect 64 bit portability issues.

Press F9 on the return line to set a breakpoint, and then press F5 to build and run the project.

When the breakpoint hits, the Debug window shows the following.

Now, let's control the string displayed for a given type:

Open the file called AutoExp.dat (which installs with Visual Studio) in the Visual Studio editor:

Click File &gt; Open &gt; File.

Locate the AutoExp.dat file. On my machine, it's at c:\Program Files\Microsoft Visual Studio 8\Common7\Packages\Debugger\Autoexp.dat.

This file describes how to customize the output of the Debug Watch, Locals, and Auto windows. It's formatted like an INI file.

Add the following line to the AutoExp.dat file, in the [AutoExpand] section.

Press F5 to go to the breakpoint.

Now the Watch window shows the following.

This is a big improvement: we've told the debugger which members of the structure to show, and how to format them! We can still click the+ to drill down the member hierarchy.

When starting a debug session, the debugger reads the AutoExpand file, and if the left of the equal sign matches the type in the Type column of the Locals/Watch/Auto window, the right side will direct how to format the displayed string. The comments at the

beginning of AutoExp.dat give more details, including more formatting options.

This is great, but it's nothing compared to what we'll do next!

You can write code that executes in the debugger process, and that can read the memory of the debugee! AutoExp.dat controls this feature too:

In the [AutoExpand] section of the AutoExp.dat file, replace the line that you added in Step 2 above with the following three lines.

Now, let's create the project that will build MyDbg.DLL, and add it to the current solution:

Click File &gt; New &gt; Project &gt;Visual C++ Win32 Project.

Name the project MyDbgEE, and select Add to Solution (rather thanCreate New Solution).

In the Win32 App Wizard that appears, change the application type to a DLL.

Change the project properties as above to non-Unicode and no 64-bit issues.

Add the following lines.

Now, we need to tell Visual Studio where to put the built DLL, so that the debugger can find it. We can use the build events in the project:

For the DLL project, click Project &gt; Properties &gt;Configuration Properties &gt;Build Events &gt;

Post Build Event &gt; Command Line.

Enter copy $(TargetPath) "$(DevEnvDir)". Make sure that you have the quotation marks and parentheses right. If you put in a description string, that string will be echoed to the Output window when building. Now, when you rebuild, the debug

DLL will be copied to the same directory as Devenv.exe.

Press F5 and see the values in the Debug window! Bring up the Task Manager and notice that the Process ID shown is the same as that of the Devenv.exe debugger process.

To make things more interesting, let's see how our debug code can read the debugger memory. We'll add some code to obscure a desired value, but we'll dig for it in the debug DLL.

Add the following code after the #include "windows.h" line in the main Test code.

Add the following code to just before the return statement.

This code creates a class, MyClass, with a pointer to another instance ofMyClass that contains the desired debug display value.

Now, we need to modify the debug DLL to dig for the value.

Copy the same structure definition as above into the debug DLL code. (Typically, these definitions will be in a shared #include file.)

Add the following code.

Press F5, and Bingo! You can still drill down into the class manually as before, so you haven't lost any functionality.

The debug DLL can be rebuilt even while debugging: it's loaded/unloaded as needed by the debugger. This means that persisting values might be cumbersome. I've used custom registry keys for persisting values, such as global variables.

I've been using this debug expression evaluator architecture for years for huge projects, including Visual Foxpro and Visual Basic.NET, and I find it indispensable and a huge time saver.

See also:

另外我從msdn的幫助文檔中找到了一些解釋:

To extend the expression evaluator for a custom data type, you write a custom viewer function in the expression evaluator Add-In DLL. The function uses a pointer to an object in the memory space of the program being debugged (not the memory space of the

expression evaluator you are extending). You cannot use normal casts with this pointer. You must read it and the data it points to using a callback function. A callback pointer of type

DEBUGHELPER* points to an object with various methods.

The syntax looks like this:

<a></a>

The sample has two implementations of this type of function,

AddIn_SystemTime and AddIn_FileTime in timeaddin.cpp. The

DEBUGHELPER struct (defined in custview.h) consists of function pointers that can assist you in writing your extension. This pointer is passed to your

CustomViewer function, and you can use it to call the helper functions.

You can get the processor type with pHelper-&gt;GetProcessorType. There are two methods for reading memory,

pHelper-&gt;ReadDebuggeeMemory and pHelper-&gt;ReadDebuggeeMemoryEx.

ReadDebuggeeMemoryEx handles 64-bit addresses and is supported by the Visual Studio .NET debugger.

ReadDebuggeeMemory does not handle 64-bit addresses and is supported by the Visual Studio .NET and Visual C++ 6.0 debuggers. If your Add-In is designed for the Visual Studio .NET debugger only, you can use

ReadDebuggeeMemoryEx. If your Add-In needs to work with Visual C++ 6.0 also, you must check the

dwVersion field and avoid calling ReadDebuggeeMemoryEx for Visual C++ 6.0.

The following code works with both debuggers and reads the contents of a localobject (whose type is

MyType) from the program being debugged:

繼續閱讀