我的目標是觀察一個指標指向的值容器。我建議natvis用于此目的。我正在使用VSCode在 Linux 系統中開發我的專案。不幸的是,我沒有成功獲得所需的價值。我只能看到指標指向的first addressand 。value
我在這里給出的示例代碼。
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我試圖創造
<?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)。我不清楚語法。
<?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 < 5">
<Item Name="{pointer_array}"> 1 </Item>
<Exec> pointer_array </Exec>
</Loop>
</CustomListItems>
</Expand>
</Type>
</AutoVisualizer>
- 在
launch.json檔案中添加行
{
// 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嗎?
uj5u.com熱心網友回復:
最后,雖然有很多其他問題將在新帖子中發布,但找到了解決方案。我錯誤地解釋了語法。
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元素。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/484006.html
下一篇:C中的函式引數對齊
