[SOLVED] sys.settrace on ESP8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
sebi
Posts: 36
Joined: Tue Mar 29, 2016 9:36 pm
Location: France

[SOLVED] sys.settrace on ESP8266

Post by sebi » Thu Sep 10, 2020 11:02 pm

Dear community,
I would like to play with sys.settrace(tracefunc) on an ESP8266.
Do you know if it's feasible?
According to those explanations on Github @Damien "added some commits (fixes) to get it building on most of the ports".
Do you know if it works on an ESP8266?
I added those 3 lines to mpconfigport.h:

Code: Select all

#define MICROPY_PY_SYS_SETTRACE       (1) // To activate sys.settrace(tracefunc)
#define MICROPY_COMP_CONST            (0) // required for MICROPY_PY_SYS_SETTRACE
#define MICROPY_PERSISTENT_CODE_SAVE  (1) // required for MICROPY_PY_SYS_SETTRACE
...but still I am getting this error when building:

Code: Select all

../../py/persistentcode.c:857:2: error: #error mp_raw_code_save_file not implemented for this platform
Do you know what mp_raw_code_save_file is used for?
Is it because the ESP8266 has limited memory that this function hasn't been implemented?
Thanks,
Last edited by sebi on Sat Sep 12, 2020 11:38 am, edited 1 time in total.

User avatar
jimmo
Posts: 1908
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Issues with sys.settrace on ESP8266

Post by jimmo » Fri Sep 11, 2020 12:08 am

sebi wrote:
Thu Sep 10, 2020 11:02 pm
Do you know what mp_raw_code_save_file is used for?
Is it because the ESP8266 has limited memory that this function hasn't been implemented?
It's used by mpy-cross to write out a .mpy file.

mpy-cross works just like a regular MicroPython port -- it takes your .py file, loads it, compiles it to bytecode, but then instead of executing it writes it out to a file. The MICROPY_PERSISTENT_CODE_SAVE macro is used to enable functionality used by mpy-cross.

However, settrace needed a lot of the same functionality, so it was probably easy during the development of settrace just to use the same flag. But settrace doesn't need mp_raw_code_save_file.
We probably need to split MICROPY_PERSISTENT_CODE_SAVE into two flags -- one for having the compiler manage the additional metadata (used by mpy-cross and settrace) and one for having functions like mp_raw_code_save_file (used only by mpy-cross).

So you might be able to get this working by just commenting out that "#error mp_raw_code_save_file not implemented for this platform" line in persistentcode.c (as nothing in a regular non-mpy-cross build requires a definition of this function). But I don't know if there are other dependencies that you will run in to.

User avatar
sebi
Posts: 36
Joined: Tue Mar 29, 2016 9:36 pm
Location: France

Re: Issues with sys.settrace on ESP8266

Post by sebi » Fri Sep 11, 2020 10:01 pm

Actually I decided to add #define MICROPY_PERSISTENT_CODE_SAVE (1) and #define MICROPY_COMP_CONST (0) to solve the errors I was getting when building with #define MICROPY_PY_SYS_SETTRACE (1) only as I was supposed to:

Code: Select all

../../py/mpconfig.h:1670:2: error: #error "MICROPY_PY_SYS_SETTRACE requires MICROPY_PERSISTENT_CODE_SAVE to be enabled"
#error "MICROPY_PY_SYS_SETTRACE requires MICROPY_PERSISTENT_CODE_SAVE to be enabled"
  ^
../../py/mpconfig.h:1673:2: error: #error "MICROPY_PY_SYS_SETTRACE requires MICROPY_COMP_CONST to be disabled"
#error "MICROPY_PY_SYS_SETTRACE requires MICROPY_COMP_CONST to be disabled"
  ^
Maybe there is a different way to solve those errors that doesn't involve MICROPY_PERSISTENT_CODE_SAVE which was intended for mpy-cross in the first place as you explained?

Anyhow, commenting out that #error mp_raw_code_save_file not implemented for this platform line in persistentcode.c allowed make to go one step further. However, now I get this error:

Code: Select all

CC build-GENERIC/frozen_content.c
build-GENERIC/frozen_content.c:271:5: error: redeclaration of enumerator 'MP_QSTR_exception'
     MP_QSTR_exception,
     ^
In file included from ../../py/obj.h:33:0,
                 from ../../py/objint.h:30,
                 from build-GENERIC/frozen_content.c:15:
build-GENERIC/genhdr/qstrdefs.generated.h:496:6: note: previous definition of 'MP_QSTR_exception' was here
QDEF(MP_QSTR_exception, (const byte*)"\xd2\xeb\x09" "exception")
      ^
../../py/qstr.h:41:23: note: in definition of macro 'QDEF'
#define QDEF(id, str) id,
                       ^
make: *** [../../py/mkrules.mk:63: build-GENERIC/build-GENERIC/frozen_content.o] Error 1
This has to do with a redeclaration. I have no idea how to solve this.

User avatar
dhylands
Posts: 3465
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Issues with sys.settrace on ESP8266

Post by dhylands » Fri Sep 11, 2020 10:10 pm

Usually, if you run into QSTR errors like this, the simple solution is to do a clean followed by a rebuild.

User avatar
sebi
Posts: 36
Joined: Tue Mar 29, 2016 9:36 pm
Location: France

Re: Issues with sys.settrace on ESP8266

Post by sebi » Sat Sep 12, 2020 10:52 am

Indeed it built perfectly after make clean.
Thanks!
I will now experiment with sys.settrace.

User avatar
sebi
Posts: 36
Joined: Tue Mar 29, 2016 9:36 pm
Location: France

Re: [SOLVED] sys.settrace on ESP8266

Post by sebi » Sun Sep 13, 2020 5:47 pm

I am playing with sys.settrace on an ESP8266 an I find it to work quite well: I am using it to send some information on UART(1) in order to help me analyse how my code is running.

However now I get an error that I was not having when using the standard firmware (the one without sys.settrace):

Code: Select all

>>> import usyslog
Traceback (most recent call last):       
  File "<stdin>", line 1, in <module>    
  File "usyslog.py", line 11, in <module>
NameError: name 'const' isn't defined
>>>
I have checked that micropython.const still exists in that firmware:

Code: Select all

>>> import micropython
>>> micropython.const(1)
1
>>>
I believe there is a bug with the sys.settrace-enabled firmware but I have no clue how to find it??!

-------------------------

EDIT: to solve this issue I added from micropython import const before line 11 in usyslog.py.
Now the question is:
Why did import usyslog worked flawlessly with the standard firmware without this modification?
Or more exactly why a = const(1) works by default with the normal firmware:

Code: Select all

>>> const(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'const' isn't defined
>>> a = const(1)
>>>
while with the sys.settrace-enabled firmware one has to import const before:

Code: Select all

>>> const(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'const' isn't defined
>>> a = const(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'const' isn't defined
>>> from micropython import const
>>> const(1)
1
>>> a = const(1)
>>>
?

User avatar
jimmo
Posts: 1908
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: [SOLVED] sys.settrace on ESP8266

Post by jimmo » Mon Sep 14, 2020 11:53 pm

sebi wrote:
Sun Sep 13, 2020 5:47 pm
Why did import usyslog worked flawlessly with the standard firmware without this modification?
Because you disabled it when you were enabling settrace :)

Code: Select all

#define MICROPY_COMP_CONST            (0) // required for MICROPY_PY_SYS_SETTRACE
The const() feature is built-in and cases the parse tree to be modified at compile time. This probably doesn't work well with settrace.

But importing const() from micropython you get a no-op version that just returns its argument.

User avatar
sebi
Posts: 36
Joined: Tue Mar 29, 2016 9:36 pm
Location: France

Re: [SOLVED] sys.settrace on ESP8266

Post by sebi » Tue Sep 15, 2020 10:15 am

jimmo wrote:
Mon Sep 14, 2020 11:53 pm
Because you disabled it when you were enabling settrace :)
Indeed I should have guessed this :?
Thanks!
I love the potential of sys.settrace!
I wonder why no programming guru in the community has built a kind of debugger/tracer using it yet?!

User avatar
aivarannamaa
Posts: 73
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: [SOLVED] sys.settrace on ESP8266

Post by aivarannamaa » Tue Sep 15, 2020 10:26 am

sebi wrote:
Tue Sep 15, 2020 10:15 am
I wonder why no programming guru in the community has built a kind of debugger/tracer using it yet?!
I suppose it's because there is no way to query local variables, ie. you can't get rid of print-debugging even if you get breakpoints working.
Aivar Annamaa
https://thonny.org

Post Reply