|
| 1 | +sys.settrace() Local Variable Name Preservation |
| 2 | +=============================================== |
| 3 | + |
| 4 | +This document describes the local variable name preservation feature for MicroPython's |
| 5 | +``sys.settrace()`` functionality, which allows debuggers and profilers to access |
| 6 | +meaningful variable names instead of just indexed values. |
| 7 | + |
| 8 | +Overview |
| 9 | +-------- |
| 10 | + |
| 11 | +MicroPython's ``sys.settrace()`` implementation traditionally provided access to local |
| 12 | +variables through generic index-based names (``local_00``, ``local_01``, etc.). |
| 13 | +The local variable name preservation feature extends this by storing and exposing |
| 14 | +the actual variable names when available. |
| 15 | + |
| 16 | +This feature enables: |
| 17 | + |
| 18 | +* Enhanced debugging experiences with meaningful variable names |
| 19 | +* Better profiling and introspection capabilities |
| 20 | +* Improved development tools that can show actual variable names |
| 21 | + |
| 22 | +Configuration |
| 23 | +------------- |
| 24 | + |
| 25 | +The feature is controlled by configuration macros: |
| 26 | + |
| 27 | +``MICROPY_PY_SYS_SETTRACE_LOCALNAMES`` |
| 28 | + Enables local variable name preservation for source files (RAM storage). |
| 29 | + Default: ``0`` (disabled) |
| 30 | + |
| 31 | +``MICROPY_PY_SYS_SETTRACE_LOCALNAMES_PERSIST`` |
| 32 | + Enables local variable name preservation in bytecode for .mpy files. |
| 33 | + Default: ``0`` (disabled, implementation pending) |
| 34 | + |
| 35 | +Dependencies |
| 36 | +~~~~~~~~~~~~ |
| 37 | + |
| 38 | +* ``MICROPY_PY_SYS_SETTRACE`` must be enabled |
| 39 | +* ``MICROPY_PERSISTENT_CODE_SAVE`` must be enabled |
| 40 | + |
| 41 | +Memory Usage |
| 42 | +~~~~~~~~~~~~ |
| 43 | + |
| 44 | +When enabled, the feature adds: |
| 45 | + |
| 46 | +* One pointer field (``local_names``) per function in ``mp_raw_code_t`` |
| 47 | +* One length field (``local_names_len``) per function in ``mp_raw_code_t`` |
| 48 | +* One qstr array per function containing local variable names |
| 49 | + |
| 50 | +Total memory overhead per function: ``8 bytes + (num_locals * sizeof(qstr))`` |
| 51 | + |
| 52 | +Implementation Details |
| 53 | +---------------------- |
| 54 | + |
| 55 | +Architecture |
| 56 | +~~~~~~~~~~~~ |
| 57 | + |
| 58 | +The implementation consists of several components: |
| 59 | + |
| 60 | +1. **Compilation Phase** (``py/compile.c``) |
| 61 | + Collects local variable names during scope analysis and stores them |
| 62 | + in an array allocated with the raw code object. |
| 63 | + |
| 64 | +2. **Storage** (``py/emitglue.h``) |
| 65 | + Extends ``mp_raw_code_t`` structure to include local variable name storage |
| 66 | + with proper bounds checking. |
| 67 | + |
| 68 | +3. **Runtime Access** (``py/profile.c``) |
| 69 | + Provides access to variable names through ``frame.f_locals`` in trace |
| 70 | + callbacks, falling back to index-based names when real names unavailable. |
| 71 | + |
| 72 | +4. **Unified Access Layer** (``py/emitglue.h``) |
| 73 | + Provides ``mp_raw_code_get_local_name()`` function with bounds checking |
| 74 | + and hybrid storage support. |
| 75 | + |
| 76 | +Data Structures |
| 77 | +~~~~~~~~~~~~~~~ |
| 78 | + |
| 79 | +Extended ``mp_raw_code_t`` structure: |
| 80 | + |
| 81 | +.. code-block:: c |
| 82 | +
|
| 83 | + typedef struct _mp_raw_code_t { |
| 84 | + // ... existing fields ... |
| 85 | + #if MICROPY_PY_SYS_SETTRACE_LOCALNAMES |
| 86 | + const qstr *local_names; // Array of local variable names |
| 87 | + uint16_t local_names_len; // Length of local_names array |
| 88 | + #endif |
| 89 | + } mp_raw_code_t; |
| 90 | +
|
| 91 | +Local Variable Name Collection |
| 92 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 93 | + |
| 94 | +During compilation in ``scope_compute_things()``: |
| 95 | + |
| 96 | +1. Check if scope is function-like and has local variables |
| 97 | +2. Allocate qstr array with size ``scope->num_locals`` |
| 98 | +3. Iterate through ``id_info`` and populate array at correct indices |
| 99 | +4. Store array pointer and length in raw code object |
| 100 | + |
| 101 | +.. code-block:: c |
| 102 | +
|
| 103 | + if (SCOPE_IS_FUNC_LIKE(scope->kind) && scope->num_locals > 0) { |
| 104 | + qstr *names = m_new0(qstr, scope->num_locals); |
| 105 | + for (int i = 0; i < scope->id_info_len; i++) { |
| 106 | + id_info_t *id = &scope->id_info[i]; |
| 107 | + if ((id->kind == ID_INFO_KIND_LOCAL || id->kind == ID_INFO_KIND_CELL) && |
| 108 | + id->local_num < scope->num_locals) { |
| 109 | + names[id->local_num] = id->qst; |
| 110 | + } |
| 111 | + } |
| 112 | + scope->raw_code->local_names = names; |
| 113 | + scope->raw_code->local_names_len = scope->num_locals; |
| 114 | + } |
| 115 | +
|
| 116 | +Bounds Checking |
| 117 | +~~~~~~~~~~~~~~~ |
| 118 | + |
| 119 | +Critical for memory safety, the unified access function includes bounds checking: |
| 120 | + |
| 121 | +.. code-block:: c |
| 122 | +
|
| 123 | + static inline qstr mp_raw_code_get_local_name(const mp_raw_code_t *rc, uint16_t local_num) { |
| 124 | + #if MICROPY_PY_SYS_SETTRACE_LOCALNAMES |
| 125 | + if (rc->local_names != NULL && local_num < rc->local_names_len && |
| 126 | + rc->local_names[local_num] != MP_QSTR_NULL) { |
| 127 | + return rc->local_names[local_num]; |
| 128 | + } |
| 129 | + #endif |
| 130 | + return MP_QSTR_NULL; // No name available |
| 131 | + } |
| 132 | +
|
| 133 | +Usage |
| 134 | +----- |
| 135 | + |
| 136 | +Python API |
| 137 | +~~~~~~~~~~ |
| 138 | + |
| 139 | +The feature integrates transparently with existing ``sys.settrace()`` usage: |
| 140 | + |
| 141 | +.. code-block:: python |
| 142 | +
|
| 143 | + import sys |
| 144 | +
|
| 145 | + def trace_handler(frame, event, arg): |
| 146 | + if event == 'line': |
| 147 | + locals_dict = frame.f_locals |
| 148 | + print(f"Local variables: {list(locals_dict.keys())}") |
| 149 | + return trace_handler |
| 150 | +
|
| 151 | + def test_function(): |
| 152 | + username = "Alice" |
| 153 | + age = 25 |
| 154 | + return username, age |
| 155 | +
|
| 156 | + sys.settrace(trace_handler) |
| 157 | + result = test_function() |
| 158 | + sys.settrace(None) |
| 159 | +
|
| 160 | +Expected output with feature enabled: |
| 161 | + |
| 162 | +.. code-block:: |
| 163 | +
|
| 164 | + Local variables: ['username', 'age'] |
| 165 | +
|
| 166 | +Expected output with feature disabled: |
| 167 | + |
| 168 | +.. code-block:: |
| 169 | +
|
| 170 | + Local variables: ['local_00', 'local_01'] |
| 171 | +
|
| 172 | +Behavior |
| 173 | +~~~~~~~~ |
| 174 | + |
| 175 | +**With Real Names Available:** |
| 176 | +Variables appear with their actual names (``username``, ``age``, etc.) |
| 177 | + |
| 178 | +**With Fallback Behavior:** |
| 179 | +Variables appear with index-based names (``local_00``, ``local_01``, etc.) |
| 180 | + |
| 181 | +**Mixed Scenarios:** |
| 182 | +Some variables may have real names while others use fallback names, |
| 183 | +depending on compilation and storage availability. |
| 184 | + |
| 185 | +Limitations |
| 186 | +----------- |
| 187 | + |
| 188 | +Source Files vs .mpy Files |
| 189 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 190 | + |
| 191 | +**Current Implementation (Phase 1):** |
| 192 | + |
| 193 | +* ✅ Source files: Full local variable name preservation |
| 194 | +* ❌ .mpy files: Fallback to index-based names (``local_XX``) |
| 195 | + |
| 196 | +**Future Implementation (Phase 2):** |
| 197 | + |
| 198 | +* ✅ Source files: Full local variable name preservation |
| 199 | +* ✅ .mpy files: Full local variable name preservation (when ``MICROPY_PY_SYS_SETTRACE_LOCALNAMES_PERSIST`` enabled) |
| 200 | + |
| 201 | +Compatibility |
| 202 | +~~~~~~~~~~~~~ |
| 203 | + |
| 204 | +* **Bytecode Compatibility:** Phase 1 maintains full bytecode compatibility |
| 205 | +* **Memory Usage:** Adds memory overhead proportional to number of local variables |
| 206 | +* **Performance:** Minimal runtime performance impact |
| 207 | + |
| 208 | +Deployment Scenarios |
| 209 | +~~~~~~~~~~~~~~~~~~~~ |
| 210 | + |
| 211 | +**Development Environment:** |
| 212 | +Enable ``MICROPY_PY_SYS_SETTRACE_LOCALNAMES`` for full debugging capabilities |
| 213 | +with source files. |
| 214 | + |
| 215 | +**Production Deployment:** |
| 216 | +Disable the feature to minimize memory usage, or enable selectively based |
| 217 | +on debugging requirements. |
| 218 | + |
| 219 | +**.mpy Distribution:** |
| 220 | +Phase 1 provides fallback behavior. Phase 2 will enable full support with |
| 221 | +``MICROPY_PY_SYS_SETTRACE_LOCALNAMES_PERSIST``. |
| 222 | + |
| 223 | +Testing |
| 224 | +------- |
| 225 | + |
| 226 | +Unit Tests |
| 227 | +~~~~~~~~~~ |
| 228 | + |
| 229 | +The feature includes comprehensive unit tests: |
| 230 | + |
| 231 | +* ``tests/basics/sys_settrace_localnames.py`` - Basic functionality test |
| 232 | +* ``tests/basics/sys_settrace_localnames_comprehensive.py`` - Detailed verification |
| 233 | + |
| 234 | +Test Coverage |
| 235 | +~~~~~~~~~~~~~ |
| 236 | + |
| 237 | +* Basic local variable access |
| 238 | +* Nested function variables |
| 239 | +* Loop variable handling |
| 240 | +* Exception handling scenarios |
| 241 | +* Mixed real/fallback naming |
| 242 | +* Memory safety (bounds checking) |
| 243 | +* Integration with existing ``sys.settrace()`` functionality |
| 244 | + |
| 245 | +Example Test |
| 246 | +~~~~~~~~~~~~ |
| 247 | + |
| 248 | +.. code-block:: python |
| 249 | +
|
| 250 | + def test_basic_names(): |
| 251 | + def trace_handler(frame, event, arg): |
| 252 | + if frame.f_code.co_name == 'test_func': |
| 253 | + locals_dict = frame.f_locals |
| 254 | + real_names = [k for k in locals_dict.keys() if not k.startswith('local_')] |
| 255 | + return real_names |
| 256 | + |
| 257 | + def test_func(): |
| 258 | + username = "test" |
| 259 | + return username |
| 260 | + |
| 261 | + sys.settrace(trace_handler) |
| 262 | + result = test_func() |
| 263 | + sys.settrace(None) |
| 264 | + # Should capture 'username' as a real variable name |
| 265 | +
|
| 266 | +Future Enhancements |
| 267 | +------------------- |
| 268 | + |
| 269 | +Phase 2: Bytecode Storage |
| 270 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 271 | + |
| 272 | +Implementation of ``MICROPY_PY_SYS_SETTRACE_LOCALNAMES_PERSIST`` to store |
| 273 | +local variable names in bytecode, enabling full support for .mpy files. |
| 274 | + |
| 275 | +**Technical Approach:** |
| 276 | +* Extend bytecode format to include local variable name tables |
| 277 | +* Modify .mpy file format to preserve debugging information |
| 278 | +* Implement bytecode-based name retrieval in ``mp_raw_code_get_local_name()`` |
| 279 | + |
| 280 | +Python Accessibility |
| 281 | +~~~~~~~~~~~~~~~~~~~~ |
| 282 | + |
| 283 | +**Goal:** Make local variable names accessible through standard Python attributes |
| 284 | + |
| 285 | +**Potential API:** |
| 286 | +* ``function.__code__.co_varnames`` - Local variable names tuple |
| 287 | +* ``frame.f_code.co_varnames`` - Access in trace callbacks |
| 288 | + |
| 289 | +Performance Optimizations |
| 290 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 291 | + |
| 292 | +* Lazy loading of variable names |
| 293 | +* Compression of name storage |
| 294 | +* Optional name interning optimizations |
| 295 | + |
| 296 | +Integration Points |
| 297 | +------------------ |
| 298 | + |
| 299 | +Debugger Integration |
| 300 | +~~~~~~~~~~~~~~~~~~~ |
| 301 | + |
| 302 | +The feature provides a foundation for enhanced debugger support: |
| 303 | + |
| 304 | +.. code-block:: python |
| 305 | +
|
| 306 | + class MicroPythonDebugger: |
| 307 | + def __init__(self): |
| 308 | + self.breakpoints = {} |
| 309 | + |
| 310 | + def trace_callback(self, frame, event, arg): |
| 311 | + if event == 'line' and self.has_breakpoint(frame): |
| 312 | + # Access local variables with real names |
| 313 | + locals_dict = frame.f_locals |
| 314 | + self.show_variables(locals_dict) |
| 315 | + return self.trace_callback |
| 316 | +
|
| 317 | +Profiler Enhancement |
| 318 | +~~~~~~~~~~~~~~~~~~~ |
| 319 | + |
| 320 | +Profilers can provide more meaningful variable analysis: |
| 321 | + |
| 322 | +.. code-block:: python |
| 323 | +
|
| 324 | + class VariableProfiler: |
| 325 | + def profile_function(self, func): |
| 326 | + def trace_wrapper(frame, event, arg): |
| 327 | + if event == 'return': |
| 328 | + locals_dict = frame.f_locals |
| 329 | + self.analyze_variable_usage(locals_dict) |
| 330 | + return trace_wrapper |
| 331 | + |
| 332 | + sys.settrace(trace_wrapper) |
| 333 | + result = func() |
| 334 | + sys.settrace(None) |
| 335 | + return result |
| 336 | +
|
| 337 | +Contributing |
| 338 | +------------ |
| 339 | + |
| 340 | +Development Guidelines |
| 341 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 342 | + |
| 343 | +When modifying the local variable name preservation feature: |
| 344 | + |
| 345 | +1. **Memory Safety:** Always include bounds checking for array access |
| 346 | +2. **Compatibility:** Maintain bytecode compatibility in Phase 1 |
| 347 | +3. **Testing:** Add tests for new functionality |
| 348 | +4. **Documentation:** Update this documentation for any API changes |
| 349 | + |
| 350 | +Code Review Checklist |
| 351 | +~~~~~~~~~~~~~~~~~~~~~ |
| 352 | + |
| 353 | +* ✅ Bounds checking implemented for all array access |
| 354 | +* ✅ Memory properly allocated and freed |
| 355 | +* ✅ Configuration macros respected |
| 356 | +* ✅ Fallback behavior maintains compatibility |
| 357 | +* ✅ Unit tests added for new functionality |
| 358 | +* ✅ Documentation updated |
| 359 | + |
| 360 | +File Locations |
| 361 | +~~~~~~~~~~~~~~ |
| 362 | + |
| 363 | +**Core Implementation:** |
| 364 | +* ``py/compile.c`` - Local name collection during compilation |
| 365 | +* ``py/emitglue.h`` - Data structures and unified access |
| 366 | +* ``py/emitglue.c`` - Initialization |
| 367 | +* ``py/profile.c`` - Runtime access through ``frame.f_locals`` |
| 368 | +* ``py/mpconfig.h`` - Configuration macros |
| 369 | + |
| 370 | +**Testing:** |
| 371 | +* ``tests/basics/sys_settrace_localnames.py`` - Unit tests |
| 372 | +* ``tests/basics/sys_settrace_localnames_comprehensive.py`` - Integration tests |
| 373 | + |
| 374 | +**Documentation:** |
| 375 | +* ``docs/develop/sys_settrace_localnames.rst`` - This document |
0 commit comments