0

I checked the core file because the process(c++ lang) running on Linux died, and the contents of the core file

[Corefile]


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

I think that there was a problem with class StdStringPrinter at printers.py. So I looked up a text that explained the problem I was looking for on this site , modified printers.py, and created a .gdbinit on my home path and wrote the content.

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

Eclipse/CDT Pretty Print Errors

But this method is a little different from the one I'm looking for because it's done in Eclipse.

my gdb version is 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

My question is to modify printers.py, write gdbinit, and then re-compile the process to test whether it has been applied as modified. How can I print my modified TEST code at Linux Terminal?

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
Kim sung
  • 19
  • 7

1 Answers1

0

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

I think you are fundamentally confused, and your problem has nothing at all to do with printers.py.

You didn't show us your GDB session, but it appears that you have tried to print some variable of type std::string, and when you did so, GDB produced this error:

RuntimeError: Cannot access memory at address 0x3b444e45203b290f

What this error means is that GDB could not read value from memory location 0x3b444e45203b290f. On an x86_64 system, such a location indeed can not be readable, because that address does not have canonical form.

Conclusion: the pointer that you followed (likely a pointer to std::string in your program) does not actually point to std::string. "Fixing" the printers.py is not going to solve that problem.

This conclusion is corroborated by

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

Finally, the pointer that you gave GDB to print: 0x3b444e45203b290f looks suspiciously like an ASCII string. Decoding it, we have: \xf); END;. So it's very likely that your program scribbled ); END; over a location where the pointer was supposed to be, and that you have a buffer overflow of some sort.

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.

This question also shows fundamental misunderstanding of how printers.py works. It has nothing to do with your program (it's loaded into GDB).

Recompiling anything (either your program or GDB) is not required. Simply restarting GDB should be all that's neccessary for it to pick up the new version of printers.py (not that that would fix anything).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362