轉(zhuǎn)自IBM開發(fā)者社區(qū)簇秒。
使用pdb進行調(diào)試:
pdb 是 python 自帶的一個包鱼喉,為 python 程序提供了一種交互的源代碼調(diào)試功能,主要特性包括設(shè)置斷點趋观、單步調(diào)試扛禽、進入函數(shù)調(diào)試、查看當前代碼皱坛、查看棧片段编曼、動態(tài)改變變量的值等。pdb 提供了一些常用的調(diào)試命令剩辟,詳情見表 1掐场。
- 表 1. pdb 常用命令
命令 | 解釋 |
---|---|
break 或 b 設(shè)置斷點 | 設(shè)置斷點 |
continue 或 c | 繼續(xù)執(zhí)行程序 |
list 或 l | 查看當前行的代碼段 |
step 或 s | 進入函數(shù) |
return 或 r | 執(zhí)行代碼直到從當前函數(shù)返回 |
exit 或 q | 中止并退出 |
next 或 n | 執(zhí)行下一行 |
pp | 打印變量的值 |
help | 幫助 |
下面結(jié)合具體的實例講述如何使用 pdb 進行調(diào)試往扔。
清單 1. 測試代碼示例
import pdb
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = a + b + c
print final
開始調(diào)試:直接運行腳本,會停留在 pdb.set_trace() 處刻肄,選擇 n+enter 可以執(zhí)行當前的 statement瓤球。在第一次按下了 n+enter 之后可以直接按 enter 表示重復執(zhí)行上一條 debug 命令。
清單 2. 利用 pdb 調(diào)試
[root@rcc-pok-idg-2255 ~]# python epdb1.py
> /root/epdb1.py(4)?()
-> b = "bbb"
(Pdb) n
> /root/epdb1.py(5)?()
-> c = "ccc"
(Pdb)
> /root/epdb1.py(6)?()
-> final = a + b + c
(Pdb) list
1 import pdb
2 a = "aaa"
3 pdb.set_trace()
4 b = "bbb"
5 c = "ccc"
6 -> final = a + b + c
7 print final
[EOF]
(Pdb)
[EOF]
(Pdb) n
> /root/epdb1.py(7)?()
-> print final
(Pdb)
退出 debug:使用 quit 或者 q 可以退出當前的 debug敏弃,但是 quit 會以一種非常粗魯?shù)姆绞酵顺龀绦蜇韵郏浣Y(jié)果是直接 crash。
清單 3. 退出 debug
[root@rcc-pok-idg-2255 ~]# python epdb1.py
> /root/epdb1.py(4)?()
-> b = "bbb"
(Pdb) n
> /root/epdb1.py(5)?()
-> c = "ccc"
(Pdb) q
Traceback (most recent call last):
File "epdb1.py", line 5, in ?
c = "ccc"
File "epdb1.py", line 5, in ?
c = "ccc"
File "/usr/lib64/python2.4/bdb.py", line 48, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib64/python2.4/bdb.py", line 67, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
打印變量的值:如果需要在調(diào)試過程中打印變量的值麦到,可以直接使用 p 加上變量名绿饵,但是需要注意的是打印僅僅在當前的 statement 已經(jīng)被執(zhí)行了之后才能看到具體的值,否則會報 NameError: < exceptions.NameError … ....> 錯誤瓶颠。
清單 4. debug 過程中打印變量
[root@rcc-pok-idg-2255 ~]# python epdb1.py
> /root/epdb1.py(4)?()
-> b = "bbb"
(Pdb) n
> /root/epdb1.py(5)?()
-> c = "ccc"
(Pdb) p b
'bbb'
(Pdb)
'bbb'
(Pdb) n
> /root/epdb1.py(6)?()
-> final = a + b + c
(Pdb) p c
'ccc'
(Pdb) p final
*** NameError: <exceptions.NameError instance at 0x1551b710 >
(Pdb) n
> /root/epdb1.py(7)?()
-> print final
(Pdb) p final
'aaabbbccc'
(Pdb)
使用 c 可以停止當前的 debug 使程序繼續(xù)執(zhí)行拟赊。如果在下面的程序中繼續(xù)有 set_statement() 的申明,則又會重新進入到 debug 的狀態(tài)粹淋,讀者可以在代碼 print final 之前再加上 set_trace() 驗證吸祟。
清單 5. 停止 debug 繼續(xù)執(zhí)行程序
[root@rcc-pok-idg-2255 ~]# python epdb1.py
> /root/epdb1.py(4)?()
-> b = "bbb"
(Pdb) n
> /root/epdb1.py(5)?()
-> c = "ccc"
(Pdb) c
aaabbbccc
顯示代碼:在 debug 的時候不一定能記住當前的代碼塊,如要要查看具體的代碼塊桃移,則可以通過使用 list 或者 l 命令顯示屋匕。list 會用箭頭 -> 指向當前 debug 的語句。
清單 6. debug 過程中顯示代碼
[root@rcc-pok-idg-2255 ~]# python epdb1.py
> /root/epdb1.py(4)?()
-> b = "bbb"
(Pdb) list
1 import pdb
2 a = "aaa"
3 pdb.set_trace()
4 -> b = "bbb"
5 c = "ccc"
6 final = a + b + c
7 pdb.set_trace()
8 print final
[EOF]
(Pdb) c
> /root/epdb1.py(8)?()
-> print final
(Pdb) list
3 pdb.set_trace()
4 b = "bbb"
5 c = "ccc"
6 final = a + b + c
7 pdb.set_trace()
8 -> print final
[EOF]
(Pdb)
在使用函數(shù)的情況下進行 debug
清單 7. 使用函數(shù)的例子
import pdb
def combine(s1,s2): # define subroutine combine, which...
s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ...
s3 = '"' + s3 +'"' # encloses it in double quotes,...
return s3 # and returns it.
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = combine(a,b)
print final
如果直接使用 n 進行 debug 則到 final=combine(a,b) 這句的時候會將其當做普通的賦值語句處理借杰,進入到 print final过吻。如果想要對函數(shù)進行 debug 如何處理呢 ? 可以直接使用 s 進入函數(shù)塊。函數(shù)里面的單步調(diào)試與上面的介紹類似蔗衡。如果不想在函數(shù)里單步調(diào)試可以在斷點處直接按 r 退出到調(diào)用的地方纤虽。
對函數(shù)進行 debug
[root@rcc-pok-idg-2255 ~]# python epdb2.py
> /root/epdb2.py(10)?()
-> b = "bbb"
(Pdb) n
> /root/epdb2.py(11)?()
-> c = "ccc"
(Pdb) n
> /root/epdb2.py(12)?()
-> final = combine(a,b)
(Pdb) s
--Call--
> /root/epdb2.py(3)combine()
-> def combine(s1,s2): # define subroutine combine, which...
(Pdb) n
> /root/epdb2.py(4)combine()
-> s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ...
(Pdb) list
1 import pdb
2
3 def combine(s1,s2): # define subroutine combine, which...
4 -> s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ...
5 s3 = '"' + s3 +'"' # encloses it in double quotes,...
6 return s3 # and returns it.
7
8 a = "aaa"
9 pdb.set_trace()
10 b = "bbb"
11 c = "ccc"
(Pdb) n
> /root/epdb2.py(5)combine()
-> s3 = '"' + s3 +'"' # encloses it in double quotes,...
(Pdb) n
> /root/epdb2.py(6)combine()
-> return s3 # and returns it.
(Pdb) n
--Return--
> /root/epdb2.py(6)combine()->'"aaabbbaaa"'
-> return s3 # and returns it.
(Pdb) n
> /root/epdb2.py(13)?()
-> print final
(Pdb)
在調(diào)試的時候動態(tài)改變值 。在調(diào)試的時候可以動態(tài)改變變量的值绞惦,具體如下實例逼纸。需要注意的是下面有個錯誤,原因是 b 已經(jīng)被賦值了济蝉,如果想重新改變 b 的賦值樊展,則應(yīng)該使用! B堆生。
清單 9. 在調(diào)試的時候動態(tài)改變值
[root@rcc-pok-idg-2255 ~]# python epdb2.py
> /root/epdb2.py(10)?()
-> b = "bbb"
(Pdb) var = "1234"
(Pdb) b = "avfe"
*** The specified object '= "avfe"' is not a function
or was not found along sys.path.
(Pdb) !b="afdfd"
(Pdb)