J 창고

Linux Kernel Debugging with Eclipse 본문

컴퓨터/Linux

Linux Kernel Debugging with Eclipse

JSFamily 2016. 2. 8. 23:07



Linux 에는 GDB라는 걸출한 Debuging Tool이 존재하지만 Text 기반의 툴이다 보니 Microsoft사의 Visual Studio에 익숙한 사람은 사용하기가 난해하다. 따라서 Eclipse와 GDB를 이어서 GUI 환경에서 디버깅 할 수 있도록 하는게 이번 포스팅의 목적이다.



1. 환경

Ubuntu 14.04.5

Eclipse Kepler

Kernel 3.19.0



2. Ubuntu 커널 버전 확인

dev@dev:/usr/src$ uname -r

3.19.0-25-generic



3. Kernel Source Download

Kernel.org 에서 다운로드

https://www.kernel.org/pub/linux/kernel/v3.x/


버전에 맞는 소스의 링크를 복사한 후 wget으로 다운로드

dev@dev:/usr/src$ sudo wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.19.tar.gz

--2016-02-08 22:23:21--  https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.19.tar.gz

Resolving www.kernel.org (www.kernel.org)... 198.145.20.140, 149.20.4.69, 199.204.44.194, ...

Connecting to www.kernel.org (www.kernel.org)|198.145.20.140|:443... connected.

HTTP request sent, awaiting response... 200 OK

Length: 123115155 (117M) [application/x-gzip]

Saving to: ‘linux-3.19.tar.gz’



4. 압축 해제

dev@dev:/usr/src$ sudo tar -zxvf linux-3.19.tar.gz 



5. Eclipse 다운로드 & 설치

Eclipse 사이트에서 Kepler 버전의 C/C++ Developers 를 다운로드

http://www.eclipse.org/kepler/ 


JDK 설치

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update

sudo apt-get install oracle-jdk7-installer



6. Eclipse 설정


6-1. Project 생성


File -> New -> Makefile Project with Existing Code





Project Path & Toolchain  설정 -> Finish





6-2. menuconfig 설정

dev@dev:/usr/src/linux-3.19$ sudo make menuconfig


ps. 필요 Package :  libncurses5-dev


Kernel hacking -> Compile-time check and compiler option -> Compile the kernel with debug info [Enable]
Kernel hacking -> Compile-time check and compiler option -> Compile the kernel with frame pointers [Enable]



6-3 Eclipse 설정

windows -> Preference -> General -> Workspace -> Uncheck "Build automatically"





windows -> Preference -> C/C++ -> Indexer -> Uncheck "Enable indexer"





Project -> Properties -> C/C++ Build -> Build Setting 에서 Build Command 변경





7. Kernel Source Build

Project -> Build All



8. Debug Configuration 

Eclipse의 Debug Configureation 을 실행


QEMU 를 통해서 원격 디버깅을 하기 때문에 C/C++ Remote Application에 Debug Option을 구성해야 함



C/C++ Remote Application을 만들면 아래와 같은 화면이 나옴



디버깅 할 바이너리(Linux Kernel Image)의 경로를 선택

(작성하면서 Kernel 버전이 바뀌었지만 빌드한 후 Directory안에 생성된 vmlinux 파일을 선택한다)






* 혹시 Remote Absolute File Path for C/C++ Application 이라는 항목이 뜨면 이것은 Remote Target에 Binary를 다운로드 하기 위한

기능으로 지금 Kernel Debug 하기 위해선 필요하지 않은 기능이다

따라서 이 기능을 없애기 위해선 아래 "Using GDB (DSF) Automatic Remote Debugging Launcher - Select other..."에서 Select Other를 클릭하여 "GDB (DSF) Manual Remote Debugging Launcher"로 바꿔준다.






Debugger Tab 에서 어디서부터 시작할 지에 대한 Stop on startup at에 start_kernel을 입력한다.

또 GDB Port Number 는 1234로 한다.





9. QEMU 실행

QEMU실행하기 앞서 QEMU 설치



QEMU에서 사용할 Root File System 다운로드

http://downloads.yoctoproject.org/releases/yocto/yocto-1.2.1/machines/qemu/qemux86/core-image-minimal-dev-qemux86.ext3



QEMU 실행

sudo qemu-system-x86_64 -s -S -no-kvm -kernel arch/x86_64/boot/bzImage -hda /mnt/core-image-minimal-dev-qemux86-64.ext3 -append "root=/dev/sda console=ttyS0" -serial stdio



위 커맨드에서 -S 옵션을 빼고 실행하면 아래와 같이 새로운 창과 함께 실행한 terminal이 serial과 같이 log가 출력된다.

-S 옵션은 Remote Debugger가 연결 될 때 까지 기다리겠다는 의미





10. Eclipse를 활용하여 GUI 환경에서 Kernel을 Debugging 해 보자!

앞서 테스트 했던 QEMU 실행 방법을 실행하여 가상 컴퓨터를 하나 실행한다(물론 -S 옵션으로 인해 자동으로 Stop 된다)





그 상태에서 Eclipse에서 설정한 Debug Configuration을 통해 Debugging을 시작한다




그러면 Linux Kernel이 로딩될 것이고 Start_kernel 함수에서 Stop되는 것을 Eclipse에서 확인 할 수 있다.


=> QEMU 에서 Start_kernel에서 멈춰있는 상태



=> Eclipse 에서 Start_kernel에서 멈춰있는 상태





이후 원하고자 하는 방식으로 Debugging을 하면 된다!






11. GDB 오류 해결

GDB 실행하고 나서 아래와 같은 메세지가 뜬다면 이건 링크와 같이 GDB Src를 직접 다운받아 수정 후에 GDB 를 실행해야 한다.

(GDB에서는 저렇게 나오지만 Eclipse에서는 Break Point에서 멈추지 않고 그냥 넘어가는 문제로 나타난다)
  => GDB/CGDB에서도 발생하는 문제로 GDB를 사용하는 Eclipse에서도 당연히 발생한다.




GDB 버전 확인




http://ftp.gnu.org/gnu/gdb/
에서 버전에 맞는 GDB 다운로드 및 링크와 같이 소스를 수정하여 빌드 및 설치
make && sudo make install


수정된 GDB를 사용하면 아래와 같이 b start_kernel 을 통해 설정한 Break Point에서 Stop 되는 것을 확인할 수 있다.











Comments