GDB调试技巧
1. GDB 調(diào)試程序
1.Run a program without any argument.
???? gdb program
2. Run a program with arguments
??? gdb --args program arg1 arg2 ... argN
??? or
??? gdb program?
??? (gdb) r arg1 arg2 ... argN???
3. start with both an executable program and a core file specified
???? gdb program core
???? (gcore file: Produce a Core File from Your Program)
4. debug a running process,
??? specify a process ID as a second argument or use option –p
??? the following would attach gdb to process 1234:
??? gdb program 1234
??? gdb –p 1234
2. 常用命令
r?- (run)Runs the program until a breakpoint or error
c?-? (continue) Continues running the program until the next breakpoint or error
fin – (finish) Continue running until just after function in the selected stack frame returns . 直接運(yùn)行至當(dāng)前函數(shù)結(jié)束,比如誤按s進(jìn)入函數(shù)A,在函數(shù)A中輸入fin會(huì)直接跳出函數(shù)A.? ? ? ? ??
ret – (return)Returns from current function; 直接退出當(dāng)前函數(shù),函數(shù)內(nèi)尚未運(yùn)行的code不會(huì)被執(zhí)行。
j location – (jump) Resume execution at location; it does not change the current stack frame, like run(set $pc = location)
s ?- (step) Runs the next line of the program (step into)
s N?- Continue running as in step, but do so N times
n – (next) Like s, but it does not step into functions
n N - Continue to the next source N line in the current (innermost) stack frame
p expr?- (print) Prints the current value of the variable “expr“
p/f expr - you can choose a different format by specifying ‘/f’, where f is a letter specifying the format。比如p/x?expr: 會(huì)以16進(jìn)制的格式打印出expr
bt?- (backtrace) Prints a stack trace
bt full N - Print the values of the local variables also of the innermost N frames.? 加上full會(huì)打印出每個(gè)frame的局部變量。N指示打印出幾級(jí)frame.
u? - (until) This command is used to avoid single stepping through a loop more than once. u 經(jīng)常用于跳出當(dāng)前循環(huán)。
u N?- Continue running your program until either the specified location is reached,? ? u 123: 會(huì)直接運(yùn)行到123行code.
???????? or the current stack frame returns
f?- (frame) print current stack frame info.
up n -?? Move n frames up the stack; n defaults to 1.? ? ? ? 顯示上面的第幾級(jí)的frame的code,注意:只是顯示code, pc并沒有發(fā)送變化,即程序并沒有執(zhí)行。
down?n -Move n frames down the stack; n defaults to 1.
q?- (quit) Quits gdb
b – (breakpoint) Set breakpoint (see next page)
3. 設(shè)置斷點(diǎn)-breakpoint
Break into a Function
(gdb) b function
Break into a Function in a given file
(gdb) b filename:function
Break on to a line in a given file
(gdb) b filename:linenum
Break upon?matching?memory address
(gdb) b *(memory address)
Set a breakpoint with condition cond
(gdb) b <...> if condition
? ? ? ? ? condition bnum experssion
# Note:
#1: The symbol <...> implies that you can use any form of breakpoints.
Example: break linenum if variable==1
Others:
b N?- Puts a breakpoint at line N
b?? ? - Puts a breakpoint at the current line ,當(dāng)前行設(shè)置斷點(diǎn)。
b+N?- Puts a breakpoint N lines down from the current line
# Note: Break will take place at line in the current source file.
# The current file is the last file whose code appeared in the debug console.
i b ? ? – show all breakpoints
d ? ? ? – delete all breakpoints, 刪除所有斷點(diǎn)(不用擔(dān)心,會(huì)有二次確認(rèn)刪除提示)。
d n ? ?– delete specified(n- breakpoints are numbered when created) breakpoint
dis n ?– disable specified(n) breakpoint , 去能斷點(diǎn)
ena n – enable ?specified(n) breakpoint, 使能斷點(diǎn)。
tb ? ?- set temporary breakpoint? ?,設(shè)置臨時(shí)斷點(diǎn),只作用一次,自動(dòng)刪除。
rb regex - set breakpoints on all functions matching the regular expression regex(grep rule), 設(shè)置一類函數(shù)斷點(diǎn),函數(shù)名符合一定的正則表達(dá)式即可。
commands n: You can give any breakpoint a series of commands to execute
when your program stops due to that breakpoint, 每一個(gè)斷點(diǎn)可以設(shè)置對(duì)應(yīng)的命令,斷點(diǎn)觸發(fā)后,這些命令會(huì)被自動(dòng)運(yùn)行。
save b [filename] : saves all current breakpoint to a file.? 在gdb退出之前,可以存儲(chǔ)目前斷點(diǎn)的信息,以便下次調(diào)試時(shí)重新加載。
source [filename]: restore breakpoints
?
4. 設(shè)置觀察點(diǎn)-watchpoint
Set a watchpoint for an expression. gdb will break when the expression expr is written into by the program and its value changes. The simplest (and the most popular) use of this command is to watch the value of a single variable:
?? ?(gdb) watch drbId
If you watch for a change in a numerically entered address you need to dereference it, as the address itself is just a constant number which will never change. gdb refuses to create a watchpoint that watches a never-changing value:
(gdb) watch 0x600850
Cannot watch constant value 0x600850.
(gdb) watch *(int *) 0x600850
Watchpoint 1: *(int *) 6293584
watch ? – write watchpoint ,? ? ? ?寫觀察點(diǎn), 當(dāng)觀察點(diǎn)的值被修改時(shí)觸發(fā)。
rwatch ?– read watchpoint,? ? ? 讀觀察點(diǎn), 當(dāng)觀察點(diǎn)的值被讀取時(shí)觸發(fā)。
awatch ?– access watchpoint, 訪問(讀寫)觀察點(diǎn), 被修改或被讀取時(shí)均會(huì)觸發(fā)。
備注,觀察點(diǎn)與硬件中斷有關(guān),不能設(shè)置的太多。
比如一個(gè)struct A, 里面有200個(gè)不同類型的數(shù)據(jù), 有全局變量 A a, 如果設(shè)置 watch a, 會(huì)報(bào)錯(cuò),提示: 觀察點(diǎn)太多。不同的硬件平臺(tái),最多設(shè)置觀察點(diǎn)的個(gè)數(shù)不盡相同。另外,如果給局部變量設(shè)置 b 一個(gè)觀察點(diǎn),則在b作用域失效后,其對(duì)應(yīng)的觀察點(diǎn)自動(dòng)失效。
5. 設(shè)置檢查點(diǎn)-checkpoint
checkpoint:
Save a snapshot of the debugged program’s current execution state
restart checkpoint-id
Restore the program state that was saved as checkpoint number checkpoint-id
checkpoint 可以保存程序某一時(shí)刻的狀態(tài)信息,用于后續(xù)恢復(fù)這一時(shí)刻。比如,一個(gè)很難復(fù)現(xiàn)的bug,你費(fèi)了九牛二虎之力終于復(fù)現(xiàn)了,你就可以保存這一檢查點(diǎn),繼續(xù)調(diào)試,如果調(diào)試期間錯(cuò)過了某些信息,需要重新調(diào)試,你就可以恢復(fù)之前保存的觀察點(diǎn),反復(fù)調(diào)試,猶如時(shí)光倒流。
6. 檢查變量和設(shè)置變量
p x :? ? ? ? ? ? ?Prints current value of variable x.
display x:? ? ?Constantly displays the value of variable x, which is shown after every step or pause. 程序沒運(yùn)行一次,都會(huì)打印此變量的值。
undispaly x: Removes the constant display
whatis x:? ? ? Print the data type of x, 查看變量的類型
info locals:? Print the local variables of the selected frame, 打印當(dāng)前的堆棧局部變量。
info variables: All global and static variable names, or those matching?REGEXP,顯示全局變量和靜態(tài)變量名。也可以模糊匹配,比如:info variables g*: 會(huì)顯示g開頭的全局變量和靜態(tài)變量名
set x=3 : sets x to a set value (3) , 程序運(yùn)行時(shí),可以修改變量的值。
x/FMT ADDRESS: Examine memory, 檢查內(nèi)存的值,非常有用。
? ? FMT is a repeat count followed by a format letter and a size letter.
? ? ? Format letters are : o(octal), x(hex), d(decimal), u(unsigned decimal), t(binary), f(float), a(address),?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? i(instruction), c(char) ?and s(string).?
? ? ? Size letters are: ? ? ? b(byte), h(halfword), w(word), g(giant, 8 bytes)
?
7. 調(diào)用程序和CRT
在gdb 調(diào)試時(shí),可以在cmd窗口直接調(diào)用工程中過的函數(shù)和c 語言標(biāo)準(zhǔn)庫(kù)。
call function()?
call function(x)
call strlen(x)
call sizeof(x)
8. 調(diào)試log輸出
如果我們要打印的變量結(jié)構(gòu)體非常龐大,這時(shí)我們可以將此變量的值保存到文件中,單獨(dú)打開保存的文件以便更容易查看、。
show logging
? ? ? ? Show the current values of the logging settings.
set logging on
?? ??? ?Enable logging.
set logging off
?? ??? ?Disable logging.
set logging file file
?? ??? ?Change the name of the current logfile. The default logfile is gdb.txt.
set logging overwrite [on|off]
?? ??? ?By default, gdb will append to the logfile. Set overwrite if you want set logging on to overwrite the logfile instead
?
9.生成Core File
A core file or core dump is a file that records the memory image of a running process and its process status (register values etc.).
generate-core-file [file]? ?:?生成core file. note: 縮寫:gcore [file]
gdb exe_name core.file? : gdb?加載core file, exe_name?為可執(zhí)行文件的名字。
總結(jié)
- 上一篇: LTE PUCCH F2 TX/RX汇总
- 下一篇: Git 简介1-常用术语