|
| 1 | +# **Debugging Python Scripts with UDB** |
| 2 | + |
| 3 | +This guide explains how to use UDB to debug Python scripts using the `python` addon. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## **Prerequisites** |
| 8 | + |
| 9 | +You will need: |
| 10 | + |
| 11 | +* A working UDB installation |
| 12 | +* The path to your Python interpreter (e.g. `/usr/bin/python`) |
| 13 | +* The Python script you want to debug |
| 14 | + |
| 15 | +## **Step 1: Start UDB with the Python Interpreter** |
| 16 | + |
| 17 | +Rather than running your Python script directly, launch UDB with your Python interpreter as the target program: |
| 18 | + |
| 19 | +``` |
| 20 | +udb /path/to/python |
| 21 | + - or - |
| 22 | +udb `which python` |
| 23 | +``` |
| 24 | + |
| 25 | +Note: If using pyenv, use: |
| 26 | +```bash |
| 27 | + udb `pyenv which python` |
| 28 | +``` |
| 29 | + |
| 30 | +## **Step 2: Install the Python Addon** |
| 31 | + |
| 32 | +At the UDB prompt, install the Python debugging addon: |
| 33 | + |
| 34 | +``` |
| 35 | +not running> extend python |
| 36 | +``` |
| 37 | + |
| 38 | +UDB will download and set up the addon automatically. |
| 39 | + |
| 40 | +⚠️ **Note:** The `python` addon is experimental and may change in incompatible ways in future releases. |
| 41 | + |
| 42 | +## **Step 3: Start Your Script** |
| 43 | + |
| 44 | +Use the `upy start` command to load and begin recording your Python script: |
| 45 | + |
| 46 | +``` |
| 47 | +not running> upy start /path/to/your_script.py [arguments] |
| 48 | +``` |
| 49 | + |
| 50 | +For example: |
| 51 | + |
| 52 | +``` |
| 53 | +not running> upy start /home/user/scripts/fizzbuzz.py 20 |
| 54 | +``` |
| 55 | + |
| 56 | +UDB will initialize the Python environment and begin recording execution. You'll see a prompt like: |
| 57 | + |
| 58 | +``` |
| 59 | +Python has been initialized. |
| 60 | +recording 5,048,331> |
| 61 | +``` |
| 62 | + |
| 63 | +## **Navigating the Recording** |
| 64 | + |
| 65 | +Once your script is running, you have two sets of commands available: |
| 66 | + |
| 67 | +### **Standard UDB commands** |
| 68 | + |
| 69 | +Use these to navigate at the **C level** (the underlying interpreter execution). For example: |
| 70 | + |
| 71 | +* `continue`, `next`, `step` — move forward |
| 72 | +* `reverse-continue`, `reverse-next` — move backward |
| 73 | +* `break` — set a C-level breakpoint |
| 74 | +* `layout dashboard` — enable the dashboard TUI layout |
| 75 | +* `Last <expression>` — travel backwards to the last time \<expression\> changed |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +### **Python-level commands (prefixed with `upy`)** |
| 80 | + |
| 81 | +Use these to navigate at the **Python level** (your script's source code). For example: |
| 82 | + |
| 83 | +| Command | Description | |
| 84 | +| ----- | ----- | |
| 85 | +| `upy continue` | Run forward until the next breakpoint or end of program | |
| 86 | +| `upy reverse-continue` | Run backward until the previous breakpoint | |
| 87 | +| `upy next` | Step forward to the next Python line | |
| 88 | +| `upy reverse-next` | Step backward to the previous Python line | |
| 89 | +| `upy break <function>` | Set a Python breakpoint at a function | |
| 90 | +| `upy break <file.py:line>` | Set a Python breakpoint at a line in `file.py` | |
| 91 | +| `upy start <script> [args]` | Start a Python script | |
| 92 | + |
| 93 | +**Example session** — running a script, setting a breakpoint, and stepping back through it: |
| 94 | + |
| 95 | +``` |
| 96 | +recording> upy continue |
| 97 | +# ... script output ... |
| 98 | +
|
| 99 | +end> upy break fizzbuzz |
| 100 | +Python breakpoint 1 at fizzbuzz () |
| 101 | +
|
| 102 | +end> upy reverse-continue |
| 103 | +Python breakpoint 1, fizzbuzz () at /home/user/scripts/fizzbuzz.py:4 |
| 104 | + #0 File "fizzbuzz.py", line 4, in fizzbuzz |
| 105 | + for i in range(1, max + 1): |
| 106 | +
|
| 107 | +> upy next |
| 108 | + #0 File "fizzbuzz.py", line 5, in fizzbuzz |
| 109 | + if i % 3 == 0 and i % 5 == 0: |
| 110 | +``` |
| 111 | + |
| 112 | +## **TUI Layout** |
| 113 | + |
| 114 | +For a visual, source-level debugging interface, type: |
| 115 | + |
| 116 | +``` |
| 117 | +layout python |
| 118 | +``` |
| 119 | + |
| 120 | +This opens a terminal UI showing your Python source code as you step through it, similar to `layout dashboard` for C-level debugging. |
| 121 | + |
| 122 | +## **Getting Help** |
| 123 | + |
| 124 | +To see all available Python debugging commands, type: |
| 125 | + |
| 126 | +``` |
| 127 | +help upy |
| 128 | +``` |
| 129 | + |
| 130 | +## **Quick Reference** |
| 131 | + |
| 132 | +``` |
| 133 | +udb `which python` # Launch UDB with Python |
| 134 | +udb /path/to/python # Launch UDB with Python |
| 135 | +extend python # Load the Python addon |
| 136 | +upy start script.py [args] # Start and record your script |
| 137 | +upy continue # Run forward |
| 138 | +upy reverse-continue # Run backward |
| 139 | +upy next # Step to next Python line |
| 140 | +upy break <function> # Set a Python breakpoint |
| 141 | +upy break <file.py:line> # Set a Python breakpoint |
| 142 | +upy watch <expression> # Set a Python watchpoint |
| 143 | +upy info locals # Display Python local variables |
| 144 | +upy backtrace # Display Python backtrace |
| 145 | +layout python # Open Python TUI view |
| 146 | +layout dashboard # Open C/C++ TUI view |
| 147 | +help upy # Show all upy commands |
| 148 | +``` |
| 149 | + |
0 commit comments