什么是coredump
开发unix程序时, 有时程序会突然死机, 没有任何的提示(有时候会提示core dumped). 此时需要查看系统中对应目录下是否有core.进程号的文件生成, 这个文件便是操作系统把程序down掉时的内存内容扔出来生成的,通过这个文件搭配上带调试信息的可执行文件可以解析出发生错误的具体原因.core dump又叫核心转储, 当程序运行过程中发生异常, 程序异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 叫core dump.
例如当收到SIGABRT信号时候,程序自动终止,并且生成一个核心转存文件,会记录问题点的核心信息。
产生coredump文件
如果是只是为了学习,可以手动产生coredump文件,例如QNX系统中可以使用slay命令手动发送信号发生core dump
Bash
//SIGABRT是一个信号,表示“Abort”(中止)
//向进程或线程发出SIGABRT信号时,它将自动终止,并生成一个核心转储文件
slay -s SIGABRT YourService
//一般在对应的日志文件夹可以找到对应的文件
# pwd
/xxxx/qnx/coredump
# ls
YourService.core.gz //这个文件就是coredump文件,十分重要
解析coredump
需要一定的环境才能解析,这个文件只是程序的尸体,因此是不能进行断点调试之类的。还有一个重要的文件就是带调试信息(符号表)的可执行文件,一般在项目进行构建时会提供,可以使用file命令进行查看,需要使用的是not stripped。带符号表也即带调试信息的文件, 在编译程序时,编译器将程序中所有定义的函数、变量和其他符号以及它们的类型、存储位置等信息存储在符号表中。
file libgpio_client.so
libgpio_client.so: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, BuildID[md5/uuid]=0c379011edab6deaa1b080bd339e0c2a, with debug_info, not stripped
也可以使用带sym后缀的文件,例如libgpio_client.so.sym
使用带sym后缀的二进制文件,bin和so都可以通过这种方式来查找
Bash
gdb 的选择依赖于目标平台,qnx 提供了以下四种:
ARMv7:ntoarmv7-gdb
ARMv8:ntoaarch64-gdb
x86:ntox86-gdb
x86 64-bit:ntox86_64-gdb
# 使用 GDB 打开 core 文件
gdb <可执行文件路径> <core dump 文件路径>
# 在 GDB 中获取线程列表
(gdb) info threads
# 切换到特定线程
(gdb) thread <线程号>
#查看所有线程信息 线程调用栈
(gdb) thread apply all bt
#查看变量值
print xxx
ubuntu:~/codeDIR/starcruiser/qnx/apps/qnx/coredump$
ubuntu:~/codeDIR/starcruiser/qnx/apps/qnx/coredump$ ntoaarch64-gdb coredump/Service Service.core
//成功进入后可以看见19行为产生coredump的原因,源自于手动触发的Abort信号:
ubuntu:~/codeDIR/starcruiser/qnx/apps/qnx/coredump$ ntoaarch64-gdb coredump/Service Service.core
GNU gdb (GDB) 7.12 [qnx700 r1234]
Copyright (C) 2016 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 "--host=x86_64-pc-linux-gnu --target=aarch64-unknown-nto-qnx7.0.0".
Type "show configuration" for configuration details.Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
coredump/Service: No such file or directory.
[New pid 2273427 tid 1]
[New pid 2273427 tid 2]
[New pid 2273427 tid 3]
[New pid 2273427 tid 4]
[New pid 2273427 tid 5]
Program terminated with signal SIGABRT, Aborted.
#0 0x000000172632b354 in ?? ()
[Current thread is 3 (pid 2273427 tid 3)]
(gdb)
在实际的解析过程中,可能会提示缺少某些对应的so库,只需要按照gdb提示进行添加即可,这些文件全部都是需要带符号表的调试文件。