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