1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright IBM Corp. 1999, 2000
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>
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 typedef struct __debug_entry debug_entry_t
;
33 typedef struct debug_info
{
34 struct debug_info
*next
;
35 struct debug_info
*prev
;
43 debug_entry_t
***areas
;
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
];
54 typedef int (debug_header_proc_t
) (debug_info_t
*id
,
55 struct debug_view
*view
,
60 typedef int (debug_format_proc_t
) (debug_info_t
*id
,
61 struct debug_view
*view
, char *out_buf
,
63 typedef int (debug_prolog_proc_t
) (debug_info_t
*id
,
64 struct debug_view
*view
,
66 typedef int (debug_input_proc_t
) (debug_info_t
*id
,
67 struct debug_view
*view
,
69 const char __user
*user_buf
,
70 size_t in_buf_size
, loff_t
*offset
);
72 int debug_dflt_header_fn(debug_info_t
*id
, struct debug_view
*view
,
73 int area
, debug_entry_t
*entry
, char *out_buf
);
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
;
84 extern struct debug_view debug_hex_ascii_view
;
85 extern struct debug_view debug_raw_view
;
86 extern struct debug_view debug_sprintf_view
;
88 /* do NOT use the _common functions */
90 debug_entry_t
*debug_event_common(debug_info_t
*id
, int level
,
91 const void *data
, int length
);
93 debug_entry_t
*debug_exception_common(debug_info_t
*id
, int level
,
94 const void *data
, int length
);
96 /* Debug Feature API: */
98 debug_info_t
*debug_register(const char *name
, int pages
, int nr_areas
,
101 debug_info_t
*debug_register_mode(const char *name
, int pages
, int nr_areas
,
102 int buf_size
, umode_t mode
, uid_t uid
,
105 void debug_unregister(debug_info_t
*id
);
107 void debug_set_level(debug_info_t
*id
, int new_level
);
109 void debug_set_critical(void);
111 void debug_stop_all(void);
114 * debug_level_enabled() - Returns true if debug events for the specified
115 * level would be logged. Otherwise returns false.
117 * @id: handle for debug log
118 * @level: debug level
121 * - %true if level is less or equal to the current debug level.
123 static inline bool debug_level_enabled(debug_info_t
*id
, int level
)
125 return level
<= id
->level
;
129 * debug_event() - writes binary debug entry to active debug area
130 * (if level <= actual debug level)
132 * @id: handle for debug log
133 * @level: debug level
134 * @data: pointer to data for debug entry
135 * @length: length of data in bytes
138 * - Address of written debug entry
141 static inline debug_entry_t
*debug_event(debug_info_t
*id
, int level
,
142 void *data
, int length
)
144 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
146 return debug_event_common(id
, level
, data
, length
);
150 * debug_int_event() - writes unsigned integer debug entry to active debug area
151 * (if level <= actual debug level)
153 * @id: handle for debug log
154 * @level: debug level
155 * @tag: integer value for debug entry
158 * - Address of written debug entry
161 static inline debug_entry_t
*debug_int_event(debug_info_t
*id
, int level
,
164 unsigned int t
= tag
;
166 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
168 return debug_event_common(id
, level
, &t
, sizeof(unsigned int));
172 * debug_long_event() - writes unsigned long debug entry to active debug area
173 * (if level <= actual debug level)
175 * @id: handle for debug log
176 * @level: debug level
177 * @tag: long integer value for debug entry
180 * - Address of written debug entry
183 static inline debug_entry_t
*debug_long_event(debug_info_t
*id
, int level
,
186 unsigned long t
= tag
;
188 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
190 return debug_event_common(id
, level
, &t
, sizeof(unsigned long));
194 * debug_text_event() - writes string debug entry in ascii format to active
195 * debug area (if level <= actual debug level)
197 * @id: handle for debug log
198 * @level: debug level
199 * @txt: string for debug entry
202 * - Address of written debug entry
205 static inline debug_entry_t
*debug_text_event(debug_info_t
*id
, int level
,
208 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
210 return debug_event_common(id
, level
, txt
, strlen(txt
));
214 * IMPORTANT: Use "%s" in sprintf format strings with care! Only pointers are
215 * stored in the s390dbf. See Documentation/s390/s390dbf.rst for more details!
217 extern debug_entry_t
*
218 __debug_sprintf_event(debug_info_t
*id
, int level
, char *string
, ...)
219 __attribute__ ((format(printf
, 3, 4)));
222 * debug_sprintf_event() - writes debug entry with format string
223 * and varargs (longs) to active debug area
224 * (if level $<=$ actual debug level).
226 * @_id: handle for debug log
227 * @_level: debug level
228 * @_fmt: format string for debug entry
229 * @...: varargs used as in sprintf()
232 * - Address of written debug entry
235 * floats and long long datatypes cannot be used as varargs.
237 #define debug_sprintf_event(_id, _level, _fmt, ...) \
239 debug_entry_t *__ret; \
240 debug_info_t *__id = _id; \
241 int __level = _level; \
243 if ((!__id) || (__level > __id->level)) \
246 __ret = __debug_sprintf_event(__id, __level, \
247 _fmt, ## __VA_ARGS__); \
252 * debug_exception() - writes binary debug entry to active debug area
253 * (if level <= actual debug level)
254 * and switches to next debug area
256 * @id: handle for debug log
257 * @level: debug level
258 * @data: pointer to data for debug entry
259 * @length: length of data in bytes
262 * - Address of written debug entry
265 static inline debug_entry_t
*debug_exception(debug_info_t
*id
, int level
,
266 void *data
, int length
)
268 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
270 return debug_exception_common(id
, level
, data
, length
);
274 * debug_int_exception() - writes unsigned int debug entry to active debug area
275 * (if level <= actual debug level)
276 * and switches to next debug area
278 * @id: handle for debug log
279 * @level: debug level
280 * @tag: integer value for debug entry
283 * - Address of written debug entry
286 static inline debug_entry_t
*debug_int_exception(debug_info_t
*id
, int level
,
289 unsigned int t
= tag
;
291 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
293 return debug_exception_common(id
, level
, &t
, sizeof(unsigned int));
297 * debug_long_exception() - writes long debug entry to active debug area
298 * (if level <= actual debug level)
299 * and switches to next debug area
301 * @id: handle for debug log
302 * @level: debug level
303 * @tag: long integer value for debug entry
306 * - Address of written debug entry
309 static inline debug_entry_t
*debug_long_exception (debug_info_t
*id
, int level
,
312 unsigned long t
= tag
;
314 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
316 return debug_exception_common(id
, level
, &t
, sizeof(unsigned long));
320 * debug_text_exception() - writes string debug entry in ascii format to active
321 * debug area (if level <= actual debug level)
322 * and switches to next debug area
325 * @id: handle for debug log
326 * @level: debug level
327 * @txt: string for debug entry
330 * - Address of written debug entry
333 static inline debug_entry_t
*debug_text_exception(debug_info_t
*id
, int level
,
336 if ((!id
) || (level
> id
->level
) || (id
->pages_per_area
== 0))
338 return debug_exception_common(id
, level
, txt
, strlen(txt
));
342 * IMPORTANT: Use "%s" in sprintf format strings with care! Only pointers are
343 * stored in the s390dbf. See Documentation/s390/s390dbf.rst for more details!
345 extern debug_entry_t
*
346 __debug_sprintf_exception(debug_info_t
*id
, int level
, char *string
, ...)
347 __attribute__ ((format(printf
, 3, 4)));
351 * debug_sprintf_exception() - writes debug entry with format string and
352 * varargs (longs) to active debug area
353 * (if level <= actual debug level)
354 * and switches to next debug area.
356 * @_id: handle for debug log
357 * @_level: debug level
358 * @_fmt: format string for debug entry
359 * @...: varargs used as in sprintf()
362 * - Address of written debug entry
365 * floats and long long datatypes cannot be used as varargs.
367 #define debug_sprintf_exception(_id, _level, _fmt, ...) \
369 debug_entry_t *__ret; \
370 debug_info_t *__id = _id; \
371 int __level = _level; \
373 if ((!__id) || (__level > __id->level)) \
376 __ret = __debug_sprintf_exception(__id, __level, \
377 _fmt, ## __VA_ARGS__);\
381 int debug_register_view(debug_info_t
*id
, struct debug_view
*view
);
383 int debug_unregister_view(debug_info_t
*id
, struct debug_view
*view
);
386 define the debug levels:
387 - 0 No debugging output to console or syslog
388 - 1 Log internal errors to syslog, ignore check conditions
389 - 2 Log internal errors and check conditions to syslog
390 - 3 Log internal errors to console, log check conditions to syslog
391 - 4 Log internal errors and check conditions to console
392 - 5 panic on internal errors, log check conditions to console
393 - 6 panic on both, internal errors and check conditions
397 #define DEBUG_LEVEL 4
400 #define INTERNAL_ERRMSG(x,y...) "E" __FILE__ "%d: " x, __LINE__, y
401 #define INTERNAL_WRNMSG(x,y...) "W" __FILE__ "%d: " x, __LINE__, y
402 #define INTERNAL_INFMSG(x,y...) "I" __FILE__ "%d: " x, __LINE__, y
403 #define INTERNAL_DEBMSG(x,y...) "D" __FILE__ "%d: " x, __LINE__, y
406 #define PRINT_DEBUG(x...) printk(KERN_DEBUG PRINTK_HEADER x)
407 #define PRINT_INFO(x...) printk(KERN_INFO PRINTK_HEADER x)
408 #define PRINT_WARN(x...) printk(KERN_WARNING PRINTK_HEADER x)
409 #define PRINT_ERR(x...) printk(KERN_ERR PRINTK_HEADER x)
410 #define PRINT_FATAL(x...) panic(PRINTK_HEADER x)
412 #define PRINT_DEBUG(x...) printk(KERN_DEBUG PRINTK_HEADER x)
413 #define PRINT_INFO(x...) printk(KERN_DEBUG PRINTK_HEADER x)
414 #define PRINT_WARN(x...) printk(KERN_DEBUG PRINTK_HEADER x)
415 #define PRINT_ERR(x...) printk(KERN_DEBUG PRINTK_HEADER x)
416 #define PRINT_FATAL(x...) printk(KERN_DEBUG PRINTK_HEADER x)
417 #endif /* DASD_DEBUG */