mac漂亮的打印gdb

时间:2019-01-28 00:00:07

标签: c++ macos gdb

我使用的是macOS High Sierra 10.13.6,无法将gdb转换为漂亮的打印容器或字符串。我按照GDB 7.6 STL pretty print with gcc-4.8 and mac os 10.9的说明进行操作,克隆了gdb漂亮打印机,并将路径添加到.gdbinit。但是gdb仍然不会打印出来。我在启动gdb之后添加了漂亮的打印机,以确保它们已注册

(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正在注册漂亮的打印机,我正在运行gcc 8.2.0和gdb 8.0.1。注册的漂亮打印机是c ++-v6,应该是v8吗?我克隆了此存储库以获得漂亮的打印机svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python

我使用/usr/local/Cellar/gcc/8.2.0/bin/g++-8 -static-libstdc++ -g -o a.out b.o c.o -pthread

构建了代码

这是尝试打印字符串时的gdb输出

(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

2 个答案:

答案 0 :(得分:0)

这似乎是一个错误。此Stack Overflow问题中提供了一个骇客解决方案:

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

从该问题复制后,您需要修改漂亮打印机代码:

--- 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.

答案 1 :(得分:0)

尝试使用 -gdwarf-3 而不是简单的 -g 进行编译。根据 https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html,GCC 在 macOS 上默认为 DWARF 版本 2,并且明确切换到 DWARF 版本 3 会使符号正确出现在 std:: 命名空间中(我还不明白为什么)。因此,您无需像 Yongwei Wu 建议的那样修补漂亮的打印机。