Use of Natvis framework to observe value pointed by pointer

516 Views Asked by At

My goal is to observe a container of value which is pointed by a pointer. I am recommended to use natvis for this purpose. I am using VSCode to develop my project in Linux system. Unfortunately, I am not succeeded to get the desired value. I can only see the first address and value pointed by the pointer.

Sample code I am giving here.

  • foo.h
#include <iostream>

class FOO
{
    public:
        FOO(uint32_t a_, uint32_t b_) : a{a_}, b{b_}
                                        {}

        void Print_Value();
        uint32_t *pointer_array;

    protected:
        uint32_t a, b;

};
  • foo.cpp
#include "foo.h"

void FOO :: Print_Value()
{
    std::cout << "a: " << a << std::endl
              << "b: " << b << std::endl;
}
  • main.cpp
#include "foo.h"

int main()
{
    FOO obj_1(58,9);
    obj_1.Print_Value();

    uint32_t foo_array[5] = {5,15,96,8,77};
    obj_1.pointer_array = foo_array;


    return 0;
}
  • natvis file I have tried to create
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="FOO">
        <DisplayString>Test</DisplayString>
        <Expand>
            <Item Name="[pointer_array]">pointer_array</Item>
        </Expand>
    </Type>
</AutoVisualizer>
  • I have also tried the following but failed. Moreover, here I am not understanding how can I include the other items (a,b). Syntax is not clear to me.
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="FOO::pointer_array">
        <DisplayString>Test</DisplayString>
        <Expand>
            <CustomListItems>
                <Variable Name="pointer_array" InitialValue="0" />
                <Size>5</Size>
                <Loop Condition="pointer_array &lt; 5">
                  <Item Name="{pointer_array}"> 1 </Item>
                  <Exec> ++pointer_array </Exec>
                </Loop>
              </CustomListItems>
        </Expand>
    </Type>
</AutoVisualizer>
  • Added line in the launch.json file
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/foo_example",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/bin/",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "visualizerFile": "${workspaceFolder}/natvis_file/file.natvis",
            "showDisplayString": true,
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description":  "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

What I expect

  • In my debug window, I can see the value of the foo_array with the index.
  • I would also like to observe the value by using the class member pointer_array
  • I am recommended to use natvis to achieve this goal. I have tried by following 1 but failed.
  • My idea/ understanding regarding natvis is not so much clear. I guess I have to use CustomListItems to achieve the goal where suing Loop I can display the value pointed by the pointer but found this, this where it has told that using VSCode it is not possible. Though, I am not sure whether I am walking in right track or not.

My queries

  • If I want to display the value pointed by the pointer in the debug window, what/how I have to write my natvis file? If a working example is given would be really helpful to understand.
  • If it is not possible, is there any way to achieve it in VSCode?
1

There are 1 best solutions below

0
On BEST ANSWER

Finally, found the solution though came lots of other questions which will post in new post. I was wrongly interpreting the syntax.

  • file.natvis
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="FOO">
        <DisplayString>Test</DisplayString>
        <Expand>
        <Item Name="[a]">a</Item>
        <ArrayItems>
            <Size>5</Size>
            <ValuePointer>pointer_array</ValuePointer>
        </ArrayItems>
        </Expand>
    </Type>
</AutoVisualizer>
  • The pointer is pointed to the array. So, iteration through the array will reveal the pointed value by the pointer. For this, natvis has the ArrayItems element.