forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 5
Local names implementation #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrewleech
wants to merge
5
commits into
pdb_support
Choose a base branch
from
local_names_implementation
base: pdb_support
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signed-off-by: Jos Verlinde <[email protected]>
This minimal change enables access to function local variables through frame.f_locals in sys.settrace() callbacks. Variables are exposed with index-based names (local_00, local_01, etc.) corresponding to their position in the VM's state array. The implementation: - Exposes all non-NULL values in code_state->state array - Uses zero-padded index naming for consistency - Maintains backward compatibility when settrace is disabled - Adds comprehensive unit tests for various usage scenarios This provides the foundation for debugging tools and profilers to access local variable values during program execution. Signed-off-by: Andrew Leech <[email protected]>
This commit implements complete local variable name preservation for MicroPython's sys.settrace() functionality, providing both RAM-based storage (Phase 1) and bytecode persistence (Phase 2) for debugging tools. Key Features: - Phase 1: Local variable names preserved in RAM during compilation - Phase 2: Local variable names stored in .mpy bytecode files - Hybrid architecture with graceful fallback behavior - Full backward and forward compatibility maintained - Bounds checking prevents memory access violations Phase 1 Implementation (MICROPY_PY_SYS_SETTRACE_LOCALNAMES): - py/compile.c: Collect local variable names during compilation - py/emitglue.h: Extended mp_raw_code_t with local_names array - py/profile.c: Expose real names through frame.f_locals - Unified access via mp_raw_code_get_local_name() with bounds checking Phase 2 Implementation (MICROPY_PY_SYS_SETTRACE_LOCALNAMES_PERSIST): - py/emitbc.c: Extended bytecode source info section with local names - py/persistentcode.c: Save/load functions for .mpy file support - Format detection via source info section size analysis - No bytecode version bump required for compatibility Testing and Documentation: - Comprehensive unit tests for both phases - Updated user documentation in docs/library/sys.rst - Complete developer documentation in docs/develop/sys_settrace_localnames.rst - All tests pass with both indexed and named variable access Memory Usage: - Phase 1: ~8 bytes + (num_locals * sizeof(qstr)) per function - Phase 2: ~1-5 bytes + (num_locals * ~10 bytes) per .mpy function - Disabled by default to minimize impact Compatibility Matrix: - Source files: Full local names support with Phase 1 - .mpy files: Index-based fallback without Phase 2, full names with Phase 2 - Graceful degradation across all MicroPython versions Signed-off-by: Andrew Leech <[email protected]>
Remove accidental submodule changes that were introduced in an earlier commit. These submodule updates were not related to the settrace functionality and should be reverted to maintain a clean commit history. Reverted submodules: - lib/nxp_driver - lib/protobuf-c - lib/wiznet5k Signed-off-by: Andrew Leech <[email protected]>
This commit completes the local variable name preservation feature by implementing Phase 2 (bytecode persistence) and updating all documentation to reflect the complete implementation. Phase 2 Implementation (MICROPY_PY_SYS_SETTRACE_LOCALNAMES_PERSIST): - py/emitbc.c: Extended bytecode generation to include local names in source info - py/persistentcode.c: Added save/load functions for .mpy local names support - py/persistentcode.h: Function declarations for Phase 2 functionality - Format detection via source info section size without bytecode version bump Documentation Updates: - docs/library/sys.rst: Enhanced user documentation with examples and features - docs/develop/sys_settrace_localnames.rst: Added Phase 2 implementation details, updated memory usage documentation, added compatibility matrix - Removed obsolete planning documents (TECHNICAL_PLAN_LOCAL_NAMES.md) Testing: - tests/basics/sys_settrace_localnames_persist.py: Phase 2 functionality tests - ports/unix/variants/standard/mpconfigvariant.h: Enabled Phase 2 for testing Configuration: - py/mpconfig.h: Updated Phase 2 dependencies documentation Key Features: - Backward/forward compatibility maintained across all MicroPython versions - .mpy files can now preserve local variable names when compiled with Phase 2 - Graceful degradation when Phase 2 disabled or .mpy lacks local names - Complete user and developer documentation covering both phases Memory Overhead: - .mpy files: ~1-5 bytes + (num_locals * ~10 bytes) per function when enabled - Runtime: Same as Phase 1 when loading local names from .mpy files Signed-off-by: Andrew Leech <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Started with #4 checked out. Then in a new Claude micropython session:
Executive Summary
This document presents architectural options for preserving local variable names in MicroPython's compilation and runtime system, enabling proper name resolution in
sys.settrace()
callbacks without modifying the bytecode format.Current Architecture Analysis
String Interning (QSTR) System
Compilation Flow
Current Data Structures
Design Constraints
Proposed Solutions
Option 1: Extend mp_raw_code_t (Recommended)
Implementation:
Advantages:
code_state->fun_bc->rc
Implementation Details:
mp_emit_glue_new_raw_code()
scope_compute_things()
after local_num assignmentframe_f_locals()
to map indices to namesMemory Cost:
sizeof(qstr) * num_locals
per function (typically 2-4 bytes per local)Option 2: Function Object Attribute
Implementation:
Advantages:
co_varnames
Disadvantages:
Option 3: Separate Global Mapping
Implementation:
Advantages:
Disadvantages:
Option 4: Encode in Bytecode Prelude
Implementation:
Advantages:
Disadvantages:
Recommended Implementation Plan
Phase 1: Core Infrastructure (Option 1)
Phase 2: Python Accessibility (Optional)
Add
co_varnames
attribute to code objects:Memory Optimization Strategies
Share common patterns:
Compress storage:
Lazy allocation:
Size Impact Analysis
Typical function with 4 locals:
Mitigation:
MICROPY_PY_SYS_SETTRACE_LOCALNAMES
Testing Strategy
Correctness tests:
Memory tests:
Compatibility tests:
Conclusion
Option 1 (Extend mp_raw_code_t) provides the best balance of:
This approach preserves the bytecode format while enabling full local variable name resolution in debugging scenarios.
The implantation here started as suggested above, but then extended to also include the "bytecode modification" option as well behind second feature flag.
● Local Variable Names Schemes Summary ## Phase 1 (RAM Storage - MICROPY_PY_SYS_SETTRACE_LOCALNAMES) preserves local variable names in memory during source file compilation by extending the mp_raw_code_t structure with a local_names array. This approach provides excellent debugging experience for source-based development with minimal implementation complexity and zero bytecode format changes, ensuring complete backward compatibility. However, it has significant limitations: local variable names are only available when running from source files, .mpy files fall back to generic local_XX naming, and there's a runtime memory overhead of ~8 bytes plus 4-8 bytes per local variable per function. This makes it ideal for development and debugging workflows but less suitable for production deployments using pre-compiled .mpy files.
Phase 2 (Bytecode Persistence -
MICROPY_PY_SYS_SETTRACE_LOCALNAMES_PERSIST) extends the bytecode format to store local variable names
directly in the source info section of .mpy files, enabling debugging support for pre-compiled modules. The major advantages include complete debugging coverage for both source and .mpy files, persistent local names that survive compilation, graceful compatibility across MicroPython versions (files with local names work on older versions, just without the names), and relatively small file size
overhead (~1-5 bytes plus ~10 bytes per local
variable). The main drawbacks are increased .mpy
file sizes proportional to the number of local variables, slightly more complex implementation requiring bytecode format extensions, and additional
compilation time to encode the local names. This
scheme provides the most comprehensive solution for production debugging scenarios where .mpy files are the primary deployment format.
Testing
Trade-offs and Alternatives