gpt4 book ai didi

c++ - GDB:如何将内存地址转换为STL对象

转载 作者:行者123 更新时间:2023-12-02 10:27:10 33 4
gpt4 key购买 nike

我正在使用GDB分析核心转储。我知道它指向“std::basic_fstream >”的内存地址。
如何在GDB提示符下将原始内存地址转换为该模板对象?
我尝试通过以下命令:
(gdb)p *('std::basic_fstream >'*)0x7fae3800b280
在当前上下文中没有符号“std::basic_fstream >”。
(gdb)p *(std::basic_fstream > *)0x7fae3800b280
表达式中语法错误,在`)0x7fae3800b280'附近。

最佳答案

I tried by below command:


大胆猜测:您的 set auto-solib-add off中是否有 ~/.gdbinit
以下示例对我来说效果很好:
cat t.cc
#include <fstream>

int main(int argc, char *argv[])
{
std::wfstream s("/dev/tty", s.out);
for (int j = 0; j < argc; j++)
s << j << " " << argv[j] << std::endl;
return 0;
}

g++ -g t.cc && gdb -q ./a.out

(gdb) b 8
Breakpoint 1 at 0x1235: file t.cc, line 8.
(gdb) run foo
Starting program: /tmp/a.out foo
0 /tmp/a.out
1 foo

Breakpoint 1, main (argc=2, argv=0x7fffffffdc28) at t.cc:8
8 return 0;
(gdb) set print static off

(gdb) p s
$1 = {<std::basic_iostream<wchar_t, std::char_traits<wchar_t> >> = {<std::basic_istream<wchar_t, std::char_traits<wchar_t> >> = {<std::basic_ios<wchar_t, std::char_traits<wchar_t> >> = ...

(gdb) p &s
$2 = (std::wfstream *) 0x7fffffffd900

(gdb) p *('std::basic_fstream<wchar_t, std::char_traits<wchar_t> >' *)0x7fffffffd900
$3 = {<std::basic_iostream<wchar_t, std::char_traits<wchar_t> >> = {<std::basic_istream<wchar_t, std::char_traits<wchar_t> >> = {<std::basic_ios<wchar_t, std::char_traits<wchar_t> >> = {<std::ios_base> = ...
您应该尝试复制上述输出。
如果可以重现它,则您的工具将按预期工作,并且您需要集中精力分析 core转储。
如果上面的示例无法按预期工作,则说明您的工具链某些部分行为不当或配置错误。
我能够获得 No symbol结果的一种方法是通过设置 auto-solib-add off:
gdb -q ./a.out
Reading symbols from ./a.out...

(gdb) b 8
Breakpoint 1 at 0x1235: file t.cc, line 8.
(gdb) set auto-solib-add off
(gdb) run
Starting program: /tmp/a.out
0 /tmp/a.out

Breakpoint 1, main (argc=1, argv=0x7fffffffdc28) at t.cc:8
8 return 0;

(gdb) p &s
$1 = (std::wfstream *) 0x7fffffffd900

(gdb) p *('std::basic_fstream<wchar_t, std::char_traits<wchar_t> >' *) 0x7fffffffd900
No symbol "std::basic_fstream<wchar_t, std::char_traits<wchar_t> >" in current context.

关于c++ - GDB:如何将内存地址转换为STL对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63884022/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com