python的斷點調試

python的斷點調試(轉自:http://control.blog.sina.com.cn/admin/article/article_add.php)

pdb這個功能好牛啊,能夠在命令行設置也能夠在程序裏用語句設置斷點 命令行:$ python -m pdb 1.py 程序裏: 引入pdb包 import pdb def main(): i, sum = 1, 0 for i in xrange(100): sum = sum + i pdb.set_trace() print sum if __name__ == '__main__': main() 命令 | 用途 break 或 b 設置斷點 continue 或 c 繼續執行程序 list 或 l 查看當前行的代碼段 step 或 s 進入函數 return 或 r 執行代碼直到從當前函數返回 exit 或 q 停止並退出 next 或 n 執行下一行 pp 打印變量的值 help 幫助
首先你選擇運行的 py
python -m pdb myscript.py
(Pdb) 會自動停在第一行,等待調試,這時你能夠看看 幫助
(Pdb) h
    說明下這幾個關鍵 命令>斷點設置 
   (Pdb)b  10 #斷點設置在本py的第10行
   或(Pdb)b  ots.py:20 #斷點設置到 ots.py第20行
   刪除斷點(Pdb)b #查看斷點編號
            (Pdb)cl 2 #刪除第2個斷點
    
>運行
    (Pdb)n #單步運行
    (Pdb)s #細點運行 也就是會下到,方法
    (Pdb)c #跳到下個斷點
>查看
    (Pdb)p param #查看當前 變量值
    (Pdb)l #查看運行到某處代碼
    (Pdb)a #查看所有棧內變量>若是是在 命令行裏的調試爲:
import pdb
def tt():
    pdb.set_trace()
    for i in range(1, 5):
        print i>>> tt()
#這裏支持 n p c 而已
> <stdin>(3)tt()
(Pdb) n
--------------------------------------------------------
附一些有用的調試命令:
w(here) 顯式當前堆棧結構。往下的是新的,就像X86構架中的那樣。
d(own) 移向新的一幀
u(p) 移向舊的一幀
b(reak) [([filename:]lineno | function) [, condition] ]
若是沒有指定文件名則使用當前文件
condition是一個字符串,必須等價於 true
The condition argument, if present, is a string which must
evaluate to true in order for the breakpoint to be honored.
tbreak [([filename:]lineno | function) [, condition] ]
臨時的breakpoint
cl(ear) [bpnumber [bpnumber ...]]
空格進行分割,清除這些斷點
disable bpnumber [bpnumber ...]
disable 斷點,能夠enable以後
ignore bpnumber count
設置某個斷點的count,當count爲0的時候斷點狀態爲active,count不爲0的時候每一次進入斷點時候count自減
condition bpnumber condition
s(tep) 單步執行,步入
n(ext) 單步執行,步過函數
c(ont(inue)) 執行直到斷點
l(ist) [first [,last]]
列出11行附近的代碼
a(rgs)
打印出當前函數的參數
p expression
答應表達式的值
(!) statement
執行statement
whatis arg
答應 arg 的類型
q(uit) 
相關文章
相關標籤/搜索