사용자 도구

사이트 도구


gdb

gdb

리눅스 환경에서 C언어로 작성 중인 프로그램이 segment falut를 출력하며 종료 되어 버릴 때 GDB를 이용한 디버깅 방법

1. 소스 파일 빌드시 디버깅 정보 포함 시키기

아래의 예에서 처럼 -g -ggdb 옵션을 포함 시킨다.

$ gcc -Wall -W -Werror -g -ggdb p33_my_deque.c -o p33_my_deque

2. core dump 파일 생성 옵션 변경

ulimit -c 명령을 사용하여 core dump 파일 생성 크기 제한을 늘려준다.

기본적으로 core dump 파일 크기 제한이 0으로 설정되어 있다.

ulimit -c unlimited

3. 프로그램 실행

segment fault가 발생한 상황과 동일하게 프로그램을 실행하여 core dump 파일이 생성되도록 한다.

4. gdb 실행

생성된 core dump 파일을 사용해 gdb를 실행하면 segment fault가 발생한 지점을 바로 확인 할 수 있다.

-c 옵션의 값으로 core dump 파일 이름을 지정한다.

gdb ./p33_my_deque -c core

정상적으로 실행된 경우의 gdb 출력 결과는 다음과 같다.

GNU gdb (Ubuntu 7.9-1ubuntu1) 7.9
Copyright (C) 2015 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./p33_my_deque...done.
 
warning: exec file is newer than core file.
[New LWP 2655]
Core was generated by `./p33_my_deque'.
---Type <return> to continue, or q <return> to quit---

Enter 키를 누르면 다음과 같이 segment fault가 발생한 위치에 대한 정보가 출력된다.

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004007d3 in pop_front () at p33_my_deque.c:83
83	        queue.front_ = poped_data->next_;
(gdb) 

이 경우 gdb 출력 결과에서 힌트를 얻어 소스 파일의 83번째 라인에서 NULL 값의 사용으로 인해 segment fault가 발생한 것을 알 수 있었다.

참고 자료

gdb.txt · 마지막으로 수정됨: 2015/06/06 21:44 저자 lindol