Skip to content

Improve sys.settrace to help support debugpy / pdb debugging#8767

Open
andrewleech wants to merge 1 commit into
micropython:masterfrom
andrewleech:pdb_support
Open

Improve sys.settrace to help support debugpy / pdb debugging#8767
andrewleech wants to merge 1 commit into
micropython:masterfrom
andrewleech:pdb_support

Conversation

@andrewleech
Copy link
Copy Markdown
Contributor

@andrewleech andrewleech commented Jun 16, 2022

Companion support for micropython/micropython-lib#1022 & micropython/micropython-lib#499

Requires micropython be built with MICROPY_PY_SYS_SETTRACE eg. unix standard variant

Includes:

@andrewleech andrewleech force-pushed the pdb_support branch 2 times, most recently from 16bd204 to e027113 Compare June 19, 2022 23:51
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jun 20, 2022

Codecov Report

❌ Patch coverage is 51.42857% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.39%. Comparing base (2dc2e30) to head (3280c2a).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
py/profile.c 44.82% 16 Missing ⚠️
py/modsys.c 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8767      +/-   ##
==========================================
- Coverage   98.46%   98.39%   -0.08%     
==========================================
  Files         176      176              
  Lines       22784    22816      +32     
==========================================
+ Hits        22435    22450      +15     
- Misses        349      366      +17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

andrewleech pushed a commit to andrewleech/micropython-lib that referenced this pull request Jun 20, 2022
andrewleech pushed a commit to andrewleech/micropython-lib that referenced this pull request Jun 20, 2022
andrewleech pushed a commit to andrewleech/micropython-lib that referenced this pull request Jun 22, 2022
Requires micropython to be compiled with MICROPY_PY_SYS_SETTRACE.
Also requires micropython/micropython#8767
andrewleech pushed a commit to andrewleech/micropython-lib that referenced this pull request Jun 22, 2022
Requires micropython to be compiled with MICROPY_PY_SYS_SETTRACE.
Also requires micropython/micropython#8767
andrewleech pushed a commit to andrewleech/micropython-lib that referenced this pull request Jun 22, 2022
Requires micropython to be compiled with MICROPY_PY_SYS_SETTRACE.
Also requires micropython/micropython#8767
Comment thread py/modsys.c Outdated
MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_gettrace_obj, mp_sys_gettrace);

// _getframe(): Return current frame object.
STATIC mp_obj_t mp_sys_getframe(size_t n_args, const mp_obj_t *args) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please call it mp_sys__getframe

Comment thread py/objfun.c Outdated
if (attr == MP_QSTR___code__) {
mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_code_t *code = MP_OBJ_TO_PTR(mp_obj_new_code(self->context, self->rc));
if (code != NULL) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can code be null?

Comment thread py/profile.c Outdated
case MP_QSTR_f_back:
dest[0] = mp_const_none;
if (o->code_state->prev_state) {
if (!o->code_state->prev_state->frame) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you find a case where this could be NULL? maybe it needs a test...

Comment thread py/profile.c Outdated
dest[0] = o->trace_obj;
break;
case MP_QSTR_f_locals:
dest[0] = MP_OBJ_FROM_PTR(o->code->dict_locals);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed? locals in uPy doesn't work well

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just return an empty dict instead?

Comment thread py/profile.c Outdated
o->lineno = mp_prof_bytecode_lineno(rc, o->lasti);
o->trace_opcodes = false;
o->callback = MP_OBJ_NULL;
o->trace_obj = MP_OBJ_NULL;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe call it f_trace

Comment thread py/profile.h Outdated

// This is the implementation for the sys.settrace
mp_obj_t mp_prof_settrace(mp_obj_t callback);
mp_obj_t mp_prof_gettrace();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please put void in arg list

Comment thread py/profile.c Outdated
return mp_const_none;
}

mp_obj_t mp_prof_gettrace() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void in arg list

Comment thread py/profile.c Outdated
return prof_trace_cb;
}

mp_obj_t mp_prof_get_frame(int depth) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change int to size_t to indicate it can't be negative?

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jun 29, 2022
@dpgeorge dpgeorge changed the title Draft: Working towards pdb debugging support. Improve sys.settrace support to help support bdb/pdb debugging Jun 29, 2022
@dpgeorge dpgeorge changed the title Improve sys.settrace support to help support bdb/pdb debugging Improve sys.settrace to help support bdb/pdb debugging Jun 29, 2022
andrewleech pushed a commit to andrewleech/micropython-lib that referenced this pull request Aug 24, 2023
Requires micropython to be compiled with MICROPY_PY_SYS_SETTRACE.
Also requires micropython/micropython#8767
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Aug 24, 2023

Code size report:

Reference:  esp32/boards/SEEED_XIAO_ESP32C6: Add new XIAO board definition. [2dc2e30]
Comparison: sys/settrace: Improve settrace to support debugpy/pdb debugging. [merge of e0237a8]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64: +35616 +4.158% standard[incl +13536(data) +32(bss)]
      stm32:    +0 +0.000% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

RetiredWizard pushed a commit to RetiredWizard/micropython that referenced this pull request Dec 30, 2023
@projectgus
Copy link
Copy Markdown
Contributor

This is an automated heads-up that we've just merged a Pull Request
that removes the STATIC macro from MicroPython's C API.

See #13763

A search suggests this PR might apply the STATIC macro to some C code. If it
does, then next time you rebase the PR (or merge from master) then you should
please replace all the STATIC keywords with static.

Although this is an automated message, feel free to @-reply to me directly if
you have any questions about this.

andrewleech pushed a commit to andrewleech/micropython-lib that referenced this pull request Jun 11, 2025
Requires micropython to be compiled with MICROPY_PY_SYS_SETTRACE.
Also requires micropython/micropython#8767
@andrewleech andrewleech changed the title Improve sys.settrace to help support bdb/pdb debugging Improve sys.settrace to help support debugpy / pdb debugging Jun 11, 2025
@andrewleech andrewleech force-pushed the pdb_support branch 4 times, most recently from 0bbcb6e to e0237a8 Compare March 26, 2026 05:12
Add sys.gettrace() to mirror settrace, add sys._getframe() for stack
frame inspection, and enable sys.settrace() by default on unix
standard variant. These additions provide the baseline infrastructure
needed for pdb and debugpy to function on MicroPython.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants