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 
16 
17 
18 
19 
20 #ifndef _KBASE_DEBUG_H
21 #define _KBASE_DEBUG_H
22 
23 #include <linux/bug.h>
24 
25 /** @brief If equals to 0, a trace containing the file, line, and function will be displayed before each message. */
26 #define KBASE_DEBUG_SKIP_TRACE 0
27 
28 /** @brief If different from 0, the trace will only contain the file and line. */
29 #define KBASE_DEBUG_SKIP_FUNCTION_NAME 0
30 
31 /** @brief Disable the asserts tests if set to 1. Default is to disable the asserts in release. */
32 #ifndef KBASE_DEBUG_DISABLE_ASSERTS
33 #ifdef CONFIG_MALI_DEBUG
34 #define KBASE_DEBUG_DISABLE_ASSERTS 0
35 #else
36 #define KBASE_DEBUG_DISABLE_ASSERTS 1
37 #endif
38 #endif				/* KBASE_DEBUG_DISABLE_ASSERTS */
39 
40 /** Function type that is called on an KBASE_DEBUG_ASSERT() or KBASE_DEBUG_ASSERT_MSG() */
41 typedef void (kbase_debug_assert_hook) (void *);
42 
43 struct kbasep_debug_assert_cb {
44 	kbase_debug_assert_hook *func;
45 	void *param;
46 };
47 
48 /**
49  * @def KBASEP_DEBUG_PRINT_TRACE
50  * @brief Private macro containing the format of the trace to display before every message
51  * @sa KBASE_DEBUG_SKIP_TRACE, KBASE_DEBUG_SKIP_FUNCTION_NAME
52  */
53 #if !KBASE_DEBUG_SKIP_TRACE
54 #define KBASEP_DEBUG_PRINT_TRACE \
55 		"In file: " __FILE__ " line: " CSTD_STR2(__LINE__)
56 #if !KBASE_DEBUG_SKIP_FUNCTION_NAME
57 #define KBASEP_DEBUG_PRINT_FUNCTION __func__
58 #else
59 #define KBASEP_DEBUG_PRINT_FUNCTION ""
60 #endif
61 #else
62 #define KBASEP_DEBUG_PRINT_TRACE ""
63 #endif
64 
65 /**
66  * @def KBASEP_DEBUG_ASSERT_OUT(trace, function, ...)
67  * @brief (Private) system printing function associated to the @see KBASE_DEBUG_ASSERT_MSG event.
68  * @param trace location in the code from where the message is printed
69  * @param function function from where the message is printed
70  * @param ... Format string followed by format arguments.
71  * @note function parameter cannot be concatenated with other strings
72  */
73 /* Select the correct system output function*/
74 #ifdef CONFIG_MALI_DEBUG
75 #define KBASEP_DEBUG_ASSERT_OUT(trace, function, ...)\
76 		do { \
77 			pr_err("Mali<ASSERT>: %s function:%s ", trace, function);\
78 			pr_err(__VA_ARGS__);\
79 			pr_err("\n");\
80 		} while (false)
81 #else
82 #define KBASEP_DEBUG_ASSERT_OUT(trace, function, ...) CSTD_NOP()
83 #endif
84 
85 #ifdef CONFIG_MALI_DEBUG
86 #define KBASE_CALL_ASSERT_HOOK() kbasep_debug_assert_call_hook()
87 #else
88 #define KBASE_CALL_ASSERT_HOOK() CSTD_NOP()
89 #endif
90 
91 /**
92  * @def KBASE_DEBUG_ASSERT(expr)
93  * @brief Calls @see KBASE_PRINT_ASSERT and prints the expression @a expr if @a expr is false
94  *
95  * @note This macro does nothing if the flag @see KBASE_DEBUG_DISABLE_ASSERTS is set to 1
96  *
97  * @param expr Boolean expression
98  */
99 #define KBASE_DEBUG_ASSERT(expr) \
100 	KBASE_DEBUG_ASSERT_MSG(expr, #expr)
101 
102 #if KBASE_DEBUG_DISABLE_ASSERTS
103 #define KBASE_DEBUG_ASSERT_MSG(expr, ...) CSTD_NOP()
104 #else
105 	/**
106 	 * @def KBASE_DEBUG_ASSERT_MSG(expr, ...)
107 	 * @brief Calls @see KBASEP_DEBUG_ASSERT_OUT and prints the given message if @a expr is false
108 	 *
109 	 * @note This macro does nothing if the flag @see KBASE_DEBUG_DISABLE_ASSERTS is set to 1
110 	 *
111 	 * @param expr Boolean expression
112 	 * @param ...  Message to display when @a expr is false, as a format string followed by format arguments.
113 	 */
114 #define KBASE_DEBUG_ASSERT_MSG(expr, ...) \
115 		do { \
116 			if (!(expr)) { \
117 				KBASEP_DEBUG_ASSERT_OUT(KBASEP_DEBUG_PRINT_TRACE, KBASEP_DEBUG_PRINT_FUNCTION, __VA_ARGS__);\
118 				KBASE_CALL_ASSERT_HOOK();\
119 				BUG();\
120 			} \
121 		} while (false)
122 #endif				/* KBASE_DEBUG_DISABLE_ASSERTS */
123 
124 /**
125  * @def KBASE_DEBUG_CODE( X )
126  * @brief Executes the code inside the macro only in debug mode
127  *
128  * @param X Code to compile only in debug mode.
129  */
130 #ifdef CONFIG_MALI_DEBUG
131 #define KBASE_DEBUG_CODE(X) X
132 #else
133 #define KBASE_DEBUG_CODE(X) CSTD_NOP()
134 #endif				/* CONFIG_MALI_DEBUG */
135 
136 /** @} */
137 
138 /**
139  * @brief Register a function to call on ASSERT
140  *
141  * Such functions will \b only be called during Debug mode, and for debugging
142  * features \b only. Do not rely on them to be called in general use.
143  *
144  * To disable the hook, supply NULL to \a func.
145  *
146  * @note This function is not thread-safe, and should only be used to
147  * register/deregister once in the module's lifetime.
148  *
149  * @param[in] func the function to call when an assert is triggered.
150  * @param[in] param the parameter to pass to \a func when calling it
151  */
152 void kbase_debug_assert_register_hook(kbase_debug_assert_hook *func, void *param);
153 
154 /**
155  * @brief Call a debug assert hook previously registered with kbase_debug_assert_register_hook()
156  *
157  * @note This function is not thread-safe with respect to multiple threads
158  * registering functions and parameters with
159  * kbase_debug_assert_register_hook(). Otherwise, thread safety is the
160  * responsibility of the registered hook.
161  */
162 void kbasep_debug_assert_call_hook(void);
163 
164 #endif				/* _KBASE_DEBUG_H */
165