免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6437 | 回复: 6
打印 上一主题 下一主题

[C++] gdb一调试模版就core dump,为什么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-19 15:46 |只看该作者 |倒序浏览
10可用积分
我的环境是solaris8+gcc2.95.2+gdb6.3

写了下面这个小程序
> cat t.cpp
#include<stdio.h>
template <class T>
void f(T t){
printf("v=%d\n",t);
}
int main(void){
f<int>(2);//显式指定模版实参
f<short>(3);//显式指定模版实参
return 0;
}
编译运行都没有问题
> gcc -g t.cpp && ./a.out
v=2
v=3

但是如果用gdb来单步跟踪./a.out就会出问题,似乎是gdb自己core dump了???????????????????
==============================================
> gdb ./a.out
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "sparc-sun-solaris2.8"...
(gdb) b 7
Breakpoint 1 at 0x10618: file t.cpp, line 7.
(gdb) n
The program is not being run.
(gdb) r
Starting program: /users/denver/gonl02/tmp/a.out

Breakpoint 1, main () at t.cpp:7
7       f<int>(2);
(gdb) n
v=2
8       f<short>(3);
(gdb) n
v=3
9       return 0;
(gdb) n
10      }
(gdb) n
0x000104e4 in _start ()
(gdb) n
Single stepping until exit from function _start,
which has no line number information.

Program exited normally.
(gdb) n
The program is not being run.
(gdb) q

==============================================
然后在当前目录下面就有了一个core文件:
> ls
a.out  core   t.cpp

==============================================
如果我刚才b 7改成b 6,gdb的结果有点区别:
(gdb) b 6
Breakpoint 1 at 0x10614: file t.cpp, line 6.
(gdb) r
Starting program: /users/denver/gonl02/tmp/a.out

Breakpoint 1, main () at t.cpp:6
6       int main(void){
(gdb) n
7       f<int>(2);
(gdb) n
void f<int> (t=0) at t.cpp:3
3       void f(T t){
(gdb) n
4       printf("v=%d\n",t);
(gdb) n
v=2
4       printf("v=%d\n",t);
(gdb) n
main () at t.cpp:8
8       f<short>(3);
(gdb) n
v=3
Segmentation fault (core dumped)

==============================================
我再用gdb去调试这个core文件:
> gdb core
GNU gdb 6.3
......(略去那些no warranty的文字)
(gdb) where
No stack.

没有堆栈?

这是怎么回事呢? 到底是gdb core了还是我的程序core了?
还请dx指点迷津,十分感谢了!

最佳答案

查看完整内容

会不会是因为你在main处下断点有问题,因为系统是这样调用exit(main())的,JDB断点下在这里,不就是中断了一下么,每个平台对于exit是否为可重入函数的支持不一样的,或许这就是问题的原因LZ你试试最简单的代码,main里只返回个0.。。。。以上内容纯属虚构,如有雷同,实属巧合

论坛徽章:
0
2 [报告]
发表于 2009-02-19 15:46 |只看该作者
会不会是因为你在main处下断点有问题,因为系统是这样调用exit(main())的,JDB断点下在这里,不就是中断了一下么,每个平台对于exit是否为可重入函数的支持不一样的,或许这就是问题的原因
LZ你试试最简单的代码,main里只返回个0.。。。。
以上内容纯属虚构,如有雷同,实属巧合

论坛徽章:
0
3 [报告]
发表于 2009-02-19 17:59 |只看该作者
顶一下吧!

论坛徽章:
0
4 [报告]
发表于 2009-02-19 21:17 |只看该作者
(gdb) n
main () at t.cpp:8
8       f<short>(3);
(gdb) n
v=3
Segmentation fault (core dumped)

core分明是第二次gdb的这里产生的,你一定要说是第一次产生的。



我再用gdb去调试这个core文件:
> gdb core
GNU gdb 6.3
......(略去那些no warranty的文字)
(gdb) where
No stack.

你为什么要省略这里的文字。。
gdb貌似不是这么用的


最后
gcc直接编译cpp,你的gcc是不是特别配置过?
第二次gdb的时候core了,你看能否重现?

论坛徽章:
0
5 [报告]
发表于 2009-02-19 22:38 |只看该作者
原帖由 r2r4 于 2009-2-19 21:17 发表
(gdb) n
main () at t.cpp:8
8       f(3);
(gdb) n
v=3
Segmentation fault (core dumped)
core分明是第二次gdb的这里产生的,你一定要说是第一次产生的。



我再用gdb去调试这个core文件:
> gdb  ...

第二次gdb产生core,是每次都能重现的。
谢谢!

论坛徽章:
0
6 [报告]
发表于 2009-02-20 10:07 |只看该作者
原帖由 alexhappy 于 2009-2-20 09:00 发表
会不会是因为你在main处下断点有问题,因为系统是这样调用exit(main())的,JDB断点下在这里,不就是中断了一下么,每个平台对于exit是否为可重入函数的支持不一样的,或许这就是问题的原因
LZ你试试最简单的代 ...

我试了一下,这种最简单的情况没有问题:

然后我做了一个试验,感觉其实和模版的关系不大,但是确实是和我在main()处设立中断点有关系,很可能就导致core dump像下面这样子:
> gdb ./a.out
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "sparc-sun-solaris2.8"...
(gdb) l
1       #include<stdio.h>
2       void f(int t){
3       printf("v=%d\n",t);
4       }
5       int main(void){
6       printf("v=%d\n",3);
7       return 0;
8       }
9
(gdb) b 5
Breakpoint 1 at 0x10620: file t.cpp, line 5.
(gdb) r
Starting program: /users/denver/gonl02/tmp/a.out

Breakpoint 1, main () at t.cpp:5
5       int main(void){
(gdb) n
6       printf("v=%d\n",3);
(gdb)
0x0002078c in _PROCEDURE_LINKAGE_TABLE_ ()
(gdb)
Single stepping until exit from function _PROCEDURE_LINKAGE_TABLE_,
which has no line number information.

Program received signal SIGSEGV, Segmentation fault.
0xff3bab78 in ?? ()
(gdb) q

如果我把断点不设置到main那一行,就能正常退出了("Program exited normally.")

谢谢!!!!!

论坛徽章:
0
7 [报告]
发表于 2009-02-20 20:27 |只看该作者
cygwin/gcc 3.4.4/gdb 6.8.0

正常

$ gcc -g -o t template.cpp

$ gdb
GNU gdb 6.8.0.20080328-cvs (cygwin-special)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-cygwin".
(gdb) file t
Reading symbols from /cygdrive/d/workspaces/workspace_cpp/t.exe...done.
(gdb) b main
Breakpoint 1 at 0x401075: file template.cpp, line 6.
(gdb) run
Starting program: /cygdrive/d/workspaces/workspace_cpp/t.exe
[New thread 5512.0x11b8]
[New thread 5512.0x104]

Breakpoint 1, main () at template.cpp:6
6       int main(void){
(gdb) n
7           f<int>(2);
(gdb) n
v=2
8           f<short>(3);
(gdb) n
v=3
9           return 0;
(gdb) n
10      }
(gdb) n
0x610060d8 in dll_crt0_1 () from /usr/bin/cygwin1.dll
(gdb) n
Single stepping until exit from function _Z10dll_crt0_1Pv,
which has no line number information.

Program exited normally.
(gdb)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP