gpt4 book ai didi

c++ - 抑制 gdb 中特定 C++ 对象属性的打印输出?

转载 作者:行者123 更新时间:2023-11-30 05:35:08 26 4
gpt4 key购买 nike

假设我们有以下文件 test.cpp,使用 g++ --std=c++11 -g test.cpp -o test.exe 编译:

// test.cpp
// g++ --std=c++11 -g test.cpp -o test.exe
// gdb -ex "b test.cpp:37" -ex "r" -ex "p obj1" -ex "p obj2" --args test.exe
#include <iostream>
#include <vector>

class Tester {
public:
std::string id = "";
std::vector<int> important;
std::vector<int> unimportant;
};

int main()
{
Tester obj1;
Tester obj2;

obj1.id = "OBJ1";
obj1.important.push_back(1);
obj1.important.push_back(2);
obj1.important.push_back(3);
obj1.important.push_back(4);
for (int i=0; i<25; i++) {
obj1.unimportant.push_back(-10000000);
}

obj2.id = "OBJ2";
obj2.important.push_back(5);
obj2.important.push_back(6);
obj2.important.push_back(7);
obj2.important.push_back(8);
for (int i=0; i<25; i++) {
obj2.unimportant.push_back(-10020000);
}

std::cout << "Before exit (for breakpoint): " << obj1.important.size() << ", " << obj2.important.size() << std::endl;

return 0;
}

如果这个程序在 gdb 中运行,通过上面给出的命令行,我得到这个:

$ gdb -ex "b test.cpp:37" -ex "r" -ex "p obj1" -ex "p obj2" --args test.exe 
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
...
Starting program: /tmp/test.exe

Breakpoint 1, main () at test.cpp:37
37 std::cout << "Before exit (for breakpoint): " << obj1.important.size() << ", " << obj2.important.size() << std::endl;
$1 = {id = "OBJ1", important = std::vector of length 4, capacity 4 = {1, 2, 3, 4},
unimportant = std::vector of length 25, capacity 32 = {-10000000, -10000000, -10000000, -10000000, -10000000, -10000000,
-10000000, -10000000, -10000000, -10000000, -10000000, -10000000, -10000000, -10000000, -10000000, -10000000, -10000000,
-10000000, -10000000, -10000000, -10000000, -10000000, -10000000, -10000000, -10000000}}
$2 = {id = "OBJ2", important = std::vector of length 4, capacity 4 = {5, 6, 7, 8},
unimportant = std::vector of length 25, capacity 32 = {-10020000, -10020000, -10020000, -10020000, -10020000, -10020000,
-10020000, -10020000, -10020000, -10020000, -10020000, -10020000, -10020000, -10020000, -10020000, -10020000, -10020000,
-10020000, -10020000, -10020000, -10020000, -10020000, -10020000, -10020000, -10020000}}

在这种情况下,我并不真正关心上面的 vector 属性 unimportant,并且由于它有很多元素,它的打印输出使阅读变得比应该的困难得多。但是,我对该对象的所有其他属性感兴趣,因此 p 单独打印它们可能需要我做很多工作才能在 gdb 命令脚本中简单地枚举它们(如果对象有很多属性)。

那么,有没有办法在 gdb 中抑制对象的单个属性(在本例中为 .unimportant)的打印输出,甚至更好一类?而不是 unimportant = std::vector of length 25, capacity 32 = {...},我会喜欢 unimportant = std::vector of length 25, capacity 32 = { __noprint__ } 在打印输出中...

最佳答案

is there a way to suppress the printout in gdb of only a single property

不定义自定义 pretty-print :否。

你可以通过为你的类定义一个自定义 pretty-print 来让 print 命令做任何你想做的事情。文档 here .

关于c++ - 抑制 gdb 中特定 C++ 对象属性的打印输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33976774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com