rtdebug.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_INIT
  43. #define RT_DEBUG_INIT 0
  44. #endif
  45. /* Turn on this to enable context check */
  46. #ifndef RT_DEBUG_CONTEXT_CHECK
  47. #define RT_DEBUG_CONTEXT_CHECK 1
  48. #endif
  49. #define RT_DEBUG_LOG(type, message) \
  50. do \
  51. { \
  52. if (type) \
  53. rt_kprintf message; \
  54. } \
  55. while (0)
  56. #define RT_ASSERT(EX) \
  57. if (!(EX)) \
  58. { \
  59. rt_assert_handler(#EX, __FUNCTION__, __LINE__); \
  60. }
  61. /* Macro to check current context */
  62. #if RT_DEBUG_CONTEXT_CHECK
  63. #define RT_DEBUG_NOT_IN_INTERRUPT \
  64. do \
  65. { \
  66. rt_base_t level; \
  67. level = rt_hw_interrupt_disable(); \
  68. if (rt_interrupt_get_nest() != 0) \
  69. { \
  70. rt_kprintf("Function[%s] shall not be used in ISR\n", __FUNCTION__); \
  71. RT_ASSERT(0) \
  72. } \
  73. rt_hw_interrupt_enable(level); \
  74. } \
  75. while (0)
  76. /* "In thread context" means:
  77. * 1) the scheduler has been started
  78. * 2) not in interrupt context.
  79. */
  80. #define RT_DEBUG_IN_THREAD_CONTEXT \
  81. do \
  82. { \
  83. rt_base_t level; \
  84. level = rt_hw_interrupt_disable(); \
  85. if (rt_thread_self() == RT_NULL) \
  86. { \
  87. rt_kprintf("Function[%s] shall not be used before scheduler start\n", \
  88. __FUNCTION__); \
  89. RT_ASSERT(0) \
  90. } \
  91. RT_DEBUG_NOT_IN_INTERRUPT; \
  92. rt_hw_interrupt_enable(level); \
  93. } \
  94. while (0)
  95. /* "scheduler available" means:
  96. * 1) the scheduler has been started.
  97. * 2) not in interrupt context.
  98. * 3) scheduler is not locked.
  99. */
  100. #define RT_DEBUG_SCHEDULER_AVAILABLE(need_check) \
  101. do \
  102. { \
  103. if (need_check) \
  104. { \
  105. rt_base_t level; \
  106. level = rt_hw_interrupt_disable(); \
  107. if (rt_critical_level() != 0) \
  108. { \
  109. rt_kprintf("Function[%s]: scheduler is not available\n", \
  110. __FUNCTION__); \
  111. RT_ASSERT(0) \
  112. } \
  113. RT_DEBUG_IN_THREAD_CONTEXT; \
  114. rt_hw_interrupt_enable(level); \
  115. } \
  116. } \
  117. while (0)
  118. #else
  119. #define RT_DEBUG_NOT_IN_INTERRUPT
  120. #define RT_DEBUG_IN_THREAD_CONTEXT
  121. #define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)
  122. #endif
  123. #else /* RT_DEBUG */
  124. #define RT_ASSERT(EX)
  125. #define RT_DEBUG_LOG(type, message)
  126. #define RT_DEBUG_NOT_IN_INTERRUPT
  127. #define RT_DEBUG_IN_THREAD_CONTEXT
  128. #define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)
  129. #endif /* RT_DEBUG */
  130. #endif /* __RTDEBUG_H__ */