µ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 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <assert.h>
29#include <stdlib.h>
30#include <stdint.h>
31#include <unistd.h>
32#include <string.h>
33#include <stdio.h>
34
37
38// ----------------------------------------------------------------------------
39
40void
41__assert_func (const char* file, int line, const char* func,
42 const char* failedexpr);
43
44#if !defined(TRACE) && !defined(OS_USE_SEMIHOSTING_SYSCALLS)
45void
46__attribute__((noreturn))
47__assert_func (const char* file __attribute__((unused)),
48 int line __attribute__((unused)),
49 const char* func __attribute__((unused)),
50 const char* failedexpr __attribute__((unused)))
51 {
52 abort ();
53 }
54
55#else
56void
57__attribute__((noreturn))
58__assert_func (const char* file, int line, const char* func,
59 const char* failedexpr)
60{
62
63 // Not atomic, but otherwise the entire string might get too long,
64 // and temporary buffer used by trace_printf() will overflow.
65#if defined(TRACE)
66
67 trace_printf ("assertion \"%s\" failed\n", failedexpr);
68 trace_printf ("file: \"%s\"\n", file);
69 trace_printf ("line: %d\n", line);
70 if (func != NULL)
71 {
72 trace_printf ("function: %s\n", func);
73 }
75 {
76 if (os_this_thread() != NULL)
77 {
78 trace_printf ("this_thread: %s @%p\n", os_thread_get_name(os_this_thread()),
80 }
81 }
82
83#elif defined(OS_USE_SEMIHOSTING_SYSCALLS)
84
85 printf ("assertion \"%s\" failed\n", failedexpr);
86 printf ("file: \"%s\"\n", file);
87 printf ("line: %d\n", line);
88 if (func != NULL)
89 {
90 printf ("function: %s\n", func);
91 }
93 {
94 if (os_this_thread() != NULL)
95 {
96 printf ("this_thread: %s @%p\n", os_thread_get_name(os_this_thread()),
98 }
99 }
100
101#endif
102 abort ();
103 /* NOTREACHED */
104}
105#endif
106
107// ----------------------------------------------------------------------------
108
109// This is STM32 specific, but can be used on other platforms too.
110// If the application needs it, add the following to your application header:
111
112//#if defined(USE_FULL_ASSERT)
113//#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t*)__FILE__, __LINE__)) void assert_failed(uint8_t* file, uint32_t line);
114//#else
115//#define assert_param(expr) ((void)0)
116//#endif /* USE_FULL_ASSERT */
117
118// In the new STM32 HAL, the USE_FULL_ASSERT macro is defined in
119// stm32??xx_hal_conf.
120
121void
122assert_failed (uint8_t* file, uint32_t line);
123
124#pragma GCC diagnostic push
125#pragma GCC diagnostic ignored "-Wunused-parameter"
126
127// Called from the assert_param() macro, usually defined in the stm32f*_conf.h
128void
129__attribute__((noreturn))
130assert_failed (uint8_t* file, uint32_t line)
131{
132#if defined(TRACE)
133
134 trace_printf ("assert_param() failed: file \"%s\", line %d\n", file, line);
135 trace_printf ("this_thread: %s\n", os_thread_get_name(os_this_thread()));
136
137#elif defined(OS_USE_SEMIHOSTING_SYSCALLS)
138
139 printf ("assert_param() failed: file \"%s\", line %d\n", file, (int)line);
140
141#endif
142
143 abort ();
144 /* NOTREACHED */
145}
146
147#pragma GCC diagnostic pop
148
149// ----------------------------------------------------------------------------
void assert_failed(uint8_t *file, uint32_t line)
Definition assert.c:130
void __assert_func(const char *file, int line, const char *func, const char *failedexpr)
Definition assert.c:58
void abort(void)
Definition exit.c:56
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,...)