首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gdb PrettyPrinter插件例程StdStringPrinter在处理std::basic_string<wchar_t(,.*)?>$时崩溃

gdb PrettyPrinter插件例程StdStringPrinter在处理std::basic_string<wchar_t(,.*)?>$时崩溃
EN

Stack Overflow用户
提问于 2014-01-23 18:07:22
回答 1查看 515关注 0票数 2

我在分析一个崩溃转储时发现Python插件("/usr/share/gdb/python/libstdcxx/v6/printers.py") pretty-printer在下面的代码行中崩溃了

代码语言:javascript
复制
return self.val['_M_dataplus']['_M_p'].string (encoding, length = len)
LookupError: unknown encoding: UCS-4

如下所示

代码语言:javascript
复制
#22 0x00002b25639bb01b in Function(PTR *, const ._210::wstring &, const ._210::wstring &, const ._210::wstring &, bool) (
    pPjmDefn=0x2aaab7409e70, pszRepositoryName=
    Traceback (most recent call last):
  File "/usr/share/gdb/python/libstdcxx/v6/printers.py", line 469, in to_string
    return self.val['_M_dataplus']['_M_p'].string (encoding, length = len)
LookupError: unknown encoding: UCS-4

我开始分析代码

代码语言:javascript
复制
class StdStringPrinter:
    "Print a std::basic_string of some kind"

    def __init__(self, encoding, val):
        self.encoding = encoding
        self.val = val

    def to_string(self):
        # Look up the target encoding as late as possible.
        encoding = self.encoding
        if encoding == 0:
            encoding = gdb.parameter('target-charset')
        elif encoding == 1:
            encoding = gdb.parameter('target-wide-charset')

        # Make sure &string works, too.
        type = self.val.type
        if type.code == gdb.TYPE_CODE_REF:
            type = type.target ()

        # Calculate the length of the string so that to_string returns
        # the string according to length, not according to first null
        # encountered.
        ptr = self.val ['_M_dataplus']['_M_p']
        realtype = type.unqualified ().strip_typedefs ()
        reptype = gdb.lookup_type (str (realtype) + '::_Rep').pointer ()
        header = ptr.cast(reptype) - 1
        len = header.dereference ()['_M_length']
        return self.val['_M_dataplus']['_M_p'].string (encoding, length = len)

并意识到有一个带有参数['gdb.parameter', 'gdb.parameter']的对gdb.parameter的调用,它返回

代码语言:javascript
复制
(gdb) python print gdb.parameter('target-wide-charset')
UCS-4
(gdb) python print gdb.parameter('target-charset')
ANSI_X3.4-1968

编码被传递给self.val['_M_dataplus']['_M_p'].string (encoding, length = len),我最好的猜测是,它调用str.encodeunicode.encode,但它们都不是seems to support UCS-4

代码语言:javascript
复制
>>> u'data'.encode('UCS-4')

Traceback (most recent call last):
  File "<pyshell#529>", line 1, in <module>
    u'data'.encode('UCS-4')
LookupError: unknown encoding: UCS-4

我强烈地感觉到这是一个Bug,有什么线索或想法吗?

EN

回答 1

Stack Overflow用户

发布于 2014-01-24 08:41:20

这取决于Python是如何构建的。您可以在gdb中执行此操作,以了解:

代码语言:javascript
复制
python import sys
python print sys.maxunicode

我以前没有见过这个;我猜大多数发行版都是使用UCS-4支持构建的。

同样值得考虑的是您系统上的wchar_t是什么。也许UCS-4在这方面也是错误的。您可以在gdb中使用"set target-wide-charset“来更改此设置。IIRC通常不可能让gdb猜测正确的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21304973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档