Python--PDB debugging tool

Source: Internet
Author: User

Poetry is a melancholy medium, and the poet's mission is lonely;--North "The Rose of Time"

Learning is a deep ballad, and our task is to enjoy him. --Little Q "20161203"

------------------------------------------------------------------------------------------------

Learn C + +, the teacher taught us to have GDB debugging tools, in the work will often use;

When learning the shell, the manager let me see the "-X" Tracking debugging parameters, I use every day;

After learning Python, I was looking for similar parameters and tools, Google gave me the PDB tool;

The Introduction debugging Tool

Pdb
Using the following code is equivalent to adding a breakpoint:
Import PDB
Pdb.set_trace () #设置断点的地方, placed in the program

Ipdb
Compared to python, we tend to ipython, have beautiful colors, and <tab> hints, as well as bash mix;

The advantage of Python's built-in pdb,ipdb is that it is actually a call to Ipython:

Import ipdb
Ipdb.set_trace ()

Pudb
is a full-screen console-based visual debugger, a bit like the Turbo C style in C language

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/8B/05/wKioL1hCYYHRI1AAAACFZHN1Rc0206.png "title=" 4.png " Style= "width:700px;height:412px", "alt=" Wkiol1hcyyhri1aaaacfzhn1rc0206.png "width=" "vspace=" 0 "hspace=" 0 "Height = "412" border= "0"/>

In order to support PUDB, you need to insert in your code
from pudb import set_trace; Set_trace () or import pudb

Rpdb
The two scenarios above require a terminal output scenario, sometimes we need to execute Python in the background , there is no output interaction, such as Django Development, the program is executed by UWSGI management, standard output is redirected, usually only through the log output information. This time we need a remote debugging tool.
RPDB will open a socket connection for remote debugging, the default port is 4444:
Import rpdb
Rpdb.set_trace (port=12345)
This way, when the program is stuck, it listens to the port and can be remotely connected for debugging:
NC 127.0.0.1 12345

Ripdb
RPDB is just a remote version of the PDB, and ripdb is the integration of RPDB and IPDB functions, both remote debugging and beautiful code colors:
Import ripdb
Ripdb.set_trace (port=12345)
If you still need <Tab> auto-completion, you need to set up the terminal:
saved_stty= ' stty-g '; Stty-icanon-opost-echo-echoe-echok-echoctl-echoke; NC 127.0.0.1 12345; Stty $SAVED _stty

"Detailed pdb/ipdb"

Test procedure: Pass two parameters for addition and subtraction

Import sysdef Add (num1=0, num2=0): Return int (NUM1) + int (num2) def sub (num1=0, num2=0): Return int (NUM1)-Int (num2) def main (): print sys.argv addition = Add (Sys.argv[1], sys.argv[2]) print addition subtraction = sub (sys.argv[ 1], sys.argv[2]) print subtractionif __name__ = = ' __main__ ': Main ()

1, into the PDB debugging, is actually an interactive source code debugger; Modify the program:

Import PDB # Add module import sysdef Add (num1=0, num2=0): Return int (NUM1) + int (num2) def sub (num1=0, num2=0): Return in T (NUM1)-Int (NUM2) def main (): Print sys.argv pdb.set_trace () # <--break points added here, set breakpoints addition = a DD (sys.argv[1], sys.argv[2]) print addition subtraction = sub (sys.argv[1], sys.argv[2]) print subtractionif __nam e__ = = ' __main__ ': Main ()

2. Program execution Trigger Debugger
Execute: Python 3_pdb.py 1 3//program stops at the first breakpoint, as follows

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/8B/05/wKioL1hCYsmQWwQkAAAUkoyAtno306.png "title=" 1.png " alt= "Wkiol1hcysmqwwqkaaaukoyatno306.png"/>

At this point we can see that the program has a breakpoint at print SYS.ARGV

and shows that the next step will be performed addition = Add (Sys.argv[1], sys.argv[2])

3, next line, n
Enter "n" carriage return, will execute addition = Add (Sys.argv[1], sys.argv[2]), and then print out the next operation;
However, there is a problem that the PDB does not go into the Add function, and the following S option resolves the problem

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8B/08/wKiom1hCYzXSfvTeAAAhQqpbBnA422.png "title=" 2.png " alt= "Wkiom1hcyzxsfvteaaahqqpbbna422.png"/>

4, printing, p
In the course of execution we want to see that the printed value of a variable, except that C can jump directly to the next breakpoint, all values during printing
"P" can print out the value of a variable, but only if the variable has already been executed. As follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8B/08/wKiom1hCY2WCTYo_AAAm3dLANS0678.png "title=" 3.png " alt= "Wkiom1hcy2wctyo_aaam3dlans0678.png"/>

5, one step, s
"S" can go inside a function, then use n/p/b/c inside the function, etc.
"R" returns the return statement of the previous entry function

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/8B/05/wKioL1hCY4_R6jV6AAA6AukGwK0704.png "title=" 4.png " alt= "Wkiol1hcy4_r6jv6aaa6aukgwk0704.png"/>

6. Add Dynamic breakpoint, B
In the program, we set a breakpoint, but when we execute a very long code, we forget to set the breakpoint in the script.

We can just use "B" to set the next breakpoint position in this environment.
Format: line number B
7, List-L
Sometimes when debugging, do not know where to run, do not know what the following code is, in order not to go out to remember what a line, you can do lowercase "L" to view the following program

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/8B/08/wKiom1hCY-Din4hJAABvn5xA7H8570.png "title=" 5.png " Style= "width:600px;height:395px", "alt=" Wkiom1hcy-din4hjaabvn5xa7h8570.png "width=" "vspace=" 0 "hspace=" 0 "Height = "395" border= "0"/>

8. Dynamic allocation of variables
During debugging, variables can be assigned to help with debugging,
(PDB)!n=5
(PDB) P n
5

9, End-Q/exit
In the debugging process, you want to exit the end of debugging, you can directly run "Q" or "Exit" enter can be
--------------------------------------------------------------------------------------------------
PDB Document: https://docs.python.org/2/library/pdb.html
The ipdb usage and the PDB are similar, just more friendly and more intuitive, as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/8B/08/wKiom1hCZDGzIyVIAABRuVQqSMo534.png "title=" 6.png " alt= "Wkiom1hczdgziyviaabruvqqsmo534.png"/>




This article is from the "North Ice--q" blog, please be sure to keep this source http://beibing.blog.51cto.com/10693373/1879356

Python--PDB debugging tool

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.