Debugging a Python program with the PDB library

Source: Internet
Author: User
Tags stack trace

Python's own PDB library, found to use the PDB to debug the program is still very convenient, of course, what remote debugging, multithreading and so on, the PDB is uncertain.

There are several ways to debug with PDB:

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

2. Enabling debugging in a python interactive environment
>>> Import PDB
>>> Import MyModule
>>> Pdb.run (' mymodule.test () ')

3. More commonly used is to insert a program in the middle of the program, relative to the general IDE inside the breakpoint and then start Debug, but this way is hardcode

if __name__ = = "__main__":
A = 1
Import PDB
Pdb.set_trace ()
b = 2
c = A + b
Print (c)
Then run the script normally, to Pdb.set_trace () then it will be fixed and you will see the debug prompt (Pdb).

Common Debug Commands

H (ELP), the commands available for the current version of the PDB are printed, and if you want to query a command, you can enter H [command], for example: "H L"-View the List command
L (IST) to list the blocks of code that are currently going to be run
(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!"
507

B (reak), set breakpoints, such as "B 77″, that is, in the current script 77 lines of the breakpoint, you can also enter the function name as a parameter, the breakpoint will hit the specific function entrance, if only knock B, will show all the existing breakpoints
(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), if followed by a parameter, clears the specified breakpoint (I never succeeded on Python2.4!!!). ), or clear all breakpoints without a parameter
(PDB) CL
Clear all breaks? Y

Disable/enable, disabling/activating breakpoints
(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, n is not entered in the called function body

S (TEP), similar to n, but if there is currently a function call, then s will enter the body of the called function
C (ont (inue)) to allow the program to function until a breakpoint is encountered
J (UMP), let the program jump to the specified number of rows
(PDB) J 497
>/home/jchen/regression/regressionlogcmp.py (497) Comparelog ()
Pdb.set_trace ()

A (RGS), prints the parameters of the current function
(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
(PDB) P _new
U './new/mrm-8137.log '

! , the exclamation mark followed by the statement, you can directly change a variable
Q (uit), exit debug
Find it interesting to debug 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 the 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 an older frame).

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

Debugging a Python program with the PDB library

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.