找回密码
 注册
查看: 492|回复: 0

linux centos 开启coredump

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2022-10-26 11:29:14 | 显示全部楼层 |阅读模式
在开发中,coredump会输出珍贵的调试信息,但在linux下默认是关闭的,所以,要手动去把它打开。) ^8 I" F  N3 m" n1 h) h6 o8 o
执行以下脚本,退出后重新登录即可,重新登录后执行ulimit -c,如果出来的是unlimited,就是已经打开了,且不限制coredump生成的core文件大小, O; R& S8 G6 K- Y

: U$ |* o8 _0 a2 O* O- m#!/bin/bash
" [7 F# `* b0 D3 R0 J" [% S9 [9 h" l( T/ K! t
### Filename: coredumpshell.sh3 ?0 `  o6 @% A5 h7 P! P& U6 v/ }7 z
### Description: enable coredump and format the name of core file on centos system
3 A- l  I+ v% i1 z' {
" b7 ?* Z$ f( P2 Q# enable coredump whith unlimited file-size for all users
" s+ H, t* \  i$ p& m7 U0 ?echo -e "\n# enable coredump whith unlimited file-size for all users\n* soft core unlimited" >> /etc/security/limits.conf+ U" e4 M, }8 b0 I- i
* f# Q4 J7 D* J+ e: i! A
# set the path of core file with permission 777 % G+ r9 C8 \! s
cd /mydata && mkdir corefile && chmod 777 corefile. t: p+ V3 u+ ]! R4 r* Q$ \

# p8 l0 Q" j: H3 x# format the name of core file.   
% c& s4 P0 B# l9 r& u% \# %% – 符号%
& Y/ E- Q4 h5 z# %p – 进程号' p* q7 Y. U. n
# %u – 进程用户id
3 w; J8 l# [5 R+ {; o$ ~- o$ {# %g – 进程用户组id
  k% l( X+ i8 B# %s – 生成core文件时收到的信号/ b. ~8 M& d; u4 C* j1 X2 c
# %t – 生成core文件的时间戳(seconds since 0:00h, 1 Jan 1970)( l: I' e, ]6 X- {& @1 m7 C4 o  x
# %h – 主机名( P9 o3 i8 U( k4 {
# %e – 程序文件名
6 G0 W% y; S- d7 Y2 fecho -e "/mydata/corefile/core-%e-%s-%u-%g-%p-%t" > /proc/sys/kernel/core_pattern! u# d) X+ J0 {6 l/ ~' `6 d% ]

0 u4 B# J$ ]" e) L9 O% z# l# for centos7 system(update 2017.2.3 21:44)
; V# v1 c/ x" E, l4 wecho -e "/mydata/corefile/core-%e-%s-%u-%g-%p-%t" > /etc/sysctl.conf9 T. K! H1 v9 @0 }- N

" W; f& N" V$ s- e4 H# d# suffix of the core file name( E7 b0 `( L! h4 S( n+ m- w1 H( J
echo -e "1" > /proc/sys/kernel/core_uses_pid
; S) G- j9 S" [
% q0 h5 Q7 @. ~* y5 ~3 i. X& N/ x, I. Q. J" |
centos下
6 M$ Q. ?. T! M) m! u/ H生成方式( J- R4 T2 ?5 O( {% w
## 查看是否开启,0表示没有开启,>0表示生成最大的coredump的大小(单位kb),可使用参数unlimited取消core文件大小的的限制+ C4 D% n% h" Y8 C
ulimit -c 6 ~0 q$ N8 O$ m, ^- v
ulimit -c unlimited  
' b/ B9 T9 S  Qulimit -c 0, G/ Q1 B9 \5 Y
ulimit -a2 [5 |# S! c9 o! Q: G
转储文件命名规范8 b( @/ h6 B& u+ I1 R6 b+ ?1 ]
## 设置是否添加pid作为扩展,1:添加 0:不添加
" R6 J0 ?9 _5 U: Z# kcat /proc/sys/kernel/core_uses_pid
% B- @- {7 s/ @4 M" y+ S! }## 设置文件产生时的命名
5 ~3 M- i( v" o# [; ocat /proc/sys/kernel/core_pattern
0 P2 r. J, a: ^) N5 b+ H##通常这样设置:
6 L# g3 G0 P( t/ B  a& H; I% @/opt/tmp/core/core-%e-%s-%u-%g-%p-%t# C9 m$ ]+ g; d5 A
参数介绍
" j! ^3 ]* |( b' j* z6 p+ `%p : insert pid into filename                             # 添加pid  z7 d5 q2 M- m
%u : insert current uid into filename                     # 添加当前uid0 t# M5 `8 Y& o: z+ M
%g : insert current gid into filename                     # 添加当前gid
; R) q9 E* v2 T. K0 |# H%s : insert signal that cased the coredump into the file  # 添加导致产生core的信号8 o0 P) b# E6 h, |1 k
%t : insert UNIX time that the coredump occurred into file# 添加core文件生成时的unix时间
4 Q! C$ s7 H- t$ ?% G%h : insert hostname where the coredump happened into file# 添加主机名
$ b. T  o( e- _9 j- O; P%e : insert coredumping executablename into file          # 添加命令名
( b+ d, I! ~* S) }: p* K0 p产生core的测试
8 h$ s! Z' E2 ?1 h( O9 D9 Ekill -s SIGSEGV $$  N" Z" n2 i0 N: O
Linux上的信号9 Y! S8 ^' ~( l9 o  C/ A1 H! a. v
发生coredump一般都是在进程收到某个信号的时候,Linux上现在大概有60多个信号,可以使用kill -l命令全部列出来。
. {$ t) Z: i, N  O1 o
! x5 Q# C4 T: Y$ G针对特定的信号,应用程序可以写对应的信号处理函数。如果不指定,则采取默认的处理方式,默认处理是coredump的信号如下:
* I! s7 `5 @  b* D7 k
8 g) ~/ T0 J% H+ _# c3) SIGQUIT 4) SIGILL 6) SIGABRT 8) SIGFPE 11) SIGSEGV
+ c' b( o6 W- X1 v# Z7) SIGBUS 31) SIGSYS 5) SIGTRAP 24) SIGXCPU 25) SIGXFSZ
" s( a; D' F4 Q: p29) SIGIOT
# Q* X  ^' L$ v" D9 g2 D* wcore的使用
5 o& m2 ^9 ?* t; H' L: E! d. w编译的程序如果不添加-g,在core中不会有行的信息,如果希望产生更详细的core文件,编译程序时需要指定-g。2 U' w2 b- i- {. C1 L3 B# X9 G& W
4 X  c9 M6 L  q
gdb execute corefile
2 ~4 G/ H" |0 E+ N) M8 Pgdb 基本使用命令: s! s; M! m! F! r% z
# 设置断点
3 d+ ~4 M* N* h6 Fb 行号|函数名
0 \% e/ n- j0 h8 W* d) K  w, v# 删除断点+ s. l. x/ ^1 G( H8 c. ]' x
delete 断点编号
- B, a0 Y) ~* d8 Z# 禁用断点
, Y4 T0 v3 q  U' \* I$ A& n, Qdisable 断点编号
8 N9 r' y( Q1 q* g# 弃用断点. l4 M* b6 r& e9 ?( t% O0 h& A
enable 断点编号5 S: `$ N* L% {+ {3 H9 @* T+ K
# 单步跟踪3 Y5 e; x9 [! n+ Z1 z3 E$ ]9 s* X
next# x( ^2 H) I9 l% w  U- q5 c, H
# 单步跟踪( Q( c. r% z$ |. V0 c" e
step. K- @4 [4 |) [% H: A& C
# 打印变量
6 o- G. h5 o8 S- r0 V8 H( q. X. uprint 变量名9 ?7 e; ~# L) D7 {7 `# i+ \) Q, a
# 设置变量4 q) A7 Z+ e/ c& |1 j+ d
set var=value
4 w" r, \6 C& g2 |9 }6 T# 查看变量类型
$ s( T* t, h- r# b4 nptype var
/ q" ~8 v* S) A8 r# 顺序执行到结束' p/ f% D8 Q' f1 m0 n
count* N. g- f( o8 e! ?  \
# 顺序执行直到某一行
, t' q0 c: g, m; R3 A0 ]util lineno% G6 L/ N: I) d6 ^
# 打印堆栈信息
, K5 ]' r) }2 u1 k" b0 E& g$ g6 Abt& R' x6 k9 F3 j, B' Z7 @. C

$ n) T3 C0 I1 O- M; y
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 02:10 , Processed in 0.018234 second(s), 23 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表