--- tags: Development Note --- [TOC] # [DN-03]GDB 使用紀錄 開始試著用 GDB 來檢查追蹤程式流程和記憶體分配,跟著 GDB 來學習... 透過 GDB 來觀察 merge_soft 程式 [https://github.com/Lukechin/mergesort_doubly_linked_list/blob/master/mergesort.c ](https://) ### **編譯可以 debug 的輸出檔** 首先使用 GDB 蹤程式前,需要在編譯階段增加 ”-g” 參數來產生 debugging info 我們先編譯 merge_soft.c 並產生含有 debugging info 的執行檔 ”merge_soft” ```shell $ gcc -g -o merge_soft merge_soft.c ``` ### **編譯時產生preprocesser** ```shell $ gcc -E -P ``` ### **透過 GDB 來追蹤程式** ```shell $ gdb merge_soft ``` ![](https://i.imgur.com/cmWPV9Z.png) ### 常用命令 + `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. ### 參考資料 + [Buffer Overflow : Example of Using GDB to Check Stack Memory](http://www.cs.ucf.edu/~czou/CAP6135-12/bufferOverFlow-gdb-example.ppt)