xref: /nrf52832-nimble/rt-thread/include/rtdebug.h (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __RTDEBUG_H__
8 #define __RTDEBUG_H__
9 
10 #include <rtconfig.h>
11 
12 /* settings depend check */
13 #ifdef RT_USING_POSIX
14 #if !defined(RT_USING_DFS) || !defined(RT_USING_DFS_DEVFS)
15 #error "POSIX poll/select, stdin need file system(RT_USING_DFS) and device file system(RT_USING_DFS_DEVFS)"
16 #endif
17 
18 #if !defined(RT_USING_LIBC)
19 #error "POSIX layer need standard C library(RT_USING_LIBC)"
20 #endif
21 
22 #endif
23 
24 #ifdef RT_USING_POSIX_TERMIOS
25 #if !defined(RT_USING_POSIX)
26 #error "termios need POSIX layer(RT_USING_POSIX)"
27 #endif
28 #endif
29 
30 /* Using this macro to control all kernel debug features. */
31 #ifdef RT_DEBUG
32 
33 /* Turn on some of these (set to non-zero) to debug kernel */
34 #ifndef RT_DEBUG_MEM
35 #define RT_DEBUG_MEM                   0
36 #endif
37 
38 #ifndef RT_DEBUG_MEMHEAP
39 #define RT_DEBUG_MEMHEAP               0
40 #endif
41 
42 #ifndef RT_DEBUG_MODULE
43 #define RT_DEBUG_MODULE                0
44 #endif
45 
46 #ifndef RT_DEBUG_SCHEDULER
47 #define RT_DEBUG_SCHEDULER             0
48 #endif
49 
50 #ifndef RT_DEBUG_SLAB
51 #define RT_DEBUG_SLAB                  0
52 #endif
53 
54 #ifndef RT_DEBUG_THREAD
55 #define RT_DEBUG_THREAD                0
56 #endif
57 
58 #ifndef RT_DEBUG_TIMER
59 #define RT_DEBUG_TIMER                 0
60 #endif
61 
62 #ifndef RT_DEBUG_IRQ
63 #define RT_DEBUG_IRQ                   0
64 #endif
65 
66 #ifndef RT_DEBUG_IPC
67 #define RT_DEBUG_IPC                   0
68 #endif
69 
70 #ifndef RT_DEBUG_INIT
71 #define RT_DEBUG_INIT                  0
72 #endif
73 
74 /* Turn on this to enable context check */
75 #ifndef RT_DEBUG_CONTEXT_CHECK
76 #define RT_DEBUG_CONTEXT_CHECK         1
77 #endif
78 
79 #define RT_DEBUG_LOG(type, message)                                           \
80 do                                                                            \
81 {                                                                             \
82     if (type)                                                                 \
83         rt_kprintf message;                                                   \
84 }                                                                             \
85 while (0)
86 
87 #define RT_ASSERT(EX)                                                         \
88 if (!(EX))                                                                    \
89 {                                                                             \
90     rt_assert_handler(#EX, __FUNCTION__, __LINE__);                           \
91 }
92 
93 /* Macro to check current context */
94 #if RT_DEBUG_CONTEXT_CHECK
95 #define RT_DEBUG_NOT_IN_INTERRUPT                                             \
96 do                                                                            \
97 {                                                                             \
98     rt_base_t level;                                                          \
99     level = rt_hw_interrupt_disable();                                        \
100     if (rt_interrupt_get_nest() != 0)                                         \
101     {                                                                         \
102         rt_kprintf("Function[%s] shall not be used in ISR\n", __FUNCTION__);  \
103         RT_ASSERT(0)                                                          \
104     }                                                                         \
105     rt_hw_interrupt_enable(level);                                            \
106 }                                                                             \
107 while (0)
108 
109 /* "In thread context" means:
110  *     1) the scheduler has been started
111  *     2) not in interrupt context.
112  */
113 #define RT_DEBUG_IN_THREAD_CONTEXT                                            \
114 do                                                                            \
115 {                                                                             \
116     rt_base_t level;                                                          \
117     level = rt_hw_interrupt_disable();                                        \
118     if (rt_thread_self() == RT_NULL)                                          \
119     {                                                                         \
120         rt_kprintf("Function[%s] shall not be used before scheduler start\n", \
121                    __FUNCTION__);                                             \
122         RT_ASSERT(0)                                                          \
123     }                                                                         \
124     RT_DEBUG_NOT_IN_INTERRUPT;                                                \
125     rt_hw_interrupt_enable(level);                                            \
126 }                                                                             \
127 while (0)
128 #else
129 #define RT_DEBUG_NOT_IN_INTERRUPT
130 #define RT_DEBUG_IN_THREAD_CONTEXT
131 #endif
132 
133 #else /* RT_DEBUG */
134 
135 #define RT_ASSERT(EX)
136 #define RT_DEBUG_LOG(type, message)
137 #define RT_DEBUG_NOT_IN_INTERRUPT
138 #define RT_DEBUG_IN_THREAD_CONTEXT
139 
140 #endif /* RT_DEBUG */
141 
142 #endif /* __RTDEBUG_H__ */
143