假设我们使用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}}
在这种情况下,我并不真正关心上面的矢量属性unimportant
,并且因为它有很多元素,所以它的打印输出使得阅读比它应该更加困难。但是,我对该对象的所有其他属性感兴趣,因此p
单独划分它们可能需要我很多工作来简单地在gdb
命令脚本中枚举它们(如果对象有很多属性)。
那么,有没有办法抑制对象中只有一个属性(在本例中为gdb
)的.unimportant
中的打印输出,甚至更好的类?而不是unimportant = std::vector of length 25, capacity 32 = {...}
,我会对打印输出中unimportant = std::vector of length 25, capacity 32 = { __noprint__ }
之类的内容感到满意......
答案 0 :(得分:1)