작성자: 주인장 디지문
(http://www.digimoon.net/)


RHEL에서 어플리케이션 core dump를 얻기 위한 설정 방법

OS version : RHEL 3.x ~ 6.x



Step 1(Global setting) - OS의 core file 기능을 on

① RHEL, CentOS, Fedora, Suse Linux 등의 리눅스 배포판들의 core file 생성 기능은 기본값으로 off되어 있습니다. core file 을 구성하기 위해서는 ulimit 커맨드를 이용해야 합니다.

/etc/profile을 편집기로 열어 아래와 같은 영구적 설정을 확인합니다. 없다면 skip하십시오(RHEL6에는 없습니다).
ulimit -S -c 0 > /dev/null 2>&1

위 설정을 아래와 같이 업데이트합니다.
ulimit -c unlimited >/dev/null 2>&1

② 현재의 Core File Limit를 확인("0"은 core file을 생성하지 않습니다)
[root@localhost ~]# ulimit -c
0
[root@localhost ~]#

아래와 같이 온라인 변경 및 확인
[root@localhost ~]# ulimit -c unlimited
[root@localhost ~]# ulimit -c
unlimited
[root@localhost ~]#

참고로 이미 실행 중인 daemon process에는 위 설정이 영향을 미치지 못하므로 daemon을 재시작해야 합니다.



Step 2(Indivisual Setting) - daemon들의 구동 방식에 따라 선택 적용

① /etc/init.d/ 의 init 스크립트(정확히는 /etc/init.d/functions을 include하여 daemon을 실행하는 스크립트)에 의해 구동된 daemon의 core dump를 얻고자 한다면 /etc/sysconfig/init 파일에 아래와 같이 추가합니다. 물론 init 스크립트를 재시작해야 daemon에 반영됩니다.
DAEMON_COREFILE_LIMIT='unlimited'


② setuid를 실행하는 프로그램은 core dump를 생성하지 않도록 RHEL에 기본 설정되어 있습니다. 민감한 정보의 유출(sensitive information being leaked)을 방지하기 위해서입니다.
setuid 실행 프로그램의 core dump를 얻고 싶다면 아래와 같이 설정합니다.

For RHEL 5, 6, 7

시스템 기본값
[root@localhost ~]# sysctl -a | grep 'fs.suid_dumpable'
fs.suid_dumpable = 0

권장설정
[root@localhost ~]# vim /etc/sysctl.conf
fs.suid_dumpable=2

debug용(privileged information가 leak될 가능성 있음)
[root@localhost ~]# vim /etc/sysctl.conf
fs.suid_dumpable=1

변경 후 온라인 적용합니다.
[root@localhost ~]# sysctl -p


③ systemd 기반으로 실행되는 프로그램은 core dump를 생성하지 않도록 RHEL에 기본 설정되어 있습니다.
/etc/systemd/system.conf에 아래 설정을 반영하고 적용합니다.

DefaultLimitCORE=infinity

# systemctl daemon-reexec


①, ②, ③에 해당하는 daemon이 아니라면 Step 2를 Skip하세요.




Step 3 - core dump 파일 저장 경로 및 이름 형식 선택

① 기본값으로 "core" 이름으로 생성됩니다.
[root@localhost ~]# cat /proc/sys/kernel/core_pattern
core

② /etc/sysctl.conf 설정
[root@localhost ~]# vim /etc/sysctl.conf
kernel.core_pattern = /tmp/core-%s-%u-%g-%p-%t

    %% - A single % character
    %p - PID of dumped process
    %u - real UID of dumped process
    %g - real GID of dumped process
    %s - number of signal causing dump
    %t - time of dump (seconds since 0:00h, 1 Jan 1970)
    %h - hostname (same as ’nodename’ returned by uname(2))
    %e - executable filename

변경 후 온라인 적용합니다.
[root@localhost ~]# sysctl -p

※ 어플리케이션 core dump 파일이름에 PID를 포함시키기 위한 커널 파라미터 설정으로는 아래 2가지 중 택일하세요.
 
 man 페이지에서 kernel.core_uses_pid는 아래와 같이 확인됩니다.
[root@localhost ~]# man /etc/sysctl.conf
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging  multi-threaded  applications.
kernel.core_uses_pid = 1
역주) 코어 덤프가 코어 파일이름에 PID를 추가할지 여부를 제어합니다. 멀티쓰레드 어플리케이션 디버깅에 유용합니다(여러 코어덤프를 PID로 구분 식별할 수 있으므로).


방법1) kernel.core_uses_pid = 1일 경우

PID가 코어파일이름에 포함됩니다.


방법2) kernel.core_uses_pid = 0일 경우

kernel.core_pattern 파라미터 값에 %p 옵션이 포함되어야 합니다.

예)
kernel.core_pattern = core.%e.%p



Step 4(optional) - 어플리케이션 crash 테스트

[root@localhost ~]# kill -s SIGSEGV <pidofrsyslog>

이제 kernel.core_pattern 파라미터에 설정한 디렉토리 경로에서 core dump 확인할 수 있습니다.
Creative Commons License