0408 | Debugging
Debugging Theory
General
- Accept that bugs happen all the time
- Process of finding, isolating and fixing bugs
- Something is not working as expected, how do I fix it?
- Fixing syntax errors is a form of debugging
print("here")
,print("here 2")
,print("here 3")
is a form of debugging tooBreakpoints
in a debugger help for larger, complex programs
Breakpoints
freeze
orbreak
an application at a certain point- Inspect or change variables, memory or registers
- Conditional breakpoints only break when condition is met
- Breakpoints help identify problem sources and track down bugs
- Testing identifies problems, debugging fixes problems
- SQLi is a bug, which needs to be found and fixed
Terminology
- Static analysis | read the program without executing
- Source code analysis | automatically check for known bugs
- Tracing or Print Debugging |
print("1")
,print("2")
,print("3")
- Remote Debugging | debug from a different remote system
- Post-mortem Debugging | debug based on crash and error logs
Notes
- Debugging is hard and tracing root causes can be complex
- Can be easy to find the effect, but not the cause
- Can be hard to reproduce the bug or issue
- Interdepencies can cause a 'fix' to result in a new bug
- Can take longer to debug than to create a bug
- Print can be useful strategy, but there are limitations
Debugging a Scrip
-
pdb | Python Debugger
- comes with the standard python distribution
- no need to install anything else
-
demo-example | pdb_demo.py | (play around with commenting/uncommenting as you see fit)
print("-"*80)
## --------------- Loading PDB --------------- ##
breakpoint()
test = "test"
print(test)
# NOTE -- by default, python will import pdb and call pdb.set_trace()
# -- set_trace hardcodes a breakpoint where the method was called
# # adding a breakpoint
# # NOTE -- runnig it -- once you run it -- breakpoint will be hit
# breakpoint()
# NOTE -- loading the debugger in a different way
# -- use `python -m pdb pdb_demo.py`
# -- will load the debugger before even starting the program
## --------------- PDB CONTROL commands --------------- ##
# PDB CONTROL commands
# -- you will have access to the program within the debugger
# PDB CONTROL commands -- exiting
# -- use `quit` or `exit` to exit it
# PDB CONTROL commands -- restarting the program
# -- use `run` to restart the program at any time
# PDB CONTROL commands -- continue execution
# -- use `c` / `cont` / `continue` to continue execution
# -- (till the next breakpoint is found)
# PDB CONTROL commands -- stepping through the code -- next
# -- stepping through the code -- `n` or `next`
# -- until the next line and stays within the current function
# PDB CONTROL commands -- stepping through the code -- step
# -- stepping through the code -- `s` or `step` -- (step into)
# -- execute the current line and will stop an a foreign function if one is called
# PDB CONTROL commands -- help
# -- use `help` to display all the documented commands in pdb
# -- getting more info about a command -- `help c`
# PDB CONTROL commands -- breakpoint
# -- use `b` or `break` to list all the break points
# -- with a line number argument --> set a break at the line
# -- `b 3`
# PDB CONTROL commands -- list the source code of the current file
# -- use `l` or `list`
# -- viewing only the firs 4 lines -- `l 1,4`
# -- use `ll` or `longlist` to list the source code for the whole file
# PDB CONTROL commands -- delete/disable a breakpoint
# -- use `disable` with the bpnumber -- `disable 1`
## --------------- DEBUGGING with PDB --------------- ##
# breakpoint()
# once run -- use `p test` to view the value of the variable test at this point of execution
# -- or `print(test)` -- you are passing an expression to be evaluated to python
# -- or modifying the variable -- `test="123"` and `print(test)`
# -- or introduce a new variable -- `new="456"` and `print(new)`
# NOTE -- breakpoint numbers
# -- whenever you add a breakpoint, the debugger assings a number to it
# -- starting with 1 and incrementing for each additional breakpoint which is added
# NOTE -- jumping during execution
# -- change the flow of the program during run time
# -- `j` or `jump` to skip or jump to certain areas within your code
# -- like to jump to line 6 -- `j 6`
# CAREFUL -- you can not jump into a function before the arguments are defined
# -- or into the middle of the try-except block
## --------------- DEBUGGING with PDB -- loop example --------------- ##
for i in range(10):
print(i)
print("-"*80)
## --------------- END --------------- ##