【问题标题】:Extend display range of ArrayItems/IndexListItems using natvis使用 natvis 扩展 ArrayItems/IndexListItems 的显示范围
【发布时间】:2022-07-10 20:43:52
【问题描述】:

我正在尝试使用由指针指向的natvis 来可视化内存内容。我还尝试将内存声明为向量。但每次我面临的问题是,在调试过程中,可视化器只能首先显示50 entry

我在这里举一个非常简单的例子。假设pointer_arrayFoo 类的成员。在驱动程序文件中,创建了一个大小为 5000 的 array,它由数组指向。我想用变量pointer_array 观察数组的值。我还试图了解natvisstd::vector 的反应方式,这就是为什么还声明了一个向量(foo_vec)作为成员变量。

foo.h:

#include <iostream>
#include <vector>

class Foo
{
    public:
        Foo(){}

        uint32_t *pointer_array;
        std::vector<uint32_t> foo_vec;
};

main.cpp:

#include "foo.h"
# define ARRAY_SIZE 5000

int main()
{
    Foo obj_1;

    uint32_t foo_array[ARRAY_SIZE];
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        foo_array[i] = i*2;
    }
    obj_1.pointer_array = foo_array;

    for(uint32_t i = 0; i < ARRAY_SIZE; i++)
    {
        obj_1.foo_vec.push_back(i*3);
    }

    return 0;
}

以下natvis file我用过。

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="Foo">
        <DisplayString>Testing_Natvis</DisplayString>
        <Expand>
        <ArrayItems>
            <Size>5000</Size>
            <ValuePointer>pointer_array</ValuePointer>
        </ArrayItems>

        <!-- Tested with IndexListItems but failed to fetch all data, still only first 49 entry -->
        <!-- <IndexListItems>
          <Size>5000</Size>
          <ValueNode>pointer_array[$i]</ValueNode>
        </IndexListItems> -->

          <!-- Same result as like as pointer_array. Only first 49 entry is appeared -->
          <!-- <IndexListItems>
            <Size>foo_vec.size()</Size>
            <ValueNode>foo_vec[$i]</ValueNode>
          </IndexListItems> -->

          <!-- <ArrayItems>
            <Size>foo_vec.size()</Size>
            <ValuePointer>&amp;foo_vec[0]</ValuePointer>
        </ArrayItems> -->
        </Expand>
    </Type>
</AutoVisualizer>

launch.json 中,我只添加了以下两行:

"visualizerFile": "${workspaceFolder}/natvis_file/file.natvis",
"showDisplayString": true,

为了更好地理解,我在这里给出了输出的屏幕截图,在 natvis 文件中我使用了IndexListItems 并给定大小 80 以查看索引 0 到 79 的值,但显示的最后一个值来自索引 49.

下面显示我给了size 值 6,而 natvis 完美地显示了从索引 0 到 5 的值。

使用 Natvis 实现所有内存条目的任何解决方法?

【问题讨论】:

  • 我不明白这个问题。你总是想显示 5000 个元素吗?或者此时数组中的元素数量?如果是第二种,你怎么知道数组中有多少元素?
  • 我想显示5000 元素。不是 5000tharray[4999] 值。所以我想看到一堆价值。 Here 我发现 debugger 不能显示超过 1000 的值,但在我的情况下 natvis 只显示前 50 个值(索引 0 到 49)。
  • @WernerHenze 我添加了 natvis 生成的输出的屏幕截图。同样在之前的评论中我已经给出了你的答案。
  • @WernerHenze 如果您愿意看一下这个问题,这对我很有帮助。我已经修改了问题(提供了2个屏幕截图,您可以在其中看到我面临的问题。还描述了背景)并回答了您的问题。如果你还有什么想知道的可以问我。我在这方面处于死胡同。
  • 它适用于std::vector 吗?如果是,您是否比较过 STL natvis 文件?

标签: c++ visual-studio-code natvis


【解决方案1】:

根据this issue on github 的说法,对 50 的限制是“设计使然”,无意更改。

【讨论】:

  • 为什么IndexListItems 不起作用?当i 达到 50 时,它是否也会停止它的迭代?
  • 找到this,请您确认一下我是对还是错?
  • 找到ExpandedItem,它可以扩展stl container (tested on std::vector)直到索引999,因为size可能存在限制,找到thisthis
【解决方案2】:

我查看了code 以了解此限制。所以,我可以提供一个解决方案的想法——你可以展示容器的一部分

<IndexListItems>
  <Size>foo_vec.size()</Size>
  <ValueNode>foo_vec[$i]</ValueNode>
</IndexListItems>
<IndexListItems>
  <Size>foo_vec.size()-50</Size>
  <ValueNode>foo_vec[$i+50]</ValueNode>
</IndexListItems>
...
<IndexListItems>
  <Size>foo_vec.size()-4950</Size>
  <ValueNode>foo_vec[$i+4950]</ValueNode>
</IndexListItems>

【讨论】:

  • 对于std::vector(也许对于所有stl container)可以使用ExpandItem,它从index 0 to 999显示。这个范围也是一个限制,见thisthis
  • 你给的方法我试过了,真的很麻烦。如果有任何chunk 会组织起来,那就太好了。想法是使用容器的大小(foo_vec.size())和块的最大数据限制(设计为 50)来查找块号。所以,为了表示我的数据,我需要5000/50 = 100 chunk。如果Natvis 可以做出这个决定并在这些块中组织数据将会很棒。我不知道这个主题是否已经存在。
猜你喜欢
  • 2012-11-24
  • 2022-12-10
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多