d39da8f3130efc2ffe9bd53a1625e028416498c4
[linux-2.6-microblaze.git] / arch / s390 / include / asm / debug.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *   S/390 debug facility
4  *
5  *    Copyright IBM Corp. 1999, 2000
6  */
7 #ifndef DEBUG_H
8 #define DEBUG_H
9
10 #include <linux/string.h>
11 #include <linux/spinlock.h>
12 #include <linux/kernel.h>
13 #include <linux/time.h>
14 #include <linux/refcount.h>
15 #include <uapi/asm/debug.h>
16
17 #define DEBUG_MAX_LEVEL            6  /* debug levels range from 0 to 6 */
18 #define DEBUG_OFF_LEVEL            -1 /* level where debug is switched off */
19 #define DEBUG_FLUSH_ALL            -1 /* parameter to flush all areas */
20 #define DEBUG_MAX_VIEWS            10 /* max number of views in proc fs */
21 #define DEBUG_MAX_NAME_LEN         64 /* max length for a debugfs file name */
22 #define DEBUG_DEFAULT_LEVEL        3  /* initial debug level */
23
24 #define DEBUG_DIR_ROOT "s390dbf" /* name of debug root directory in proc fs */
25
26 #define DEBUG_DATA(entry) (char *)(entry + 1) /* data is stored behind */
27                                               /* the entry information */
28
29 typedef struct __debug_entry debug_entry_t;
30
31 struct debug_view;
32
33 typedef struct debug_info {
34         struct debug_info *next;
35         struct debug_info *prev;
36         refcount_t ref_count;
37         spinlock_t lock;
38         int level;
39         int nr_areas;
40         int pages_per_area;
41         int buf_size;
42         int entry_size;
43         debug_entry_t ***areas;
44         int active_area;
45         int *active_pages;
46         int *active_entries;
47         struct dentry *debugfs_root_entry;
48         struct dentry *debugfs_entries[DEBUG_MAX_VIEWS];
49         struct debug_view *views[DEBUG_MAX_VIEWS];
50         char name[DEBUG_MAX_NAME_LEN];
51         umode_t mode;
52 } debug_info_t;
53
54 typedef int (debug_header_proc_t) (debug_info_t *id,
55                                    struct debug_view *view,
56                                    int area,
57                                    debug_entry_t *entry,
58                                    char *out_buf);
59
60 typedef int (debug_format_proc_t) (debug_info_t *id,
61                                    struct debug_view *view, char *out_buf,
62                                    const char *in_buf);
63 typedef int (debug_prolog_proc_t) (debug_info_t *id,
64                                    struct debug_view *view,
65                                    char *out_buf);
66 typedef int (debug_input_proc_t) (debug_info_t *id,
67                                   struct debug_view *view,
68                                   struct file *file,
69                                   const char __user *user_buf,
70                                   size_t in_buf_size, loff_t *offset);
71
72 int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view,
73                          int area, debug_entry_t *entry, char *out_buf);
74
75 struct debug_view {
76         char name[DEBUG_MAX_NAME_LEN];
77         debug_prolog_proc_t *prolog_proc;
78         debug_header_proc_t *header_proc;
79         debug_format_proc_t *format_proc;
80         debug_input_proc_t  *input_proc;
81         void                *private_data;
82 };
83
84 extern struct debug_view debug_hex_ascii_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
110 void debug_stop_all(void);
111
112 /**
113  * debug_level_enabled() - Returns true if debug events for the specified
114  *                         level would be logged. Otherwise returns false.
115  *
116  * @id:         handle for debug log
117  * @level:      debug level
118  *
119  * Return:
120  * - %true if level is less or equal to the current debug level.
121  */
122 static inline bool debug_level_enabled(debug_info_t *id, int level)
123 {
124         return level <= id->level;
125 }
126
127 /**
128  * debug_event() - writes binary debug entry to active debug area
129  *                 (if level <= actual debug level)
130  *
131  * @id:         handle for debug log
132  * @level:      debug level
133  * @data:       pointer to data for debug entry
134  * @length:     length of data in bytes
135  *
136  * Return:
137  * - Address of written debug entry
138  * - %NULL if error
139  */
140 static inline debug_entry_t *debug_event(debug_info_t *id, int level,
141                                          void *data, int length)
142 {
143         if ((!id) || (level > id->level) || (id->pages_per_area == 0))
144                 return NULL;
145         return debug_event_common(id, level, data, length);
146 }
147
148 /**
149  * debug_int_event() - writes unsigned integer debug entry to active debug area
150  *                     (if level <= actual debug level)
151  *
152  * @id:         handle for debug log
153  * @level:      debug level
154  * @tag:        integer value for debug entry
155  *
156  * Return:
157  * - Address of written debug entry
158  * - %NULL if error
159  */
160 static inline debug_entry_t *debug_int_event(debug_info_t *id, int level,
161                                              unsigned int tag)
162 {
163         unsigned int t = tag;
164
165         if ((!id) || (level > id->level) || (id->pages_per_area == 0))
166                 return NULL;
167         return debug_event_common(id, level, &t, sizeof(unsigned int));
168 }
169
170 /**
171  * debug_long_event() - writes unsigned long debug entry to active debug area
172  *                     (if level <= actual debug level)
173  *
174  * @id:         handle for debug log
175  * @level:      debug level
176  * @tag:        long integer value for debug entry
177  *
178  * Return:
179  * - Address of written debug entry
180  * - %NULL if error
181  */
182 static inline debug_entry_t *debug_long_event(debug_info_t *id, int level,
183                                               unsigned long tag)
184 {
185         unsigned long t = tag;
186
187         if ((!id) || (level > id->level) || (id->pages_per_area == 0))
188                 return NULL;
189         return debug_event_common(id, level, &t, sizeof(unsigned long));
190 }
191
192 /**
193  * debug_text_event() - writes string debug entry in ascii format to active
194  *                      debug area (if level <= actual debug level)
195  *
196  * @id:         handle for debug log
197  * @level:      debug level
198  * @txt:        string for debug entry
199  *
200  * Return:
201  * - Address of written debug entry
202  * - %NULL if error
203  */
204 static inline debug_entry_t *debug_text_event(debug_info_t *id, int level,
205                                               const char *txt)
206 {
207         if ((!id) || (level > id->level) || (id->pages_per_area == 0))
208                 return NULL;
209         return debug_event_common(id, level, txt, strlen(txt));
210 }
211
212 /*
213  * IMPORTANT: Use "%s" in sprintf format strings with care! Only pointers are
214  * stored in the s390dbf. See Documentation/s390/s390dbf.rst for more details!
215  */
216 extern debug_entry_t *
217 __debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
218         __attribute__ ((format(printf, 3, 4)));
219
220 /**
221  * debug_sprintf_event() - writes debug entry with format string
222  *                         and varargs (longs) to active debug area
223  *                         (if level $<=$ actual debug level).
224  *
225  * @_id:        handle for debug log
226  * @_level:     debug level
227  * @_fmt:       format string for debug entry
228  * @...:        varargs used as in sprintf()
229  *
230  * Return:
231  * - Address of written debug entry
232  * - %NULL if error
233  *
234  * floats and long long datatypes cannot be used as varargs.
235  */
236 #define debug_sprintf_event(_id, _level, _fmt, ...)                     \
237 ({                                                                      \
238         debug_entry_t *__ret;                                           \
239         debug_info_t *__id = _id;                                       \
240         int __level = _level;                                           \
241                                                                         \
242         if ((!__id) || (__level > __id->level))                         \
243                 __ret = NULL;                                           \
244         else                                                            \
245                 __ret = __debug_sprintf_event(__id, __level,            \
246                                               _fmt, ## __VA_ARGS__);    \
247         __ret;                                                          \
248 })
249
250 /**
251  * debug_exception() - writes binary debug entry to active debug area
252  *                     (if level <= actual debug level)
253  *                     and switches to next debug area
254  *
255  * @id:         handle for debug log
256  * @level:      debug level
257  * @data:       pointer to data for debug entry
258  * @length:     length of data in bytes
259  *
260  * Return:
261  * - Address of written debug entry
262  * - %NULL if error
263  */
264 static inline debug_entry_t *debug_exception(debug_info_t *id, int level,
265                                              void *data, int length)
266 {
267         if ((!id) || (level > id->level) || (id->pages_per_area == 0))
268                 return NULL;
269         return debug_exception_common(id, level, data, length);
270 }
271
272 /**
273  * debug_int_exception() - writes unsigned int debug entry to active debug area
274  *                         (if level <= actual debug level)
275  *                         and switches to next debug area
276  *
277  * @id:         handle for debug log
278  * @level:      debug level
279  * @tag:        integer value for debug entry
280  *
281  * Return:
282  * - Address of written debug entry
283  * - %NULL if error
284  */
285 static inline debug_entry_t *debug_int_exception(debug_info_t *id, int level,
286                                                  unsigned int tag)
287 {
288         unsigned int t = tag;
289
290         if ((!id) || (level > id->level) || (id->pages_per_area == 0))
291                 return NULL;
292         return debug_exception_common(id, level, &t, sizeof(unsigned int));
293 }
294
295 /**
296  * debug_long_exception() - writes long debug entry to active debug area
297  *                         (if level <= actual debug level)
298  *                         and switches to next debug area
299  *
300  * @id:         handle for debug log
301  * @level:      debug level
302  * @tag:        long integer value for debug entry
303  *
304  * Return:
305  * - Address of written debug entry
306  * - %NULL if error
307  */
308 static inline debug_entry_t *debug_long_exception (debug_info_t *id, int level,
309                                                    unsigned long tag)
310 {
311         unsigned long t = tag;
312
313         if ((!id) || (level > id->level) || (id->pages_per_area == 0))
314                 return NULL;
315         return debug_exception_common(id, level, &t, sizeof(unsigned long));
316 }
317
318 /**
319  * debug_text_exception() - writes string debug entry in ascii format to active
320  *                          debug area (if level <= actual debug level)
321  *                          and switches to next debug area
322  * area
323  *
324  * @id: handle for debug log
325  * @level:      debug level
326  * @txt:        string for debug entry
327  *
328  * Return:
329  * - Address of written debug entry
330  * - %NULL if error
331  */
332 static inline debug_entry_t *debug_text_exception(debug_info_t *id, int level,
333                                                   const char *txt)
334 {
335         if ((!id) || (level > id->level) || (id->pages_per_area == 0))
336                 return NULL;
337         return debug_exception_common(id, level, txt, strlen(txt));
338 }
339
340 /*
341  * IMPORTANT: Use "%s" in sprintf format strings with care! Only pointers are
342  * stored in the s390dbf. See Documentation/s390/s390dbf.rst for more details!
343  */
344 extern debug_entry_t *
345 __debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
346         __attribute__ ((format(printf, 3, 4)));
347
348
349 /**
350  * debug_sprintf_exception() - writes debug entry with format string and
351  *                             varargs (longs) to active debug area
352  *                             (if level <= actual debug level)
353  *                             and switches to next debug area.
354  *
355  * @_id:        handle for debug log
356  * @_level:     debug level
357  * @_fmt:       format string for debug entry
358  * @...:        varargs used as in sprintf()
359  *
360  * Return:
361  * - Address of written debug entry
362  * - %NULL if error
363  *
364  * floats and long long datatypes cannot be used as varargs.
365  */
366 #define debug_sprintf_exception(_id, _level, _fmt, ...)                 \
367 ({                                                                      \
368         debug_entry_t *__ret;                                           \
369         debug_info_t *__id = _id;                                       \
370         int __level = _level;                                           \
371                                                                         \
372         if ((!__id) || (__level > __id->level))                         \
373                 __ret = NULL;                                           \
374         else                                                            \
375                 __ret = __debug_sprintf_exception(__id, __level,        \
376                                                   _fmt, ## __VA_ARGS__);\
377         __ret;                                                          \
378 })
379
380 int debug_register_view(debug_info_t *id, struct debug_view *view);
381
382 int debug_unregister_view(debug_info_t *id, struct debug_view *view);
383
384 /*
385    define the debug levels:
386    - 0 No debugging output to console or syslog
387    - 1 Log internal errors to syslog, ignore check conditions
388    - 2 Log internal errors and check conditions to syslog
389    - 3 Log internal errors to console, log check conditions to syslog
390    - 4 Log internal errors and check conditions to console
391    - 5 panic on internal errors, log check conditions to console
392    - 6 panic on both, internal errors and check conditions
393  */
394
395 #ifndef DEBUG_LEVEL
396 #define DEBUG_LEVEL 4
397 #endif
398
399 #define INTERNAL_ERRMSG(x,y...) "E" __FILE__ "%d: " x, __LINE__, y
400 #define INTERNAL_WRNMSG(x,y...) "W" __FILE__ "%d: " x, __LINE__, y
401 #define INTERNAL_INFMSG(x,y...) "I" __FILE__ "%d: " x, __LINE__, y
402 #define INTERNAL_DEBMSG(x,y...) "D" __FILE__ "%d: " x, __LINE__, y
403
404 #if DEBUG_LEVEL > 0
405 #define PRINT_DEBUG(x...)       printk(KERN_DEBUG PRINTK_HEADER x)
406 #define PRINT_INFO(x...)        printk(KERN_INFO PRINTK_HEADER x)
407 #define PRINT_WARN(x...)        printk(KERN_WARNING PRINTK_HEADER x)
408 #define PRINT_ERR(x...)         printk(KERN_ERR PRINTK_HEADER x)
409 #define PRINT_FATAL(x...)       panic(PRINTK_HEADER x)
410 #else
411 #define PRINT_DEBUG(x...)       printk(KERN_DEBUG PRINTK_HEADER x)
412 #define PRINT_INFO(x...)        printk(KERN_DEBUG PRINTK_HEADER x)
413 #define PRINT_WARN(x...)        printk(KERN_DEBUG PRINTK_HEADER x)
414 #define PRINT_ERR(x...)         printk(KERN_DEBUG PRINTK_HEADER x)
415 #define PRINT_FATAL(x...)       printk(KERN_DEBUG PRINTK_HEADER x)
416 #endif /* DASD_DEBUG */
417
418 #endif /* DEBUG_H */