2015-11-28 72 views
1

比方說,我們有以下文件,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__ } ...

回答

1

有沒有辦法抑制只有一個單一的屬性

的gdb的打印輸出沒有定義自定義漂亮的打印機:不。

您可以讓print命令通過爲您的班級定義自定義漂亮打印機來執行任何您想要的操作。文檔here

+0

謝謝你,@EmployedRussian - 很好有這個證實;乾杯! – sdaau

相關問題