Debugging, Profiling, and Reversing Python Code
CSE 598 – Applied Program Analysis and Debugging
Fall 2025
Fish Wang
Arizona State University
Python Interpreters
2
Python Debugging
3
Demo
4
Attaching to a Running Process
5
Python Profiling
6
Python CPU Profilers
7
Demo
8
Analyzing the Profiling Result
9
Behind The Scene
10
Python Bytecode
11
Interprets an instruction
Updates the state
Python Bytecode
def func(a):
return a + 1
12
Line number
Byte offset
Bytecode instructions
1 0 RESUME 0
2 2 LOAD_FAST 0 (a)
4 LOAD_CONST 1 (1)
6 BINARY_OP 0 (+)
10 RETURN_VALUE
How to…
13
.py vs .pyc
14
Understanding Bytecode
1 0 RESUME 0
2 2 LOAD_FAST 0 (a)
4 LOAD_CONST 1 (1)
6 BINARY_OP 0 (+)
10 RETURN_VALUE
15
https://docs.python.org/3/library/dis.html#python-bytecode-instructions
The CPython Memory Model
16
The CPython Memory Model
17
Decompiling Python Bytecode
1 0 RESUME 0
2 2 LOAD_FAST 0 (a)
4 LOAD_CONST 1 (1)
6 BINARY_OP 0 (+)
10 RETURN_VALUE
18
nop
stack_push(a)
stack_push(1)
stack_push(stack_pop() + stack_pop())
return stack_top()
Decompiling Python Bytecode
1 0 RESUME 0
2 2 LOAD_FAST 0 (a)
4 LOAD_CONST 1 (1)
6 BINARY_OP 0 (+)
10 RETURN_VALUE
19
nop
v0 = a
v1 = 1
v2 = v0 + v1; kill(v0); kill(v1)
return v2
v2 = a + 1
return v2
return a + 1
Congratulations!
20
func_0 vs func_1
21
General Python Rules of Thumb
22
Questions?
23