Python Debugging With TotalView
Rogue Wave has added Python debugging capabilities in the TotalView debugger. More information about this can be found atwww.roguewave.com/products-services/features/mixed-language-debugging.
Python Script Debugging Using pdb
to use pdb as a stand-alone debugger, type:
python3 -m pdb myscript.py
Consult thePython 2.X Debugger Commands orPython 3.X Debugger Commands documentation.
Example Debugging Session
import os, sysimport stringdef plusAndSquare(a, b):res = 0res = (a + b)res = res * resprint '('+`a`+'+'+`b`+')^2 = ' + `res`def main():print "Enter the first number:"first = sys.stdin.readline()print "Enter the second number:"second = sys.stdin.readline()plusAndSquare(string.atoi(first),string.atoi(second))main()
example.py
This script asks for two numbers from the user then adds them and squares the result.
%python -m pdb example.py> /g/g2/dahn/DEBUG/PY/<string>(0)?()(Pdb) b main<- Set a breakpoint at main functionBreakpoint 1 at /g/g2/dahn/DEBUG/PY/example.py:17(Pdb) c<- continue> /g/g2/dahn/DEBUG/PY/<string>(1)?()(Pdb) c<- continue until we hit a breakpoint> /g/g2/dahn/DEBUG/PY/example.py(17)main()-> print "Enter the first number:"(Pdb) l<- list the source code showing where we are12 print '('+`a`+'+'+`b`+')^2 = ' + `res`131415 def main():1617 B-> print "Enter the first number:"18 first = sys.stdin.readline()19 print "Enter the second number:"20 second = sys.stdin.readline()21 plusAndSquare(string.atoi(first), string.atoi(second))22(Pdb) b plusAndSquare<- Set a breakpoint at another functionBreakpoint 2 at /g/g2/dahn/DEBUG/PY/example.py:7(Pdb) c<- continue until we hit a breakpointEnter the first number:4Enter the second number:7> /g/g2/dahn/DEBUG/PY/example.py(7)plusAndSquare()-> res = 0(Pdb) l<- list the source code showing where we are2 import os, sys3 import string45 def plusAndSquare(a, b):67 B-> res = 089 res = (a + b)10 res = res * res1112 print '('+`a`+'+'+`b`+')^2 = ' + `res`(Pdb) n<- next> /g/g2/dahn/DEBUG/PY/example.py(9)plusAndSquare()-> res = (a + b)(Pdb) n<- next> /g/g2/dahn/DEBUG/PY/example.py(10)plusAndSquare()-> res = res * res(Pdb) n<- next> /g/g2/dahn/DEBUG/PY/example.py(12)plusAndSquare()-> print '('+`a`+'+'+`b`+')^2 = ' + `res`(Pdb) l<- list the source code showing where we are7 B res = 089 res = (a + b)10 res = res * res1112 -> print '('+`a`+'+'+`b`+')^2 = ' + `res`131415 def main():1617 B print "Enter the first number:"(Pdb) p res<- print what value a variable, res, has121(Pdb) w<- print the stack trace of the current frame/g/g2/dahn/DEBUG/PY/<string>(1)?()/g/g2/dahn/DEBUG/PY/example.py(23)?()-> main()/g/g2/dahn/DEBUG/PY/example.py(21)main()-> plusAndSquare(string.atoi(first), string.atoi(second))> /g/g2/dahn/DEBUG/PY/example.py(12)plusAndSquare()-> print '('+`a`+'+'+`b`+')^2 = ' + `res`(Pdb) a<- print the argument list of the current framea = 4b = 7(Pdb) n <- next(4+7)^2 = 121--Return--> /g/g2/dahn/DEBUG/PY/example.py(12)plusAndSquare()->None-> print '('+`a`+'+'+`b`+')^2 = ' + `res`(Pdb) n<- next--Return--> /g/g2/dahn/DEBUG/PY/example.py(21)main()->None-> plusAndSquare(string.atoi(first), string.atoi(second))(Pdb) n<- next--Return--> /g/g2/dahn/DEBUG/PY/example.py(23)?()->None-> main()(Pdb) n<- next--Return--> /g/g2/dahn/DEBUG/PY/<string>(1)?()->None(Pdb) n<- next