【问题标题】:How do I apply the printers.py modification? (Linux OS)如何应用 printers.py 修改? (Linux 操作系统)
【发布时间】:2020-09-07 13:57:13
【问题描述】:

我检查了核心文件,因为在Linux上运行的进程(c++ lang)死了,核心文件的内容

[核心文件]


File "/usr/lib64/../share/gdb/python/libstdcxx/v6/printers.py", line 558, in to_string

return self.val['_M_dataplus']['_M_p'].lazy_string (length = len)

RuntimeError: Cannot access memory at address 0x3b444e45203b290f

我认为printers.py 中的StdStringPrinter 类存在问题。 所以我在这个网站上查找了解释我正在寻找的问题的文本,修改了 printers.py,并在我的主路径上创建了一个 .gdbinit 并编写了内容。

How to enable gdb pretty printing for C++ STL objects in Eclipse CDT?

Eclipse/CDT Pretty Print Errors

但是这种方法与我正在寻找的方法有点不同,因为它是在 Eclipse 中完成的。

我的 gdb 版本是 7.6.1-94.el7

[打印机.py]

class StdStringPrinter:
"Print a std::basic_string of some kind"

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

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

    sys.stdout.write("HelloWorld")  // TEST Code
    # 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']
    if hasattr(ptr, "lazy_string"):
        return ptr.lazy_string (length = len)
    return ptr.string (length = len)

def display_hint (self):
    return 'string'

[.gdbinit]

python
import sys
sys.path.insert(0, '/home/Hello/gcc-4.8.2/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

我的问题是修改printers.py,写gdbinit,然后重新编译过程测试是否已经应用为修改。 如何在 Linux 终端打印修改后的 TEST 代码?

【问题讨论】:

    标签: python c++ linux gdb


    【解决方案1】:

    我认为 Printers.py 中的 StdStringPrinter 类存在问题

    我认为您从根本上感到困惑,并且您的问题printers.py毫无关系

    您没有向我们展示您的 GDB 会话,但您似乎试图打印一些 std::string 类型的变量,当您这样做时,GDB 产生了以下错误:

    RuntimeError: Cannot access memory at address 0x3b444e45203b290f

    这个错误意味着 GDB 无法从内存位置0x3b444e45203b290f 读取值。在x86_64 系统上,这样的位置确实无法读取,因为该地址没有canonical form

    结论:您所遵循的指针(可能是程序中指向std::string 的指针)实际上并未指向std::string。 “修复”printers.py 并不能解决这个问题。

    这个结论得到了证实

    在 Linux 上运行的进程(c++ lang)死了,

    最后,你给 GDB 打印的指针:0x3b444e45203b290f 看起来很像 ASCII 字符串。解码它,我们有:\xf); END;。因此,您的程序很可能在指针应该位于的位置上乱涂了); END;,并且您有某种缓冲区溢出。

    附言

    我的问题是修改printers.py,写gdbinit,然后重新编译过程测试是否已经应用为修改。

    这个问题也显示了对printers.py 工作原理的根本误解。它与您的程序无关(它已加载到 GDB 中)。

    重新编译任何东西(您的程序或GDB)是不需要的。只需重新启动 GDB 即可获得新版本的 printers.py(并不是说可以解决任何问题)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 2013-01-06
      • 2020-08-01
      • 2017-03-25
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多