2

最近、gdb7 の python 拡張機能に取り組んでいます。これを使用して、デバッグ中に C++ コンテナー (リストなど) の内容を表示するための小さなツールを書きたいと思っています。しかし、リストを扱うときに問題が発生しました。これは、テスト用の私の C++ コードです。

int main() {
list<int> int_lst;
for (int i = 0; i < 10; ++i)
    int_lst.push_back(i);

for(list<int>::const_iterator citer = int_lst.begin();
    citer != int_lst.end(); ++citer)
    cout << *citer << " ";
cout << endl;

return 0;
}

そして、「gdb を使用したデバッグ」チュートリアルに従って小さな python コードを作成し、int_lst の内容を表示しようとします。

import gdb
class Hello(gdb.Command):

    def __init__(self):
        super(Hello, self).__init__("plist", gdb.COMMAND_OBSCURE)

    def invoke(self, arg, from_tty):
        cpp_lst = gdb.parse_and_eval("int_lst")
        header = cpp_lst['_M_impl']['_M_node']
        next = header['_M_next']
        # next is _List_node_base, I have to cast it to its derived type for data
        next.dynamic_cast(gdb.lookup_type("std::_List_node<``int>").pointer())
Hello()

C++ STL では、std::_List_node_base はリスト内のノードの基本クラスですが、派生テンプレート クラス std::_List_node のみが値を含むデータ メンバー「_M_data」を持っているため、それを dynamic_cast する必要がありますが、gdb は文句を言います:

Error occurred in Python command: Couldn't determine value's most derived type for dynamic_cast

私はそれに数時間を費やしましたが、この問題についてのヒントや、この小さなツールを達成するための提案を経験した人はいますか? あなたの助けに本当に感謝します、ありがとう!

4

2 に答える 2

0

dynamic_cast()からも同じエラーが表示されます。dynamic_cast( )の代わりにcast()を使用すると、次のように機能します。

list-pretty-print.cc

#include <iostream>
#include <list>

/* https://github.com/scottt/debugbreak */
#include "debugbreak/debugbreak.h"

using namespace std;

int main()
{
    list<int> int_lst;

    debug_break();

    for (int i = 0; i < 10; ++i)
        int_lst.push_back(i);

    debug_break();

    for(list<int>::const_iterator citer = int_lst.begin();
            citer != int_lst.end(); ++citer)
        cout << *citer << " ";
    cout << endl;
    return 0;
}

list-pretty-print.py

import gdb

class PList(gdb.Command):
    def __init__(self):
        super(PList, self).__init__('plist', gdb.COMMAND_OBSCURE)

    def invoke(self, arg, from_tty):
        int_list_pointer_type = gdb.lookup_type('std::_List_node<int>').pointer()
        lst = gdb.parse_and_eval('int_lst')
        node = lst['_M_impl']['_M_node']
        nxt = node['_M_next']

        if node.address == nxt:
            gdb.write('{}\n')
            return
        else:
            gdb.write('{')

        while True:
            e = nxt.cast(int_list_pointer_type).dereference()
            gdb.write('%d, ' % (e['_M_data'],))
            nxt = e['_M_next']
            if node.address == nxt:
                gdb.write('}\n')
                return

PList()

test-list-pretty-print.gdb

set confirm off
set pagination off
set python print-stack full

file list-pretty-print
source list-pretty-print.py
run
up 2
plist

continue
up 2
plist

quit

サンプルセッション

$ gdb -q -x test-list-pretty-print.gdb

Program received signal SIGTRAP, Trace/breakpoint trap.
trap_instruction () at list-pretty-print.cc:15
15      for (int i = 0; i < 10; ++i)
#2  main () at list-pretty-print.cc:13
13      debug_break();
{}

Program received signal SIGTRAP, Trace/breakpoint trap.
trap_instruction () at list-pretty-print.cc:20
20      for(list<int>::const_iterator citer = int_lst.begin();
#2  main () at list-pretty-print.cc:18
18      debug_break();
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, }

一連の式を見ると、dynamic_cast()がそこで動作することになっているかどうかわかりません:

(gdb) whatis int_lst
type = std::list<int, std::allocator<int> >
(gdb) whatis int_lst._M_impl
type = std::_List_base<int, std::allocator<int> >::_List_impl
(gdb) whatis int_lst._M_impl._M_node._M_next
type = std::__detail::_List_node_base *
(gdb) python print gdb.parse_and_eval('int_lst._M_impl._M_node._M_next').dynamic_type
std::__detail::_List_node_base *
(gdb) python print gdb.parse_and_eval('int_lst._M_impl._M_node._M_next').dynamic_cast(gdb.lookup_type('std::_List_node<int>').pointer())
Traceback (most recent call last):
  File "<string>", line 1, in <module>
gdb.error: Couldn't determine value's most derived type for dynamic_cast
Error while executing Python code.
于 2013-05-24T17:22:43.063 に答える