Introduction

This document provides a context of the issues with using the sprintf family of functions (in the following text only print functions) in the MPU-protected multitasking environment of PXROS-HR. Within this document, multitasking (or multithreading) covers both situations when the tasks run on the same or different cores.

This document, in particular, describes the necessary steps to be done to use print functions with PXROS-HR when using HighTec Compiler Suite version 4.9.3.0 or higher.

The application note is valid both for sprintf and snprintf functions. However, it is recommended to use the snprintf variant to avoid the overflow of the provided buffer.

The application must ensure that the task calling the print function has data access to all function arguments (read or read/write based on how the argument is used), including, for example, access to strings passed via pointers. When a pointer is passed for printing a string, the task must have access to the complete string area.

Libraries

The HighTec’s TriCore C/C++ Development Platform is shipped with three libraries (description is provided in the toolchain User’s Guide):

  • libgcc.a

  • libos.a

  • libdnk_c.a

The print functions are implemented in the Dinkumware library libdnk_c.a, and the function declarations are provided in <stdio.h> header file. The libos.a and libgcc.a libraries provide supportive functions called by the print functions internally.

Thread safety and re-entrance

There are two function categories in a multitasking (multithreading) environment:

  • Thread-safe function

    "A function is thread-safe if multiple threads can execute the same function at the same time safely."

    All print functions are thread-safe when the Thread-safety assumptions are fulfilled.

  • Re-entrant function

    "A function is re-entrant if it can be interrupted in the middle of execution, and it’s safe to call the same function again in the same thread."

    As far as the print functions do not modify any global or static variables, they are re-entrant.

Thread-safety assumptions

List of allowed print functions:

  • sprintf

  • snprintf

  • vsprintf

  • vsnprintf

All the other functions are not allowed to use. For the allowed functions, the following assumptions must be fulfilled:

  • tasks have access to all variables that are passed as arguments (including indirect access via pointers)

  • buffers, where to put the formatted output, are used by the tasks exclusively

  • pointer to a buffer, where to put the formatted output, cannot be NULL

  • the pointer to the format string cannot be NULL

  • any pointer passed as argument to be printed by %s cannot be NULL

  • any pointer passed as argument to be printed by %n cannot be NULL

Furthermore, it is not allowed to:

  • use concurrently any function that modifies static or global variables of the Dinkumware library

  • use wide-character strings