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