使用Natvis框架观察指针指向的值

Use of Natvis framework to observe value pointed by pointer

我的目标是观察指针指向的值容器。为此,我建议使用 natvis。我正在使用 VSCode 在 Linux 系统中开发我的项目。不幸的是,我没有成功获得所需的值。只能看到指针指向的first addressvalue

我在这里给出的示例代码。

#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;

};
#include "foo.h"

void FOO :: Print_Value()
{
    std::cout << "a: " << a << std::endl
              << "b: " << b << std::endl;
}
#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;
}
<?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>
<?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>
{
    // 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
                }
            ]
        }
    ]
}

我期待的

我的查询

最后,找到了 solution,虽然还有很多其他问题会 post 在新的 post 中。我错误地解释了语法。

  • 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>
  • 指针指向数组。因此,遍历数组将显示指针指向的值。为此,natvis 具有 ArrayItems 元素。