0Pricing
Assembly Language & x86 Low-Level Systems Programming · 강의

어셈블리 디버깅에 GDB 사용

중단점 설정, 레지스터와 메모리 검사, 어셈블리 코드 단계별 실행을 위해 GNU 디버거(GDB)를 능숙하게 사용하는 방법을 익힙니다.

어셈블리 디버깅에 GDB 사용은(는) CoddyKit의 무료 Assembly Language & x86 Low-Level Systems Programming 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Assembly Language & x86 Low-Level Systems Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Assembly Language & x86 Low-Level Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Debugging's Best Friend: GDB

Welcome to the world of debugging! When working with low-level languages like assembly, understanding exactly what your program is doing, instruction by instruction, is crucial.

The GNU Debugger (GDB) is an incredibly powerful tool that lets you:

  • Pause your program at specific points (breakpoints).
  • Step through code line by line.
  • Inspect the values in registers and memory.
  • Understand program flow and identify issues.

It's an essential skill for any assembly programmer!

Preparing Your Assembly Code

Before GDB can help us, we need to compile our assembly code with special 'debug symbols'. These symbols tell GDB about variable names, function labels, and source code lines, making debugging much easier.

For NASM assembly on Linux, you typically use the -g flag during compilation and linking:

  • Assemble: nasm -f elf32 -g your_code.asm -o your_code.o
  • Link: ld -m elf_i386 -g your_code.o -o your_executable

The -g flag embeds the debugging information directly into the object file and executable.

Our Target: A Simple Program

Let's use a straightforward assembly program as our debugging target. This program prints a message to the console and then performs a simple addition before exiting.

We'll compile this with debug symbols and then dive into GDB.

; filename: debug_example.asm

section .data
    hello_msg db "Hello from Assembly!", 0xA ; Message to print
    hello_len equ $ - hello_msg

section .text
    global _start

_start:
    ; --- Part 1: Print "Hello from Assembly!" ---
    ; sys_write syscall (Linux x86 32-bit)
    mov eax, 4          ; syscall number for sys_write
    mov ebx, 1          ; file descriptor 1 (stdout)
    mov ecx, hello_msg  ; address of string to write
    mov edx, hello_len  ; length of string
    int 0x80            ; invoke kernel

    ; --- Part 2: Perform a simple calculation ---
    mov eax, 10         ; Move 10 into EAX
    mov ebx, 5          ; Move 5 into EBX
    add eax, ebx        ; Add EBX to EAX (EAX becomes 15)

    ; --- Part 3: Exit the program ---
    ; sys_exit syscall (Linux x86 32-bit)
    mov eax, 1          ; syscall number for sys_exit
    mov ebx, 0          ; exit code 0 (success)
    int 0x80            ; invoke kernel

Launching GDB

Once your program is compiled with debug symbols, launching GDB is simple. Open your terminal and type gdb followed by your executable's name.

For our example, if the executable is named debug_executable, you would type:

gdb ./debug_executable

GDB will load your program and present you with its prompt, usually (gdb). Your program isn't running yet; GDB is just ready to receive commands.

Setting Your First Breakpoint

A breakpoint is a marker that tells GDB to pause your program's execution when it reaches a specific instruction or memory address. This is how you stop the program at a point of interest.

To set a breakpoint, use the break or b command, followed by a function name, label, or memory address. For assembly, we often use labels.

  • (gdb) break _start: Sets a breakpoint at the program's entry point.
  • (gdb) b *0x80480a0: Sets a breakpoint at a specific memory address (example address).

Running and Resuming Execution

After setting breakpoints, you can start your program or resume its execution.

  • run (or r): Starts your program from the beginning. It will run until it hits the first breakpoint, finishes, or crashes.
  • continue (or c): Resumes execution after your program has hit a breakpoint. It will continue running until the next breakpoint or program termination.

Try setting a breakpoint at _start and then using run. You'll see GDB pause right at the beginning of your program!

Step-by-Step Execution

Once your program is paused at a breakpoint, you can execute instructions one at a time. This is invaluable for seeing the exact effect of each instruction.

  • stepi (or si): Executes the next single instruction. If the instruction is a call to a procedure, si will enter that procedure.
  • nexti (or ni): Executes the next single instruction. If the instruction is a call, ni will execute the entire procedure and stop at the instruction immediately *after* the call.

Use si to meticulously trace through every instruction, or ni to skip over procedure calls you're not interested in.

Peeking at Registers

Registers are the CPU's small, super-fast storage locations. In assembly, you're constantly moving data in and out of them. GDB lets you inspect their current values.

  • info registers (or i r): Displays the current values of all general-purpose registers, segment registers, and the instruction pointer (EIP/RIP) and flags.
  • info registers eax: Displays the value of a specific register, like EAX.
  • print $eax: Another way to print a specific register's value. The $ prefix tells GDB it's a register.

After our add eax, ebx instruction, you could check eax to see its new value (15).

Examining Memory Content

Beyond registers, you'll often need to inspect what's stored in memory. The x command (examine) is your friend here.

Its format is x/<count><format><unit-size> <address>:

  • count (N): How many units to display.
  • format (F): How to display (e.g., x for hex, d for decimal, s for string, i for instructions).
  • unit-size (U): Size of each unit (e.g., b for byte, h for halfword/2 bytes, w for word/4 bytes, g for giant/8 bytes).

Examples:

  • (gdb) x/s hello_msg: Display the string at hello_msg.
  • (gdb) x/4xw $esp: Display 4 words (4 bytes each) in hex starting from the stack pointer.

Disassembling Code on the Fly

Sometimes you're debugging and need to see the assembly instructions around your current execution point. The disassemble command (or disas) converts machine code back into assembly.

  • (gdb) disassemble: Disassembles the function currently being executed.
  • (gdb) disas _start: Disassembles the entire _start function.
  • (gdb) disas $eip, +20: Disassembles 20 bytes of instructions starting from the current instruction pointer (EIP).

This helps you quickly orient yourself and see the surrounding code context.

GDB Command Challenge

Which of the following GDB commands are used to control program execution (start, pause, resume, step)?

Debugging's Power Unleashed

Congratulations! You've taken your first steps into mastering GDB for assembly debugging. We covered:

  • Why GDB is essential for low-level programming.
  • How to compile assembly with debug symbols.
  • Basic GDB commands like run, break, and continue.
  • Stepping through code with stepi and nexti.
  • Inspecting registers (info registers) and memory (x).
  • Disassembling code on the fly (disassemble).

These fundamental skills will empower you to understand and troubleshoot complex assembly programs, revealing their inner workings instruction by instruction. Keep practicing!

자주 묻는 질문

“어셈블리 디버깅에 GDB 사용” 강의는 무료인가요?

네 — “어셈블리 디버깅에 GDB 사용” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Assembly Language & x86 Low-Level Systems Programming 강의 전체를 잠금 해제할 수 있습니다. Assembly Language & x86 Low-Level Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

“어셈블리 디버깅에 GDB 사용”에서 뭘 배우나요?

중단점 설정, 레지스터와 메모리 검사, 어셈블리 코드 단계별 실행을 위해 GNU 디버거(GDB)를 능숙하게 사용하는 방법을 익힙니다. 브라우저에서 직접 실행하는 실습 코드로 Assembly Language & x86 Low-Level Systems Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Assembly Language & x86 Low-Level Systems Programming을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Assembly Language & x86 Low-Level Systems Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“어셈블리 디버깅에 GDB 사용” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Assembly Language & x86 Low-Level Systems Programming 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Assembly Language & x86 Low-Level Systems Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 어셈블리 디버깅에 GDB 사용
  2. 디스어셈블리 도구 소개
  3. 기초 리버스 엔지니어링 기법
  4. 추적과 후킹을 활용한 동적 분석
← Assembly Language & x86 Low-Level Systems Programming(으)로 돌아가기