python pdb調試方法分享

來源:互聯網
上載者:User
代碼如下:


import pdb

def pdb_test(arg):
for i in range(arg):
print(i)
return arg

pdb.run("pdb_test(3)")

b 函數名、行號:

打斷點,b可以查詢所有的斷點。

代碼如下:


(Pdb) b pdb_test
Breakpoint 1 at c:\users\plpcc\desktop\pdbtest.py:3
(Pdb) b
Num Type Disp Enb Where
breakpoint keep yes at c:\users\plpcc\desktop\pdbtest.py:3

c:

運行程式,直到遇到斷點。

代碼如下:


(Pdb) c
> c:\users\plpcc\desktop\pdbtest.py(4)pdb_test()
-> for i in range(arg):

l:

查看斷點周圍的代碼

代碼如下:


(Pdb) l
import pdb

B def pdb_test(arg):
-> for i in range(arg):
print(i)
return arg

pdb.run("pdb_test(3)")

a:

查看參數

代碼如下:


(Pdb) a
arg = 3

s, n:


單步運行,區別s會進入路徑中的函數,n不會進入

p:

查看錶達式的值

代碼如下:


(Pdb) p i

condition:

條件斷點,只有條件為true斷點才命中

代碼如下:


> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i)
(Pdb) l
import pdb

def pdb_test(arg):
for i in range(arg):
B-> print(i)
return arg

pdb.run("pdb_test(3)")
[EOF]
(Pdb) b
Num Type Disp Enb Where
breakpoint keep yes at c:\users\plpcc\desktop\pdbtest.py:5
(Pdb) condition 2 i==1 //i==1時才觸發斷點2
New condition set for breakpoint 2.
(Pdb) b
Num Type Disp Enb Where
breakpoint keep yes at c:\users\plpcc\desktop\pdbtest.py:5
stop only if i==1
(Pdb) c
//i==0直接列印未斷住
> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i) //觸發斷點,i==1
(Pdb) p i

bt:

查看呼叫堆疊

代碼如下:


(Pdb) bt
c:\python33\lib\bdb.py(405)run()
-> exec(cmd, globals, locals)
(1)()
> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i)
r:


執行到函數返回

代碼如下:


(Pdb) r

--Return--
> c:\users\plpcc\desktop\pdbtest.py(6)pdb_test()->3 //代碼位置、函數傳回值->3
-> return arg //代碼位置的語句
(Pdb) l
import pdb

def pdb_test(arg):
for i in range(arg):
print(i)
-> return arg

pdb.run("pdb_test(3)")

通過pdb.set_trace() 在代碼中指定位置嵌入一個斷點,通常可以通過調試開關來控制

代碼如下:


import pdb

__DEBUG__ = True

def pdb_test(arg):
if True == __DEBUG__:
pdb.set_trace()
for i in range(arg):
print(i)
return arg

pdb_test(3)

運行後在pdb.set_trace()位置被斷住,當__DEBUG__ = False,代碼正常運行

代碼如下:


> c:\users\plpcc\desktop\pdbtest.py(8)pdb_test()
-> for i in range(arg):
(Pdb) l
__DEBUG__ = True

def pdb_test(arg):
if True == __DEBUG__:
pdb.set_trace()
-> for i in range(arg):
print(i)
return arg

pdb_test(3)
[EOF]

通過pdb.pm()進行事後調試,可以跟蹤異常程式最後的堆載資訊:

代碼如下:


Traceback (most recent call last):
File "C:\Users\plpcc\Desktop\pdbTest.py", line 13, in
pdb_test(3)
File "C:\Users\plpcc\Desktop\pdbTest.py", line 10, in pdb_test
1/0
ZeroDivisionError: division by zero
>>> import pdb
>>> pdb.pm()
> c:\users\plpcc\desktop\pdbtest.py(10)pdb_test()
-> 1/0
(Pdb) l
def pdb_test(arg):
if True == __DEBUG__:
pdb.set_trace()
for i in range(arg):
print(i)
-> 1/0
return arg

pdb_test(3)

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.