繁体   English   中英

gdb:将内存地址转换为STL对象

[英]gdb: Cast memory address to an STL object

我有一个我知道是STL对象的内存地址。 说地址是0x603340,我知道那里有地图

如何显示该内存的内容作为gdb中的对象?

我尝试了这个:

p ('std::map<std::string, std::string*, std::less<std::string>, std::allocator<std::pair<std::string const, std::string*> > >'*) 0x603340

这让我:

No symbol "std::map<std::string, std::string*, std::less<std::string>, std::allocator<std::pair<std::string const, std::string*> > >" in current context.

知道我在做什么错吗? 谢谢。

我已将其作为第一步: gdb的简单stl容器检查器 我没有设法直接使用pmap ,但是我利用了它并创建了自己的scmy my_pmap:

>cat my.txt
define my_pmap
  set $my_obj =  *((std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::string*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::string*> > >*) $arg0)
  print $my_obj
  set $i = 0
  set $node = $my_obj._M_t._M_impl._M_header._M_left
  set $end = $my_obj._M_t._M_impl._M_header
  set $tree_size = $my_obj._M_t._M_impl._M_node_count

  while $i < $tree_size
    set $value = (void *)($node + 1)
    printf "elem[%u].left: ", $i
    p *( std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)$value
    set $value = $value + sizeof(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
    printf "elem[%u].right: ", $i
    p **((std::basic_string<char, std::char_traits<char>, std::allocator<char> >**)$value)
    if $node._M_right != 0
      set $node = $node._M_right
      while $node._M_left != 0
        set $node = $node._M_left
      end
    else
      set $tmp_node = $node._M_parent
      while $node == $tmp_node._M_right
        set $node = $tmp_node
        set $tmp_node = $tmp_node._M_parent
      end
      if $node._M_right != $tmp_node
        set $node = $tmp_node
      end
    end
    set $i=$i+1
  end
  printf "Map size = %u\n", $tree_size
end

然后,我编写了一个简短的C ++测试程序:

#include <map>
#include <string>
#include <iostream>

int main()
{
  std::string local_s1 = "Local1";
  std::string local_s2= "Local2";

  typedef std::map<std::string, std::string*> my_type_t;
  my_type_t my_map;
  my_map.insert(std::make_pair("Key-string-1", &local_s1));
  my_map.insert(std::make_pair("Key-string-2", &local_s2));
  my_map.insert(std::make_pair("Key-string-3", &local_s2));

  return 0;
}

然后我测试了我的脚本:

>gdb -q ./a.out
Reading symbols from /import/home/sergey.kurenkov/src/linux.x64.6.0/tests/test.gdb_map/a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x400c7c: file main.cpp, line 7.
Starting program: /import/home/sergey.kurenkov/src/linux.x64.6.0/tests/test.gdb_map/a.out

Temporary breakpoint 1, main () at main.cpp:7
7         std::string local_s1 = "Local1";
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6.x86_64
(gdb) source my.txt
(gdb) n
8         std::string local_s2= "Local2";
(gdb)
11        my_type_t my_map;
(gdb)
12        my_map.insert(std::make_pair("Key-string-1", &local_s1));
(gdb)
13        my_map.insert(std::make_pair("Key-string-2", &local_s2));
(gdb)
14        my_map.insert(std::make_pair("Key-string-3", &local_s2));
(gdb)
16        return 0;
(gdb) p &my_map
$1 = (my_type_t *) 0x7fffffffe040
(gdb) my_pmap 0x7fffffffe040
elem[0].left: $2 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603088 "Key-string-1"}}
elem[0].right: $3 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603028 "Local1"}}
elem[1].left: $4 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x6030f8 "Key-string-2"}}
elem[1].right: $5 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603058 "Local2"}}
elem[2].left: $6 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603168 "Key-string-3"}}
elem[2].right: $7 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603058 "Local2"}}
Map size = 3
(gdb)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM