Custom Search

Thursday, April 10, 2014

How to debug Python Application with pdb breakpoint

1)
* Add following code in the application

import pdb
pdb.set_trace()


2)
* Run the application.
* Then you can see a prompt like

(Pdb)

3)
* Type "l" and Press "Enter" to see where you are

(Pdb) l



4)
* Type "p name_of_the_variable" to see the value of a variable

(Pdb) p variabl1
(Pdb) p variabl2

or
(Pdb) pp variabl3

5)
* Press "Enter" to repeate the last command

(Pdb)

6)
* Type "n" and Press "Enter" to execute next statement.
* Type "l" and Press "Enter" to see where yyou are

(Pdb) n
(Pdb) l


7)
* Type "s" and Press "Enter" to Stepping into subroutines/method/function.
* Type "r" and Press "Enter" to goto the end of the current subroutine/method
/function.
(Pdb) s
(Pdb) r


8)
* Type "c" and Press "Enter" to let the program continue running and exit from pdb.

(Pdb) c

9)
* Type "a" and Press "Enter" to Print the argument list of the current function.

(Pdb) a

10)
Very useful

* Type "w" and Press "Enter" to 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.
* You can use this with "d" and "u".

11)
* Type "d" and Press "Enter" to Move the current frame one level down in the stack trace (to a newer frame).
* You can use this with "w".

(Pdb) d

12)
* Type "u" and Press "Enter" to Move the current frame one level up in the stack trace (to an older frame).
* You can use this with "u".

(Pdb) u

13)
Set Break point on file "utils.py" and line number 66.

(Pdb) b heatclient/common/utils.py:66
Continue the execution upto the break point
(Pdb) c
(Pdb) w


or

Set Break point on first statement of a function named "print_list".
(Pdb) b utils.print_list
Continue the execution upto the break point
(Pdb) c
(Pdb) w


or


(Pdb) import heatclient
Set Break point on first statement of a class method named "json_request".
(Pdb) b heatclient.common.http.HTTPClient.json_request
Continue the execution upto the break point
(Pdb) c
(Pdb) w

https://github.com/openstack/python-heatclient/blob/master/heatclient/common/http.py

or

(Pdb) import httplib
(Pdb) b httplib.HTTPConnection.getresponse
(Pdb) c
(Pdb) w
/usr/lib/python2.7/httplib.py


14)
* "tbreak" is Temporary breakpoint, which is removed automatically when it is first hit. The arguments are the same as break.
Set Break point on file "utils.py" and line number 66.
(Pdb) tbreak heatclient/common/utils.py:66
Continue the execution upto the break point
(Pdb) c

1 comment: