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

newlib-nano: Printf formatting does not work properly for some numeric types #1891

Open
miri64 opened this issue Oct 28, 2014 · 62 comments
Open
Assignees
Labels
Platform: ARM Platform: This PR/issue effects ARM-based platforms State: don't stale State: Tell state-bot to ignore this issue Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)

Comments

@miri64
Copy link
Member

miri64 commented Oct 28, 2014

Possibly other boards are effected, too. Compare the following program

#include <stdio.h>
#include <inttypes.h>

int main(void)
{
    uint64_t test = 0xabcdef0123456789;

    printf("%llx\n", test);
    printf("%lx\n", test);
    printf("%" PRIx64 "\n", test);

    return 0;
}

on iot-lab_M3

2014-10-28 13:15:00,163 - INFO # kernel_init(): This is RIOT! (Version: 2014.05-1144-gd91ec-beutlin-od)
2014-10-28 13:15:00,165 - INFO # kernel_init(): jumping into first task...
2014-10-28 13:15:00,166 - INFO # lx
2014-10-28 13:15:00,166 - INFO # a
2014-10-28 13:15:00,166 - INFO # lx

on native

kernel_init(): This is RIOT! (Version: 2014.05-1145-ga2223-beutlin-od)
kernel_init(): jumping into first task...
abcdef0123456789
23456789
abcdef0123456789

and on msba2

2014-10-28 13:15:00,163 - INFO # kernel_init(): This is RIOT! (Version: 2014.05-1144-gd91ec-beutlin-od)
2014-10-28 13:15:00,165 - INFO # kernel_init(): jumping into first task...
2014-10-28 13:15:00,166 - INFO # abcdef0123456789
2014-10-28 13:15:00,166 - INFO # 23456789
2014-10-28 13:15:00,166 - INFO # abcdef0123456789
@miri64 miri64 added the Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) label Oct 28, 2014
@miri64 miri64 added this to the Release NEXT MAJOR milestone Oct 28, 2014
@miri64
Copy link
Member Author

miri64 commented Oct 28, 2014

For msba2 and iot-lab_M3 I used the same toolchain (ARM Embedded with gcc 4.8.4 20140725), btw.

@miri64 miri64 changed the title Printf formating does not work properly on iot-lab_M3 for 64 bit numbers. Printf formatting does not work properly on iot-lab_M3 for 64 bit numbers. Oct 28, 2014
@Kijewski
Copy link
Contributor

Please try printf("%Lx\n", test); and printf("%" PRIx64 "\n", test);

@miri64
Copy link
Member Author

miri64 commented Oct 28, 2014

2014-10-28 13:51:09,132 - INFO # 1
2014-10-28 13:51:09,132 - INFO # lx

respectively

@thomaseichinger
Copy link
Member

This is a result of newlib-nano. When omitting

export LINKFLAGS += -specs=nano.specs -lc -lnosys

in boards/iot-lab_M3/Makfile.include results should be correct.
For the record, printing floating point numbers doesn't work neither.
Could you verify this for me?

@OlegHahm
Copy link
Member

I only get the right result when setting -O0

@thomaseichinger
Copy link
Member

With arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 4.8.4 20140725 (release) [ARM/embedded-4_8-branch revision 213147] and -Os without nano.specs I get

2014-10-28 15:16:00,505 - INFO # abcdef0123456789
2014-10-28 15:16:00,505 - INFO # a
2014-10-28 15:16:00,505 - INFO # abcdef0123456789

The second printf throws a warning

main.c:31:5: warning: format '%lx' expects argument of type 'long unsigned int', but argument 2 has type 'uint64_t' [-Wformat=]
     printf("%lx\n", test);
     ^

@miri64
Copy link
Member Author

miri64 commented Oct 28, 2014

Get the same result with the same configuration as @thomaseichinger has. The second printf was just a fall-back case, since %llx wasn't functioning. So everything is fine.
Should I close this as a memo for future references?

@OlegHahm
Copy link
Member

Hm, there's certainly an issue with some formatters and printf in combination with a certain toolchain, platform, and optimization flags.

@thomaseichinger
Copy link
Member

@OlegHahm could you tell us your toolchain version and platform? I'd add a section to iot-lab_M3's wiki page.

@thomaseichinger
Copy link
Member

As this issue is related to newlib-nano close this as Memo? @OlegHahm @authmillenon

@OlegHahm
Copy link
Member

arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 4.8.4 20140526 (release) [ARM/embedded-4_8-branch revision 211358] in default configuration gives:

2014-11-24 19:12:46,271 - INFO # kernel_init(): This is RIOT! (Version: e5a3-tbilisi)
2014-11-24 19:12:46,272 - INFO # kernel_init(): jumping into first task...
2014-11-24 19:12:46,273 - INFO # lx
2014-11-24 19:12:46,273 - INFO # a
2014-11-24 19:12:46,273 - INFO # lx

arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 4.8.4 20140526 (release) [ARM/embedded-4_8-branch revision 211358] w/o nano.specs gives:

2014-11-24 19:06:42,607 - INFO # kernel_init(): This is RIOT! (Version: e5a3-tbilisi)
2014-11-24 19:06:42,607 - INFO # kernel_init(): jumping into first task...
2014-11-24 19:06:42,607 - INFO # lx
2014-11-24 19:06:42,608 - INFO # a
2014-11-24 19:06:42,608 - INFO # lx

arm-none-eabi-gcc (Arch Repository) 4.9.2 (also w/o nano.specs) gives:

2014-11-24 19:03:54,716 - INFO # kernel_init(): This is RIOT! (Version: e5a3-tbilisi)
2014-11-24 19:03:54,717 - INFO # kernel_init(): jumping into first task...
2014-11-24 19:03:54,717 - INFO # 2345678900000001
2014-11-24 19:03:54,717 - INFO # a
2014-11-24 19:03:54,718 - INFO # 234567890000000a

@OlegHahm OlegHahm modified the milestones: Release 2014.12, Release NEXT MAJOR Jan 6, 2015
@DipSwitch
Copy link
Member

This problem is probably caused by the stack alignment. Cortex-M stacks MUST be 8 byte aligned I see in the implementation of thread.c you are only aligning to the processor register size.

@OlegHahm
Copy link
Member

@DipSwitch
Copy link
Member

Yeah I just saw it in the thread_arch, I've seen this before when the stack was misaligned, my bad.

@OlegHahm
Copy link
Member

No problem.

@PeterKietzmann
Copy link
Member

But the described bug in this issue is still present?

@OlegHahm
Copy link
Member

Long time I haven't checked.

@PeterKietzmann
Copy link
Member

Possible job for @A-Paul ? Or are you already busy with work?

@DipSwitch
Copy link
Member

Bug is still present. Checked today, to see if the stack alignment was correct and it was.

@OlegHahm
Copy link
Member

OlegHahm commented Dec 4, 2015

Wondering if there is anything we can do about this. Seems more like Newlib-nano issue.

@miri64 miri64 mentioned this issue Oct 24, 2017
6 tasks
@kYc0o kYc0o added this to To Do - Other platform issues in Bug tracker 2018.04 Oct 31, 2017
@cladmi
Copy link
Contributor

cladmi commented Jan 24, 2018

This could definitely go into a documentation, its common that I write something like printf("size: %zu\n", size_t_variable); And it works for some platforms but not all as newlib-nano does not have all supports. I think its usually solved by casting to (unsigned int) but I don't think I have seen a place where this is summarized.

@kYc0o
Copy link
Contributor

kYc0o commented Apr 17, 2018

Can't we instead enforce the usage of fmt and portable printf descriptors?

it works for some platforms but not all as newlib-nano does not have all supports

Is this really per platform or per architecture? It's hard for me to think that such a printf() works in a samr21-xpro but not in a iotlab-m3...

@kaspar030
Copy link
Contributor

I think its usually solved by casting to (unsigned int) but I don't think I have seen a place where this is summarized.

That doesn't help. The problem is that depending on compile time configuration of newlib, it might or might not support "%z".

Is this really per platform or per architecture? It's hard for me to think that such a printf() works in a samr21-xpro but not in a iotlab-m3...

It totally depends on how the C library was compiled.

@kYc0o
Copy link
Contributor

kYc0o commented Jul 17, 2018

So what do we do here? It seems that is also a won't fix...

@cladmi
Copy link
Contributor

cladmi commented Aug 6, 2018

I added some reference in the coding conventions:

https://github.com/RIOT-OS/RIOT/wiki/Coding-conventions#-wformat

Feel free to add more informations and rephrase.

@jnohlgard jnohlgard changed the title newlib-nano: Printf formatting does not work properly for some numberic types newlib-nano: Printf formatting does not work properly for some numeric types Sep 11, 2018
@stale
Copy link

stale bot commented Aug 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions.

@stale stale bot added the State: stale State: The issue / PR has no activity for >185 days label Aug 10, 2019
@stale stale bot closed this as completed Sep 10, 2019
Bug tracker 2018.04 automation moved this from To Do - Other platform issues to Done Sep 10, 2019
@fjmolinas fjmolinas added State: don't stale State: Tell state-bot to ignore this issue and removed State: stale State: The issue / PR has no activity for >185 days labels Oct 29, 2021
@fjmolinas
Copy link
Contributor

This should not have been closed, its still an issue, the thing happens with PRIu8 BTW

@maribu
Copy link
Member

maribu commented May 17, 2023

I see the following approaches to move forward:

  1. Document that printf with newlib_nano is not fully working
  2. Add a printf_int_extra module (or so), similar to printf_float, which would force a standard C lib / C lib variant with support for all integer format specifiers
  3. Implement a custom printf() for newlib. (Which likely would be both smaller and better)
  4. Close this issue, sneak away, and hope nobody notices.

@OlegHahm
Copy link
Member

I'm wondering if we could even implement a compile-time warning when using newlib_nano and printf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: ARM Platform: This PR/issue effects ARM-based platforms State: don't stale State: Tell state-bot to ignore this issue Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
No open projects
Development

No branches or pull requests