Skip to content

libs/libc/stream: Add profiles and options to printf(). - #21493

Merged
acassis merged 10 commits into
apache:masterfrom
casaroli:printf-options-up
Nov 8, 2026
Merged

acassis merged 10 commits into
apache:masterfrom
casaroli:printf-options-up

Conversation

@casaroli

@casaroli casaroli commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Part of #21467, step 2.

Summary

#21474 makes printf safe for correct calls and adds a few bytes. This PR lets a board take bytes out again, and gives three profiles:

  • Default: today's features. It is the default, and existing configs do not change.
  • Minimal: removes what a small board can do without.
  • Full: turns on floating point, numbered arguments and locale.

Each option has its own prompt, so Minimal or Default can add or remove one feature at a time. A removed conversion still reads its argument at the right size and prints ?, so the next conversion stays correct.

Option Minimal Default Full With the option off
LIBC_FLOATINGPOINT off as today on %f: ?
LIBC_NUMBERED_ARGS off off on as today
LIBC_LOCALE off off on as today
LIBC_PRINTF_LONG_LONG on with SYSLOG_TIMESTAMP or FS_PROCFS, else off on on %lld, %jd: ?
LIBC_PRINTF_ALT_FORM off on on %#x of 255 prints ff; %p keeps 0x
LIBC_PRINTF_INT_PRECISION off on on %.3d: ?; %.3s stays
STREAM_OUT_BUFFER_SIZE 0 64 64 each stream call of dprintf() is one write()
LIBC_PRINTF_OCTAL on on on %o: ?
LIBC_PRINTF_LEFT_JUSTIFY on on on %-5d pads on the left
LIBC_PRINTF_SHORT on on on %hd, %hhx: ?

The 64-bit option stays on in Minimal with SYSLOG_TIMESTAMP or FS_PROCFS, because the log timestamps and /proc print time_t and uint64_t. Without them, time and ulimit in nsh print ?.

The last commit reads a long as an int where they have the same size. It has no option and does not change the output.

Impact

Default: no feature changes. 90 configs, on arm, arm64, risc-v, xtensa, sim and avr, have the same savedefconfig as #21474; their .config has only the new LIBC_PRINTF_* lines.

No ABI change. Not a breaking change. No architecture code. Flat, protected and kernel builds.

Image text against master, float and numbered arguments off:

Cortex-M0 Cortex-M3 rv32 arm64
Minimal, FS_PROCFS and SYSLOG_TIMESTAMP off -884 -460 -362 -240
Minimal, FS_PROCFS on -288 -324 -226 -240

Boards: nucleo-f072rb:nsh, nucleo-f103rb:nsh, rv-virt:nsh, qemu-armv8a:nsh. FS_PROCFS is on in the defconfig of all but the Cortex-M3 board.

Image text with each board's defaults; rv32 and arm64 have float on by default:

Cortex-M0 Cortex-M3 rv32 arm64
master 46,668 34,320 230,006 327,968
Minimal 46,380 33,860 227,988 325,584
Default 46,724 34,436 230,438 328,272
Full 57,864 39,500 231,698 330,068

One option off from Default, float and numbered arguments off:

Cortex-M0 Cortex-M3 rv32 arm64
64-bit integers -616 -196 -136 +0
# -112 -156 -114 -152
integer precision -124 -168 -56 -56
%h, %hh -120 -164 -64 -104
%o -44 -84 -62 -80
- -68 -88 -60 -104
no dprintf() buffer -148 -160 -202 -284

With #20996 merged, 64-bit integers off saves less, because libgcc no longer has the division: -252, -200, -168, 0. These four numbers are from one commit before this head. This PR has one line for that case, and it does nothing without #20996.

Documentation: no page describes these options. The Kconfig help has the text, and step 5 of #21467 is the documentation.

Testing

  • ostest and a test program for these cases, exit 0 and 0 failures: rv-virt:nsh Minimal with 64-bit on and off, qemu-armv8a:knsh Minimal, and both boards with Full.
  • Host harness under ASan and UBSan, 34 builds: each profile, each option added to Minimal or removed from Default. 0 reports, 0 timeouts, for correct and wrong usage.
  • Builds with EXTRAFLAGS="-Wno-cpp -Werror", each of Minimal, Default and Full: nucleo-f072rb:nsh, nucleo-f103rb:nsh, rv-virt:nsh, qemu-armv8a:nsh, mps2-an500:knsh (protected), qemu-armv8a:knsh, rv-virt:knsh (kernel). Each single option off on the four flat boards.

Default, Full, the harness and the single-option builds ran one rebuild before this head. The later commits are a style fix, the Kconfig default of the 64-bit option, and one line that has no effect without #20996.

Not run: Cortex-M0 at run time; a protected or kernel build of 32-bit Minimal with 64-bit off on this head; the speed of dprintf() with no buffer; a size row with SYSLOG_TIMESTAMP on; a CMake build.

tools/checkpatch.sh -c -u -m -g passes.

@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: L The size of the change in this PR is large labels Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

esp32-devkitc

  • ROM: .flash.text -64 B (-0.0%, 129,348 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text -64 B (-0.1%, 95,196 B / 3,342,304 B, total: 3% used)

mirtoo

  • kseg0_progmem: .text -80 B (-0.1%, 68,404 B / 131,072 B, total: 52% used)

rx65n-rsk2mb

  • ROM: .text -16 B (-0.0%, 91,688 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text -128 B (-0.0%, 389,808 B)

stm32-nucleo-f103rb

  • flash: .text -12 B (-0.0%, 34,880 B / 131,072 B, total: 27% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@casaroli please fix the conflict.

@casaroli
casaroli marked this pull request as ready for review November 6, 2026 07:45
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium and removed Area: Documentation Improvements or additions to documentation Size: L The size of the change in this PR is large labels Nov 6, 2026
Comment thread libs/libc/stdio/Kconfig Resolved
Comment thread libs/libc/stream/lib_ultoa_invert.h Resolved
Comment thread libs/libc/locale/Kconfig Resolved
Comment thread libs/libc/stdio/Kconfig Resolved
Comment thread libs/libc/stream/lib_ultoa_invert.h Resolved
Comment thread libs/libc/stream/lib_libvsprintf.c Resolved
Comment thread libs/libc/stream/lib_libvsprintf.c Resolved
@casaroli
casaroli marked this pull request as draft November 6, 2026 08:53
@casaroli
casaroli marked this pull request as ready for review November 6, 2026 11:47
Add the braces that nxstyle asks for.  There is no code change.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
A choice of three profiles sets the defaults of the printf feature options:
Minimal, Default and Full.  Each option keeps its own prompt, so a user can
add any feature to any profile.

Default is the default of the choice, and the defaults of its options do not
change, so no defconfig changes.  Full turns on floating point, numbered
arguments and locale.  Minimal turns off floating point.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
On a 32-bit CPU each integer conversion goes through 64-bit division, even
for %d.  With LIBC_PRINTF_LONG_LONG off, the digit loop uses long.  A value
longer than long is read at its size, and printf() prints "?".

The option is on in the Default and Full profiles.  In the Minimal profile it
stays on with SYSLOG_TIMESTAMP or FS_PROCFS, because the timestamps of syslog
and the times in /proc print with %jd.  It has no effect where long has 64
bits.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
With LIBC_PRINTF_ALT_FORM off, the flag is parsed and ignored.  %p keeps its
0x.  FL_ALT_FORM is 0 then, so the compiler removes the code that tests it.

The option is on in the Default and Full profiles.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
With LIBC_PRINTF_OCTAL off, %o reads its argument and prints "?".

The option is on in all profiles.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
With LIBC_PRINTF_LEFT_JUSTIFY off, the flag is parsed and ignored, and the
output is padded on the left.

The option is on in all profiles.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
…intf().

With LIBC_PRINTF_INT_PRECISION off, a conversion such as %.3d reads its
argument and prints "?".  The precision of %s and of a floating point
conversion does not change.

The option is on in the Default and Full profiles.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
With LIBC_PRINTF_SHORT off, a conversion such as %hhx reads its argument and
prints "?".  It does not print the value without the conversion to char,
because that value can be wrong for a correct call.

The option is on in all profiles.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
With STREAM_OUT_BUFFER_SIZE at 0, vdprintf() writes through the raw stream,
and lib_bufferedoutstream.c is not built.  This removes the code and the
buffer on the stack, and each write of printf() becomes one write().

The size is 0 by default in the Minimal profile only.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
printf() has two reads for %ld and %d.  Where long has the size of int, both
read the same slot.  One read is enough, and the code is smaller.  There is no
change in the output.

Part of apache#21467

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@acassis
acassis merged commit e3c8639 into apache:master Nov 8, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants