|
|
在开发中,coredump会输出珍贵的调试信息,但在linux下默认是关闭的,所以,要手动去把它打开。
- |3 D: V, }* V+ _执行以下脚本,退出后重新登录即可,重新登录后执行ulimit -c,如果出来的是unlimited,就是已经打开了,且不限制coredump生成的core文件大小( E. G) V8 K0 Z5 b1 d* T9 K
7 G6 D8 x) I* p: K+ E#!/bin/bash, l' a0 C5 S, Y- b4 D
0 z7 |. s5 S7 x+ v. Z( o) i
### Filename: coredumpshell.sh$ q) T* R8 H" U$ O9 G( a, U
### Description: enable coredump and format the name of core file on centos system" G/ }. }0 }" j1 N7 g3 h+ }
% _8 L4 n; T# G8 h
# enable coredump whith unlimited file-size for all users
% J) K0 \0 i' ^6 N2 k7 e- M! K/ v4 vecho -e "\n# enable coredump whith unlimited file-size for all users\n* soft core unlimited" >> /etc/security/limits.conf
; Q. B i" o( Q9 ]8 J( r3 [7 j6 S# q* w$ i) A( C
# set the path of core file with permission 777
0 t. z% z* {9 Z, e% w8 R+ J" Lcd /mydata && mkdir corefile && chmod 777 corefile
/ C" R' o& t2 ?! A5 ]* i; f" i1 y- k) d, z& J4 Z
# format the name of core file.
' H3 d) }7 _0 H3 U) U# %% – 符号%1 |( I9 M7 `8 Z1 R0 t6 ]
# %p – 进程号
8 ?0 ]! K0 L3 V# %u – 进程用户id
8 O; ^, Z, @5 I$ I# _, I& I# %g – 进程用户组id4 f _) E7 O% U7 c0 i
# %s – 生成core文件时收到的信号
8 C$ h: F! @+ r; h$ G, R: i# %t – 生成core文件的时间戳(seconds since 0:00h, 1 Jan 1970)
, V$ ^% \ p6 R# %h – 主机名* o2 O+ Z3 A% V" n
# %e – 程序文件名
8 N/ S: P j5 ]echo -e "/mydata/corefile/core-%e-%s-%u-%g-%p-%t" > /proc/sys/kernel/core_pattern
- {6 ~, m4 h- N) ]0 C- J1 Q# b# h) {% v' `$ C. m, V. n
# for centos7 system(update 2017.2.3 21:44): ~8 y" ?+ D. u9 D8 K1 L
echo -e "/mydata/corefile/core-%e-%s-%u-%g-%p-%t" > /etc/sysctl.conf
- c- G9 z ]$ _; Y0 Q: F) f/ }6 x' V0 C. P5 R$ _& |& \
# suffix of the core file name
! x5 |0 \$ t- ?) Y$ y2 pecho -e "1" > /proc/sys/kernel/core_uses_pid7 F. Q% P8 p4 {7 w
$ X' j% F% o/ t) p6 K9 M
3 A+ }. N& j$ f; bcentos下
0 Z J1 ?# C+ S, v6 `" K% X& H生成方式
2 W1 l: Q6 s- m7 o7 V## 查看是否开启,0表示没有开启,>0表示生成最大的coredump的大小(单位kb),可使用参数unlimited取消core文件大小的的限制9 e4 [: X1 A0 W
ulimit -c 1 i$ Y( A/ S P g' M6 n8 [
ulimit -c unlimited * [! d: G* B8 Y" Y
ulimit -c 0
; c7 I+ ~% ^. R ^7 h$ R5 u) E7 bulimit -a; P* p& T6 w3 |9 ]1 g7 g
转储文件命名规范# W1 F/ s3 c3 J+ G) z$ R
## 设置是否添加pid作为扩展,1:添加 0:不添加* Q) V7 {5 C9 c# t2 s" \4 p6 j# {
cat /proc/sys/kernel/core_uses_pid9 i9 X8 s9 }! ^4 D* G& [
## 设置文件产生时的命名
. g7 ?# E$ U0 i* acat /proc/sys/kernel/core_pattern
7 _8 H5 @ P' T: o1 P- Z" s" U##通常这样设置:" w- Y, N o0 \0 K/ A
/opt/tmp/core/core-%e-%s-%u-%g-%p-%t
5 g! k' |5 ?: b5 s参数介绍
9 |* }1 C+ @9 b" i%p : insert pid into filename # 添加pid9 ^. v4 h$ Z8 Y! X: @
%u : insert current uid into filename # 添加当前uid# u; A- J g% Q, G: L7 z
%g : insert current gid into filename # 添加当前gid
; L: _! \1 I" C9 W& y7 K7 H%s : insert signal that cased the coredump into the file # 添加导致产生core的信号% A% ^% @; N/ f. J# e# n! M9 o3 u
%t : insert UNIX time that the coredump occurred into file# 添加core文件生成时的unix时间2 f1 U& g! W- @" `3 W; Y/ m. d/ e. V
%h : insert hostname where the coredump happened into file# 添加主机名
& ?# T% H9 i$ r; g7 a' E%e : insert coredumping executablename into file # 添加命令名
8 f- R& f+ I; W( c, n. l产生core的测试* R( w* j% m1 E
kill -s SIGSEGV $$
2 {% z' s9 u; ], d0 vLinux上的信号, g/ F- m* C* M7 R3 c6 d
发生coredump一般都是在进程收到某个信号的时候,Linux上现在大概有60多个信号,可以使用kill -l命令全部列出来。; h) D. i" e; Z" J5 {0 Q
( g' m& B" w8 x% [" q
针对特定的信号,应用程序可以写对应的信号处理函数。如果不指定,则采取默认的处理方式,默认处理是coredump的信号如下:
6 J; c9 L; i+ b- m# `8 w4 O3 [ G1 K
3) SIGQUIT 4) SIGILL 6) SIGABRT 8) SIGFPE 11) SIGSEGV
5 i9 [! ?6 d8 s: J, D7) SIGBUS 31) SIGSYS 5) SIGTRAP 24) SIGXCPU 25) SIGXFSZ
0 T/ s3 Q0 F- t$ K9 F8 |/ H P29) SIGIOT; g8 h& V. I! l: L! ]' m/ {
core的使用
: }5 J* Q/ X8 T$ a+ z- B( q, q" O编译的程序如果不添加-g,在core中不会有行的信息,如果希望产生更详细的core文件,编译程序时需要指定-g。; U7 z# @% i$ v l, l
) |, C6 w, k: jgdb execute corefile( i1 @, m( X# F9 ^1 P- n+ f
gdb 基本使用命令4 @* p t! r3 t
# 设置断点
% Q, w5 w$ Y' \9 D& eb 行号|函数名$ b7 w# a/ |; n0 }! U
# 删除断点- k/ F7 _' C) e. v# G* L e
delete 断点编号
7 I- J1 _, S6 ]4 H0 B# Y2 {' ^, M' e# 禁用断点
( U8 l5 s [0 G! _) E8 X- idisable 断点编号( |8 u+ @& W: J3 F4 M0 w$ C! [
# 弃用断点% x3 b% N3 w/ w9 w) c9 e
enable 断点编号
& h5 }* ~+ ]; q& E& p# 单步跟踪
d, f [$ q$ c1 ~( x! c8 B+ bnext- k& S; F5 j6 Y: H# o2 Q
# 单步跟踪
7 i: D2 A4 S8 M% b! kstep
$ C3 ?0 R8 k4 n$ A. `# Z3 _1 n# 打印变量
! r# s% y/ @# I% ^* C6 Tprint 变量名. \% Q/ b8 g0 Y1 H2 t
# 设置变量
1 c" x$ ~( Q9 P6 Y+ T$ cset var=value3 R' d; E+ P; S) v0 l) l
# 查看变量类型
0 y9 ?& C" ^, Y6 }8 e* p- o* mptype var$ V9 v( k+ t* i
# 顺序执行到结束& p4 t( u( S/ p( u
count
% L3 n5 u7 |; o* B6 I+ T# 顺序执行直到某一行, [7 }4 {( C; U. r+ X$ u1 p
util lineno
; T& u! M$ p/ O% m/ b4 G. {, ~/ o# 打印堆栈信息- o- ]7 ~# Z' W0 ~
bt
8 Z% d+ X+ c. c! @9 `
# M6 ~9 }( d; k. m5 l |
|