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

Suppressing printout of specific C++ object properties in gdb?

假设我们有以下文件 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;
 = {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}}
 = {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),或者更好的是class?而不是 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

没有定义自定义漂亮打印机:否。

您可以让 print 命令为您的 class 定义一个自定义漂亮的打印机来做任何您想做的事情。文档 here.