Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不能';t确定值';动态_cast的最派生类型 我最近在研究GDB7的Python扩展,我只是想用它编写一个小工具,在调试时显示C++容器的内容(比如列表)友好,但是在处理列表时遇到了麻烦。这是我的C++测试代码:< /P> 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; }_C++_Python_Gdb_Dynamic Cast - Fatal编程技术网

不能';t确定值';动态_cast的最派生类型 我最近在研究GDB7的Python扩展,我只是想用它编写一个小工具,在调试时显示C++容器的内容(比如列表)友好,但是在处理列表时遇到了麻烦。这是我的C++测试代码:< /P> 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; }

不能';t确定值';动态_cast的最派生类型 我最近在研究GDB7的Python扩展,我只是想用它编写一个小工具,在调试时显示C++容器的内容(比如列表)友好,但是在处理列表时遇到了麻烦。这是我的C++测试代码:< /P> 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; },c++,python,gdb,dynamic-cast,C++,Python,Gdb,Dynamic Cast,我在上面花了几个小时,有经验的人能给我一些关于这个问题的提示吗,或者给我一些完成这个小工具的建议吗?我真的很感谢你的帮助,谢谢 如果没有更多的信息,很难说出哪里出了问题 GDB正在尝试使用RTTI信息来查找该字段的完整对象。这在某种程度上是失败的。您可以尝试通过使用“设置打印对象打开”,然后打印有问题的字段,从CLI重现该问题 或者,您想使用dynamic_cast有什么特别的原因吗?只需使用简单的“cast”方法即可。我认为这将绕过这些检查 请注意,对于这种特定情况,您可能只需要选择现有的li

我在上面花了几个小时,有经验的人能给我一些关于这个问题的提示吗,或者给我一些完成这个小工具的建议吗?我真的很感谢你的帮助,谢谢

如果没有更多的信息,很难说出哪里出了问题

GDB正在尝试使用RTTI信息来查找该字段的完整对象。这在某种程度上是失败的。您可以尝试通过使用“设置打印对象打开”,然后打印有问题的字段,从CLI重现该问题

或者,您想使用dynamic_cast有什么特别的原因吗?只需使用简单的“cast”方法即可。我认为这将绕过这些检查


请注意,对于这种特定情况,您可能只需要选择现有的libstdc++pretty打印机。它们与gdb中现有的“print”命令集成,并已在libstdc++中处理每一个复杂的数据结构。许多发行版在程序使用libstdc++时会自动启用所有这些代码。

我在dynamic_cast()中也看到了同样的错误。使用cast()而不是dynamic\u cast()可以:

列出漂亮的打印。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;
}
示例会话

$ 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, }
$gdb-q-x test-list-pretty-print.gdb
程序接收信号信号陷阱,跟踪/断点陷阱。
trap_指令()位于list pretty print.cc:15
15表示(int i=0;i<10;++i)
#2 main()位于list pretty print.cc:13
13调试_break();
{}
程序接收信号信号陷阱,跟踪/断点陷阱。
trap_指令()位于list pretty print.cc:20
20 for(list::const_iterator citer=int_lst.begin();
#2 main()位于列表pretty print.cc:18
18调试_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.
(gdb)什么是国际性的
类型=标准::列表
(gdb)国际惯例是什么
type=std::_List\u base::_List\u impl
(gdb)下一个节点是什么
类型=标准::\详细信息::\列表\节点\基础*
(gdb)python print gdb.parse_and_eval('int_lst.\u M_impl.\u M_node.\u M_next')。动态类型
标准::\详细信息:\列表\节点\基础*
(gdb)python打印gdb.parse_和_eval('int_lst.\u M_impl.\u M_node.\u M_next')。动态(gdb.lookup_type('std::\u List_node')。指针())
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
gdb.error:无法确定动态\u转换的值的最派生类型
执行Python代码时出错。

我正在研究类似的问题,我认为如果被检查的类受到多重继承的约束,普通C转换可能会失败。
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()
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, }
(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.