You copied the Doc URL to your clipboard.

Why the simulation stalls when printf() is used in C code

Article ID: 103489487

Published date: 24 Jul 2017

Last updated: -

Applies to: Cortex-M0, Cortex-M0Plus, Cortex-M3, Cortex-M4, Cortex-M7

Problem/Question

Why the simulation stalls when printf() is used in my C code?

Scenario

This knowledge article is intended for software developers who are having issues when using printf() on ARM cores.

The symptoms of the issue are as follows:

    • The issue occurs during RTL simulation.

      • The simulation stalls during the execution of printf().

      The above issue can occur under the following circumstances:

        • C code is compiled using ARM compiler 4, 5 or 6.

          • The stack memory area is uninitialised.

            • The format specifier is used without width or precision, for example:

              printf("Example string : %d",example_int)
              • The issue occurs independently of the specifier character used or the type of variable printed.

              Answer

              The ARM compiler produces an assembly code which tries to load the width and precision values from a specific memory address located in the stack area, regardless of those values being given to the printf() format specifier or not. If the stack memory area has not been initialised this can potentially result in loading X values into the registers of the processor, which ultimately can cause the simulation to stall.

              It is important to state that the stall issue is caused by the X values, therefore the same assembly code cannot cause any issues in real life systems, it only affects RTL simulation.

              Workaround

              The easiest workaround for the issue is to explicitly specify the width or precision in the format specifier, for example:

              printf("Example string : %1d",example_int)

              Another way to work around the issue is by adding the following code to the beginning of the C code:

              #if defined ( __CC_ARM )
              #if (__ARMCC_VERSION < 400000)
              #else
              // Insist on keeping widthprec, to avoid X propagation by benign code in C-lib
              #pragma import _printf_widthprec
              #endif
              #endif

              Example

              No example.

              Related Information

              None.

              Was this page helpful? Yes No