Using the PDB library debugger in Python _python

Source: Internet
Author: User
Tags stack trace

Python's own PDB library found it handy to debug a program with a PDB, and of course, what remote debugging, multithreading, and so on, the PDB is uncertain.

There are several options to use PDB debugging:

1. Command line to start the target program, plus the-m parameter, so call myscript.py Word breakpoint is the execution of the program before the first line

Copy Code code as follows:

PYTHON-M PDB myscript.py

2. Enable debugging in a python interactive environment
Copy Code code as follows:

>>> Import PDB
>>> Import MyModule
>>> Pdb.run (' mymodule.test () ')

3. More commonly used is to insert a program in the middle of the program, as opposed to a breakpoint in the General IDE and then start debug, but this way is hardcode

Copy Code code as follows:

if __name__ = = "__main__":
A = 1
Import PDB
Pdb.set_trace ()
b = 2
c = A + b
Print (c)

Then run the script normally, and by the Pdb.set_trace (), you'll see the debugger's prompt (PDB).

Common Debugging commands

H (ELP), print the current version of the PDB available commands, if you want to query a command, you can enter H [command], such as: "H L"-View List command
L (IST), which lists the code blocks that are currently going to be run

Copy Code code as follows:

(PDB) L
497 Pdb.set_trace ()
498 Base_data = {}
499 new_data = {}
Try:
501 execfile (base_file_name,{},base_data)
502-> execfile (new_file_name,{},new_data)
503 except:
504 Logger.writelog ("error! Load result log error! ")
505 print "Load CMP logs error!"
506 Raise Exception, "load CMP logs error!"
40V

B (reak), set breakpoints, such as "B 77″, is the current script in the 77 lines of the breakpoint, can also enter the function name as a parameter, the breakpoint hit the specific function entry, if only B, will show all existing breakpoints

Copy Code code as follows:

(PDB) B 504
Breakpoint 4 at/home/jchen/regression/regressionlogcmp.py:504

Condition Bpnumber [Condition], set a conditional breakpoint, the following statement is the 4th breakpoint plus the condition "a==3"
(Pdb) Condition 4 a==3
(PDB) b
Num Type Disp Enb Where
4 Breakpoint Keep Yes at/home/jchen/regression/regressionlogcmp.py:504
Stop only if a==3


CL (ear) that, if followed by an argument, clears the specified breakpoint (I've never succeeded in Python2.4!!! If you remove all breakpoints without parameters
Copy Code code as follows:

(PDB) CL
Clear all breaks? Y

Disable/enable, disabling/activating breakpoints
Copy Code code as follows:

(Pdb) Disable 3
(PDB) b
Num Type Disp Enb Where
3 Breakpoint Keep No at/home/jchen/regression/regressionlogcmp.py:505

N (EXT), let the program run the next line, if the current statement has a function call, with n is not going into the body of the called function

S (TEP), similar to n, but if there is currently a function call, then S will go into the called function body
C (ont (inue)) to allow the program to run normally until a breakpoint is encountered
J (UMP), let the program jump to the specified number of lines

Copy Code code as follows:

(PDB) J 497
>/home/jchen/regression/regressionlogcmp.py (497) Comparelog ()
-> Pdb.set_trace ()

A (RGS), print the parameters of the current function
Copy Code code as follows:

(PDB) A
_logger =
_base =./base/mrm-8137.log
_new =./new/mrm-8137.log
_caseid = 5550001
_tostepnum = 10
_cmpmap = {' _bcmpbinarylog ': ' true ', ' _bcmplog ': ' true ', ' _bcmpresp ': ' True '}

P, one of the most useful commands to print a variable
Copy Code code as follows:

(PDB) P _new
U './new/mrm-8137.log '

! , the exclamation point followed by the statement, you can directly change a variable
Q (uit), exit debug
It is also interesting to find that debugging a program at the command line, record it and share it.

W, Print a stack trace, with the most recent frame at the bottom. An arrow indicates the ' Current frame ', which determines the context of most commands. ' BT ' is a alias for this command.

D, move the "current" frame one level down in the stack trace
(to a newer frame).

U, move the "current" frame one level up in the stack trace
(to a older frame).

Using the U and D commands, we can switch between stack frames to get information about their related context variables. W can display some of the most recent stack frame information.

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.