如何在DDD(或gdb)中用unique_ptr调试C ++ 11代码? [英] How to debug C++11 code with unique_ptr in DDD (or gdb)?

查看:764
本文介绍了如何在DDD(或gdb)中用unique_ptr调试C ++ 11代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: unique_ptr 很好,但我发现它们在 DDD gdb



我使用的gdb漂亮的打印机是gcc的一部分(例如, /usr/share/gcc-4.8.2/python/libstdcxx/v6/printers.py )。例如:

  $ print pTest 
std :: unique_ptr< MyType>包含0x2cef0a0

然而,解引用指针不起作用:

  $ print * pTest 
无法找到运算符*。

当我需要访问该值时,我必须手动复制指针并将其转换为正确的例如:

  print *((MyType *)0x2cef0a0)

如果这个过程仍在运行,这个版本可以运行(仍然很丑但是更好):

<$ p $如果分析核心转储


$ b $,print * pTest.get()//将不起作用b

直接在DDD中 Display * pTest 的方法也行不通。它只会导致以下错误:

 <错误:找不到运算符*。> 

有没有办法在DDD中用unique_ptr调试C ++ 11代码(没有像我用繁琐的变通办法那样打破工作流程)?






我不害怕使用gdb命令,但DDD整合将是一个加号。例如,通过双击数据结构中的指针往往比打字更快。



我已经尝试删除漂亮的打印机,但它也不是最佳。我能想到的最好的是:

pre $ print pTest._M_t-> _M_head_impl

code>


解决方案

这个问题实际上与C ++ 11,unique_ptr或漂亮打印无关。问题是gcc不会发出std :: unique_ptr :: operator *的代码,可以通过gdb调用它来解引用unique_ptr。例如,如果你添加 * pTest; 到你的代码,那么gdb会执行解引用。



类似的问题是描述在SO后如何打印/评估c ++模板在gdb中运行。在 https://sourceware.org/上为auto_ptr描述了几乎相同的问题毫升/射手/ 2012-Q1 / msg00003.html 。如果我正确理解线程,则一种解决方法是修补漂亮的打印机,并在打印unique_ptr时打印出解除引用的指针。可以在 http://sourceware.org/bugzilla/show_bug上找到gdb错误报告。 cgi?id = 12937



gdb wiki位于 https://sourceware.org/gdb/wiki/STLSupport 描述了更漂亮的打印解决方案,可以有其他解决方法。



编辑:一个更优雅的解决方案,迫使编译器为包括operator *在内的所有成员模板发出代码,就是显式实例化类:

 模板class std :: unique_ptr< MyType>; 


std::unique_ptr are nice, but I find them less comfortable when debugging in DDD or gdb.

I'm using the gdb pretty printers that are part of gcc (e.g., /usr/share/gcc-4.8.2/python/libstdcxx/v6/printers.py). That is a big win for readability, for example:

$ print pTest
std::unique_ptr<MyType> containing 0x2cef0a0

However, dereferencing the pointer does not work:

$ print *pTest
Could not find operator*.

When I need to access the value, I have to manually copy the pointer and cast it to the correct type, for example:

print *((MyType*) 0x2cef0a0)

If the process is still running, this version works (still ugly but better):

print *pTest.get() // will not work if analyzing a core dump

The straightforward approach to Display *pTest in DDD does not work either. It only results in the following error:

<error: Could not find operator*.>

Is there a way to debug C++11 code with unique_ptr in DDD (without breaking the workflow like I do with my cumbersome workarounds)?


I'm not afraid to use gdb commands, but DDD integration would be a plus. For example, following pointers in data structures by just double-clicking on them is often faster than typing.

I already tried to drop the pretty printer, but it is also not optimal. The best that I could come up with is the following:

 print pTest._M_t->_M_head_impl

解决方案

This problem is actually not related to C++11, unique_ptr or pretty printing. The problem is that gcc does not emit code for std::unique_ptr::operator* that could be called by gdb to dereference the unique_ptr. If you for instance add *pTest; to your code then gdb does perform the dereferencing.

A similar problem is described in the SO post How to `print`/evaluate c++ template functions in gdb. Almost the same problem is described for an auto_ptr at https://sourceware.org/ml/archer/2012-q1/msg00003.html. If I understand the thread correctly one workaround would be to patch the pretty printer and also print out the dereferenced pointer when printing the unique_ptr. A gdb bug report can be found at http://sourceware.org/bugzilla/show_bug.cgi?id=12937.

The gdb wiki at https://sourceware.org/gdb/wiki/STLSupport describes more pretty printing solutions, which could have other workarounds.

Edit: A more elegant solution forcing the compiler to emit code for all member templates including operator* is to explicitly instantiate the class:

template class std::unique_ptr<MyType>;

这篇关于如何在DDD(或gdb)中用unique_ptr调试C ++ 11代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆