µOS++ IIIe Reference  v6.3.15
“Perfekt ist nicht gut genug”
The third edition of µOS++, a POSIX inspired open source system, written in C++.
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 
35 #include <cmsis-plus/diag/trace.h>
36 
37 // ----------------------------------------------------------------------------
38 
39 void
40 __assert_func (const char* file, int line, const char* func,
41  const char* failedexpr);
42 
43 #if !defined(TRACE) && !defined(OS_USE_SEMIHOSTING_SYSCALLS)
44 void
45 __attribute__((noreturn))
46 __assert_func (const char* file __attribute__((unused)),
47  int line __attribute__((unused)),
48  const char* func __attribute__((unused)),
49  const char* failedexpr __attribute__((unused)))
50  {
51  abort ();
52  }
53 
54 #else
55 void
56 __attribute__((noreturn))
57 __assert_func (const char* file, int line, const char* func,
58  const char* failedexpr)
59 {
60  // Not atomic, but otherwise the entire string might get too long,
61  // and temporary buffer used by trace_printf() will overflow.
62 #if defined(TRACE)
63 
64  trace_printf ("assertion \"%s\" failed\n", failedexpr);
65  trace_printf ("file: \"%s\"\n", file);
66  trace_printf ("line: %d\n", line);
67  if (func != NULL)
68  {
69  trace_printf ("function: %s\n", func);
70  }
71 
72 #elif defined(OS_USE_SEMIHOSTING_SYSCALLS)
73 
74  printf ("assertion \"%s\" failed\n", failedexpr);
75  printf ("file: \"%s\"\n", file);
76  printf ("line: %d\n", line);
77  if (func != NULL)
78  {
79  printf ("function: %s\n", func);
80  }
81 #endif
82  abort ();
83  /* NOTREACHED */
84 }
85 #endif
86 
87 // ----------------------------------------------------------------------------
88 
89 // This is STM32 specific, but can be used on other platforms too.
90 // If the application needs it, add the following to your application header:
91 
92 //#if defined(USE_FULL_ASSERT)
93 //#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t*)__FILE__, __LINE__)) void assert_failed(uint8_t* file, uint32_t line);
94 //#else
95 //#define assert_param(expr) ((void)0)
96 //#endif /* USE_FULL_ASSERT */
97 
98 // In the new STM32 HAL, the USE_FULL_ASSERT macro is defined in
99 // stm32??xx_hal_conf.
100 
101 void
102 assert_failed (uint8_t* file, uint32_t line);
103 
104 #pragma GCC diagnostic push
105 #pragma GCC diagnostic ignored "-Wunused-parameter"
106 
107 // Called from the assert_param() macro, usually defined in the stm32f*_conf.h
108 void
109 __attribute__((noreturn))
110 assert_failed (uint8_t* file, uint32_t line)
111 {
112 #if defined(TRACE)
113 
114  trace_printf ("assert_param() failed: file \"%s\", line %d\n", file, line);
115 
116 #elif defined(OS_USE_SEMIHOSTING_SYSCALLS)
117 
118  printf ("assert_param() failed: file \"%s\", line %d\n", file, (int)line);
119 
120 #endif
121 
122  abort ();
123  /* NOTREACHED */
124 }
125 
126 #pragma GCC diagnostic pop
127 
128 // ----------------------------------------------------------------------------
void assert_failed(uint8_t *file, uint32_t line)
Definition: assert.c:110
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition: trace.cpp:74
int trace_printf(const char *format,...)
void __assert_func(const char *file, int line, const char *func, const char *failedexpr)
Definition: assert.c:57
void abort(void)
Definition: exit.c:52