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

时间:2014-01-23 10:07:22

标签: python c++ debugging gdb pretty-print

我正在分析崩溃转储,我意识到Python插件pretty-printer(“/ usr / share / gdb / python / libstdcxx / v6 / printers.py”)在以下行中崩溃了

return self.val['_M_dataplus']['_M_p'].string (encoding, length = len)
LookupError: unknown encoding: UCS-4

如下图所示

#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

我开始分析代码

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']会返回

(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

>>> 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,任何线索或想法?

1 个答案:

答案 0 :(得分:0)

这取决于Python的构建方式。您可以从gdb执行此操作以查找:

python import sys
python print sys.maxunicode

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

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