xref: /openbmc/linux/arch/s390/include/asm/debug.h (revision d2c43ff1)
1 /*
2  *   S/390 debug facility
3  *
4  *    Copyright IBM Corp. 1999, 2000
5  */
6 #ifndef DEBUG_H
7 #define DEBUG_H
8 
9 #include <linux/string.h>
10 #include <linux/spinlock.h>
11 #include <linux/kernel.h>
12 #include <linux/time.h>
13 #include <linux/refcount.h>
14 #include <uapi/asm/debug.h>
15 
16 #define DEBUG_MAX_LEVEL            6  /* debug levels range from 0 to 6 */
17 #define DEBUG_OFF_LEVEL            -1 /* level where debug is switched off */
18 #define DEBUG_FLUSH_ALL            -1 /* parameter to flush all areas */
19 #define DEBUG_MAX_VIEWS            10 /* max number of views in proc fs */
20 #define DEBUG_MAX_NAME_LEN         64 /* max length for a debugfs file name */
21 #define DEBUG_DEFAULT_LEVEL        3  /* initial debug level */
22 
23 #define DEBUG_DIR_ROOT "s390dbf" /* name of debug root directory in proc fs */
24 
25 #define DEBUG_DATA(entry) (char*)(entry + 1) /* data is stored behind */
26                                              /* the entry information */
27 
28 typedef struct __debug_entry debug_entry_t;
29 
30 struct debug_view;
31 
32 typedef struct debug_info {
33 	struct debug_info* next;
34 	struct debug_info* prev;
35 	refcount_t ref_count;
36 	spinlock_t lock;
37 	int level;
38 	int nr_areas;
39 	int pages_per_area;
40 	int buf_size;
41 	int entry_size;
42 	debug_entry_t*** areas;
43 	int active_area;
44 	int *active_pages;
45 	int *active_entries;
46 	struct dentry* debugfs_root_entry;
47 	struct dentry* debugfs_entries[DEBUG_MAX_VIEWS];
48 	struct debug_view* views[DEBUG_MAX_VIEWS];
49 	char name[DEBUG_MAX_NAME_LEN];
50 	umode_t mode;
51 } debug_info_t;
52 
53 typedef int (debug_header_proc_t) (debug_info_t* id,
54 				   struct debug_view* view,
55 				   int area,
56 				   debug_entry_t* entry,
57 				   char* out_buf);
58 
59 typedef int (debug_format_proc_t) (debug_info_t* id,
60 				   struct debug_view* view, char* out_buf,
61 				   const char* in_buf);
62 typedef int (debug_prolog_proc_t) (debug_info_t* id,
63 				   struct debug_view* view,
64 				   char* out_buf);
65 typedef int (debug_input_proc_t) (debug_info_t* id,
66 				  struct debug_view* view,
67 				  struct file* file,
68 				  const char __user *user_buf,
69 				  size_t in_buf_size, loff_t* offset);
70 
71 int debug_dflt_header_fn(debug_info_t* id, struct debug_view* view,
72 		         int area, debug_entry_t* entry, char* out_buf);
73 
74 struct debug_view {
75 	char name[DEBUG_MAX_NAME_LEN];
76 	debug_prolog_proc_t* prolog_proc;
77 	debug_header_proc_t* header_proc;
78 	debug_format_proc_t* format_proc;
79 	debug_input_proc_t*  input_proc;
80 	void*                private_data;
81 };
82 
83 extern struct debug_view debug_hex_ascii_view;
84 extern struct debug_view debug_raw_view;
85 extern struct debug_view debug_sprintf_view;
86 
87 /* do NOT use the _common functions */
88 
89 debug_entry_t* debug_event_common(debug_info_t* id, int level,
90                                   const void* data, int length);
91 
92 debug_entry_t* debug_exception_common(debug_info_t* id, int level,
93                                       const void* data, int length);
94 
95 /* Debug Feature API: */
96 
97 debug_info_t *debug_register(const char *name, int pages, int nr_areas,
98                              int buf_size);
99 
100 debug_info_t *debug_register_mode(const char *name, int pages, int nr_areas,
101 				  int buf_size, umode_t mode, uid_t uid,
102 				  gid_t gid);
103 
104 void debug_unregister(debug_info_t* id);
105 
106 void debug_set_level(debug_info_t* id, int new_level);
107 
108 void debug_set_critical(void);
109 void debug_stop_all(void);
110 
111 static inline bool debug_level_enabled(debug_info_t* id, int level)
112 {
113 	return level <= id->level;
114 }
115 
116 static inline debug_entry_t*
117 debug_event(debug_info_t* id, int level, void* data, int length)
118 {
119 	if ((!id) || (level > id->level) || (id->pages_per_area == 0))
120 		return NULL;
121         return debug_event_common(id,level,data,length);
122 }
123 
124 static inline debug_entry_t*
125 debug_int_event(debug_info_t* id, int level, unsigned int tag)
126 {
127         unsigned int t=tag;
128 	if ((!id) || (level > id->level) || (id->pages_per_area == 0))
129 		return NULL;
130         return debug_event_common(id,level,&t,sizeof(unsigned int));
131 }
132 
133 static inline debug_entry_t *
134 debug_long_event (debug_info_t* id, int level, unsigned long tag)
135 {
136         unsigned long t=tag;
137 	if ((!id) || (level > id->level) || (id->pages_per_area == 0))
138 		return NULL;
139         return debug_event_common(id,level,&t,sizeof(unsigned long));
140 }
141 
142 static inline debug_entry_t*
143 debug_text_event(debug_info_t* id, int level, const char* txt)
144 {
145 	if ((!id) || (level > id->level) || (id->pages_per_area == 0))
146 		return NULL;
147         return debug_event_common(id,level,txt,strlen(txt));
148 }
149 
150 /*
151  * IMPORTANT: Use "%s" in sprintf format strings with care! Only pointers are
152  * stored in the s390dbf. See Documentation/s390/s390dbf.txt for more details!
153  */
154 extern debug_entry_t *
155 __debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
156 	__attribute__ ((format(printf, 3, 4)));
157 
158 #define debug_sprintf_event(_id, _level, _fmt, ...)			\
159 ({									\
160 	debug_entry_t *__ret;						\
161 	debug_info_t *__id = _id;					\
162 	int __level = _level;						\
163 	if ((!__id) || (__level > __id->level))				\
164 		__ret = NULL;						\
165 	else								\
166 		__ret = __debug_sprintf_event(__id, __level,		\
167 					      _fmt, ## __VA_ARGS__);	\
168 	__ret;								\
169 })
170 
171 static inline debug_entry_t*
172 debug_exception(debug_info_t* id, int level, void* data, int length)
173 {
174 	if ((!id) || (level > id->level) || (id->pages_per_area == 0))
175 		return NULL;
176         return debug_exception_common(id,level,data,length);
177 }
178 
179 static inline debug_entry_t*
180 debug_int_exception(debug_info_t* id, int level, unsigned int tag)
181 {
182         unsigned int t=tag;
183 	if ((!id) || (level > id->level) || (id->pages_per_area == 0))
184 		return NULL;
185         return debug_exception_common(id,level,&t,sizeof(unsigned int));
186 }
187 
188 static inline debug_entry_t *
189 debug_long_exception (debug_info_t* id, int level, unsigned long tag)
190 {
191         unsigned long t=tag;
192 	if ((!id) || (level > id->level) || (id->pages_per_area == 0))
193 		return NULL;
194         return debug_exception_common(id,level,&t,sizeof(unsigned long));
195 }
196 
197 static inline debug_entry_t*
198 debug_text_exception(debug_info_t* id, int level, const char* txt)
199 {
200 	if ((!id) || (level > id->level) || (id->pages_per_area == 0))
201 		return NULL;
202         return debug_exception_common(id,level,txt,strlen(txt));
203 }
204 
205 /*
206  * IMPORTANT: Use "%s" in sprintf format strings with care! Only pointers are
207  * stored in the s390dbf. See Documentation/s390/s390dbf.txt for more details!
208  */
209 extern debug_entry_t *
210 __debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
211 	__attribute__ ((format(printf, 3, 4)));
212 
213 #define debug_sprintf_exception(_id, _level, _fmt, ...)			\
214 ({									\
215 	debug_entry_t *__ret;						\
216 	debug_info_t *__id = _id;					\
217 	int __level = _level;						\
218 	if ((!__id) || (__level > __id->level))				\
219 		__ret = NULL;						\
220 	else								\
221 		__ret = __debug_sprintf_exception(__id, __level,	\
222 						  _fmt, ## __VA_ARGS__);\
223 	__ret;								\
224 })
225 
226 int debug_register_view(debug_info_t* id, struct debug_view* view);
227 int debug_unregister_view(debug_info_t* id, struct debug_view* view);
228 
229 /*
230    define the debug levels:
231    - 0 No debugging output to console or syslog
232    - 1 Log internal errors to syslog, ignore check conditions
233    - 2 Log internal errors and check conditions to syslog
234    - 3 Log internal errors to console, log check conditions to syslog
235    - 4 Log internal errors and check conditions to console
236    - 5 panic on internal errors, log check conditions to console
237    - 6 panic on both, internal errors and check conditions
238  */
239 
240 #ifndef DEBUG_LEVEL
241 #define DEBUG_LEVEL 4
242 #endif
243 
244 #define INTERNAL_ERRMSG(x,y...) "E" __FILE__ "%d: " x, __LINE__, y
245 #define INTERNAL_WRNMSG(x,y...) "W" __FILE__ "%d: " x, __LINE__, y
246 #define INTERNAL_INFMSG(x,y...) "I" __FILE__ "%d: " x, __LINE__, y
247 #define INTERNAL_DEBMSG(x,y...) "D" __FILE__ "%d: " x, __LINE__, y
248 
249 #if DEBUG_LEVEL > 0
250 #define PRINT_DEBUG(x...) printk ( KERN_DEBUG PRINTK_HEADER x )
251 #define PRINT_INFO(x...) printk ( KERN_INFO PRINTK_HEADER x )
252 #define PRINT_WARN(x...) printk ( KERN_WARNING PRINTK_HEADER x )
253 #define PRINT_ERR(x...) printk ( KERN_ERR PRINTK_HEADER x )
254 #define PRINT_FATAL(x...) panic ( PRINTK_HEADER x )
255 #else
256 #define PRINT_DEBUG(x...) printk ( KERN_DEBUG PRINTK_HEADER x )
257 #define PRINT_INFO(x...) printk ( KERN_DEBUG PRINTK_HEADER x )
258 #define PRINT_WARN(x...) printk ( KERN_DEBUG PRINTK_HEADER x )
259 #define PRINT_ERR(x...) printk ( KERN_DEBUG PRINTK_HEADER x )
260 #define PRINT_FATAL(x...) printk ( KERN_DEBUG PRINTK_HEADER x )
261 #endif				/* DASD_DEBUG */
262 
263 #endif				/* DEBUG_H */
264