1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright IBM Corp. 1999, 2020
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>
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 */
24 #define DEBUG_DIR_ROOT "s390dbf" /* name of debug root directory in proc fs */
26 #define DEBUG_DATA(entry) (char *)(entry + 1) /* data is stored behind */
27 /* the entry information */
29 #define __DEBUG_FEATURE_VERSION 3 /* version of debug feature */
31 struct __debug_entry
{
32 unsigned long clock
: 60;
33 unsigned long exception
: 1;
34 unsigned long level
: 3;
39 typedef struct __debug_entry debug_entry_t
;
43 typedef struct debug_info
{
44 struct debug_info
*next
;
45 struct debug_info
*prev
;
53 debug_entry_t
***areas
;
57 struct dentry
*debugfs_root_entry
;
58 struct dentry
*debugfs_entries
[DEBUG_MAX_VIEWS
];
59 struct debug_view
*views
[DEBUG_MAX_VIEWS
];
60 char name
[DEBUG_MAX_NAME_LEN
];
64 typedef int (debug_header_proc_t
) (debug_info_t
*id
,
65 struct debug_view
*view
,
70 typedef int (debug_format_proc_t
) (debug_info_t
*id
,
71 struct debug_view
*view
, char *out_buf
,
73 typedef int (debug_prolog_proc_t
) (debug_info_t
*id
,
74 struct debug_view
*view
,
76 typedef int (debug_input_proc_t
) (debug_info_t
*id
,
77 struct debug_view
*view
,
79 const char __user
*user_buf
,
80 size_t in_buf_size
, loff_t
*offset
);
82 int debug_dflt_header_fn(debug_info_t
*id
, struct debug_view
*view
,
83 int area
, debug_entry_t
*entry
, char *out_buf
);
86 char name
[DEBUG_MAX_NAME_LEN
];
87 debug_prolog_proc_t
*prolog_proc
;
88 debug_header_proc_t
*header_proc
;
89 debug_format_proc_t
*format_proc
;
90 debug_input_proc_t
*input_proc
;
94 extern struct debug_view debug_hex_ascii_view
;
95 extern struct debug_view debug_sprintf_view
;
97 /* do NOT use the _common functions */
99 debug_entry_t
*debug_event_common(debug_info_t
*id
, int level
,
100 const void *data
, int length
);
102 debug_entry_t
*debug_exception_common(debug_info_t
*id
, int level
,
103 const void *data
, int length
);
105 /* Debug Feature API: */
107 debug_info_t
*debug_register(const char *name
, int pages
, int nr_areas
,
110 debug_info_t
*debug_register_mode(const char *name
, int pages
, int nr_areas
,
111 int buf_size
, umode_t mode
, uid_t uid
,
114 void debug_unregister(debug_info_t
*id
);
116 void debug_set_level(debug_info_t
*id
, int new_level
);
118 void debug_set_critical(void);
120 void debug_stop_all(void);
123 * debug_level_enabled() - Returns true if debug events for the specified
124 * level would be logged. Otherwise returns false.
126 * @id: handle for debug log
127 * @level: debug level
130 * - %true if level is less or equal to the current debug level.
132 static inline bool debug_level_enabled(debug_info_t
*id
, int level
)
134 return level
<= id
->level
;
138 * debug_event() - writes binary debug entry to active debug area
139 * (if level <= actual debug level)
141 * @id: handle for debug log
142 * @level: debug level
143 * @data: pointer to data for debug entry
144 * @length: length of data in bytes
147 * - Address of written debug entry
150 static inline debug_entry_t
*debug_event(debug_info_t
*id
, int level
,
151 void *data
, int length
)
153 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
155 return debug_event_common(id
, level
, data
, length
);
159 * debug_int_event() - writes unsigned integer debug entry to active debug area
160 * (if level <= actual debug level)
162 * @id: handle for debug log
163 * @level: debug level
164 * @tag: integer value for debug entry
167 * - Address of written debug entry
170 static inline debug_entry_t
*debug_int_event(debug_info_t
*id
, int level
,
173 unsigned int t
= tag
;
175 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
177 return debug_event_common(id
, level
, &t
, sizeof(unsigned int));
181 * debug_long_event() - writes unsigned long debug entry to active debug area
182 * (if level <= actual debug level)
184 * @id: handle for debug log
185 * @level: debug level
186 * @tag: long integer value for debug entry
189 * - Address of written debug entry
192 static inline debug_entry_t
*debug_long_event(debug_info_t
*id
, int level
,
195 unsigned long t
= tag
;
197 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
199 return debug_event_common(id
, level
, &t
, sizeof(unsigned long));
203 * debug_text_event() - writes string debug entry in ascii format to active
204 * debug area (if level <= actual debug level)
206 * @id: handle for debug log
207 * @level: debug level
208 * @txt: string for debug entry
211 * - Address of written debug entry
214 static inline debug_entry_t
*debug_text_event(debug_info_t
*id
, int level
,
217 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
219 return debug_event_common(id
, level
, txt
, strlen(txt
));
223 * IMPORTANT: Use "%s" in sprintf format strings with care! Only pointers are
224 * stored in the s390dbf. See Documentation/s390/s390dbf.rst for more details!
226 extern debug_entry_t
*
227 __debug_sprintf_event(debug_info_t
*id
, int level
, char *string
, ...)
228 __attribute__ ((format(printf
, 3, 4)));
231 * debug_sprintf_event() - writes debug entry with format string
232 * and varargs (longs) to active debug area
233 * (if level $<=$ actual debug level).
235 * @_id: handle for debug log
236 * @_level: debug level
237 * @_fmt: format string for debug entry
238 * @...: varargs used as in sprintf()
241 * - Address of written debug entry
244 * floats and long long datatypes cannot be used as varargs.
246 #define debug_sprintf_event(_id, _level, _fmt, ...) \
248 debug_entry_t *__ret; \
249 debug_info_t *__id = _id; \
250 int __level = _level; \
252 if ((!__id) || (__level > __id->level)) \
255 __ret = __debug_sprintf_event(__id, __level, \
256 _fmt, ## __VA_ARGS__); \
261 * debug_exception() - writes binary debug entry to active debug area
262 * (if level <= actual debug level)
263 * and switches to next debug area
265 * @id: handle for debug log
266 * @level: debug level
267 * @data: pointer to data for debug entry
268 * @length: length of data in bytes
271 * - Address of written debug entry
274 static inline debug_entry_t
*debug_exception(debug_info_t
*id
, int level
,
275 void *data
, int length
)
277 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
279 return debug_exception_common(id
, level
, data
, length
);
283 * debug_int_exception() - writes unsigned int debug entry to active debug area
284 * (if level <= actual debug level)
285 * and switches to next debug area
287 * @id: handle for debug log
288 * @level: debug level
289 * @tag: integer value for debug entry
292 * - Address of written debug entry
295 static inline debug_entry_t
*debug_int_exception(debug_info_t
*id
, int level
,
298 unsigned int t
= tag
;
300 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
302 return debug_exception_common(id
, level
, &t
, sizeof(unsigned int));
306 * debug_long_exception() - writes long debug entry to active debug area
307 * (if level <= actual debug level)
308 * and switches to next debug area
310 * @id: handle for debug log
311 * @level: debug level
312 * @tag: long integer value for debug entry
315 * - Address of written debug entry
318 static inline debug_entry_t
*debug_long_exception (debug_info_t
*id
, int level
,
321 unsigned long t
= tag
;
323 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
325 return debug_exception_common(id
, level
, &t
, sizeof(unsigned long));
329 * debug_text_exception() - writes string debug entry in ascii format to active
330 * debug area (if level <= actual debug level)
331 * and switches to next debug area
334 * @id: handle for debug log
335 * @level: debug level
336 * @txt: string for debug entry
339 * - Address of written debug entry
342 static inline debug_entry_t
*debug_text_exception(debug_info_t
*id
, int level
,
345 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
347 return debug_exception_common(id
, level
, txt
, strlen(txt
));
351 * IMPORTANT: Use "%s" in sprintf format strings with care! Only pointers are
352 * stored in the s390dbf. See Documentation/s390/s390dbf.rst for more details!
354 extern debug_entry_t
*
355 __debug_sprintf_exception(debug_info_t
*id
, int level
, char *string
, ...)
356 __attribute__ ((format(printf
, 3, 4)));
360 * debug_sprintf_exception() - writes debug entry with format string and
361 * varargs (longs) to active debug area
362 * (if level <= actual debug level)
363 * and switches to next debug area.
365 * @_id: handle for debug log
366 * @_level: debug level
367 * @_fmt: format string for debug entry
368 * @...: varargs used as in sprintf()
371 * - Address of written debug entry
374 * floats and long long datatypes cannot be used as varargs.
376 #define debug_sprintf_exception(_id, _level, _fmt, ...) \
378 debug_entry_t *__ret; \
379 debug_info_t *__id = _id; \
380 int __level = _level; \
382 if ((!__id) || (__level > __id->level)) \
385 __ret = __debug_sprintf_exception(__id, __level, \
386 _fmt, ## __VA_ARGS__);\
390 int debug_register_view(debug_info_t
*id
, struct debug_view
*view
);
392 int debug_unregister_view(debug_info_t
*id
, struct debug_view
*view
);
395 define the debug levels:
396 - 0 No debugging output to console or syslog
397 - 1 Log internal errors to syslog, ignore check conditions
398 - 2 Log internal errors and check conditions to syslog
399 - 3 Log internal errors to console, log check conditions to syslog
400 - 4 Log internal errors and check conditions to console
401 - 5 panic on internal errors, log check conditions to console
402 - 6 panic on both, internal errors and check conditions
406 #define DEBUG_LEVEL 4
409 #define INTERNAL_ERRMSG(x,y...) "E" __FILE__ "%d: " x, __LINE__, y
410 #define INTERNAL_WRNMSG(x,y...) "W" __FILE__ "%d: " x, __LINE__, y
411 #define INTERNAL_INFMSG(x,y...) "I" __FILE__ "%d: " x, __LINE__, y
412 #define INTERNAL_DEBMSG(x,y...) "D" __FILE__ "%d: " x, __LINE__, y
415 #define PRINT_DEBUG(x...) printk(KERN_DEBUG PRINTK_HEADER x)
416 #define PRINT_INFO(x...) printk(KERN_INFO PRINTK_HEADER x)
417 #define PRINT_WARN(x...) printk(KERN_WARNING PRINTK_HEADER x)
418 #define PRINT_ERR(x...) printk(KERN_ERR PRINTK_HEADER x)
419 #define PRINT_FATAL(x...) panic(PRINTK_HEADER x)
421 #define PRINT_DEBUG(x...) printk(KERN_DEBUG PRINTK_HEADER x)
422 #define PRINT_INFO(x...) printk(KERN_DEBUG PRINTK_HEADER x)
423 #define PRINT_WARN(x...) printk(KERN_DEBUG PRINTK_HEADER x)
424 #define PRINT_ERR(x...) printk(KERN_DEBUG PRINTK_HEADER x)
425 #define PRINT_FATAL(x...) printk(KERN_DEBUG PRINTK_HEADER x)
426 #endif /* DASD_DEBUG */