如何显示 gdb 以正确显示我的变量?
How can I show gdb to show my variables properly?
例如,我有一个简单的子字符串 class 和一个简单的数组。当我调试时很头疼,因为我需要多次点击才能获得某种合理的信息。有没有一种方法可以标记我的来源有某种配置,当我打印变量 a 我实际上是说
p *a.start@(a.end-a.start)
当我检查数组时,我希望它以上面的样式显示变量(p *arr.array@arr.pos
是一团糟)
当前 GDB 输出
$ gdb ./a.out
(gdb) br a.cpp:38
Breakpoint 1 at 0x128f: file a.cpp, line 38.
(gdb) r
(gdb) p a
= {start = 0x555555556004 "My test string that has two parts", end = 0x555555556012 " that has two parts"}
(gdb)
我的源代码是用g++ -g a.cpp
编译的
#include <cstdio>
struct MyString
{
const char *start, *end;
int size() { return end-start; }
};
template<class T>
struct MyArray
{
int pos;
T array[10];
MyArray() : pos(0) {}
void push(T val) {
if (pos >= 10)
return;
array[pos++] = val;
}
};
struct MoreComplex
{
int val;
MyString substring;
};
int main() {
const char* str = "My test string that has two parts";
MyString a{str, str+14};
MyString b{str+20, str+27};
MoreComplex c{5, b};
MyArray<MyString> arr;
arr.push(a);
arr.push(b);
MyArray<MoreComplex*> arr2;
arr2.push(&c);
puts("Breakpoint here");
return 0;
}
如果您有 python-enabled 构建的 gdb,您可以为您的类型添加自定义 pretty printers。
您可以尝试将以下内容添加到您的 .gdbinit 中:
python
class MyStringPrinter:
"Print a MyString"
def __init__ (self, val):
self.val = val
def to_string (self):
ptr = self.val['start']
len = self.val['end'] - ptr
return ptr.string (length = len)
def display_hint (self):
return 'string'
pp = gdb.printing.RegexpCollectionPrettyPrinter("mine")
pp.add_printer('MyString', '^MyString$', MyStringPrinter)
gdb.printing.register_pretty_printer(gdb.current_objfile(), pp)
end
这将在打印 MyString
对象时给出此结果:
(gdb) p a
= "My test string"
(gdb) p b
= "has two"
(gdb) p c
= {val = 5, substring = "has two"}
例如,我有一个简单的子字符串 class 和一个简单的数组。当我调试时很头疼,因为我需要多次点击才能获得某种合理的信息。有没有一种方法可以标记我的来源有某种配置,当我打印变量 a 我实际上是说
p *a.start@(a.end-a.start)
当我检查数组时,我希望它以上面的样式显示变量(p *arr.array@arr.pos
是一团糟)
当前 GDB 输出
$ gdb ./a.out
(gdb) br a.cpp:38
Breakpoint 1 at 0x128f: file a.cpp, line 38.
(gdb) r
(gdb) p a
= {start = 0x555555556004 "My test string that has two parts", end = 0x555555556012 " that has two parts"}
(gdb)
我的源代码是用g++ -g a.cpp
#include <cstdio>
struct MyString
{
const char *start, *end;
int size() { return end-start; }
};
template<class T>
struct MyArray
{
int pos;
T array[10];
MyArray() : pos(0) {}
void push(T val) {
if (pos >= 10)
return;
array[pos++] = val;
}
};
struct MoreComplex
{
int val;
MyString substring;
};
int main() {
const char* str = "My test string that has two parts";
MyString a{str, str+14};
MyString b{str+20, str+27};
MoreComplex c{5, b};
MyArray<MyString> arr;
arr.push(a);
arr.push(b);
MyArray<MoreComplex*> arr2;
arr2.push(&c);
puts("Breakpoint here");
return 0;
}
如果您有 python-enabled 构建的 gdb,您可以为您的类型添加自定义 pretty printers。
您可以尝试将以下内容添加到您的 .gdbinit 中:
python
class MyStringPrinter:
"Print a MyString"
def __init__ (self, val):
self.val = val
def to_string (self):
ptr = self.val['start']
len = self.val['end'] - ptr
return ptr.string (length = len)
def display_hint (self):
return 'string'
pp = gdb.printing.RegexpCollectionPrettyPrinter("mine")
pp.add_printer('MyString', '^MyString$', MyStringPrinter)
gdb.printing.register_pretty_printer(gdb.current_objfile(), pp)
end
这将在打印 MyString
对象时给出此结果:
(gdb) p a
= "My test string"
(gdb) p b
= "has two"
(gdb) p c
= {val = 5, substring = "has two"}