首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Natvis框架观察指针指向的值

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

Stack Overflow用户
提问于 2022-05-19 20:08:58
回答 1查看 130关注 0票数 1

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

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

  • foo.h
代码语言:javascript
复制
#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
代码语言:javascript
复制
#include "foo.h"

void FOO :: Print_Value()
{
    std::cout << "a: " << a << std::endl
              << "b: " << b << std::endl;
}
  • main.cpp
代码语言:javascript
复制
#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
代码语言:javascript
复制
<?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>
  • 我也尝试过以下几种方法,但失败了。此外,在这里,我不理解如何包括其他项目(a,b)。我不清楚语法。
代码语言:javascript
复制
<?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>
  • launch.json文件中添加行
代码语言:javascript
复制
{
    // 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
                }
            ]
        }
    ]
}

我所期望的

  • 在调试窗口中,我可以看到带有索引的foo_array的值。
  • 我还想通过使用类成员pointer_array来观察这个值。
  • 建议我使用natvis来实现这个目标。我通过跟踪1尝试过,但是失败了。
  • 我对natvis的想法/理解不太清楚。我想我必须使用CustomListItems来达到起诉Loop的目的--我可以显示指针指向的值,但是找到了,在那里它告诉使用VSCode是不可能的。虽然,我不确定我是否走在正确的轨道上。

我的疑问

  • 如果我想在调试窗口中显示指针所指向的值,我必须如何编写我的natvis文件?如果给出一个有用的例子,就会对理解很有帮助。
  • 如果这是不可能的,有没有办法在VSCode中实现?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-20 21:08:49

最后,找到了解决方案,尽管出现了许多其他问题,这些问题将在新的帖子中发布。我错误地解释了语法。

  • file.natvis
代码语言:javascript
复制
<?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>
  • 指针指向数组。因此,通过数组的迭代将显示指针指向的值。为此,natvisArrayItems元素。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72310540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档