rtdebug.h 6.1 KB
Newer Older
D
dzzxzz 已提交
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
mysterywolf's avatar
mysterywolf 已提交
5 6 7
 *
 * Change Logs:
 * Date                 Author             Notes
8 9
 */

10 11 12
#ifndef __RTDEBUG_H__
#define __RTDEBUG_H__

13 14
#include <rtconfig.h>

15 16 17 18
/* Using this macro to control all kernel debug features. */
#ifdef RT_DEBUG

/* Turn on some of these (set to non-zero) to debug kernel */
19
#ifndef RT_DEBUG_MEM
20
#define RT_DEBUG_MEM                   0
21 22
#endif

23
#ifndef RT_DEBUG_MEMHEAP
24
#define RT_DEBUG_MEMHEAP               0
25 26
#endif

27
#ifndef RT_DEBUG_MODULE
28
#define RT_DEBUG_MODULE                0
29 30 31
#endif

#ifndef RT_DEBUG_SCHEDULER
32
#define RT_DEBUG_SCHEDULER             0
33 34 35
#endif

#ifndef RT_DEBUG_SLAB
36
#define RT_DEBUG_SLAB                  0
37 38 39
#endif

#ifndef RT_DEBUG_THREAD
40
#define RT_DEBUG_THREAD                0
41 42 43
#endif

#ifndef RT_DEBUG_TIMER
44
#define RT_DEBUG_TIMER                 0
45 46 47
#endif

#ifndef RT_DEBUG_IRQ
48
#define RT_DEBUG_IRQ                   0
49 50 51
#endif

#ifndef RT_DEBUG_IPC
52
#define RT_DEBUG_IPC                   0
53
#endif
54

55 56 57 58
#ifndef RT_DEBUG_DEVICE
#define RT_DEBUG_DEVICE                0
#endif

B
bernard 已提交
59
#ifndef RT_DEBUG_INIT
60
#define RT_DEBUG_INIT                  0
B
bernard 已提交
61 62
#endif

63 64
/* Turn on this to enable context check */
#ifndef RT_DEBUG_CONTEXT_CHECK
65
#define RT_DEBUG_CONTEXT_CHECK         1
66
#endif
67

68 69 70 71 72 73 74 75 76 77 78
#define RT_DEBUG_LOG(type, message)                                           \
do                                                                            \
{                                                                             \
    if (type)                                                                 \
        rt_kprintf message;                                                   \
}                                                                             \
while (0)

#define RT_ASSERT(EX)                                                         \
if (!(EX))                                                                    \
{                                                                             \
79
    rt_assert_handler(#EX, __FUNCTION__, __LINE__);                           \
80
}
81

82 83
/* Macro to check current context */
#if RT_DEBUG_CONTEXT_CHECK
84 85 86 87 88 89 90
#define RT_DEBUG_NOT_IN_INTERRUPT                                             \
do                                                                            \
{                                                                             \
    rt_base_t level;                                                          \
    level = rt_hw_interrupt_disable();                                        \
    if (rt_interrupt_get_nest() != 0)                                         \
    {                                                                         \
Z
ZHANG Jinglong 已提交
91
        rt_kprintf("Function[%s] shall not be used in ISR\n", __FUNCTION__);  \
92 93 94 95 96
        RT_ASSERT(0)                                                          \
    }                                                                         \
    rt_hw_interrupt_enable(level);                                            \
}                                                                             \
while (0)
G
Grissiom 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

/* "In thread context" means:
 *     1) the scheduler has been started
 *     2) not in interrupt context.
 */
#define RT_DEBUG_IN_THREAD_CONTEXT                                            \
do                                                                            \
{                                                                             \
    rt_base_t level;                                                          \
    level = rt_hw_interrupt_disable();                                        \
    if (rt_thread_self() == RT_NULL)                                          \
    {                                                                         \
        rt_kprintf("Function[%s] shall not be used before scheduler start\n", \
                   __FUNCTION__);                                             \
        RT_ASSERT(0)                                                          \
    }                                                                         \
    RT_DEBUG_NOT_IN_INTERRUPT;                                                \
    rt_hw_interrupt_enable(level);                                            \
}                                                                             \
while (0)
117 118 119 120 121 122

/* "scheduler available" means:
 *     1) the scheduler has been started.
 *     2) not in interrupt context.
 *     3) scheduler is not locked.
 */
H
Henson 已提交
123
#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)                              \
124 125
do                                                                            \
{                                                                             \
H
Henson 已提交
126
    if (need_check)                                                           \
127
    {                                                                         \
H
Henson 已提交
128 129 130 131 132 133 134 135 136 137
        rt_base_t level;                                                      \
        level = rt_hw_interrupt_disable();                                    \
        if (rt_critical_level() != 0)                                         \
        {                                                                     \
            rt_kprintf("Function[%s]: scheduler is not available\n",          \
                    __FUNCTION__);                                            \
            RT_ASSERT(0)                                                      \
        }                                                                     \
        RT_DEBUG_IN_THREAD_CONTEXT;                                           \
        rt_hw_interrupt_enable(level);                                        \
138 139 140
    }                                                                         \
}                                                                             \
while (0)
141 142
#else
#define RT_DEBUG_NOT_IN_INTERRUPT
G
Grissiom 已提交
143
#define RT_DEBUG_IN_THREAD_CONTEXT
H
Henson 已提交
144
#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)
145
#endif
146

147 148 149
#else /* RT_DEBUG */

#define RT_ASSERT(EX)
150
#define RT_DEBUG_LOG(type, message)
151
#define RT_DEBUG_NOT_IN_INTERRUPT
152
#define RT_DEBUG_IN_THREAD_CONTEXT
H
Henson 已提交
153
#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)
154 155 156 157

#endif /* RT_DEBUG */

#endif /* __RTDEBUG_H__ */