<commit_before>/*
 * This file is part of the µOS++ distribution.
 *   (https://github.com/micro-os-plus)
 * Copyright (c) 2015 <NAME>.
 *
 * µOS++ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, version 3.
 *
 * µOS++ is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef CMSIS_PLUS_DIAG_TRACE_H_
#define CMSIS_PLUS_DIAG_TRACE_H_

// ----------------------------------------------------------------------------

#if defined(__cplusplus)
#include <cstdint>
#include <cstddef>
#include <cstdarg>
#else
#include <stdint.h>
#include <stdarg.h>
#endif

#include <sys/types.h>

#if defined(__cplusplus)

// This is a very annoying issue, some very old libraries still
// define putchar() as a macro. This is abusive, since it prevents
// the use of putchar() in other name spaces.
#if defined(putchar)
#undef putchar
#endif

#endif

// ----------------------------------------------------------------------------

/**
 * @brief Insert a BKPT0 for debugger usage.
 */
inline void
__attribute__((always_inline))
trace_dbg_bkpt (void)
{
  asm volatile ("bkpt 0");
}

#if defined(TRACE)

#if defined(__cplusplus)

namespace os
{
  /**
   * @brief Tracing support namespace.
   * @ingroup cmsis-plus-diag
   * @details
   * The trace device is an independent output channel, intended
   * for diagnostic purposes.
   *
   * The API is simple, and mimics the standard C output calls:
   * - `os::trace::printf()` / `trace_printf()`
   * - `os::trace::puts()` / `trace_puts()`
   * - `os::trace::putchar()` / `trace_putchar()`
   *
   * The implementation is done in:
   * - os::trace::initialize()
   * - os::trace::write()
   *
   * If these functions are not defined in another place, there are
   * weak definitions that simply discard the trace output.
   *
   * Trace support is enabled by adding the `TRACE` macro definition.
   *
   * When `TRACE` is not defined, all functions are inlined to empty bodies.
   * This has the advantage that the trace calls do not need to be
   * conditionally compiled with
   * <tt> \#ifdef TRACE </tt> / <tt> \#endif </tt>
   */
  namespace trace
  {
    // ----------------------------------------------------------------------

    void
    initialize (void);

    ssize_t
    write (const void* buf, std::size_t nbyte);

    // ----------------------------------------------------------------------

    /**
     * @brief Write a formatted string to the trace device.
     * @param [in] format A null terminate string with the format.
     * @return A nonnegative number for success.
     *
     * @ingroup cmsis-plus-diag
     */
    int
    printf (const char* format, ...);

    /**
     * @brief Write a formatted variable arguments list to the trace device.
     * @param [in] format A null terminate string with the format.
     * @param [in] args A variable arguments list.
     * @return A nonnegative number for success.
     *
     * @ingroup cmsis-plus-diag
     */
    int
    vprintf (const char* format, std::va_list args);

    /**
     * @brief Write the string and a line terminator to the trace device.
     * @param s A null terminated string.
     * @return A nonnegative number for success.
     *
     * @ingroup cmsis-plus-diag
     */
    int
    puts (const char* s);

    /**
     * @brief Write the single character to the trace device.
     * @param c A single byte character.
     * @return The written character.
     *
     * @ingroup cmsis-plus-diag
     */
    int
    putchar (int c);

    /**
     * @brief Write the argv[] array to the trace device.
     * @param argc The number of argv[] strings.
     * @param argv An array of pointer to args.
     *
     * @ingroup cmsis-plus-diag
     */
    void
    dump_args (int argc, char* argv[]);

    // ------------------------------------------------------------------------

    /**
     * @brief Insert a BKPT0 for debugger usage.
     */
    inline void
    __attribute__((always_inline))
    dbg_bkpt (void)
    {
      trace_dbg_bkpt ();
    }

  } /* namespace trace */
} /* namespace os */

#endif /* defined(__cplusplus) */

#if defined(__cplusplus)
extern "C"
{
#endif

  // ----- Implementation -----
  // These two functions must be implemented for a specific trace channel.

  /**
   * Called from startup.
   */
  void
  __initialize_trace (void);

  ssize_t
  trace_write (const void* buf, size_t nbyte);

  // ----- Portable -----

  int
  trace_printf (const char* format, ...);

  int
  trace_vprintf (const char* format, va_list args);

  int
  trace_puts (const char* s);

  int
  trace_putchar (int c);

  void
  trace_dump_args (int argc, char* argv[]);

#if defined(__cplusplus)
}
#endif

#else /* !defined(TRACE) */

// Empty definitions when trace is not defined

#if defined(__cplusplus)

namespace os
  {
    namespace trace
      {
        // ----------------------------------------------------------------

        inline void
        initialize (void);

        inline ssize_t
        write (const void* buf, std::size_t nbyte);

        // ----------------------------------------------------------------

        inline int
        printf (const char* format, ...);

        inline int
        vprintf (const char* format, std::va_list args);

        inline int
        puts (const char* s);

        inline int
        putchar (int c);

        inline void
        dump_args (int argc, char* argv[]);

        // ----------------------------------------------------------------

        inline void __attribute__((always_inline))
        initialize (void)
          {
          }

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"

        inline ssize_t __attribute__((always_inline))
        write (const void* buf, std::size_t nbyte)
          {
            return (ssize_t)nbyte;
          }

        inline int __attribute__((always_inline))
        printf (const char* format, ...)
          {
            return 0;
          }

        inline int __attribute__((always_inline))
        vprintf (const char* format, std::va_list args)
          {
            return 0;
          }

        inline int __attribute__((always_inline))
        puts (const char* s)
          {
            return 0;
          }

        inline int __attribute__((always_inline))
        putchar (int c)
          {
            return c;
          }

        inline void __attribute__((always_inline))
        dumpArgs (int argc, char* argv[])
          {
          }

#pragma GCC diagnostic pop

      } /* namespace trace */
  } /* namespace os */

#endif /* defined(__cplusplus) */

#if defined(__cplusplus)
extern "C"
  {
#endif

    inline void
    __initialize_trace (void);

    // Implementation dependent
    inline ssize_t
    trace_write (const void* buf, size_t nbyte);

    inline int
    trace_printf (const char* format, ...);

    inline int
    trace_vprintf (const char* format, va_list args);

    inline int
    trace_puts (const char* s);

    inline int
    trace_putchar (int c);

    inline void
    trace_dump_args (int argc, char* argv[]);

#if defined(__cplusplus)
  }
#endif

inline void
__attribute__((always_inline))
__initialize_trace (void)
  {
  }

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"

inline ssize_t
__attribute__((always_inline))
trace_write (const void* buf, size_t nbyte)
  {
    return (ssize_t)nbyte;
  }

inline int
__attribute__((always_inline))
trace_printf (const char* format, ...)
  {
    return 0;
  }

inline int
__attribute__((always_inline))
trace_vprintf (const char* format, va_list args)
  {
    return 0;
  }

inline int
__attribute__((always_inline))
trace_puts (const char* s)
  {
    return 0;
  }

inline int
__attribute__((always_inline))
trace_putchar (int c)
  {
    return c;
  }

inline void
__attribute__((always_inline))
trace_dump_args (int argc, char* argv[])
  {
  }

#pragma GCC diagnostic pop

#endif /* defined(TRACE) */

// ----------------------------------------------------------------------------

#endif /* CMSIS_PLUS_DIAG_TRACE_H_ */
<commit_msg>diag/trace.h: include stdlib<commit_after>/*
 * This file is part of the µOS++ distribution.
 *   (https://github.com/micro-os-plus)
 * Copyright (c) 2015 <NAME>.
 *
 * µOS++ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, version 3.
 *
 * µOS++ is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef CMSIS_PLUS_DIAG_TRACE_H_
#define CMSIS_PLUS_DIAG_TRACE_H_

// ----------------------------------------------------------------------------

#if defined(__cplusplus)
#include <cstdint>
#include <cstddef>
#include <cstdarg>
#include <cstdlib>
#else
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#endif

#include <sys/types.h>

#if defined(__cplusplus)

// This is a very annoying issue, some very old libraries still
// define putchar() as a macro. This is abusive, since it prevents
// the use of putchar() in other name spaces.
#if defined(putchar)
#undef putchar
#endif

#endif

// ----------------------------------------------------------------------------

/**
 * @brief Insert a BKPT0 for debugger usage.
 */
inline void
__attribute__((always_inline))
trace_dbg_bkpt (void)
{
  asm volatile ("bkpt 0");
}

#if defined(TRACE)

#if defined(__cplusplus)

namespace os
{
  /**
   * @brief Tracing support namespace.
   * @ingroup cmsis-plus-diag
   * @details
   * The trace device is an independent output channel, intended
   * for diagnostic purposes.
   *
   * The API is simple, and mimics the standard C output calls:
   * - `os::trace::printf()` / `trace_printf()`
   * - `os::trace::puts()` / `trace_puts()`
   * - `os::trace::putchar()` / `trace_putchar()`
   *
   * The implementation is done in:
   * - os::trace::initialize()
   * - os::trace::write()
   *
   * If these functions are not defined in another place, there are
   * weak definitions that simply discard the trace output.
   *
   * Trace support is enabled by adding the `TRACE` macro definition.
   *
   * When `TRACE` is not defined, all functions are inlined to empty bodies.
   * This has the advantage that the trace calls do not need to be
   * conditionally compiled with
   * <tt> \#ifdef TRACE </tt> / <tt> \#endif </tt>
   */
  namespace trace
  {
    // ----------------------------------------------------------------------

    void
    initialize (void);

    ssize_t
    write (const void* buf, std::size_t nbyte);

    // ----------------------------------------------------------------------

    /**
     * @brief Write a formatted string to the trace device.
     * @param [in] format A null terminate string with the format.
     * @return A nonnegative number for success.
     *
     * @ingroup cmsis-plus-diag
     */
    int
    printf (const char* format, ...);

    /**
     * @brief Write a formatted variable arguments list to the trace device.
     * @param [in] format A null terminate string with the format.
     * @param [in] args A variable arguments list.
     * @return A nonnegative number for success.
     *
     * @ingroup cmsis-plus-diag
     */
    int
    vprintf (const char* format, std::va_list args);

    /**
     * @brief Write the string and a line terminator to the trace device.
     * @param s A null terminated string.
     * @return A nonnegative number for success.
     *
     * @ingroup cmsis-plus-diag
     */
    int
    puts (const char* s);

    /**
     * @brief Write the single character to the trace device.
     * @param c A single byte character.
     * @return The written character.
     *
     * @ingroup cmsis-plus-diag
     */
    int
    putchar (int c);

    /**
     * @brief Write the argv[] array to the trace device.
     * @param argc The number of argv[] strings.
     * @param argv An array of pointer to args.
     *
     * @ingroup cmsis-plus-diag
     */
    void
    dump_args (int argc, char* argv[]);

    // ------------------------------------------------------------------------

    /**
     * @brief Insert a BKPT0 for debugger usage.
     */
    inline void
    __attribute__((always_inline))
    dbg_bkpt (void)
    {
      trace_dbg_bkpt ();
    }

  } /* namespace trace */
} /* namespace os */

#endif /* defined(__cplusplus) */

#if defined(__cplusplus)
extern "C"
{
#endif

  // ----- Implementation -----
  // These two functions must be implemented for a specific trace channel.

  /**
   * Called from startup.
   */
  void
  __initialize_trace (void);

  ssize_t
  trace_write (const void* buf, size_t nbyte);

  // ----- Portable -----

  int
  trace_printf (const char* format, ...);

  int
  trace_vprintf (const char* format, va_list args);

  int
  trace_puts (const char* s);

  int
  trace_putchar (int c);

  void
  trace_dump_args (int argc, char* argv[]);

#if defined(__cplusplus)
}
#endif

#else /* !defined(TRACE) */

// Empty definitions when trace is not defined

#if defined(__cplusplus)

namespace os
  {
    namespace trace
      {
        // ----------------------------------------------------------------

        inline void
        initialize (void);

        inline ssize_t
        write (const void* buf, std::size_t nbyte);

        // ----------------------------------------------------------------

        inline int
        printf (const char* format, ...);

        inline int
        vprintf (const char* format, std::va_list args);

        inline int
        puts (const char* s);

        inline int
        putchar (int c);

        inline void
        dump_args (int argc, char* argv[]);

        // ----------------------------------------------------------------

        inline void __attribute__((always_inline))
        initialize (void)
          {
          }

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"

        inline ssize_t __attribute__((always_inline))
        write (const void* buf, std::size_t nbyte)
          {
            return (ssize_t)nbyte;
          }

        inline int __attribute__((always_inline))
        printf (const char* format, ...)
          {
            return 0;
          }

        inline int __attribute__((always_inline))
        vprintf (const char* format, std::va_list args)
          {
            return 0;
          }

        inline int __attribute__((always_inline))
        puts (const char* s)
          {
            return 0;
          }

        inline int __attribute__((always_inline))
        putchar (int c)
          {
            return c;
          }

        inline void __attribute__((always_inline))
        dumpArgs (int argc, char* argv[])
          {
          }

#pragma GCC diagnostic pop

      } /* namespace trace */
  } /* namespace os */

#endif /* defined(__cplusplus) */

#if defined(__cplusplus)
extern "C"
  {
#endif

    inline void
    __initialize_trace (void);

    // Implementation dependent
    inline ssize_t
    trace_write (const void* buf, size_t nbyte);

    inline int
    trace_printf (const char* format, ...);

    inline int
    trace_vprintf (const char* format, va_list args);

    inline int
    trace_puts (const char* s);

    inline int
    trace_putchar (int c);

    inline void
    trace_dump_args (int argc, char* argv[]);

#if defined(__cplusplus)
  }
#endif

inline void
__attribute__((always_inline))
__initialize_trace (void)
  {
  }

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"

inline ssize_t
__attribute__((always_inline))
trace_write (const void* buf, size_t nbyte)
  {
    return (ssize_t)nbyte;
  }

inline int
__attribute__((always_inline))
trace_printf (const char* format, ...)
  {
    return 0;
  }

inline int
__attribute__((always_inline))
trace_vprintf (const char* format, va_list args)
  {
    return 0;
  }

inline int
__attribute__((always_inline))
trace_puts (const char* s)
  {
    return 0;
  }

inline int
__attribute__((always_inline))
trace_putchar (int c)
  {
    return c;
  }

inline void
__attribute__((always_inline))
trace_dump_args (int argc, char* argv[])
  {
  }

#pragma GCC diagnostic pop

#endif /* defined(TRACE) */

// ----------------------------------------------------------------------------

#endif /* CMSIS_PLUS_DIAG_TRACE_H_ */

{'fraction_non_alphanumeric': 0.17438724053008092, 'fraction_numerical': 0.0016418435557640435, 'mean_word_length': 1.9123975409836065, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1532449', 'n_tokens_mistral': 5020, 'n_tokens_neox': 4833, 'n_words': 1638}