프로세스가 비정상적으로 종료될 경우, core 파일을 통하여 원인 분석을 할 수 있으며,
프로세스가 비정상적으로 종료될 경우, core 파일을 남길지 여부는 다음과 같은 명령어를 통해서
설정이 가능합니다.
[step 1] 비정상 종료 되는 샘플 프로그램 작성
#include <stdio.h> #include <stdlib.h> void Abnormal() { int n = 1024; char *p = (char *)malloc(sizeof(char) * 1); free(p); free(p); /* double free */ } void AbnormalContainer() { Abnormal(); } void Normal() { printf("normal function.\n"); } int main(int argc, char **argv) { AbnormalContainer(); Normal(); return 0; }
[step 2] 비정상 종료되는 샘플 소스 컴파일
[root@linux temp]# gcc -o main main.c -g
※ 주 의 사 항
gdb를 사용하여 core파일을 분석하기 위해서는 반드시 실행파일 컴파일시에 -g 옵션을 사용해야 합니다.
[step 3] 비정상 종료되는 샘플 실행
[root@linux temp]# ./main
*** glibc detected *** ./main: double free or corruption (fasttop): 0x0000000000601010 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3cd3472832]
/lib64/libc.so.6(cfree+0x8c)[0x3cd3475f2c]
./main[0x400577]
./main[0x400587]
./main[0x4005b2]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x3cd341e074]
./main[0x400499]
======= Memory map: ========
00400000-00401000 r-xp 00000000 fd:00 47153195 /root/temp/main
00600000-00601000 rw-p 00000000 fd:00 47153195 /root/temp/main
00601000-00622000 rw-p 00601000 00:00 0 [heap]
3cd2200000-3cd221b000 r-xp 00000000 fd:00 61440048 /lib64/ld-2.7.so
3cd241a000-3cd241b000 r--p 0001a000 fd:00 61440048 /lib64/ld-2.7.so
3cd241b000-3cd241c000 rw-p 0001b000 fd:00 61440048 /lib64/ld-2.7.so
3cd3400000-3cd354d000 r-xp 00000000 fd:00 61440050 /lib64/libc-2.7.so
3cd354d000-3cd374d000 ---p 0014d000 fd:00 61440050 /lib64/libc-2.7.so
3cd374d000-3cd3751000 r--p 0014d000 fd:00 61440050 /lib64/libc-2.7.so
3cd3751000-3cd3752000 rw-p 00151000 fd:00 61440050 /lib64/libc-2.7.so
3cd3752000-3cd3757000 rw-p 3cd3752000 00:00 0
3cddc00000-3cddc0d000 r-xp 00000000 fd:00 61440308 /lib64/libgcc_s-4.1.2-20070925.so.1
3cddc0d000-3cdde0d000 ---p 0000d000 fd:00 61440308 /lib64/libgcc_s-4.1.2-20070925.so.1
3cdde0d000-3cdde0e000 rw-p 0000d000 fd:00 61440308 /lib64/libgcc_s-4.1.2-20070925.so.1
2aaaaaaab000-2aaaaaaad000 rw-p 2aaaaaaab000 00:00 0
2aaaaaacc000-2aaaaaacd000 rw-p 2aaaaaacc000 00:00 0
2aaaac000000-2aaaac021000 rw-p 2aaaac000000 00:00 0
2aaaac021000-2aaab0000000 ---p 2aaaac021000 00:00 0
7fffc8065000-7fffc807a000 rw-p 7ffffffea000 00:00 0 [stack]
7fffc81fd000-7fffc81ff000 r-xp 7fffc81fd000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
[root@linux temp]# ls -al
total 476
drwxr-xr-x 2 root root 4096 2008-08-06 09:05 ./
drwxr-xr-- 32 root root 4096 2008-08-06 09:04 ../
-rw------- 1 root root 454656 2008-08-06 09:05 core.2805
-rwxrwxr-x 1 root root 8935 2008-08-06 09:04 main*
-rw-r--r-- 1 root root 260 2008-08-06 09:04 main.c
[step 4] gdb를 사용한 core 파일 분석
gdb를 사용하여 core파일을 분석하기 위해서는, 다음과 같이 gdb를 실행시키면 됩니다.
[root@linux temp]# gdb ./main ./core.2876
GNU gdb Red Hat Linux (6.6-45.fc8rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu"...
Using host libthread_db library "/lib64/libthread_db.so.1".
warning: Can't read pathname for load map: Input/output error.
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 /lib64/libgcc_s.so.1...done.
Loaded symbols for /lib64/libgcc_s.so.1
Core was generated by `./main'.
Program terminated with signal 6, Aborted.
#0 0x0000003cd3430ec5 in raise () from /lib64/libc.so.6
(gdb) backtrace --> backtrace 명령어를 사용해서 죽기 직전까지 호출되었던 스택 정보 확인
#0 0x0000003cd3430ec5 in raise () from /lib64/libc.so.6
#1 0x0000003cd3432970 in abort () from /lib64/libc.so.6
#2 0x0000003cd346b0db in __libc_message () from /lib64/libc.so.6
#3 0x0000003cd3472832 in _int_free () from /lib64/libc.so.6
#4 0x0000003cd3475f2c in free () from /lib64/libc.so.6
#5 0x0000000000400577 in Abnormal () at main.c:10
#6 0x0000000000400587 in AbnormalContainer () at main.c:15
#7 0x00000000004005b2 in main (argc=1, argv=0x7fffc8077798) at main.c:25
Missing separate debuginfos, use: debuginfo-install gcc.x86_64 glibc.x86_64
(gdb) break Abnormal --> 마지막으로 호출된 함수에 break point 설정
Breakpoint 1 at 0x400550: file main.c, line 6.
(gdb) r --> 실행후 차례대로 디버깅
Starting program: /root/temp/main
Breakpoint 1, Abnormal () at main.c:6
6 int n = 1024;
(gdb) s
7 char *p = (char *)malloc(sizeof(char) * 1);
(gdb) s
9 free(p);
(gdb) s
10 free(p); /* double free */
(gdb) s
*** glibc detected *** /root/temp/main: double free or corruption (fasttop): 0x0000000000601010 ***
출처 : http://blog.daum.net/aswip
댓글