日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

GDB调试技巧

發布時間:2023/11/27 生活经验 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GDB调试技巧 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. GDB 調試程序

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?- runRuns 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 . 直接運行至當前函數結束,比如誤按s進入函數A,在函數A中輸入fin會直接跳出函數A.? ? ? ? ??

ret – (returnReturns from current function; 直接退出當前函數,函數內尚未運行的code不會被執行。

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: 會以16進制的格式打印出expr

bt?- (backtrace) Prints a stack trace

bt full N - Print the values of the local variables also of the innermost N frames.? 加上full會打印出每個frame的局部變量。N指示打印出幾級frame.

u? - (until) This command is used to avoid single stepping through a loop more than once. u 經常用于跳出當前循環。

u N?- Continue running your program until either the specified location is reached,? ? u 123: 會直接運行到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.? ? ? ? 顯示上面的第幾級的frame的code,注意:只是顯示code, pc并沒有發送變化,即程序并沒有執行。

down?n -Move n frames down the stack; n defaults to 1.

q?- (quit) Quits gdb

b – (breakpoint) Set breakpoint (see next page)

3. 設置斷點-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 ,當前行設置斷點。
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, 刪除所有斷點(不用擔心,會有二次確認刪除提示)。
d n ? ?– delete specified(n- breakpoints are numbered when created) breakpoint
dis n ?– disable specified(n) breakpoint , 去能斷點
ena n – enable ?specified(n) breakpoint, 使能斷點。

tb ? ?- set temporary breakpoint? ?,設置臨時斷點,只作用一次,自動刪除。
rb regex - set breakpoints on all functions matching the regular expression regex(grep rule), 設置一類函數斷點,函數名符合一定的正則表達式即可。


commands n: You can give any breakpoint a series of commands to execute
when your program stops due to that breakpoint, 每一個斷點可以設置對應的命令,斷點觸發后,這些命令會被自動運行。

save b [filename] : saves all current breakpoint to a file.? 在gdb退出之前,可以存儲目前斷點的信息,以便下次調試時重新加載。
source [filename]: restore breakpoints
?

4. 設置觀察點-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 ,? ? ? ?寫觀察點, 當觀察點的值被修改時觸發。
rwatch ?– read watchpoint,? ? ? 讀觀察點, 當觀察點的值被讀取時觸發。
awatch ?– access watchpoint, 訪問(讀寫)觀察點, 被修改或被讀取時均會觸發。

備注,觀察點與硬件中斷有關,不能設置的太多。

比如一個struct A, 里面有200個不同類型的數據, 有全局變量 A a, 如果設置 watch a, 會報錯,提示: 觀察點太多。不同的硬件平臺,最多設置觀察點的個數不盡相同。另外,如果給局部變量設置 b 一個觀察點,則在b作用域失效后,其對應的觀察點自動失效。

5. 設置檢查點-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 可以保存程序某一時刻的狀態信息,用于后續恢復這一時刻。比如,一個很難復現的bug,你費了九牛二虎之力終于復現了,你就可以保存這一檢查點,繼續調試,如果調試期間錯過了某些信息,需要重新調試,你就可以恢復之前保存的觀察點,反復調試,猶如時光倒流。

6. 檢查變量和設置變量

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. 程序沒運行一次,都會打印此變量的值。
undispaly x: Removes the constant display
whatis x:? ? ? Print the data type of x, 查看變量的類型
info locals:? Print the local variables of the selected frame, 打印當前的堆棧局部變量。

info variables: All global and static variable names, or those matching?REGEXP,顯示全局變量和靜態變量名。也可以模糊匹配,比如:info variables g*: 會顯示g開頭的全局變量和靜態變量名

set x=3 : sets x to a set value (3) , 程序運行時,可以修改變量的值。
x/FMT ADDRESS: Examine memory, 檢查內存的值,非常有用。
? ? 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. 調用程序和CRT

在gdb 調試時,可以在cmd窗口直接調用工程中過的函數和c 語言標準庫。

call function()?
call function(x)
call strlen(x)
call sizeof(x)

8. 調試log輸出

如果我們要打印的變量結構體非常龐大,這時我們可以將此變量的值保存到文件中,單獨打開保存的文件以便更容易查看、。

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?為可執行文件的名字。

總結

以上是生活随笔為你收集整理的GDB调试技巧的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。