rtdebug.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 this to enable context check */
  15. #ifndef RT_DEBUG_CONTEXT_CHECK
  16. #define RT_DEBUG_CONTEXT_CHECK 1
  17. #endif
  18. #define RT_ASSERT(EX) \
  19. if (!(EX)) \
  20. { \
  21. rt_assert_handler(#EX, __FUNCTION__, __LINE__); \
  22. }
  23. /* Macro to check current context */
  24. #if RT_DEBUG_CONTEXT_CHECK
  25. #define RT_DEBUG_NOT_IN_INTERRUPT \
  26. do \
  27. { \
  28. rt_base_t level; \
  29. level = rt_hw_interrupt_disable(); \
  30. if (rt_interrupt_get_nest() != 0) \
  31. { \
  32. rt_kprintf("Function[%s] shall not be used in ISR\n", __FUNCTION__); \
  33. RT_ASSERT(0) \
  34. } \
  35. rt_hw_interrupt_enable(level); \
  36. } \
  37. while (0)
  38. /* "In thread context" means:
  39. * 1) the scheduler has been started
  40. * 2) not in interrupt context.
  41. */
  42. #define RT_DEBUG_IN_THREAD_CONTEXT \
  43. do \
  44. { \
  45. rt_base_t level; \
  46. level = rt_hw_interrupt_disable(); \
  47. if (rt_thread_self() == RT_NULL) \
  48. { \
  49. rt_kprintf("Function[%s] shall not be used before scheduler start\n", \
  50. __FUNCTION__); \
  51. RT_ASSERT(0) \
  52. } \
  53. RT_DEBUG_NOT_IN_INTERRUPT; \
  54. rt_hw_interrupt_enable(level); \
  55. } \
  56. while (0)
  57. /* "scheduler available" means:
  58. * 1) the scheduler has been started.
  59. * 2) not in interrupt context.
  60. * 3) scheduler is not locked.
  61. * 4) interrupt is not disabled.
  62. */
  63. #define RT_DEBUG_SCHEDULER_AVAILABLE(need_check) \
  64. do \
  65. { \
  66. if (need_check) \
  67. { \
  68. rt_bool_t interrupt_disabled; \
  69. rt_base_t level; \
  70. interrupt_disabled = rt_hw_interrupt_is_disabled(); \
  71. level = rt_hw_interrupt_disable(); \
  72. if (rt_critical_level() != 0) \
  73. { \
  74. rt_kprintf("Function[%s]: scheduler is not available\n", \
  75. __FUNCTION__); \
  76. RT_ASSERT(0) \
  77. } \
  78. if (interrupt_disabled == RT_TRUE) \
  79. { \
  80. rt_kprintf("Function[%s]: interrupt is disabled\n", \
  81. __FUNCTION__); \
  82. RT_ASSERT(0) \
  83. } \
  84. RT_DEBUG_IN_THREAD_CONTEXT; \
  85. rt_hw_interrupt_enable(level); \
  86. } \
  87. } \
  88. while (0)
  89. #else
  90. #define RT_DEBUG_NOT_IN_INTERRUPT
  91. #define RT_DEBUG_IN_THREAD_CONTEXT
  92. #define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)
  93. #endif
  94. #else /* RT_DEBUG */
  95. #define RT_ASSERT(EX)
  96. #define RT_DEBUG_NOT_IN_INTERRUPT
  97. #define RT_DEBUG_IN_THREAD_CONTEXT
  98. #define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)
  99. #endif /* RT_DEBUG */
  100. #endif /* __RTDEBUG_H__ */