리눅스데브코스 [6주차 - 5]<GDB>

심우열·2023년 5월 16일
0

1. GDB란?

  • 디버거의 한 종류
  • 리눅스에서 커맨드라인으로 동작

1. Toolchains

  • GNU toolchain
    -> GCC + binutils + C library + GDB
  • GCC
    -> GNU Compiler Collection - C, C++
    -Binutils
    -> assembler, linker and utilities
    -> nm, objdump, readelf, gdb
  • GDB
    -> the GNU debugger

2. GDB

  • 서버와 임베디드 장비는 PC 운영체제처럼 GUI가 없음
  • 상황에 따라 gdb를 통해 디버깅 + 개발

3. GDB 커맨드

1. 기본

  • gdb [프로그램명]
    -> 프로그램에 GDB 모드 진입
  • gdb -p [PID]
    -> PID에 GDB 모드 진입
  • (gdb) run [인자]
    -> GDB 실행
  • quit
    -> GDB 종료

2. step

  • s: step and si
    -> 한 라인 수행, 만약 함수를 만나면 함수 안으로 진입
  • n: next
    -> 한 라인 수행, 함수를 만나도 다음 라인 진행
  • stepi & nexti
    -> 어셈블리어 단위로 수행
  • l
    -> 소스코드 출력

3. 실행

  • c: continue
    -> 다음 브레이크 포인트를 만날 때까지 수행
  • f: finish
    -> 현재 함수가 반환 할 때까지 수행

4. Breakpoints

  • break [location]
    -> 특정 위치에서 멈춘다
    -> Location: memoryAddress 또는 main.c:50(코드 라인)
  • delete, disable, enable
    -> 브레이크 포인트 수정

5. Watchpoints

  • watch [변수명]
    -> 특정 변수가 변경되는것을 감지

6. 출력

  • x prints
    -> 메모리값 출력
    -> x/x: hexadecimal
    -> x/i: assembly
  • print [변수명]
  • p ((struct 데이터 타입) 0X20000
  • display [변수명]
    -> step을 옮겨도 항상 변수의 데이터값을 출력

7. info

  • info registers
    -> CPU 레지스터 출력
  • info frame
    -> 현재 스택 프레임 출력
  • list [location]
    ->소스코드 출력
  • backtrace
    -> 함수 콜 스택 출력
  • info threads
    -> Thread 리스트 출력
  • info breakpoints
    -> Breakpoint 리스트 출력

8. fork debugging

  • set follow-fork-mode child
    -> 기본값은 parent
    -> 프로세스 생성시(fork()) 자식 또는 부모 프로세스 디버깅할지 설정
  • show follow-fork-mode
    -> 현재 어떤 프로세스로 디버깅 중인지 확인

GDB GUI tools

  • TUI
  • CGDB
  • DDD
  • VSCODE
profile
Dev Ops, "Git, Linux, Docker, Kubernetes, ansible, " .

0개의 댓글