pdb基于命令行的調(diào)試工具,非常類(lèi)似gnu的gdb(調(diào)試c/c++)
命令列表
命令 | 簡(jiǎn)寫(xiě)命令 | 作用 |
---|---|---|
break | b | 設(shè)置斷點(diǎn) |
cintinue | c | 繼續(xù)執(zhí)行程序 |
list | l | 查看當(dāng)前行的代碼段 |
step | s | 進(jìn)入函數(shù) |
return | r | 執(zhí)行代碼直到從當(dāng)前函數(shù)返回 |
quit | q | 終止并退出 |
next | n | 執(zhí)行下一行 |
p | 打印變量的值 | |
help | h | 幫助 |
args | a | 參看傳入?yún)?shù) |
回車(chē) | 重復(fù)上一條命令 | |
break | b | 顯示所有斷點(diǎn) |
break lineno | b lineno | 在指定行設(shè)置斷點(diǎn) |
break file:lineno | b file:lineno | 在指定文件的行設(shè)置斷點(diǎn) |
clear num | 刪除指定斷點(diǎn) | |
bt | 查看函數(shù)調(diào)用棧幀 |
演示
-
執(zhí)行時(shí)調(diào)試
python -m pdb 'file_name(.py)'
說(shuō)明:pdb 是模塊,測(cè)試代碼:
def sum(a, b): result = a + b print('result=%d'%result) a = 100 b = 200 c = a + b sum(a, b) print(c) print('hello world') print('hello python')
-
正常執(zhí)行:命令
python 'file_name(.py)'
PS C:\Users\SS沈\Desktop> python .\test01.py result=300 300 hello world hello python
-
調(diào)試執(zhí)行并顯示執(zhí)行代碼(每次顯示11行汪榔,本人理解柔纵,顯示多了阐枣,反而不容易查看):命令
python -m pdb 'file_name(.py)'
:調(diào)試佑钾、命令l
:查看當(dāng)前執(zhí)行代碼PS C:\Users\SS沈\Desktop> python -m pdb .\test01.py > c:\users\ss沈\desktop\test01.py(1)<module>() -> def sum(a, b): # 表示下一步要執(zhí)行的代碼行 (Pdb) l 1 -> def sum(a, b): 2 result = a + b 3 print('result=%d'%result) 4 5 a = 100 6 b = 200 7 c = a + b 8 9 sum(a, b) 10 print(c) 11 (Pdb) l #命令 l (大寫(xiě)L)是顯示代碼命令 12 print('hello world') 13 14 print('hello python') [EOF] # 表示顯示全部代碼
-
向下執(zhí)行一行代碼:命令
n
(Pdb) n > c:\users\ss沈\desktop\test01.py(5)<module>() -> a = 100 # -> 代表下一步要執(zhí)行的代碼 (Pdb) l 1 def sum(a, b): 2 result = a + b 3 print('result=%d'%result) 4 5 -> a = 100 6 b = 200 7 c = a + b 8 9 sum(a, b) 10 print(c) 11 (Pdb)
-
繼續(xù)執(zhí)行代碼(就像沒(méi)有使用pdb調(diào)試一樣):命令
c
(Pdb) c result=300 300 hello world hello python # 將程序執(zhí)行完畢 The program finished and will be restarted # 程序執(zhí)行完畢悠鞍,將重新執(zhí)行 > c:\users\ss沈\desktop\test01.py(1)<module>() -> def sum(a, b): # -》 表示下一步要執(zhí)行的代碼 (Pdb)
-
添加斷點(diǎn)(執(zhí)行到指定行數(shù)停止):命令
b number_line
The program finished and will be restarted > c:\users\ss沈\desktop\test01.py(1)<module>() -> def sum(a, b): (Pdb) b 7 # 添加一個(gè)斷點(diǎn)(在7行處) Breakpoint 1 at c:\users\ss沈\desktop\test01.py:7 (Pdb) c # 使用 c 執(zhí)行程序 > c:\users\ss沈\desktop\test01.py(7)<module>() -> c = a + b (Pdb) l # 顯示當(dāng)前執(zhí)行代碼 2 result = a + b 3 print('result=%d'%result) 4 5 a = 100 6 b = 200 7 B-> c = a + b #程序執(zhí)行到 7 行 停止(添加斷點(diǎn)的意義) 8 9 sum(a, b) 10 print(c) 11 12 print('hello world') (Pdb)
說(shuō)明:添加斷點(diǎn)的意義弓坞,就是想針對(duì)查看某行的代碼執(zhí)行過(guò)程
-
查看斷定:命令
b
(Pdb) b # 查看斷點(diǎn) Num Type Disp Enb Where 1 breakpoint keep yes at c:\users\ss沈\desktop\test01.py:7 # 第一個(gè)斷點(diǎn)是‘文件’的第7行 breakpoint already hit 1 time (Pdb) b 9 # 將第9行添加一個(gè)斷點(diǎn) Breakpoint 2 at c:\users\ss沈\desktop\test01.py:9 (Pdb) c # 執(zhí)行 > c:\users\ss沈\desktop\test01.py(9)<module>() -> sum(a, b) (Pdb) l # 顯示當(dāng)前執(zhí)行代碼 4 5 a = 100 6 b = 200 7 B c = a + b 8 9 B-> sum(a, b) # -> 表示下一步執(zhí)行的代碼 10 print(c) 11 12 print('hello world') 13 14 print('hello python') (Pdb) b Num Type Disp Enb Where 1 breakpoint keep yes at c:\users\ss沈\desktop\test01.py:7 breakpoint already hit 1 time 2 breakpoint keep yes at c:\users\ss沈\desktop\test01.py:9 # 第二個(gè)斷點(diǎn)是第9行 breakpoint already hit 1 time (Pdb)
-
清除斷點(diǎn):命令:
clear num
(Pdb) b Num Type Disp Enb Where 1 breakpoint keep yes at c:\users\ss沈\desktop\test01.py:7 breakpoint already hit 1 time 2 breakpoint keep yes at c:\users\ss沈\desktop\test01.py:9 breakpoint already hit 1 time (Pdb) clear 1 # 清除斷點(diǎn) 1 Deleted breakpoint 1 at c:\users\ss沈\desktop\test01.py:7 (Pdb) b # 顯示斷點(diǎn) Num Type Disp Enb Where 2 breakpoint keep yes at c:\users\ss沈\desktop\test01.py:9 # 斷點(diǎn) 1 被刪除 breakpoint already hit 1 time (Pdb)
-
進(jìn)入函數(shù)執(zhí)行(在使用命令:
n
時(shí),調(diào)用函數(shù)是當(dāng)作一行語(yǔ)句來(lái)執(zhí)行的):命令s
(Pdb) b # 查看斷點(diǎn) Num Type Disp Enb Where 1 breakpoint keep yes at c:\users\ss沈\desktop\test01.py:7 2 breakpoint keep yes at c:\users\ss沈\desktop\test01.py:9 (Pdb) clear 1 # 清除斷點(diǎn) 1 Deleted breakpoint 1 at c:\users\ss沈\desktop\test01.py:7 (Pdb) c # 執(zhí)行程序拉宗,沒(méi)有斷點(diǎn)一次性執(zhí)行完畢峦树,如有斷點(diǎn),再斷點(diǎn)處停止 > c:\users\ss沈\desktop\test01.py(9)<module>() -> sum(a, b) # 當(dāng)前下一步要執(zhí)行的程序 (Pdb) l # 查看當(dāng)前執(zhí)行代碼 4 5 a = 100 6 b = 200 7 c = a + b 8 9 B-> sum(a, b) # 斷點(diǎn) -> 表示在此斷點(diǎn)停止執(zhí)行 10 print(c) 11 12 print('hello world') 13 14 print('hello python') (Pdb) s # 進(jìn)入函數(shù)執(zhí)行代碼 --Call-- # 調(diào)用函數(shù) > c:\users\ss沈\desktop\test01.py(1)sum() -> def sum(a, b): # 回到函數(shù)定義語(yǔ)句旦事,可直觀查看函數(shù)執(zhí)行 (Pdb) l 1 -> def sum(a, b): 2 result = a + b 3 print('result=%d'%result) 4 5 a = 100 6 b = 200 7 c = a + b 8 9 B sum(a, b) 10 print(c) 11 (Pdb) p a # 查看傳入函數(shù)的參數(shù):查看參數(shù) a (print 打涌) 100 (Pdb) p b # 查看參數(shù) b 200 (Pdb) a # 查看所有參數(shù)(args) a = 100 b = 200 (Pdb)
-
進(jìn)入函數(shù)的執(zhí)行步驟:
(Pdb) n # n 的作用沒(méi)有改變,每次執(zhí)行一行 > c:\users\ss沈\desktop\test01.py(3)sum() -> print('result=%d'%result) # 當(dāng)前下一步要執(zhí)行的代碼 (Pdb) l 1 def sum(a, b): 2 result = a + b 3 -> print('result=%d'%result) # 執(zhí)行下一步要執(zhí)行的代碼 4 5 a = 100 6 b = 200 7 c = a + b 8 9 B sum(a, b) 10 print(c) 11 (Pdb) p result # 打印參數(shù) result 300 (Pdb) n result=300 --Return-- # 表示函數(shù)執(zhí)行結(jié)束姐浮,有返回值就返回 > c:\users\ss沈\desktop\test01.py(3)sum()->None -> print('result=%d'%result) # 沒(méi)有仔細(xì)測(cè)試谷遂,感覺(jué)就是函數(shù)的最后一行 (Pdb) n > c:\users\ss沈\desktop\test01.py(10)<module>() -> print(c) # 下一步要執(zhí)行的代碼 (Pdb) l 5 a = 100 6 b = 200 7 c = a + b 8 9 B sum(a, b) 10 -> print(c) 11 12 print('hello world') 13 14 print('hello python') [EOF] (Pdb)
-
快速執(zhí)行到函數(shù)最后一行:命令
r
(Pdb) s # 進(jìn)入函數(shù)執(zhí)行命令 --Call-- > c:\users\ss沈\desktop\test01.py(1)sum() -> def sum(a, b): (Pdb) l 1 -> def sum(a, b): 2 result = a + b 3 print('result=%d'%result) 4 5 a = 100 6 b = 200 7 c = a + b 8 9 B sum(a, b) 10 print(c) 11 (Pdb) r # 快速執(zhí)行導(dǎo)函數(shù)最后一行 result=300 --Return-- # 表示函數(shù)執(zhí)行結(jié)束 > c:\users\ss沈\desktop\test01.py(3)sum()->None -> print('result=%d'%result) (Pdb) n > c:\users\ss沈\desktop\test01.py(10)<module>() -> print(c) (Pdb)
-
退出調(diào)試:命令
q
(Pdb) q PS C:\Users\SS沈\Desktop>
-
交互式調(diào)用
-
進(jìn)入python或ipython解釋器
>>> def sum(a, b): result = a + b print('result=%d'%result) return result >>> import pdb # 導(dǎo)入pdb模塊 >>> pdb.run('sum(3, 5)') # 調(diào)試函數(shù),參數(shù)賦值 > <string>(1)<module>() (Pdb) s # 使用 s 待用函數(shù) --Call-- > <pyshell#12>(1)sum() (Pdb)
說(shuō)明:命令與上面相同卖鲤,本人理解就是一個(gè)函數(shù)的調(diào)試肾扰,完全可以使用上面進(jìn)行調(diào)試
-
-
程序里埋點(diǎn)
說(shuō)明:導(dǎo)入
import pdb
在斷點(diǎn)處寫(xiě)入 pdb.set_track() 。在sum(a, b)
處加入斷點(diǎn)蛋逾,程序遇到pdb.set_trace
就進(jìn)入調(diào)試模式import pdb def sum(a, b): result = a + b print('result=%d'%result) a = 100 b = 200 c = a + b # 添加斷點(diǎn) pdb.set_trace() sum(a, b) print(c) print('hello world') print('hello python')
-
演示
> c:\users\ss沈\desktop\ten\爬蟲(chóng)\test02.py(15)<module>() -> sum(a, b) (Pdb) l 10 c = a + b 11 12 # 添加斷點(diǎn) 13 pdb.set_trace() 14 15 -> sum(a, b) 16 print(c) 17 18 print('hello world') 19 20 print('hello python') (Pdb) s --Call-- > c:\users\ss沈\desktop\ten\爬蟲(chóng)\test02.py(4)sum() -> def sum(a, b): (Pdb)
說(shuō)明:其他命令和上面一樣
-
-
日志調(diào)試
待續(xù)-----------