data breakpoints with pdb
...much like you can watch a memory address in gdb...
- GDB uses data breakpoints, this is made easy with hardware support (hardware watchpoints), this typically involves marking the memory pages read-only which then trips an exception handler on memory access. When hardware watchpoints are not available it uses software watchpoints, these are only useful in single threads and are much slower.
- PDB does not support data breakpoints, so the short answer is NO, you cannot do it with PDB out of the box.
printing variables when hitting breakpoints in pdb
For watching a variable when you are hitting a breakpoint, you can use the commands
command. E.g. printing some_variable
when hitting breakpoint #1 (canonical example from pdb
doc).
(Pdb) commands 1
(com) print(some_variable)
(com) end
(Pdb)
Additionally, you can use the condition
command to ensure the breakpoint is only hit whenever the variable takes a certain value.
eg:
(Pdb) condition 1 some_variable==some_value
other solutions
You can use tracing / profiling functions to examine things step by step using sys.settrace
and checking out the opcodes being executed.
https://docs.python.org/3/library/sys.html#sys.settrace
Here is some code to get you started:
import sys
import dis
def tracefn(frame, event, arg):
if event == 'call':
print("## CALL", frame)
frame.f_trace_opcodes = True
elif event == 'opcode':
opcode = frame.f_code.co_code[frame.f_lasti]
opname = dis.opname[opcode]
print("## OPCODE", opname)
return tracefn
watchme = 123
def foo():
global watchme
watchme = 122
sys.settrace(tracefn)
foo()
You will probably need to spy on all the STORE_*
opcodes.
https://docs.python.org/3/library/dis.html
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…