筆記:gdb 執行/帶參數/停止/顯示help


執行gdb

第一個參數ffprobe_g是待會要debug的程式。
$ gdb ffprobe_g
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
...
Type "apropos word" to search for commands related to "word"...
Reading symbols from ffprobe_g...done.
(gdb)

如果該程式有symbol在內,會顯示上面藍色標示字串,  如果要debug應該要載入symbol。
如果你的程式已經strip過,可能就沒有symbol可以參考,需要編譯成沒有strip

這裡也可以不指定第1參數,之後再指定, 請跳到"載入/改變執行程式"部份。

顯示子指令i/info

(gdb) i
List of info subcommands:

info address -- Describe where symbol SYM is stored
info all-registers -- List of all registers and their contents
info args -- Argument variables of current stack frame
....

顯示說明h/help

(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
....

針對特定指令顯示說明

(gdb) h help
Print list of commands.

(gdb) h run
Start debugged program.  You may specify arguments to give it.
Args may include "*", or "[...]"; they are expanded using "sh".
Input and output redirection with ">", "<", or ">>" are also allowed.
...

執行程式r/run

(gdb) r
Starting program: ffprobe_g
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
ffprobe version N-96259-g481f4380fb Copyright (c) 2007-2020 the FFmpeg developers
...
[Inferior 1 (process 27770) exited with code 01]
沒設定中斷點會直接執行到結束

如果你已經有執行一個程式,就會顯示以下訊息, 你可以決定是否要重新執行程式
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n)

執行程式時帶入的參數r with args

(gdb) r -i content/movie.ts
Starting program: ffprobe_g -i content/movie.ts
...
Input #0, mpegts, from 'content/movie.ts':
  Duration: 00:01:05.32, start: 46641.738644, bitrate: 9902 kb/s
  Program 3
    Stream #0:0[0x1022](chi): Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 192 kb/s
    Stream #0:1[0x1023]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709, top first), 1920x1080 [SAR 1:1 DAR 16:9], 29.97 fps, 59.94 tbr, 90k tbn, 59.94 tbc
    Stream #0:2[0x1024](eng): Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 195 kb/s
[Inferior 1 (process 3273) exited normally]

如果已經打過參數,下次直接用r就會代入上次的參數。

設定目前參數set args

如果要設定參數就打在set args 後面, 例如
(gdb) set args -i content/movie.ts

和set args和r的不同點,set args只會設定,r是會設定後執行程式,

取消目前參數

如果要取消參數,用set args不帶後續參數即可。
(gdb) set args

顯示目前參數show args

(gdb) show args
Argument list to give program being debugged when it is started is "".

在執行gdb時就帶入執行程式參數-ex

$ gdb ffprobe_g -ex "set args -i content/movie.ts"

參數不能直接加在gdb參數後面, 上面示範使用-ex執行set args的指令來完成

載入/改變執行程式exec-file

如果你在啟動gdb時沒有給定第一個程式參數,就打算執行程式,會得到下面錯誤訊息
(gdb) r
Starting program: 
No executable file specified.
Use the "file" or "exec-file" command.

這時候要用exec-file
(gdb) exec-file ffmpeg_g

然後再r就可以執行程式

卸載執行程式

(gdb) exec-file
不帶任何程式名稱時就是卸載執行程式, 這時r就會顯示錯誤No executable file specified.

載入程式及符號

(gdb) file ffprobe_g
Reading symbols from ffprobe_g...done.

(gdb) file ffprobe
Reading symbols from ffprobe...(no debugging symbols found)...done.
找不到symbol的狀況

卸載程式及符號

(gdb) file
No executable file now.
Discard symbol table from `ffprobe_g'? (y or n) y
No symbol file now.

顯示目前載入的程式

(gdb) info files
Symbols from "ffprobe_g".
Local exec file:
  `ffprobe_g', file type elf64-x86-64.
  Entry point: 0x498be0
  0x0000000000400238 - 0x0000000000400254 is .interp
  0x0000000000400254 - 0x0000000000400274 is .note.ABI-tag
  0x0000000000400274 - 0x0000000000400298 is .note.gnu.build-id
  0x0000000000400298 - 0x0000000000400390 is .gnu.hash
...

卸載程式後再顯示
(gdb) file
No executable file now.
No symbol file now.
(gdb) info files
(gdb)

離開GDB, q/quit

(gdb) q
$

如果正在執行程式,他會顯示目前正在執行中的程式,是否要離開
(gdb) q
A debugging session is active.

  Inferior 1 [process 6423] will be killed.

Quit anyway? (y or n)

關閉執行debug中的程式k,kill

(gdb) kill
Kill the program being debugged? (y or n)

正在執行debug的程式狀態i prog/info program

如果沒有執行中的程式
(gdb) i prog
The program being debugged is not being run.

如果正停留在breakpoint中
(gdb) i prog 
Using the running image of child Thread 0x7ffff7f2e8c0 (LWP 12516).
Program stopped at 0x496750.
It stopped at breakpoint 1.
Type "info stack" or "info registers" for more information

關於下面的inferior相關, 也適用於上面單一程式的情形
https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-and-Programs.html

顯示執行中的inferiors

(gdb) info inferiors
  Num  Description       Executable     
* 1    process 7128      ffprobe_g

(gdb) inferior 1

關閉執行中的inferiors

(gdb) kill inferiors 1

再查看一下執行中的程式,沒有process id就是沒有正在執行
(gdb) info inferiors
  Num  Description       Executable     
* 1                ffprobe_g

顯示執行中inferior的資訊

(gdb) inferior 1
[Switching to inferior 1 [] (ffprobe_g)]

(gdb) inferior 2
Inferior ID 2 not known.


參考:
http://puremonkey2010.blogspot.com/2010/07/gdb-gdb.html

留言