Problem Solving using Python
(ITFC0101)
Python Debugger (pdb)
Dr Naveen Kumar Gupta
Debugging
Basic Definitions (according to Software Testing)
• Error: Mistake made by humans during coding
• Defect: An error found during the testing phase
• Bug: A defect to be resolved by the development team
• Failure: When a build/ software/ program does not meet its specifications
• Crash: System not responding
What is Debugging?
• Process of identifying and fixing errors & defects in code
• These errors are commonly called "bugs".
• Debugging involves
• closely examining code
• stepping through executions
• Analyzing the behavior
• It allows programmers to methodically correct problems and produce working
software
What is a Debugger?
• A software tool designed specifically for debugging code
• Provides specialized commands and features for testing code:
• Step through code line-by-line
• Pause execution at certain lines
• Inspect values of variables
• Track how code is being executed
• Set breakpoints to pause execution
• Debuggers make the debugging process much easier!
Python Debuggers
• pdb - Comes built-in with Python
• ipdb - Enhanced pdb with IPython features
• pudb - Full screen terminal GUI debugger
• PyCharm - Popular Python IDE with debugging
• pdbpp, rpdb, Winpdb - More advanced debuggers
• Commercial debuggers like Visual Studio
Python Debugger: pdb
What is pdb?
• A python debugger included in the standard library
• Allows interactive debugging by
• stepping through code
• inspecting variables
• setting breakpoints, etc.
• Catches bugs and helps understand program flow
Debugging
• PDB comes built-in to the Python standard library
• Defined as the class Pdb which internally makes use of:
• bdb (basic debugger functions)
• cmd (support for line-oriented command interpreters) modules
Key Features of pdb
• Interactive Debugging: pdb allows you to pause the execution of your program at any
point, giving you the ability to interactively explore the state of the program.
• Setting Breakpoints: Breakpoints are points in your code where the program will stop
executing, allowing you to inspect variables or step through code. This can be done using
the pdb.set_trace() function or the break command.
• Stepping Through Code: pdb provides commands like n (next), s (step), and c (continue) to
navigate through the code, allowing you to understand the flow of execution..
Key Features of pdb
• Variable Inspection and Modification: You can inspect the values of
variables using the p (print) command and even modify them during runtime.
• Post-mortem Debugging: After a program crashes, pdb can be used to
inspect the state of the program at the point of failure
• Source code listing
• Viewing stack traces
Debugging
Start debugging within the program using breakpoint
• import pdb
• pdb.set_trace() or breakpoint()
• Run script normally, and execution will stop on breakpoint
• A breakpoint is automatically called on a line below call set_trace()
• With python 3.7 and later versions
• A built-in function called breakpoint() which works in the same manner
Starting pdb Python Code:
• Import pdb module
• Insert pdb.set_trace() at debugging
point
• Or run Python script with -m pdb
• python -m pdb myscript.py
Debugging
Debugging within the program
Command Function
Help/ h To display all commands
where Display the stack trace and line number of the current line
Execute the current line and move to the next line ignoring
next
function calls
step Step into functions called at the current line
exit / quit / q Terminate pdb mode
Some useful commands to remember:
• b: set a breakpoint
• c: continue debugging until you hit a breakpoint
• s: step through the code
• n: to go to next line of code
• l: list source code for current file (default: 11 lines including line being executed)
• u: navigate up a stack frame
• d: navigate down a stack frame
• p: to print the value of an expression in the current context
pdb Commands:
Setting Stepping Inspecting Function
Breakpoints Through Code Variables arguments
• b (breakpoint) - set breakpoint • n (next) - step to next line • p (print) - print expression • a (args) - print function
• cl (clear) - remove breakpoint • s (step) - step into function • l (list) - show current line arguments
• c (continue) - resume execution • q (quit)- quits the debugger
pdb Commands (Setting Breakpoints):
Setting Breakpoints
•b (breakpoint) - set breakpoint
•cl (clear) - remove breakpoint
Set a breakpoint in running code
a) break linenumber
b) break functionname
c) break filename:linenumber
Temporary breakpoint: Automatically deleted after that breakpoint has been hit
a) tbreak linenumber
Conditional breakpoint: Stop at certain line if condition is true
a) break linenumber, condition
b 4, a > 4
Python pdb Breakpoints
Syntax:
break filename: lineno, condition
Managing Breakpoints
After adding breakpoints with the help of numbers assigned to them, we can
manage the breakpoints using the enable and disable and remove command.
disable tells the debugger not to stop when that breakpoint is reached, while
enable turns on the disabled breakpoints.
Given below is the implementation to manage breakpoints using Example 4
pdb Commands (Stepping):
Stepping Through Code
•n (next) - step to next line
•s (step) - step into function
•c (continue) - resume execution
•j (jump) – Jump to specific line of code
th
•e.g., j 4jump to 4 line of current program OR
•jump 4
pdb Commands (Inspecting Variables):
Inspecting Variables
•p (print) - print expression
•l (list) - show current line
•q (quit / exit) - quits the debugger
pdb Commands (function arguments):
Function arguments
•a (args) - print function arguments
Example1: Debugging a Simple Python program of addition of
numbers using Python pdb module
Output/
Command
prompt:
Example 2: Checking variable type using pdb ‘whatis’
command
python -m pdb exppdb.py (put your file name instead of exppdb.py)
Example 3: Navigating in pdb prompt
Example 4: Post-mortem debugging using Python pdb
module
• Post-mortem debugging means entering debug mode after the program is
finished with the execution process (failure has already occurred)
Example 4: Post-mortem debugging using Python pdb
module
• pm() and post_mortem() functions
• These functions look for active trace back and start the debugger
• Reach at the line in the call stack where the exception occurred
Thank You!