如何在gdb中打印/评估c ++模板函数 [英] How to `print`/evaluate c++ template functions in gdb

查看:1567
本文介绍了如何在gdb中打印/评估c ++模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用gdb print 命令来评估c ++模板函数的结果。在下面的代码中有一个简单的 id 函数,我试着 print results of id x),但是它好像 id id 从来不存在。我使用的代码如下,用 g ++ -std = c ++ 11 -g test7.cpp 编译:

  template< typename T> 
T id(T x){return x;}

int main(){
int i = 0;
i = i + 1;
}

在GDB中,我尝试 print 如下:

 断点1,test.cpp的main():6 
6 i = i + 1;
(gdb)print i
$ 1 = 0
(gdb)print id(i)
在当前上下文中没有符号id。
(gdb)print id< int>(i)
无符号id< int>在当前上下文中。

正如你所看到的,我总是得到无符号id。



有一个相关的文章 GDB不允许在OSX中步进到模板函数。在那里的答案中,模板函数可以至少反汇编 d。在我的情况下,即使反汇编也没有给出任何内容:

  id< int> 
无符号id< int>在当前上下文中。

是否可以评估模板功能?



p PS我使用的是来自TDM-GCC(4.8.1-2)的GDB 7.6.1。



谢谢。

解决方案

在源代码中没有显式实例,编译器会将模板代码视为静态内联代码,如果未使用则优化它。一个显式的实例将创建一个具有外部链接的符号(虽然它仍然可以在技术上由链接器优化,但在我的测试中它没有...):

  template< typename T> 
T id(T x){return x;}

template int id< int> (int x);

int main(){
int i = 0;
i = i + 1;
}

gdb 我把要调用的C ++函数放在单引号内:

 断点1,main()at tmpl.cc:7 
7 int i = 0;
(gdb)n
8 i = i + 1;
(gdb)pi
$ 1 = 0
(gdb)p'id< int>(int)'(i)
$ 2 = 0
b $ b

您在关于创建可变参数模板的显式实例的注释中的问题,语法是相同的。您必须为计划调用模板的每个不同参数列表创建不同的显式实例。


I was wondering if it is possible to use gdb print command to evaluate results of c++ template functions. In the following code with a simple id function, I tried to print results of id(x), but it's as if id or id<t> never existed. The code I use is below, compiled with g++ -std=c++11 -g test7.cpp:

template<typename T>
T id(T x) {return x;}

int main() {
  int i = 0;
  i = i + 1;
} 

In GDB, I tried to print as follows:

Breakpoint 1, main () at test7.cpp:6
6         i = i + 1;
(gdb) print i
$1 = 0
(gdb) print id(i)
No symbol "id" in current context.
(gdb) print id<int>(i)
No symbol "id<int>" in current context.

As you can see, I always get "No symbol id".

There is a related post about GDB not allowing stepping into template functions in OSX. In the answers there, the template function can at least be disassembled. In my case, even disassemble gives nothing:

(gdb) disassemble id<int>
No symbol "id<int>" in current context.

Is it possible to evaluate template functions at all?

P.S. I am using GDB 7.6.1 coming from TDM-GCC (4.8.1-2).

Thanks.

解决方案

Without an explicit instance in the source code, the compiler will treat the template code as it would "static inline" code and optimize it out if it is unused. An explicit instance will create a symbol with external linkage (although it could still be technically optimized away by the linker, but in my test it did not...):

template<typename T>
T id(T x) {return x;}

template int id<int> (int x);

int main() {
  int i = 0;
  i = i + 1;
} 

Within gdb, I place the C++ function I want to call within single quotes:

Breakpoint 1, main () at tmpl.cc:7
7     int i = 0;
(gdb) n
8     i = i + 1;
(gdb) p i
$1 = 0
(gdb) p 'id<int>(int)'(i)
$2 = 0
(gdb)

Your question in your comment about creating an explicit instance of a variadic template, the syntax is the same. You have to create a different explicit instance for each different parameter list you plan to call the template with.

这篇关于如何在gdb中打印/评估c ++模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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