如何应用 printers.py 修改? (Linux OS)

How do I apply the printers.py modification? (Linux OS)

我检查了核心文件,因为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 的 class 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

[printer.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 终端打印修改后的测试代码?

I think that there was a problem with class StdStringPrinter at printers.py

我认为你根本就糊涂了,你的问题与完全无关printers.py

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

RuntimeError: Cannot access memory at address 0x3b444e45203b290f

此错误的意思是 GDB 无法从内存位置 0x3b444e45203b290f 读取值。在x86_64系统上,这样的位置确实是不可读的,因为那个地址没有canonical form.

结论:您遵循的指针(可能是您程序中指向 std::string 的指针)实际上并未指向 std::string。 "Fixing" printers.py 无法解决该问题。

这个结论得到

的证实

the process(c++ lang) running on Linux died,

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

P.S.

My question is to modify printers.py, write gdbinit, and then re-compile the process to test whether it has been applied as modified.

这个问题也表明了对 printers.py 工作原理的根本误解。它与您的程序没有任何关系(它已加载到 GDB 中)。

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