Python PDB debugging method sharing

Source: Internet
Author: User
The code is as follows:


Import PDB

def pdb_test (ARG):
For I in range (ARG):
Print (i)
return arg

Pdb.run ("Pdb_test (3)")

b function name, line number:

Break point, B can query all breakpoints.

The code is as follows:


(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:

Run the program until a breakpoint is encountered.

The code is as follows:


(PDB) C
> c:\users\plpcc\desktop\pdbtest.py (4) pdb_test ()
-I in Range (ARG):

L

To view the code around a breakpoint

The code is as follows:


(PDB) L
Import PDB

B def pdb_test (ARG):
-I in Range (ARG):
Print (i)
return arg

Pdb.run ("Pdb_test (3)")

A:

View parameters

The code is as follows:


(PDB) A
arg = 3

S, N:


Single-step operation, the difference s will enter the function in the path, n will not enter

P:

View the value of an expression

The code is as follows:


(PDB) P I

Condition

Conditional breakpoint, only if the condition is true breakpoint is hit

The code is as follows:


> 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 trigger Breakpoint 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 Direct Printing not broken
> c:\users\plpcc\desktop\pdbtest.py (5) pdb_test ()
Print (i)//trigger breakpoint, I==1
(PDB) P I

Bt:

Viewing the call stack

The code is as follows:


(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:


Execute to function return

The code is as follows:


(PDB) R

--return--
> c:\users\plpcc\desktop\pdbtest.py (6) pdb_test ()->3//code location, function return value->3
return arg//Code location statement
(PDB) L
Import PDB

def pdb_test (ARG):
For I in range (ARG):
Print (i)
--Return ARG

Pdb.run ("Pdb_test (3)")

Embedding a breakpoint by Pdb.set_trace () at the specified position in the code can usually be controlled by a debug switch

The code is as follows:


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 () position is broken after run, when __debug__ = False, the code runs correctly

The code is as follows:


> c:\users\plpcc\desktop\pdbtest.py (8) pdb_test ()
-I in Range (ARG):
(PDB) L
__debug__ = True

def pdb_test (ARG):
if True = = __debug__:
Pdb.set_trace ()
-I in Range (ARG):
Print (i)
return arg

Pdb_test (3)
[EOF]

After debugging by PDB.PM (), you can trace the last heap information of the exception program:

The code is as follows:


Traceback (most recent):
File "C:\Users\plpcc\Desktop\pdbTest.py", line +, in
Pdb_test (3)
File "C:\Users\plpcc\Desktop\pdbTest.py", line ten, in Pdb_test
1/0
Zerodivisionerror:division by Zero
>>> Import PDB
>>> pdb.pm ()
> c:\users\plpcc\desktop\pdbtest.py (Ten) 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)

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.