Skip to content
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

Print and debug improvements #1750

Merged
merged 3 commits into from
Jun 29, 2022

Conversation

matthijskooijman
Copy link
Contributor

Summary

This PR makes some changes to the printing/printf code, both in the Print class and in core_debug.

The main change that I needed is to add a "v" version of the core_debug() function. Like vprintf vs printf, the vcore_debug function does not accept individual arguments to interpolate into format string, but a va_list instance. This allows it to be called from another function that is itself a varargs function and forwards its variable arguments.

I originally thought I needed this for Print::printf, so I implemented Print::vprintf too. I ended up using core_debug() instead, but the vprintf() implementation might be useful for others (and adds zero code space if unused), so I just added it.

Finally, when looking at the core_debug() code, I noticed the function was static and declared in a header file, which causes it to potentially take up a lot more flash space than needed, so I fixed that as well. See the commit message for details.

Static functions are private to the compilation unit they are emitted
in, so they cannot be shared between compilation units. This means that
any source file that uses `core_debug()` where the compiler does not
inline (all) calls, will have its own private copy of this function
emitted. In practice, gcc seems to never inline this function (even with
-O3), leading to one copy of the function for each compilation unit it
is used in.

This fixes this by removing the `static` keyword from the function.
However, this prevents the function from being emitted completely in
C compilation units (C++ is different and emits multiple copies,
discarding all but one later). This means that if the function is only
used from C compilation units and not inlined everywhere, you get
a linker error. Thet `static` keyword was probably added to work around
this, without realizing the overhead.

The proper way to prevent this linker error is to add an `extern`
definition for the function in a single source file, so this adds
a `core_debug.c` with exactly that.

In practice, this means that this commit saves 40 bytes of space for
each compilation unit where `core_debug()` is used (beyond the first).
Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

Hi @matthijskooijman

Thanks for the PR.
You have to move this include outside the #if !defined(NDEBUG)

else it does not built properly when Core logs are not enabled 😉

And please fix astyle issue.

@fpistm fpistm added this to In progress in STM32 core based on ST HAL via automation Jun 27, 2022
@fpistm fpistm added this to the 2.3.1/2.4.0 milestone Jun 27, 2022
This does the same as core_debug, but (like printf vs vprintf) accepts
an already processed va_list to allow other vararg functions to forward
their argument lists to this function.
This does the same as printf, but (like the vprintf libc function)
accepts an already processed va_list to allow other vararg functions to
forward their argument lists to this function.
@matthijskooijman
Copy link
Contributor Author

Good call, should have tested that. Fixed (and tested ;-p) now.

Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

Thanks @matthijskooijman for this PR, it LGTM.
About the failed CI, it is not linked to this PR but to an update of the arduino-lint which raised new error/warning. This is fixed thanks #1754.

STM32 core based on ST HAL automation moved this from In progress to Reviewer approved Jun 29, 2022
@fpistm fpistm merged commit d1be5bd into stm32duino:main Jun 29, 2022
STM32 core based on ST HAL automation moved this from Reviewer approved to Done Jun 29, 2022
@matthijskooijman
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants