比方說,我們有以下文件,test.cpp
,與g++ --std=c++11 -g test.cpp -o test.exe
編譯:禁止在gdb中打印輸出特定的C++對象屬性?
// 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
,而且由於它有很多元素,因此它的打印輸出會使讀取比應該更困難。但是,我對該對象的所有其他屬性感興趣,因此單獨對它們進行沖洗可能會花費我相當多的工作來簡單地枚舉它們在gdb
命令腳本中(如果該對象具有多個屬性)。
那麼,是否有一種方法可以在gdb
中打印一個對象的單個屬性(在本例中爲.unimportant
),或者甚至更好的一個類?取而代之的unimportant = std::vector of length 25, capacity 32 = {...}
,我會很高興的東西,如在打印輸出unimportant = std::vector of length 25, capacity 32 = { __noprint__ }
...
謝謝你,@EmployedRussian - 很好有這個證實;乾杯! – sdaau