Sub topic 7
Setting, listing, disabling, and clearing breakpoints
This chapter assumes you can already start a pdb session with breakpoint() or python -m pdb, and that you know n, s, and c from the stepping chapter. Here you will learn to manage breakpoints. A breakpoint is a location where pdb stops the program and gives you the (Pdb) prompt. A session often needs many of them, so you need to create, inspect, switch off, and remove them quickly.
Why manage breakpoints instead of stepping
Stepping with n through a long loop is slow. A breakpoint lets you run at full speed with c and stop only where something interesting happens. Pdb keeps a numbered table of breakpoints for the session. Every command in this chapter reads or changes that table.
The practice script
Save this as inventory.py. The line numbers matter, because the examples refer to them.
def price_with_tax(price, rate):
return round(price * (1 + rate), 2)
def process_item(item):
name, price = item
total = price_with_tax(price, 0.2)
return f"{name}: {total}"
def main():
items = [("pen", 1.5), ("book", 12.0), ("lamp", 30.0)]
for item in items:
print(process_item(item))
main()Start it under the debugger with python -m pdb inventory.py. Pdb stops on line 1, before any function exists.
Setting breakpoints: b and tbreak
The break command, abbreviated b, takes a location and an optional condition. A location is one of these:
- A line number in the current file, such as
b 7. - A file and line number, such as
b inventory.py:2. - A function name, such as
b process_item.
Pdb prints Breakpoint N at file:line and assigns the next number, starting at 1. The tbreak command takes the same arguments but creates a temporary breakpoint. A temporary breakpoint deletes itself the first time it stops the program. Use it for "get me to this point once".
(Pdb) b 7
Breakpoint 1 at /tmp/demo/inventory.py:7
(Pdb) b inventory.py:2, price > 10
Breakpoint 2 at /tmp/demo/inventory.py:2
(Pdb) tbreak 8
Breakpoint 3 at /tmp/demo/inventory.py:8The text after the comma in breakpoint 2 is a condition. Pdb stops there only when the expression is true. The next chapter covers conditions and ignore counts in detail.
Where a breakpoint can go
Pdb can only stop on a line that executes code. Blank lines and comments are rejected, usually with *** Blank or comment. Two further rules cause most surprises:
- A breakpoint on a
defline fires when Python runs thedefstatement, which is once at import time. It does not fire when the function is called. To stop on each call, put the breakpoint on the first line of the function body, or give the function name, which pdb resolves to that line. - A function name must be resolvable at the moment you type it. At the first line of
inventory.py,b process_itemfails because thedefhas not run yet. Useb inventory.py:6until the function exists, or runcto a later stop first.
Listing breakpoints
Typing b with no arguments prints the table.
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at /tmp/demo/inventory.py:7
2 breakpoint keep yes at /tmp/demo/inventory.py:2
stop only if price > 10
3 breakpoint del yes at /tmp/demo/inventory.py:8Each column tells you something:
| Column | Meaning |
|---|---|
| Num | The number you pass to `disable`, `enable`, and `clear`. |
| Disp | `keep` stays after a hit. `del` is temporary and is removed after its first hit. |
| Enb | `yes` if active, `no` if disabled. |
| Where | The file and line. |
Below a row you may also see stop only if ... for a condition, and breakpoint already hit N times once it has been reached.
Disabling and enabling
The disable command switches a breakpoint off but keeps it in the table, with its number, condition, and hit count. enable switches it back on. Both accept several numbers at once.
(Pdb) disable 1
Disabled breakpoint 1 at /tmp/demo/inventory.py:7
(Pdb) c
Deleted breakpoint 3 at /tmp/demo/inventory.py:8
> /tmp/demo/inventory.py(8)process_item()
-> return f"{name}: {total}"
(Pdb) c
> /tmp/demo/inventory.py(2)price_with_tax()
-> return round(price * (1 + rate), 2)
(Pdb) p price
12.0
(Pdb) enable 1
Enabled breakpoint 1 at /tmp/demo/inventory.py:7The sequence shows three things. First, the program passed line 7 for the book without stopping because breakpoint 1 was disabled. Second, temporary breakpoint 3 reported its own deletion when it fired. Third, breakpoint 2 skipped the pen, whose price is 1.5, and stopped for the book, whose price is 12.0. If you call disable or enable with no number, current Python versions apply it to every breakpoint.
Disable a breakpoint when you might need it again, for example one that sits on a hot path you want to pass quickly this time.
Clearing breakpoints
The clear command, abbreviated cl, removes breakpoints permanently. It has three forms:
cl 2removes breakpoint number 2. You can list several:cl 1 2.cl inventory.py:7removes every breakpoint on that line, however many numbers they have.clalone asksClear all breakpoints?and waits foryorn.
(Pdb) cl 2
Deleted breakpoint 2 at /tmp/demo/inventory.py:2
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at /tmp/demo/inventory.py:7
breakpoint already hit 1 timePdb has no delete command. clear is the only way to remove a breakpoint.
Numbers are not reused
After you clear breakpoint 2, the next b creates breakpoint 4 if 3 already existed. Numbers only increase within a session, so a number in your notes or scrollback never silently refers to a different breakpoint. When you want to turn something off, always read the current number from the b listing instead of trusting your memory.
The life of a breakpoint
Reading the hit counts
The already hit line counts how many times execution reached the line while the breakpoint was enabled. That includes visits where a condition was false and pdb did not stop. In the session above, breakpoint 2 shows 2 hits even though it stopped once, because the pen call evaluated the condition and rejected it. A large hit count on a conditional breakpoint tells you the line is hot and the condition is doing the filtering.
A disabled breakpoint does not count hits. If you re-enable it later, the count continues from where it stopped.
Session scope and persistence
Breakpoints exist only for the current pdb session. If you restart the program, the table starts empty. Two ways to keep a setup are:
- Put
breakpoint()calls in the source, which are the same as setting a breakpoint by hand. Remove them before committing. - Put
bcommands in a.pdbrcfile, covered in a later chapter.
Common mistakes
- Setting a breakpoint on a comment or blank line and not noticing the error message. Check that pdb printed
Breakpoint N at. - Putting a breakpoint on a
defline and expecting a stop on every call. - Running
clwith no argument and answeringywithout realizing it removes breakpoints you wanted. - Using a breakpoint number from an earlier listing after clearing others. Run
bagain first. - Forgetting that a restart discards all breakpoints, so a long setup is worth saving in
.pdbrc.
Exercise
In a session on inventory.py you ran b 7, b 2, and tbreak 8, then cl 2, and then b 14. What number does the new breakpoint get, and which breakpoints are still in the table before the new one is added?
Show answer
The new breakpoint is number 4, because numbers increase and are never reused, even after cl 2. Before it is added, the table holds breakpoint 1 (line 7) and breakpoint 3 (the temporary one on line 8, still present unless it has already been hit).
Exercise
You want pdb to ignore breakpoint 1 for now but keep its condition and hit count so you can use it again later. Which command do you use, and why not cl 1?
Show answer
Use disable 1. It keeps the breakpoint, its condition, and its hit count in the table, and enable 1 restores it. cl 1 deletes it for good, so you would have to recreate it and its condition with a new number.