如何分析 Linux user space's segmentation fault
Linux 用户态码字有时会遇到如下错误:
Segmentation fault (core dumped) ..
当你没头绪时,或许可以借用工具来定位。这个错误中 core dumped
提示生成了一个 core 文件。那它在哪了?如何分析? 具体来看,OS 是 Ubuntu 18.04。
- Step 1. check core 文件大小
tj@u1804:~/code/erofs/erofs-utils$ ulimit -a
core file size (blocks, -c) 0
ps: 这里是0不能生成core文件。
- Step 2. 设置 core 文件大小不受限制
ulimit -c unlimited
确认下:
tj@u1804:~/code/erofs/erofs-utils$ ulimit -a
core file size (blocks, -c) unlimited
- Step 3. 设置 apport
对于非安装包,ubuntu 下 apport 是拒绝写 core dump file 的。
先创建设置文件:
~/.config/apport/settings
然后添加如下配置:
[main]
unpackaged=true
- Step 4. 生成 core file
此时再出现 crash 时,core dump 文件存在 /var/crash 目录,后缀类似是 .crash 文件,比如我的:
_home_tj_code_erofs_erofs-utils_mkfs_mkfs.erofs.1000.crash
btw: 此文件 gdb 不能直接读,需要用 apport 解下。
- Step 5. 用 apport 解包
apport-unpack /var/crash/xxx.crash xxx_dir
解完后生成如下:
-rw-rw-r-- 1 tj tj 5 Nov 15 15:35 Architecture
-rw-rw-r-- 1 tj tj 13045760 Nov 15 15:35 CoreDump
-rw-rw-r-- 1 tj tj 24 Nov 15 15:35 Date
-rw-rw-r-- 1 tj tj 12 Nov 15 15:35 DistroRelease
-rw-rw-r-- 1 tj tj 50 Nov 15 15:35 ExecutablePath
-rw-rw-r-- 1 tj tj 10 Nov 15 15:35 ExecutableTimestamp
-rw-rw-r-- 1 tj tj 4 Nov 15 15:35 _LogindSession
-rw-rw-r-- 1 tj tj 5 Nov 15 15:35 ProblemType
-rw-rw-r-- 1 tj tj 109 Nov 15 15:35 ProcCmdline
...
- Step 6. 用 gdb 读
gdb "$(cat ExecutablePath)" CoreDump
如果出现如下:
Program terminated with signal SIGSEGV, Segmentation fault.
warning: Unexpected size of section `.reg-xstate/13869' in core file.
#0 0x000055e46a7c0a29 in ?? ()
(gdb)
可能是 gdb 版本问题,换个版本之类 or have a break then just read your code...
Done.
本站采用CC BY-NC-SA 4.0进行许可 | 转载请注明原文链接 - 如何分析 Linux user space's segmentation fault