Saturday, May 21, 2011

Becoming a Console Programmer : Extending The Watch Window (autoexp.dat)

Watch windows are important, they show us our data in various forms and generally enable us to debug effectively, sometimes however they need help.

Setup a simple windows console project and use the following code:
struct s_test_level1
{
    int m_count;
    char* m_string;
    float m_value;

    void print(void)
    {
        if (m_string)
        {
            printf("%s %d %f\n",m_string, m_count, m_value);
        }
    }
};

static const int k_num_elements= 32;
struct s_test_level2
{
    s_test_level1 m_elements[k_num_elements];
};

int _tmain(int argc, _TCHAR* argv[])
{
    s_test_level2 test;

    memset(&test,0,sizeof(test));

    test.m_elements[0].m_string= "my foo";
    test.m_elements[0].m_count= strlen(test.m_elements[0].m_string);
    test.m_elements[0].m_value=3.14159265358f; // everyone loves Pi

    test.m_elements[0].print();

    return 0; // <<= breakpoint here
}

now run the program putting a breakpoint on indicated line. Open up a watch window and drop test into it opening up the m_elements array, you should see something like


This is a really simple but even now we see some details we don't always need... lets reformat it.

open up the file

VS2010 PC: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Packages\Debugger\autoexp.dat
VS2008 PC: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\autoexp.dat
Xbox360: C:\Program Files (x86)\Microsoft Xbox 360 SDK\bin\win32\autoexp.dat for 360

search for "[AutoExpand]" and add the line

s_test_level1=<m_count,d>, <m_string,s>, <m_value,f>f

save that file and re run the test program.... you should now see



a much easier to read version of the same data.

All the original information is still available should you need it but this new format is much easier to quickly consume.

This form of watch window help is somewhat limited however, you can only interpret existing data, remove superfluous detail, do some VERY rudimentary math and generally make things cleaner and easier to consume; see the next post for MORE.

All of the help required to use autoexp.dat is within the file itself. Note that the "[Visualizer]" section is currently not available for xbox 360 targets but is VERY powerful for other targets.

enjoy and feel free to ask questions.

3 comments:

Ryan said...

Great post! Thanks for the info!

I'm curious if there's an easy way to auto-populate a watch window with specific variables?

For example, say I want to look at a single property of each object in a list...

myList[0].prop1
myList[1].prop1
myList[2].prop1
etc...

Do you have any tips/tricks you could offer for that scenario? As it stands, I'm typing each expression in the watch window by hand. That's fine for 5 elements, but when there's 35 elements... life becomes pretty miserable.

Thanks!

Andy Firth said...

you can insert into the watch window using a macro (just record one of you adding it and then edit the macro).

another option for your specific example tho would be to write a visualizer section such that you could view the entire array in preview mode by hitting the [+] sign.

Ryan said...

Brilliant! Thanks Andy! Totally forgot that VS had macros. LOL

Visitor IP Address City