简体   繁体   中英

mac pretty print gdb

I'm on macOS High Sierra 10.13.6 and not able to get gdb to pretty print containers or strings. I followed the instructions here GDB 7.6 STL pretty print with gcc-4.8 and mac os 10.9 , cloned the gdb pretty printers and added the path to .gdbinit. However gdb still won't pretty print. I added the pretty printers after starting gdb to make sure they were registered

(gdb) python
>import sys
>sys.path.insert(0, '/Users/liamadams/Documents/gdb/python')
>from libstdcxx.v6.printers import register_libstdcxx_printers
>register_libstdcxx_printers (None)
>end
Traceback (most recent call last):
  File "<string>", line 4, in <module>
  File "/Users/liamadams/Documents/gdb/python/libstdcxx/v6/printers.py", line 1739, in register_libstdcxx_printers
    gdb.printing.register_pretty_printer(obj, libstdcxx_printer)
  File "/usr/local/Cellar/gdb/8.0.1/share/gdb/python/gdb/printing.py", line 152, in register_pretty_printer
    printer.name)
RuntimeError: pretty-printer already registered: libstdc++-v6

gdb is registering the pretty printers, I'm running gcc 8.2.0 and gdb 8.0.1. The pretty printer registered is c++-v6, should it be v8? I cloned this repo to get the pretty printers svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python

I built my code using /usr/local/Cellar/gcc/8.2.0/bin/g++-8 -static-libstdc++ -g -o a.out bo co -pthread

Here is the gdb output when trying to print a string

(gdb) p currentItem
$1 = {static npos = <optimized out>, 
  _M_dataplus = {<allocator<char>> = {<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x7ffeefbff900 "mining"}, _M_string_length = 6, {
    _M_local_buf = "mining\000\000\v\000\000\000\000\000\000", 
    _M_allocated_capacity = 113723912907117}}

(gdb) whatis currentItem
type = string

It seems to be a bug. A hack solution is provided in this Stack Overflow question:

Bug in gdb libstdc++ pretty-printers with gcc 8 installed from macports (macOS)

Copying from that question, you need to modify the pretty-printer code:

--- python/libstdcxx/v6/printers.py.old 2018-10-13 13:57:11.000000000 +0300
+++ python/libstdcxx/v6/printers.py 2018-10-13 14:35:55.000000000 +0300
@@ -1306,2 +1306,4 @@
     def add(self, name, function):
+        if (name.startswith('std::')):
+            self.add(name[5:], function)
         # A small sanity check.

Try compiling with -gdwarf-3 instead of simply -g . According to https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html , GCC defaults to DWARF version 2 on macOS, and explicitely switching to DWARF version 3 makes the symbols correctly appear in the std:: namespace (I don't yet understand why). So you would not need to patch the pretty printers as Yongwei Wu suggests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM