Try   HackMD

[DN-03]GDB 使用紀錄

開始試著用 GDB 來檢查追蹤程式流程和記憶體分配,跟著 GDB 來學習
透過 GDB 來觀察 merge_soft 程式 https://github.com/Lukechin/mergesort_doubly_linked_list/blob/master/mergesort.c

編譯可以 debug 的輸出檔

首先使用 GDB 蹤程式前,需要在編譯階段增加 ”-g” 參數來產生 debugging info
我們先編譯 merge_soft.c 並產生含有 debugging info 的執行檔 ”merge_soft”

$ gcc -g -o merge_soft merge_soft.c

編譯時產生preprocesser

$ gcc -E -P

透過 GDB 來追蹤程式

$ gdb merge_soft

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

常用命令

  • b <line_number> - setting breakpoint at <line_number>

  • info b - print breakpoints information

  • disable b <break_number> - disable target breakpoint

  • continue - run after code

  • Next - run next instruct (no entry sub function)

  • Step - run next instruct (entry sub funxtion)

  • List - list the source code and each execution’s corresponding line number

  • Backtrace - show trace of all function calls in stack

  • Info frame - List address, language, address of arguments/local variables and which registers were saved in frame.

    • This will show where the return address is saved
    • Return address is in Register EIP
    • Calling stack pointer is in Register EBP
  • x &variable - show the address and value of a local variable (in hex format)

  • x address - print binary representation of 4 bytes of memory pointed to by address.

參考資料