gdb强行生成core文件

时间:2021-10-05 16:41:47

调试卡死的时候,有时需要杀死进程重启后再分析原因,这时候需要先把卡死状态保持起来,生成core文件,重启后慢慢分析。

1、gcore

usage:  gcore [-o filename] pid

用法如下:

[root@htdev ~]# gcore 19388 
[Thread debugging using libthread_db enabled]
[New Thread 0x2b46ed999890 (LWP 19388)]
[New Thread 0x43e62940 (LWP 19399)]
[New Thread 0x43461940 (LWP 19398)]
[New Thread 0x42a60940 (LWP 19397)]
[New Thread 0x4205f940 (LWP 19396)]
[New Thread 0x41018940 (LWP 19390)]
[New Thread 0x41007940 (LWP 19389)]
0x000000364b4d06d9 in syscall () from /lib64/libc.so.6
Saved corefile core.19388

2、gdb的generate-core-file命令

[root@htdev ~]# gdb attach 19388
或者[root@htdev ~]# gdb -q - 19388
GNU gdb Fedora (6.8-37.el5)
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 "x86_64-redhat-linux-gnu"...
attach: No such file or directory.
Attaching to process 19388
Reading symbols from /usr/local/bin/node...done.
Reading symbols from /lib64/librt.so.1...done.
Loaded symbols for /lib64/librt.so.1
Reading symbols from /lib64/libdl.so.2...done.
Loaded symbols for /lib64/libdl.so.2
Reading symbols from /usr/lib64/libstdc++.so.6...done.
Loaded symbols for /usr/lib64/libstdc++.so.6
Reading symbols from /lib64/libm.so.6...done.
Loaded symbols for /lib64/libm.so.6
Reading symbols from /lib64/libgcc_s.so.1...done.
Loaded symbols for /lib64/libgcc_s.so.1
Reading symbols from /lib64/libpthread.so.0...done.
[Thread debugging using libthread_db enabled]
[New Thread 0x2b46ed999890 (LWP 19388)]
[New Thread 0x43e62940 (LWP 19399)]
[New Thread 0x43461940 (LWP 19398)]
[New Thread 0x42a60940 (LWP 19397)]
[New Thread 0x4205f940 (LWP 19396)]
[New Thread 0x41018940 (LWP 19390)]
[New Thread 0x41007940 (LWP 19389)]
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libc.so.6...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /root/eicn/work/web/node_modules/hiredis/build/Release/hiredis.node...done.
Loaded symbols for /root/eicn/work/web/node_modules/hiredis/build/Release/hiredis.node
0x000000364b4d06d9 in syscall () from /lib64/libc.so.6
(gdb) generate-core-file
Saved corefile core.19388
(gdb) detach
Detaching from program: /usr/local/bin/node, process 19388

3、修改生成core大小

使用ulimit -a查看当前情况,ulimit -c只查看core文件大小

[root@htdev ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 16383
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 16383
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@htdev ~]# ulimit -c
0

使用ulimit -c filesize命令,可以限制core文件的大小(filesize的单位为kbyte)。若ulimit -c unlimited,则表示core文件的大小不受限制。如果生成的信息超过此大小,将会被裁剪,最终生成一个不完整的core文件。在调试此 core文件的时候,gdb会提示错误。

[root@htdev ~]# ulimit -c unlimited


4、设置core文件路径

1.编辑环境配置文件,让shell启动时自动设置ulimit
2.更改core文件生成路径
3.sysctl配置生效
vi /etc/profile
ulimit -c unlimited > /dev/null 2>&1
vi /etc/sysctl.conf
kernel.core_uses_pid = 1
kernel.core_pattern=/tmp/core-%e-%p
sysctl -p /etc/sysctl.conf