µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
assert.c
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#include <assert.h>
14#include <stdlib.h>
15#include <stdint.h>
16#include <unistd.h>
17#include <string.h>
18#include <stdio.h>
19
20#if defined(OS_USE_OS_APP_CONFIG_H)
21#include <cmsis-plus/os-app-config.h>
22#endif
23
26
27// ----------------------------------------------------------------------------
28
29#pragma GCC diagnostic push
30#if defined(__clang__)
31#pragma clang diagnostic ignored "-Wreserved-identifier"
32#else
33#pragma GCC diagnostic ignored "-Wredundant-decls"
34#endif
35void
36__assert_func (const char* file, int line, const char* func,
37 const char* failedexpr);
38#pragma GCC diagnostic pop
39
40#if !defined(TRACE) && !defined(OS_USE_SEMIHOSTING_SYSCALLS)
41void
42__attribute__((noreturn))
43__assert_func (const char* file __attribute__((unused)),
44 int line __attribute__((unused)),
45 const char* func __attribute__((unused)),
46 const char* failedexpr __attribute__((unused)))
47 {
48 abort ();
49 }
50
51#else
52void
53__attribute__((noreturn))
54__assert_func (const char* file, int line, const char* func,
55 const char* failedexpr)
56{
58
59 // Not atomic, but otherwise the entire string might get too long,
60 // and temporary buffer used by trace_printf() will overflow.
61#if defined(TRACE)
62
63 trace_printf ("assertion \"%s\" failed\n", failedexpr);
64 trace_printf ("file: \"%s\"\n", file);
65 trace_printf ("line: %d\n", line);
66 if (func != NULL)
67 {
68 trace_printf ("function: %s\n", func);
69 }
71 {
72 if (os_this_thread() != NULL)
73 {
74 trace_printf ("this_thread: %s @%p\n", os_thread_get_name(os_this_thread()),
76 }
77 }
78
79#elif defined(OS_USE_SEMIHOSTING_SYSCALLS)
80
81 printf ("assertion \"%s\" failed\n", failedexpr);
82 printf ("file: \"%s\"\n", file);
83 printf ("line: %d\n", line);
84 if (func != NULL)
85 {
86 printf ("function: %s\n", func);
87 }
89 {
90 if (os_this_thread() != NULL)
91 {
92 printf ("this_thread: %s @%p\n", os_thread_get_name(os_this_thread()),
94 }
95 }
96
97#endif
98 abort ();
99 /* NOTREACHED */
100}
101#endif
102
103// ----------------------------------------------------------------------------
104
105#if defined(OS_INCLUDE_ASSERT_FAILED)
106
107// This is STM32 specific, but can be used on other platforms too.
108// If the application needs it, add the following to your application header:
109
110//#if defined(USE_FULL_ASSERT)
111//#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t*)__FILE__, __LINE__)) void assert_failed(uint8_t* file, uint32_t line);
112//#else
113//#define assert_param(expr) ((void)0)
114//#endif /* USE_FULL_ASSERT */
115
116// In the new STM32 HAL, the USE_FULL_ASSERT macro is defined in
117// stm32??xx_hal_conf.
118
119void
120assert_failed (uint8_t* file, uint32_t line);
121
122#pragma GCC diagnostic push
123#if defined(__clang__)
124#pragma clang diagnostic ignored "-Wunused-parameter"
125#elif defined(__GNUC__)
126#pragma GCC diagnostic ignored "-Wunused-parameter"
127#endif
128
129// Called from the assert_param() macro, usually defined in the stm32f*_conf.h
130void
131__attribute__((noreturn))
132assert_failed (uint8_t* file, uint32_t line)
133{
134#if defined(TRACE)
135
136 trace_printf ("assert_param() failed: file \"%s\", line %d\n", file, line);
137 trace_printf ("this_thread: %s\n", os_thread_get_name(os_this_thread()));
138
139#elif defined(OS_USE_SEMIHOSTING_SYSCALLS)
140
141 printf ("assert_param() failed: file \"%s\", line %d\n", file, (int)line);
142
143#endif
144
145 abort ();
146 /* NOTREACHED */
147}
148
149#pragma GCC diagnostic pop
150
151#endif // defined(OS_INCLUDE_ASSERT_FAILED)
152
153// ----------------------------------------------------------------------------
void __assert_func(const char *file, int line, const char *func, const char *failedexpr)
Definition assert.c:54
void abort(void)
Definition exit.c:45
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
bool os_irq_in_handler_mode(void)
Check if the CPU is in handler mode.
os_irq_state_t os_irq_critical_enter(void)
Enter an interrupts critical section.
const char * os_thread_get_name(os_thread_t *thread)
Get the thread name.
os_thread_t * os_this_thread(void)
Get the current running thread.
int trace_printf(const char *format,...)