mali_kbase_debug.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. *
  3. * (C) COPYRIGHT 2012-2015 ARM Limited. All rights reserved.
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * A copy of the licence is included with the program, and can also be obtained
  11. * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  12. * Boston, MA 02110-1301, USA.
  13. *
  14. */
  15. #ifndef _KBASE_DEBUG_H
  16. #define _KBASE_DEBUG_H
  17. #include <linux/bug.h>
  18. /** @brief If equals to 0, a trace containing the file, line, and function will be displayed before each message. */
  19. #define KBASE_DEBUG_SKIP_TRACE 0
  20. /** @brief If different from 0, the trace will only contain the file and line. */
  21. #define KBASE_DEBUG_SKIP_FUNCTION_NAME 0
  22. /** @brief Disable the asserts tests if set to 1. Default is to disable the asserts in release. */
  23. #ifndef KBASE_DEBUG_DISABLE_ASSERTS
  24. #ifdef CONFIG_MALI_DEBUG
  25. #define KBASE_DEBUG_DISABLE_ASSERTS 0
  26. #else
  27. #define KBASE_DEBUG_DISABLE_ASSERTS 1
  28. #endif
  29. #endif // ifndef KBASE_DEBUG_DISABLE_ASSERTS
  30. /** Function type that is called on an KBASE_DEBUG_ASSERT() or KBASE_DEBUG_ASSERT_MSG() */
  31. typedef void (kbase_debug_assert_hook) (void *);
  32. struct kbasep_debug_assert_cb {
  33. kbase_debug_assert_hook *func;
  34. void *param;
  35. };
  36. /**
  37. * @def KBASEP_DEBUG_PRINT_TRACE
  38. * @brief Private macro containing the format of the trace to display before every message
  39. * @sa KBASE_DEBUG_SKIP_TRACE, KBASE_DEBUG_SKIP_FUNCTION_NAME
  40. */
  41. #if !KBASE_DEBUG_SKIP_TRACE
  42. #define KBASEP_DEBUG_PRINT_TRACE \
  43. "In file: " __FILE__ " line: " CSTD_STR2(__LINE__)
  44. #if !KBASE_DEBUG_SKIP_FUNCTION_NAME
  45. #define KBASEP_DEBUG_PRINT_FUNCTION __func__
  46. #else
  47. #define KBASEP_DEBUG_PRINT_FUNCTION ""
  48. #endif
  49. #else // !KBASE_DEBUG_SKIP_TRACE
  50. #define KBASEP_DEBUG_PRINT_TRACE ""
  51. #endif
  52. /**
  53. * @def KBASEP_DEBUG_ASSERT_OUT(trace, function, ...)
  54. * @brief (Private) system printing function associated to the @see KBASE_DEBUG_ASSERT_MSG event.
  55. * @param trace location in the code from where the message is printed
  56. * @param function function from where the message is printed
  57. * @param ... Format string followed by format arguments.
  58. * @note function parameter cannot be concatenated with other strings
  59. */
  60. /* Select the correct system output function*/
  61. #ifdef CONFIG_MALI_DEBUG
  62. #define KBASEP_DEBUG_ASSERT_OUT(trace, function, ...)\
  63. do { \
  64. pr_err("Mali<ASSERT>: %s function:%s ", trace, function);\
  65. pr_err(__VA_ARGS__);\
  66. pr_err("\n");\
  67. } while (false)
  68. #else // ifdef CONFIG_MALI_DEBUG
  69. #define KBASEP_DEBUG_ASSERT_OUT(trace, function, ...) CSTD_NOP()
  70. #endif
  71. #ifdef CONFIG_MALI_DEBUG
  72. #define KBASE_CALL_ASSERT_HOOK() kbasep_debug_assert_call_hook()
  73. #else
  74. #define KBASE_CALL_ASSERT_HOOK() CSTD_NOP()
  75. #endif
  76. /**
  77. * @def KBASE_DEBUG_ASSERT(expr)
  78. * @brief Calls @see KBASE_PRINT_ASSERT and prints the expression @a expr if @a expr is false
  79. *
  80. * @note This macro does nothing if the flag @see KBASE_DEBUG_DISABLE_ASSERTS is set to 1
  81. *
  82. * @param expr Boolean expression
  83. */
  84. #define KBASE_DEBUG_ASSERT(expr) \
  85. KBASE_DEBUG_ASSERT_MSG(expr, #expr)
  86. #if KBASE_DEBUG_DISABLE_ASSERTS
  87. #define KBASE_DEBUG_ASSERT_MSG(expr, ...) CSTD_NOP()
  88. #else
  89. /**
  90. * @def KBASE_DEBUG_ASSERT_MSG(expr, ...)
  91. * @brief Calls @see KBASEP_DEBUG_ASSERT_OUT and prints the given message if @a expr is false
  92. *
  93. * @note This macro does nothing if the flag @see KBASE_DEBUG_DISABLE_ASSERTS is set to 1
  94. *
  95. * @param expr Boolean expression
  96. * @param ... Message to display when @a expr is false, as a format string followed by format arguments.
  97. */
  98. #define KBASE_DEBUG_ASSERT_MSG(expr, ...) \
  99. do { \
  100. if (!(expr)) { \
  101. KBASEP_DEBUG_ASSERT_OUT(KBASEP_DEBUG_PRINT_TRACE, KBASEP_DEBUG_PRINT_FUNCTION, __VA_ARGS__);\
  102. KBASE_CALL_ASSERT_HOOK();\
  103. BUG();\
  104. } \
  105. } while (false)
  106. #endif // KBASE_DEBUG_DISABLE_ASSERTS
  107. /**
  108. * @def KBASE_DEBUG_CODE( X )
  109. * @brief Executes the code inside the macro only in debug mode
  110. *
  111. * @param X Code to compile only in debug mode.
  112. */
  113. #ifdef CONFIG_MALI_DEBUG
  114. #define KBASE_DEBUG_CODE(X) X
  115. #else
  116. #define KBASE_DEBUG_CODE(X) CSTD_NOP()
  117. #endif
  118. /** @} */
  119. /**
  120. * @brief Register a function to call on ASSERT
  121. *
  122. * Such functions will \b only be called during Debug mode, and for debugging
  123. * features \b only. Do not rely on them to be called in general use.
  124. *
  125. * To disable the hook, supply NULL to \a func.
  126. *
  127. * @note This function is not thread-safe, and should only be used to
  128. * register/deregister once in the module's lifetime.
  129. *
  130. * @param[in] func the function to call when an assert is triggered.
  131. * @param[in] param the parameter to pass to \a func when calling it
  132. */
  133. void kbase_debug_assert_register_hook(kbase_debug_assert_hook *func, void *param);
  134. /**
  135. * @brief Call a debug assert hook previously registered with kbase_debug_assert_register_hook()
  136. *
  137. * @note This function is not thread-safe with respect to multiple threads
  138. * registering functions and parameters with
  139. * kbase_debug_assert_register_hook(). Otherwise, thread safety is the
  140. * responsibility of the registered hook.
  141. */
  142. void kbasep_debug_assert_call_hook(void);
  143. #endif // ifndef _KBASE_DEBUG_H