# [AIdrifter CS 浮生筆錄](https://hackmd.io/bKL1LVo8RaCKn02uHJW2BA?view) : REMOTE GDB compile 之前要記得加 `-g` option # Reomte GDB ## Target Board(Target) - telnet進去板子或是直接再console上面操作,有兩種模式 - attach process - execute the binary ```shell # Attach process telnet 172.16.22.227 gdbserver :2001 --attach `ps | grep "application\/daemon\.bin" | awk '{print $1}'` & # run binary gdbserver :2001 ./test_bin ``` ## GDB toolchain server(Host) ```shell (gdb) arm-linux-gnueabihf-gdb ./optee_test -x your_scripts.sh # symbol paths (gdb) set solib-absolute-prefix ./symbols # connect to target board (gdb) target remote 172.16.22.227:2001 # ignore some signal (gdb) set pagination off (gdb) handle SIG38 nostop (gdb) handle SIG44 nostop (gdb) handle SIGTSTP nostop ``` :::info [Add]Linaro Toolchain will read symbol form target board :) ::: # GDB Command ## flow control ```shell (gdb) b main (gdb) c # continue to next break point (gdb) fin # come back to function() entry point (gdb) s # jump into current function(enter the sub function and execute it ) (gdb) n # move next line ( PC = PC + 4) ``` ## break point ```shell (gdb) b main Breakpoint 1 at 0x664: file main.c, line 12. (gdb) disable 1 # disalbe break point (gdb) enable 1 # enable break point ``` ## display variable ```shell (gdb) p variable # show variable information on GDB (gdb) x/40wx $esp # real linux machine (gdb) x/40wx $rsp # vmware ubuntu replace real linux machine (gdb) x/40wx $sp # GDB provides $sp pseudo-register that is automatically mapped to correct stack pointer register for a given platform. (gdb) x/4wx $eax (gdb) x/i $address (gdb) x/i never_use 0x4005b6 <never_use>: push %rbp (gdb) p/x 0xffffcf0c - 0xffffcef4 # 0x18 (gdb) p/d 0xffffcf0c - 0xffffcef4 # 24 ``` ## assembly language ```shell (gdb) lay asm # show asm language (gdb) set disassembly-flavor intel (gdb) ni # run one assembly language ``` ## backtrace ```shell (gdb) bt # Print a backtrace of the entire stack ``` ## frame stack we can use `up` and `down` to jump frame stack and show variable information in stack ## show source code some environment problem ```shell export TERM=vt100 export TERMINFO=/usr/share/terminfo ``` - show source code ```shell <gdb> lay src # show source code ``` - ==**lay src** 可以不用`printf()`打天下了== ![](https://i.imgur.com/Mu6Jsuk.png) - disable lay src - `ctrl + x` + `a` # Advance ## 檢查thread 狀態 ```shell thread apply all bt ``` ## 查deadlock - 如果沒有remote gdb ## 動態追蹤 - 再沒有hardware watch support的時候 - `commands $break_point_number` ```shell (gdb) b fun Breakpoint 1 at 0x664: file main.c, line 12. # commands (gdb) commands 1 Type commands for breakpoint(s) 1, one per line. End with a line saying just "end". >bt > echo *pstDMS_SetWin_Info >p *pstDMS_SetWin_Info >c >end ``` ## Ref - GDB 各指令解釋 - 以下是 gdb 的常見指令(其中 () 內為簡短指令): - help (h):顯示指令簡短說明。例:help breakpoint - file:開啟檔案。等同於 gdb filename - run (r):執行程式,或是從頭再執行程式。 - kill:中止程式的執行。 - backtrace (bt):堆疊追蹤。會顯示出上層所有的 frame 的簡略資訊。 - print (p):印出變數內容。例:print i,印出變數 i 的內容。 - list (l):印出程式碼。若在編譯時沒有加上 -g 參數,list 指令將無作用。 - whatis:印出變數的型態。例: whatis i,印出變數 i 的型態。 - breakpoint (b, bre, break):設定中斷點 使用 info breakpoint (info b) 來查看已設定了哪些中斷點。 在程式被中斷後,使用 info line 來查看正停在哪一行。 - continue (c, cont):繼續執行。和 breakpoint 搭配使用。 - frame:顯示正在執行的行數、副程式名稱、及其所傳送的參數等等 frame 資訊。 frame 2:看到 #2,也就是上上一層的 frame 的資訊。 - next (n):單步執行,但遇到 frame 時不會進入 frame 中單步執行。 - step (s):單步執行。但遇到 frame 時則會進入 frame 中單步執行。 - until:直接跑完一個 while 迴圈。 - return:中止執行該 frame(視同該 frame 已執行完畢), 並返回上個 frame 的呼叫點。功用類似 C 裡的 return 指令。 - finish:執行完這個 frame。當進入一個過深的 frame 時,如:C 函式庫, 可能必須下達多個 finish 才能回到原來的進入點。 - up:直接回到上一層的 frame,並顯示其 stack 資訊,如進入點及傳入的參數等。 - up 2:直接回到上三層的 frame,並顯示其 stack 資訊。 - down:直接跳到下一層的 frame,並顯示其 stack 資訊。 必須使用 up 回到上層的 frame 後,才能用 down 回到該層來。 - display:在遇到中斷點時,自動顯示某變數的內容。 - undisplay:取消 display,取消自動顯示某變數功能。 - commands:在遇到中斷點時要自動執行的指令。 - info:顯示一些特定的資訊。如: info break,顯示中斷點, info share,顯示共享函式庫資訊。 - disable:暫時關閉某個 breakpoint 或 display 之功能。 - enable:將被 disable 暫時關閉的功能再啟用。