µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
trace.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2015-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#ifndef CMSIS_PLUS_DIAG_TRACE_H_
13#define CMSIS_PLUS_DIAG_TRACE_H_
14
15// ----------------------------------------------------------------------------
16
17#if defined(__cplusplus)
18#include <cstdint>
19#include <cstddef>
20#include <cstdarg>
21#include <cstdlib>
22#else
23#include <stdint.h>
24#include <stdarg.h>
25#include <stdlib.h>
26#endif
27
28#include <sys/types.h>
29
30#if defined(__cplusplus)
31
32// To be effective, <stdio.h> must be included *before* this patch.
33#include <stdio.h>
34
35// This is a very annoying issue, some very old libraries still
36// define putchar() as a macro. This is abusive, since it prevents
37// the use of putchar() in other name spaces.
38#if defined(putchar)
39#undef putchar
40#endif
41
42#endif
43
44// ----------------------------------------------------------------------------
45
49inline void __attribute__ ((always_inline))
51{
52#if defined(__ARM_EABI__)
53 __asm__ volatile ("bkpt 0");
54#endif
55}
56
57#if defined(TRACE)
58
59#if defined(__cplusplus)
60
61namespace os
62{
90 namespace trace
91 {
92 // ------------------------------------------------------------------------
93
94 void
95 initialize (void);
96
97 ssize_t
98 write (const void* buf, std::size_t nbyte);
99
100 // ------------------------------------------------------------------------
101
109 int
110 printf (const char* format, ...);
111
120 int
121 vprintf (const char* format, std::va_list args);
122
130 int
131 puts (const char* s);
132
140 int
141 putchar (int c);
142
150 void
151 dump_args (int argc, char* argv[]);
152
160 void
161 flush (void);
162
163 // ------------------------------------------------------------------------
164
168 inline void __attribute__ ((always_inline))
169 dbg_bkpt (void)
170 {
172 }
173
174 } /* namespace trace */
175} /* namespace os */
176
177#endif /* defined(__cplusplus) */
178
179#if defined(__cplusplus)
180extern "C"
181{
182#endif
183
184 // ----- Implementation -----
185 // These functions must be implemented for a specific trace channel.
186
190 void
191 trace_initialize (void);
192
193 ssize_t
194 trace_write (const void* buf, size_t nbyte);
195
196 void
197 trace_flush (void);
198
199 // ----- Portable -----
200
201 int
202 trace_printf (const char* format, ...);
203
204 int
205 trace_vprintf (const char* format, va_list args);
206
207 int
208 trace_puts (const char* s);
209
210 int
212
213 void
214 trace_dump_args (int argc, char* argv[]);
215
216#if defined(__cplusplus)
217}
218#endif
219
220#else /* !defined(TRACE) */
221
222// Empty definitions when trace is not defined
223
224#if defined(__cplusplus)
225
226namespace os
227{
228 namespace trace
229 {
230 // ------------------------------------------------------------------------
231
232 inline void
233 initialize (void);
234
235 inline ssize_t
236 write (const void* buf, std::size_t nbyte);
237
238 // ------------------------------------------------------------------------
239
240 inline int
241 printf (const char* format, ...);
242
243 inline int
244 vprintf (const char* format, std::va_list args);
245
246 inline int
247 puts (const char* s);
248
249 inline int
250 putchar (int c);
251
252 inline void
253 dump_args (int argc, char* argv[]);
254
255 // ------------------------------------------------------------------------
256
257 inline void __attribute__ ((always_inline))
258 initialize (void)
259 {
260 }
261
262#pragma GCC diagnostic push
263#if defined(__clang__)
264#pragma clang diagnostic ignored "-Wunused-parameter"
265#elif defined(__GNUC__)
266#pragma GCC diagnostic ignored "-Wunused-parameter"
267#endif
268
269 inline ssize_t __attribute__ ((always_inline))
270 write (const void* buf, std::size_t nbyte)
271 {
272 return static_cast<ssize_t> (nbyte);
273 }
274
275 inline void __attribute__ ((always_inline))
276 flush (void)
277 {
278 }
279
280 inline int __attribute__ ((always_inline))
281 printf (const char* format, ...)
282 {
283 return 0;
284 }
285
286 inline int __attribute__ ((always_inline))
287 vprintf (const char* format, std::va_list args)
288 {
289 return 0;
290 }
291
292 inline int __attribute__ ((always_inline))
293 puts (const char* s)
294 {
295 return 0;
296 }
297
298 inline int __attribute__ ((always_inline))
299 putchar (int c)
300 {
301 return c;
302 }
303
304 inline void __attribute__ ((always_inline))
305 dump_args (int argc, char* argv[])
306 {
307 }
308
309#pragma GCC diagnostic pop
310
311 } /* namespace trace */
312} /* namespace os */
313
314#endif /* defined(__cplusplus) */
315
316#if defined(__cplusplus)
317extern "C"
318{
319#endif
320
321 inline void
322 trace_initialize (void);
323
324 // Implementation dependent
325 inline ssize_t
326 trace_write (const void* buf, size_t nbyte);
327
328 inline void
329 trace_flush (void);
330
331 inline int
332 trace_printf (const char* format, ...);
333
334 inline int
335 trace_vprintf (const char* format, va_list args);
336
337 inline int
338 trace_puts (const char* s);
339
340 inline int
341 trace_putchar (int c);
342
343 inline void
344 trace_dump_args (int argc, char* argv[]);
345
346#if defined(__cplusplus)
347}
348#endif
349
350inline void __attribute__ ((always_inline))
351trace_initialize (void)
352{
353}
354
355#pragma GCC diagnostic push
356#if defined(__clang__)
357#pragma clang diagnostic ignored "-Wunused-parameter"
358#elif defined(__GNUC__)
359#pragma GCC diagnostic ignored "-Wunused-parameter"
360#endif
361
362#pragma GCC diagnostic push
363#if defined(__clang__)
364#pragma clang diagnostic ignored "-Wold-style-cast"
365#elif defined(__GNUC__)
366#if defined(__cplusplus)
367#pragma GCC diagnostic ignored "-Wold-style-cast"
368#endif
369#endif
370
371inline ssize_t __attribute__ ((always_inline))
372trace_write (const void* buf, size_t nbyte)
373{
374 return (ssize_t)(nbyte);
375}
376
377inline void __attribute__ ((always_inline))
378trace_flush (void)
379{
380}
381
382#pragma GCC diagnostic pop
383
384inline int __attribute__ ((always_inline))
385trace_printf (const char* format, ...)
386{
387 return 0;
388}
389
390inline int __attribute__ ((always_inline))
391trace_vprintf (const char* format, va_list args)
392{
393 return 0;
394}
395
396inline int __attribute__ ((always_inline))
397trace_puts (const char* s)
398{
399 return 0;
400}
401
402inline int __attribute__ ((always_inline))
403trace_putchar (int c)
404{
405 return c;
406}
407
408inline void __attribute__ ((always_inline))
409trace_dump_args (int argc, char* argv[])
410{
411}
412
413#pragma GCC diagnostic pop
414
415#endif /* defined(TRACE) */
416
417// ----------------------------------------------------------------------------
418
419#endif /* CMSIS_PLUS_DIAG_TRACE_H_ */
int puts(const char *s)
Write the string and a line terminator to the trace device.
Definition trace.cpp:102
int vprintf(const char *format, std::va_list args)
Write a formatted variable arguments list to the trace device.
Definition trace.cpp:76
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
int putchar(int c)
Write the single character to the trace device.
Definition trace.cpp:120
void dump_args(int argc, char *argv[])
Write the argv[] array to the trace device.
Definition trace.cpp:139
void dbg_bkpt(void)
Insert a BKPT0 for debugger usage.
Definition trace.h:169
void initialize(void)
Definition trace.cpp:37
ssize_t write(const void *buf, std::size_t nbyte)
Write the given number of bytes to the trace output channel.
Definition trace.cpp:46
void flush(void)
Flush the output.
Definition trace.cpp:52
System namespace.
int trace_putchar(int c)
void trace_flush(void)
Definition trace.cpp:177
int trace_vprintf(const char *format, va_list args)
void trace_initialize(void)
Definition trace.cpp:165
void trace_dbg_bkpt(void)
Insert a BKPT0 for debugger usage.
Definition trace.h:50
void trace_dump_args(int argc, char *argv[])
int trace_printf(const char *format,...)
ssize_t trace_write(const void *buf, size_t nbyte)
int trace_puts(const char *s)