在调试C++应用程序时,是否有一些“默认函数”可以像字符串一样打印对象到GDB上?例如:toString();
还是我的类必须实现类似的功能?
在调试C++应用程序时,是否有一些“默认函数”可以像字符串一样打印对象到GDB上?例如:toString();
还是我的类必须实现类似的功能?
print
命令打印std::string
(或其他任何内容)。然而,苦于C++模板容器内部可能不是令人愉快的事情。在最近的工具链版本中(GDB + Python + Pretty Printers通常作为大多数用户友好的Linux发行版开发包的一部分安装在一起),这些被自动识别并打印(漂亮!)。例如:$ cat test.cpp
#include <string>
#include <iostream>
int main()
{
std::string s = "Hello, World!";
std::cout << s << std::endl;
}
$ g++ -Wall -ggdb -o test ./test.cpp
$ gdb ./test
(gdb) break main
Breakpoint 1 at 0x400ae5: file ./test.cpp, line 6.
(gdb) run
Starting program: /tmp/test
Breakpoint 1, main () at ./test.cpp:6
6 std::string s = "Hello, World!";
Missing separate debuginfos, use: debuginfo-install glibc-2.16-28.fc18.x86_64 libgcc-4.7.2-8.fc18.x86_64 libstdc++-4.7.2-8.fc18.x86_64
(gdb) next
7 std::cout << s << std::endl;
(gdb) p s
$1 = "Hello, World!"
(gdb)
std::string
,您可以调用它的 c_str()
成员函数,该函数返回 const char*
:(gdb) p str.c_str()
$1 = "Hello, World!"
定义operator<<
并从GDB中调用它
C++中Java的toString的等效方法?在评论中提到,operator<<
是在类上定义to string方法最常见的方式。
这可能是最明智的方法,因为生成的字符串方法本身将成为代码库的一部分,因此:
不幸的是,我还没有找到完全合理的调用operator<<
的方法,真是一团糟: 在gdb中调用 operator<<
这在我的hello world测试中起作用:
(gdb) call (void)operator<<(std::cerr, my_class)
MyClass: i = 0(gdb)
结尾没有换行符,但我可以接受这样。
main.cpp
#include <iostream>
struct MyClass {
int i;
MyClass() { i = 0; }
};
std::ostream& operator<<(std::ostream &oss, const MyClass &my_class) {
return oss << "MyClass: i = " << my_class.i;
}
int main() {
MyClass my_class;
std::cout << my_class << std::endl;
}
编译使用:
g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o main.out main.cpp
在GDB 8.1.0和Ubuntu 18.04中测试通过。