用gdb来调试fwrite异常产生的core
来源:互联网 发布:网络电视机顶盒牌子 编辑:程序博客网 时间:2024/05/21 10:22
fwrite的用法, 想必大家肯定很熟悉了。 但是, 很多人在调用fopen后, 不定返回的fp进行非空判断, 恕我直言, 确实有点小流氓。 实际上,在linux开发中, 受权限影响, 很多时候调用fopen会失败, 返回NULL. 在本文中, 我们来看看, 万一产生了core, 该如下定位。
实际上, 对于gdb调试core, 我们已经比较熟悉了, 现在来强化一下, 如下:
[taoge@localhost test]$ lstest.c[taoge@localhost test]$ cat test.c -n 1 #include <stdio.h> 2 3 int main() 4 { 5 FILE *fp = NULL; 6 int a = 1; 7 int b = 2; 8 int c = a + b; 9 10 fwrite("abc", sizeof("abc"), 1, fp); 11 12 return 0; 13 }[taoge@localhost test]$ gcc -g test.c [taoge@localhost test]$ lsa.out test.c[taoge@localhost test]$ ./a.out Segmentation fault (core dumped)[taoge@localhost test]$ lsa.out core.3355 test.c[taoge@localhost test]$ [taoge@localhost test]$ [taoge@localhost test]$ [taoge@localhost test]$ gdb a.out core.3355 GNU gdb (GDB) Red Hat Enterprise Linux (7.1-29.el6)Copyright (C) 2010 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-redhat-linux-gnu".For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>...Reading symbols from /home/taoge/test/a.out...done.[New Thread 3355]Missing separate debuginfo for Try: yum --disablerepo='*' --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/74/d23352fd770753e375bd0caecf375bd77bded5Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.Loaded symbols for /lib/libc.so.6Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.Loaded symbols for /lib/ld-linux.so.2Core was generated by `./a.out'.Program terminated with signal 11, Segmentation fault.#0 0x008de17c in fwrite () from /lib/libc.so.6Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686(gdb) (gdb) (gdb) (gdb) bt#0 0x008de17c in fwrite () from /lib/libc.so.6#1 0x08048418 in main () at test.c:10(gdb)
定位到了代码的第10行, fwrite产生了core, 在追根溯源, 发现fp为NULL, 于是一切得到了合理解释。
最后再次说明一下, 在实际开发中, 一定要对fopen的返回值进行校验, 我至少碰到过一次了。项目复杂后, 这种问题搞得人满头大汗。 如果是概率性问题, 那就头要大了。 偶尔, 我在博客中, 为了图简便, 省略了一些检验, 呵呵。
0 0
- 用gdb来调试fwrite异常产生的core
- GDB调试Core文件(程序异常产生的文件)
- 用gdb来调试对stl的空vector进行操作而产生的core
- 用gdb来调试除数为0而产生的core
- linux core 文件的产生及gdb调试
- 用gdb调试core文件
- 用gdb调试core文件
- gdb调试core异常文件之where命令
- 使用gdb+core文件调试程序异常奔溃bug
- gdb调试malloc-free 产生的coredown
- Linux core文件的产生和调试
- Linux core文件的产生和调试
- 用gdb调试core dump文件
- 用gdb调试core dump文件
- 用gdb调试core dump文件
- 用gdb调试core dump文件
- 用gdb调试core dump文件
- 用gdb调试core dump文件
- 信息系统项目管理师考后想
- “互联网+”商业模式的精髓
- 带参数启动另外一个exe程序
- 为 Key-Value 数据库实现MVCC 事务
- springMVC教程-快速入手-深入分析
- 用gdb来调试fwrite异常产生的core
- 【2015/11/15】为了解决账号登录显示用户名和登录之后退出的问题倒腾一晚上!
- 搭建maven+spring+freemaker+mybatis环境之四
- 10 种机器学习算法的要点(附 Python 和 R 代码)
- Python_元组、字典内建方法详解
- Git常用命令
- 《并查集》hdu acm 5.1.7 c++ map的使用
- 不同场景下 MySQL 的迁移方案
- Android适配器——ArrayAdapter的各种重载