forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 5
Allow sys.settrace() to inspect locals() variables including (optional) name resolution. #4
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
Josverl
wants to merge
6
commits into
andrewleech:pdb_support
Choose a base branch
from
Josverl:pdb_support_localvars
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c923b54
py/profile: Add tracing for local variables.
Josverl 8f3531d
sys.settrace: Add localnames, part 1.
Josverl 2f9968f
Fixup : Rename Macros to standard.
Josverl 9afe2a9
py/profile: Fix naming for placeholder local variable names.
Josverl 00b6390
py/modsys: Update to mp_sys_gettrace(void).
Josverl 05b7818
py/py.cmake: Add localnames.c to support sys.settrace().
Josverl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule micropython-lib
updated
81 files
Submodule nxp_driver
updated
4296 files
Submodule protobuf-c
updated
53 files
Submodule wiznet5k
updated
7 files
+0 −17 | Ethernet/W5200/w5200.c | |
+27 −64 | Ethernet/socket.c | |
+16 −18 | Ethernet/socket.h | |
+2 −28 | Ethernet/wizchip_conf.c | |
+2 −22 | Ethernet/wizchip_conf.h | |
+10 −10 | Internet/DHCP/dhcp.c | |
+6 −6 | Internet/DNS/dns.c |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2025 Jos Verlinde | ||
* | ||
* Permission is hereby granted, free | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include "py/runtime.h" | ||
#include "py/localnames.h" | ||
|
||
#if MICROPY_PY_SYS_SETTRACE_SAVE_NAMES | ||
|
||
// Initialize the local names structure | ||
void mp_local_names_init(mp_local_names_t *local_names) { | ||
if (local_names == NULL) { | ||
return; | ||
} | ||
|
||
local_names->num_locals = 0; | ||
local_names->order_count = 0; | ||
|
||
// Initialize all entries with null qstrs and invalid indices | ||
for (uint16_t i = 0; i < MICROPY_PY_SYS_SETTRACE_NAMES_MAX; i++) { | ||
local_names->local_names[i] = MP_QSTRnull; | ||
local_names->local_nums[i] = UINT16_MAX; // Invalid index marker | ||
} | ||
} | ||
|
||
// Get the name of a local variable by its index | ||
qstr mp_local_names_get_name(const mp_local_names_t *local_names, uint16_t local_num) { | ||
if (local_names == NULL || local_num >= MICROPY_PY_SYS_SETTRACE_NAMES_MAX) { | ||
return MP_QSTRnull; | ||
} | ||
|
||
// Direct array access | ||
return local_names->local_names[local_num]; | ||
} | ||
|
||
// Look up the original local_num by order index (source code order) | ||
uint16_t mp_local_names_get_local_num(const mp_local_names_t *local_names, uint16_t order_idx) { | ||
if (local_names == NULL || order_idx >= local_names->order_count) { | ||
return UINT16_MAX; // Invalid index | ||
} | ||
|
||
return local_names->local_nums[order_idx]; | ||
} | ||
|
||
// Add or update a name mapping for a local variable | ||
void mp_local_names_add(mp_local_names_t *local_names, uint16_t local_num, qstr qstr_name) { | ||
if (local_names == NULL || local_num >= MICROPY_PY_SYS_SETTRACE_NAMES_MAX) { | ||
return; | ||
} | ||
|
||
// Store name directly using local_num as index | ||
local_names->local_names[local_num] = qstr_name; | ||
|
||
// Update number of locals if needed | ||
if (local_num >= local_names->num_locals) { | ||
local_names->num_locals = local_num + 1; | ||
} | ||
|
||
// Also store in order of definition for correct runtime mapping | ||
if (local_names->order_count < MICROPY_PY_SYS_SETTRACE_NAMES_MAX) { | ||
uint16_t idx = local_names->order_count; | ||
local_names->local_nums[idx] = local_num; | ||
local_names->order_count++; | ||
} | ||
|
||
// Refine runtime slot mapping logic | ||
// Test the hypothesis that variables are assigned from highest slots down | ||
uint16_t runtime_slot = local_num; // Default to direct mapping | ||
|
||
if (local_names->order_count > 0) { | ||
// Find position in order array | ||
for (uint16_t i = 0; i < local_names->order_count; ++i) { | ||
if (local_names->local_nums[i] == local_num) { | ||
runtime_slot = i; | ||
break; | ||
} | ||
} | ||
} | ||
local_names->runtime_slots[local_num] = runtime_slot; | ||
} | ||
|
||
// Get the runtime slot for a local variable by its index | ||
uint16_t mp_local_names_get_runtime_slot(const mp_local_names_t *local_names, uint16_t local_num) { | ||
if (local_names == NULL || local_num >= MICROPY_PY_SYS_SETTRACE_NAMES_MAX) { | ||
return UINT16_MAX; // Invalid slot | ||
} | ||
return local_names->runtime_slots[local_num]; | ||
} | ||
|
||
#endif // MICROPY_PY_SYS_SETTRACE_SAVE_NAMES |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2025 Contributors to the MicroPython project | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#ifndef MICROPY_INCLUDED_PY_LOCALNAMES_H | ||
#define MICROPY_INCLUDED_PY_LOCALNAMES_H | ||
|
||
#include "py/obj.h" | ||
#include "py/qstr.h" | ||
|
||
#if MICROPY_PY_SYS_SETTRACE_SAVE_NAMES | ||
|
||
#define MICROPY_PY_SYS_SETTRACE_NAMES_MAX 32 // Maximum number of local variables to store names for | ||
|
||
// Structure to hold variable name mappings for a function scope | ||
typedef struct _mp_local_names_t { | ||
uint16_t num_locals; // Total number of local variables with names | ||
qstr local_names[MICROPY_PY_SYS_SETTRACE_NAMES_MAX]; // Array of variable names, indexed by local_num | ||
uint16_t local_nums[MICROPY_PY_SYS_SETTRACE_NAMES_MAX]; // Reverse mapping: name index -> local_num (for correct state array mapping) | ||
uint16_t order_count; // Number of variables stored in order they were defined | ||
uint16_t runtime_slots[MICROPY_PY_SYS_SETTRACE_NAMES_MAX]; // Mapping of local_num to runtime slots | ||
} mp_local_names_t; | ||
|
||
// Initialize the local names structure | ||
void mp_local_names_init(mp_local_names_t *local_names); | ||
|
||
// Function to look up a variable name by its index | ||
qstr mp_local_names_get_name(const mp_local_names_t *local_names, uint16_t local_num); | ||
|
||
// Function to look up the original local_num by order index (source code order) | ||
uint16_t mp_local_names_get_local_num(const mp_local_names_t *local_names, uint16_t order_idx); | ||
|
||
// Function to add or update a name mapping for a local variable | ||
void mp_local_names_add(mp_local_names_t *local_names, uint16_t local_num, qstr qstr_name); | ||
|
||
// Function to get the runtime slot of a local variable by its index | ||
uint16_t mp_local_names_get_runtime_slot(const mp_local_names_t *local_names, uint16_t local_num); | ||
|
||
#endif // MICROPY_PY_SYS_SETTRACE_SAVE_NAMES | ||
|
||
#endif // MICROPY_INCLUDED_PY_LOCALNAMES_H |
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.