-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
esp32/mphalport: Add _FUNCTION_, _LINE_, _FILE_ info to check_esp_err(). #10888
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
esp32/mphalport: Add _FUNCTION_, _LINE_, _FILE_ info to check_esp_err(). #10888
Conversation
feaf5f5
to
f97c02b
Compare
@robert-hh |
I see that it makes life easier to tell, where an error occurred. It increase the code size. For a GENERIC build it is 2080 bytes, or 0.12%. This seems not much, especially in relation, but Damien refused to include the Pin.cpu table just because of an even smaller code size increase. |
Thanks @robert-hh
I think that this is a small fee for convenience in development and informativeness when using.
Could you draw @dpgeorge attention to this PR? |
The code size increase dropped from 2080 bytes to 1792 bytes. At ESP32 ports there usually a lot of flash space available, and RTOS seems to use lot of it. The additional information is useful, but most of the time needed during development. So maybe you could use a DEBUG type switch to enable it. |
df39831
to
3257d02
Compare
@dpgeorge |
83d5c05
to
ca2ec2f
Compare
@andrewleech P.S. I hope I haven't been too pushy lately. |
Yeah PR requests really aren't standard practice around here... but if you'd like to review some of my PR's I'd be happy to return the favour ;-) That being said I rarely go near esp hardware so don't know that much about the port. |
In this case, adding this kind of debug information takes a lot of flash space which only really provides benefit to C developers, not the general micropython users. |
From #12346 (comment)
I think we have an agreement. |
7864cd5
to
4295f24
Compare
If MICROPY_ERROR_REPORTING is equal to the MICROPY_ERROR_REPORTING_NORMAL during final compilation If MICROPY_ERROR_REPORTING is equal to the MICROPY_ERROR_REPORTING_DETAILED during development Thanks |
@jimmo @DvdGiessen @UnexpectedMaker @projectgus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks potentially very useful to me, thanks @IhorNehrutsa . I only have one major concern, see the inline comment.
e000297
to
d25d168
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with the change to include the information in the OSError itself. Thanks @IhorNehrutsa!
@IhorNehrutsa The CI commit message check is failing on the follow-up commit because the Sign-off has to be at the end. Could you squash these to one commit, please? I appreciate the Co-Author credit but I'm happy for the commit to just be from you, you did all the work. :) |
d25d168
to
008d306
Compare
I use GitHub Desktop. Sometimes it behaves strangely. Thanks @projectgus |
The raised OSError should have as its first argument an integer error code. The second argument is optional and can be a descriptive string. So this PR needs to be changed so that the function/line/file info is added to the string that is the second argument. You'll need to keep the For example the output should be something like:
|
@dpgeorge
It looks like the bug inside the snprintf() |
ports/esp32/mphalport.c
Outdated
char err_msg[64]; | ||
esp_err_to_name_r(code, err_msg, sizeof(err_msg)); | ||
char str[256]; | ||
snprintf(str, sizeof(str), MP_ERROR_TEXT("0x%04X, in function '%s' at line %d in file '%s'"), code, func, line, file); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
snprintf won't work with MP_ERROR_TEXT
. Just remove the MP_ERROR_TEXT
macro.
ports/esp32/mphalport.c
Outdated
o_str2->len = strlen((char *)o_str2->data); | ||
o_str2->hash = qstr_compute_hash(o_str2->data, o_str2->len); | ||
// raise | ||
mp_obj_t args[3] = { MP_OBJ_NEW_SMALL_INT(pcode), MP_OBJ_FROM_PTR(o_str), MP_OBJ_FROM_PTR(o_str2)}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to just have two arguments, the pcode and then an error message string.
With the last commit, it looks as expected
Thanks @dpgeorge |
ports/esp32/mphalport.c
Outdated
esp_err_to_name_r(code, err_msg, sizeof(err_msg)); | ||
char str[512]; // 256 bytes size leeds to abnormal output | ||
snprintf(str, sizeof(str), "0x%04X %s in function '%s' at line %d in file '%s'", code, err_msg, func, line, file); | ||
o_str->data = (const byte *)str; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work because the str object is taking a pointer to the C stack, and the data on the C stack will not be valid once this function returns. Instead, the string data must be dynamically allocated here. Probably easiest to use vstr_printf
for that.
ports/esp32/mphalport.c
Outdated
char err_msg[64]; | ||
esp_err_to_name_r(code, err_msg, sizeof(err_msg)); | ||
vstr_t vstr; | ||
vstr_init(&vstr, 256); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
256 is quite large. I suggest something smaller like 80.
ports/esp32/mphalport.c
Outdated
vstr_t vstr; | ||
vstr_init(&vstr, 256); | ||
vstr_printf(&vstr, "0x%04X %s in function '%s' at line %d in file '%s'", code, err_msg, func, line, file); | ||
o_str->data = (const byte *)vstr.buf; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to use vstr_null_terminated
to access the buffer and make sure it's null terminated.
Currently, check_esp_err() raises an exception without a location in the source code, eg: Traceback (most recent call last): File "<stdin>", line 8, in <module> OSError: (-258, 'ESP_ERR_INVALID_ARG') This commit allows additional error reporting (function, line and file) to be enabled via detailed exceptions. Change the error reporting config to #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED) and then exception messages from IDF errors look like: Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: (-258, "0x0102 ESP_ERR_INVALID_ARG in function 'set_duty_u16' at line 342 in file './machine_pwm.c'") Signed-off-by: Ihor Nehrutsa <IhorNehrutsa@gmail.com>
177a4cb
to
00930b2
Compare
Thanks for updating based on the feedback. This is now merged. |
This PR can be useful for ESP32 developers.
Now check_esp_err() raises an exception without a location in the source code.
This PR adds debugging information to the exception and will be useful during code development.
Info about FUNCTION, LINE, FILE may be added.
During the development change the
micropython/ports/esp32/mpconfigport.h
Line 50 in 3637252
to the
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED)
REPL output may look like
or in a different way.
Discussion is welcome.
Edited:
If MICROPY_ERROR_REPORTING is equal to the MICROPY_ERROR_REPORTING_NORMAL during final compilation
then the size of the binary code is not increased.
If MICROPY_ERROR_REPORTING is equal to the MICROPY_ERROR_REPORTING_DETAILED during development
then FUNCTION, LINE, FILE information is added to the output.
Edited:
With last commit output like:
Edited:
With last commit output like: