gdb - 如何转换为模板类型?
问题描述
在 gdb 中,如果你有一个指向某个东西的指针,你可以在打印它之前强制转换它。
例如,这有效:
print *(int*) 0xDEADBEEF
但是,我该如何打印std::vector<T>
?特别是一个std::vector<std::string>
?
如果是std::string
,我可以使用std::__cxx11::string
,whatis std::string
输出,但我无法说服 gdb 喜欢std::vector<int>
(例如)。正如它所说,引用它并没有帮助,No symbol "std::vector<int>" in current context.
解决方案
您可以做到这一点的一种方法是使用类型的错位名称。例如,std::vector<int>
当前 gcc 和 libstdc++ 的错误名称是_ZSt6vectorIiSaIiEE
,我是通过在Compiler Explorer上编译以下代码找到的:
#include <vector>
void foo(std::vector<int>) {}
// Mangled symbol name: _Z3fooSt6vectorIiSaIiEE
// _Z means "this is C++".
// 3foo means "identifier 3 chars long, which is `foo`"
// Strip those off and you're left with: St6vectorIiSaIiEE
// Add the _Z back: _ZSt6vectorIiSaIiEE
的损坏名称std::vector<std::string>
是:_ZSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE
,可以使用 进行验证whatis
。
实际表演演员:
print *(_Z3fooSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE*) 0xDEADBEEF
推荐阅读
- php - AJAX 不向脚本发送数据
- nginx - 通过 nginx 将 POST 数据传递到 unix 域套接字
- asp.net-core-2.2 - 删除 ASP.Net Core 2.2 InProcess 上的 HTTP 标头
- python - 无法运行天蓝色命令行
- reactjs - 将复杂的 div 容器转换为 React 组件
- javascript - 在两个 html 页面之间传递会话
- ios - 在 Xcode 中识别静态单元格的正确方法?
- reactjs - 使用 React Router 从数据库中的 slug 生成 URL
- java - JPA Hibernate - 使用给定 ID 插入,而不是序列
- java - 带有登录的 Webservice 方法显示 java 错误