-
Notifications
You must be signed in to change notification settings - Fork 3k
Add minimal printf #11051
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
Add minimal printf #11051
Conversation
Supports %d %u %X %p %s Doesn't use malloc
Use mbed_app.json to enable floating points. Automatically ignore flags and width specifiers.
This makes it possible to use the minimal printf and snprintf through mbed_retarget.cpp
Add support for width specifiers
Add compile profiles
This is used by mbed-os-linker-report and doesn't increase the code size.
Add "-g" to 'release' and 'develop' profiles
ssize_t doesn't exist at all in armcc.
Remove references to ssize_t in the code
This commit adds mostly integer (and buffer) overflow checks for the current buffer index (`result` variable).
- Improved comments to explain the checks on 'result'. - Check for non-NULL format specifier.
[BUGFIX][IOTUC-18] Library fixes
The minimal-printf implementation supports a number of length modifiers (j, z and t) that are not supported by the native mbed OS libc implementation. The compliance test has tests for these modifiers, which means that it isn't possible to check the output of mbed-printf against a known good implementation (libc's printf) when running on mbed OS. This, in turn, can give the impression that the tests for these modifiers pass, when that might not be the case. To address this issue, this PR removes the tests for these modifiers in mbed OS. This PR was created because some of the tests for these modifiers actually fail in Linux, for example: ``` >>> Running case ARMmbed#3: 'printf %u'... hhu: 0 hhu: 0 hhu: 255 hhu: 255 hu: 0 hu: 0 hu: 65535 hu: 65535 u: 0 u: 0 u: 4294967295 u: 4294967295 lu: 0 lu: 0 lu: 4294967295 lu: 4294967295 llu: 0 llu: 0 llu: 18446744073709551615 llu: 18446744073709551615 ju: 0 ju: 0 ju: 4294967295 ju: 18446744073709551615 :188::FAIL: Expected 7 Was 16 >>> 'printf %u': 0 passed, 1 failed with reason 'Assertion Failed' ```
In the implementation, don't always display double hex digits when printing with "%X". This is in line with the behaviour observed both in mbed OS's printf (Newlib) and Linux's printf (glibc). In the tests, always compare the baseline result with the result returned by the minimal printf implementation, instead of comparing with a constant value.
The printf(3) man page says "The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit, then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available." The last part of this spec (returning the number of characters which would have been written to the string if enough space had been available) was not implemented in minimal-printf. This PR changes that by redirecting all the processed characters through the "minimal_printf_putchar" helper function. This function will not write to the buffer if there's no space left, but it will always increment the number of written characters, in order to match the above description. minimal_printf_putchar also contains checks for overflows, so these checks are no longer needed in the rest of the code. Other changes: - In some cases, mbed_minimal_formatted_string didn't put the string terminator in the output buffer. This PR ensures that this always happens. - Fixed a bug in printed hexadecimal numbers introduced by a previous commit. - Added buffer overflow tests for snprintf.
Implementation and test fixes
Change the default output from STDIO UART to SWO by overriding: "minimal-printf.console-output": "SWO"
I have squashed all the commits done for the integration in mbed-os and review comments into the last two commits. All previous commits come from the original library and it would beb useful to keep them. All comments have now been addressed. |
CI started |
Test run: FAILEDSummary: 3 of 4 test jobs failed Failed test jobs:
|
Test run: FAILEDSummary: 1 of 11 test jobs failed Failed test jobs:
|
Test run: SUCCESSSummary: 11 of 11 test jobs passed |
Run 5 seemed to have failed due to a license issue
Job restarted and now successful. |
@SeppoTakalo Are you happy with the commit history from the original library + three extra commits for this PR? |
Test run: SUCCESSSummary: 11 of 11 test jobs passed |
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 but optimization of the arm toolchain should improved in the future as per #11051 (comment).
Any review from @ARMmbed/mbed-os-tools ? |
The part of this PR which affects tools has already been reviewed (and modified) by @madchutney. PR is ready to go. |
My issue is that this conflates two independent things - "minimal printf" and "simplified stdin/out retargetting", and attempts to short-circuit the retargetting in printf itself, without actually removing it. This PR fails with a very simple test program:
The minimal printf should not be trying to work around the retargetting - it should just use If you want to simplify the retargetting, do it properly in a separate PR - that could make it so that And the two options - simplified printf and simplified retargetting should be totally independent of each other. There's no reason to link them. |
Description
This PR imports https://github.com/ARMmbed/minimal-printf into mbed-os.
To replace the standard implementation of the printf functions with the ones from the minimal-printf library, the custom
minimal-printf
profile should be used when compiling with mbed-cli. For example:Minimal-printf supports floating point and stream printing by default. It has the following restrictions:
Pelion Device Management Client example compiled with GCC_ARM, release profile compared to current master (with std printf):
Pull request type
Reviewers
@bulislaw @hugueskamba
Release Notes
To replace the standard implementation of the printf functions with the ones from the minimal-printf library, the custom
minimal-printf
profile should be used when compiling with mbed-cli. For example: