rtdebug.h 6.8 KB

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