gdb调试core dump入门实践(顺便复习一下之前介绍过的addr2line命令调试)

本文主要是介绍gdb调试core dump入门实践(顺便复习一下之前介绍过的addr2line命令调试),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        调试技能是软件开发的必备技能, 不会调试, 就抓不到bug, 就很痛苦。 本文我们来一起聊聊gdb调试core

 

         Part 1:

        在前面的博文中, 我们聊过重要的addr2line调试, 现在再来一起看看, 就当是复习吧。

        程序如下:

 

#include <stdio.h>int main()
{int *p = NULL;*p = 0;printf("bad\n");return 0;
}

       几乎所有的码农都能一眼看出错在哪里, 但在大型项目中, 光靠肉眼怎行? 必须借助工具, 我们用addr2line来搞起,编译并运行:

 

 

[taoge@localhost test]$ cat main.c -n1  #include <stdio.h>23  int main()4  {5          int *p = NULL;6          *p = 0;78          printf("bad\n");9          return 0;10  }11
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ 
[taoge@localhost test]$ 
[taoge@localhost test]$ 
[taoge@localhost test]$ dmesg | grep a.out 
virtual kernel memory layout:
a.out[2282]: segfault at 0 ip 080483c9 sp bfacd460 error 6 in a.out[8048000+1000]
a.out[2303]: segfault at 0 ip 080483c9 sp bfadc1a0 error 6 in a.out[8048000+1000]
a.out[2307]: segfault at 0 ip 080483c9 sp bfef76e0 error 6 in a.out[8048000+1000]
a.out[4153]: segfault at 0 ip 080483c9 sp bf9f2490 error 6 in a.out[8048000+1000]
a.out[4932]: segfault at 0 ip 080483c9 sp bfcfd2a0 error 6 in a.out[8048000+1000]
a.out[5408]: segfault at 0 ip 080483c9 sp bf894b40 error 6 in a.out[8048000+1000]
[taoge@localhost test]$ addr2line -e a.out 080483c9
/home/taoge/test/main.c:6
[taoge@localhost test]$ 

        可以看到, 程序core dump了, 用dmesg命令查出程序core对应的地址为:080483c9, 然后呢, 利用addr2line命令转换为对应的代码行, 可以看到问题出在第6行, 回头看代码, 果真如此。

 

        在实际开发中, dmesg命令用的不多, 那怎么知道像080483c9这样的地址呢? 通常来讲, 程序core dump之后, 在日志中会记录堆栈信息的, 可以在日志文件中搜索backtrace这个字段, 然后查找地址。

 

 

       Part 2:

       接下来先预热一下, 搞点前戏, 我们来用gdb的r命令来玩玩, 如下:

 

[taoge@localhost test]$ rm a.out
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ gdb a.out 
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.
(gdb) r
Starting program: /home/taoge/test/a.out Program received signal SIGSEGV, Segmentation fault.
0x080483c9 in main () at main.c:6
6               *p = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686
(gdb) quit
A debugging session is active.Inferior 1 [process 5489] will be killed.Quit anyway? (y or n) y
[taoge@localhost test]$ 

        可以看到, 在gdb调试的时候, 用r命令让程序再跑起来, 同样可以定位到问题出在第6行。还没完, 我们来看bt命令:

 

 

[taoge@localhost test]$ rm a.out
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ gdb a.out 
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.
(gdb) bt
No stack.
(gdb) r
Starting program: /home/taoge/test/a.out Program received signal SIGSEGV, Segmentation fault.
0x080483c9 in main () at main.c:6
6               *p = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686
(gdb) bt
#0  0x080483c9 in main () at main.c:6
(gdb) 

        可以看到, 第一个bt显示no stack, 因为程序a.out还没有跑起来。 好, 用r让程序run起来,继续bt,  就可以看到函数堆栈, 也能定位到第6行。 我们仔细看一下, gdb也找到了080483c9这个地址,进而找到了第6行,  所以完全可以认为, gdb里面内置了addr2line命令。


 

Part 3:

        好的, 还是来说我们的重头戏------gdb调试core

 

        我们先来看如下操作:

 

[taoge@localhost test]$ rm a.out 
[taoge@localhost test]$ ls
main.c
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ls
a.out  main.c
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ ls
a.out  main.c
[taoge@localhost test]$ 

        可以看到, 程序core dump后, 并没有生成什么core文件, 原因是ulimit对应的开关没有打开, 我们来看看:

 

 

[taoge@localhost test]$ ulimit -c
0
[taoge@localhost test]$ ulimit -c 999
[taoge@localhost test]$ ulimit -c
999
[taoge@localhost test]$ rm a.out 
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ls
a.out  main.c
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ ls
a.out  core.5911  main.c
[taoge@localhost test]$ gdb a.out core.5911 
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 5911]
Missing separate debuginfo for 
Try: yum --disablerepo='*' --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/74/d23352fd770753e375bd0caecf375bd77bded5
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483c9 in main () at main.c:6
6               *p = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686
(gdb) bt
#0  0x080483c9 in main () at main.c:6
(gdb) quit
[taoge@localhost test]$

        看上面的过程, ulimit -c是用来查询所设定的core文件大小的, 可以看到, 在默认情况下为0, 所以执行./a.out的时候, 并没有core文件生成。 我们把它设置为999, 然后在执行./a.out, 可以看到, 有core文件(core.5911)生成。 然后利用gdb来调试core, 刚执行gdb a.out core.5911的之后, 就看出了问题出在第6行, 此时如果用bt命令来看堆栈, 也可以看出问题出在第6行。
 

 


        那上述调试各有什么特点呢? 

        我们知道,在实际中, 有很多问题是概率发生的, 很难重现。 此时, 如果用gdb的r命令(实际相当于重新运行程序)则是不可能的, 所以,本文part 2中的方法不太实用。

 

        对于概率性问题, 我们通常采用的是part 1和 part 3的方法。

        先说part 1方法:当程序出现堆栈错误时, 我们可以从日志中看到出错的地址, 然后用part 1中介绍的方法来查询, 以前经常这么玩。

        再说part 3方法:当程序出现堆栈错误时, 如果产生了core文件, 我们一定要视之为宝贝, 记得保存, 否则很可能被冲掉。 拿到core文件后, 我们可以用part 3中介绍的方法来调试core, 以前也这么玩过。

 

        有一个重要的问题必须指出, 在本文中, 所有的编译都加了-g这个选项, 主要是为了调试(保存了调试所需的调试信息), 如果没有-g, 那么只能知道程序出错的堆栈地址, 却无法知道对应的代码行, 前功尽弃, 抓狂不已抓狂

        在实际项目开发中(比如嵌入式开发), 真正的可执行文件/动态库等都都不能太大, 比如说,生成的xxx.so库有10M,  嵌入式那点小设备哪能奢侈地装这么大的东西啊?

所以要对xxx.so文件进行脱衣服操作(strip xxx.so), 去掉一些调试信息, 形成对应的yyy.so,  真正发布的时候, 只发布yyy.so就行了, 如果出了问题, 需要定位的时候, 再用对应的xxx.so来定位, 因为xxx.so中有调试信息, 而yyy.so的衣服被脱了, 没有对应的调试信息了。   而且, 嵌入式设备生成了core文件, 但设备一般不支持gbd调试, 所以要把设备中的core文件拷贝到linux机器上进行调试, 千万要记得保存编译的环境哦。

 

         OK, gdb调试core dump的入门介绍就到此为止, 在后续的博文中, 我会继续介绍更多有关gdb调试的内容, 到时再一起嗨。

 

 

 

 

 

 

这篇关于gdb调试core dump入门实践(顺便复习一下之前介绍过的addr2line命令调试)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/684558

相关文章

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

SQLite3命令行工具最佳实践指南

《SQLite3命令行工具最佳实践指南》SQLite3是轻量级嵌入式数据库,无需服务器支持,具备ACID事务与跨平台特性,适用于小型项目和学习,sqlite3.exe作为命令行工具,支持SQL执行、数... 目录1. SQLite3简介和特点2. sqlite3.exe使用概述2.1 sqlite3.exe

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

MySQL复杂SQL之多表联查/子查询详细介绍(最新整理)

《MySQL复杂SQL之多表联查/子查询详细介绍(最新整理)》掌握多表联查(INNERJOIN,LEFTJOIN,RIGHTJOIN,FULLJOIN)和子查询(标量、列、行、表子查询、相关/非相关、... 目录第一部分:多表联查 (JOIN Operations)1. 连接的类型 (JOIN Types)