1S390 Debug Feature
2==================
3
4files: arch/s390/kernel/debug.c
5       arch/s390/include/asm/debug.h
6
7Description:
8------------
9The goal of this feature is to provide a kernel debug logging API
10where log records can be stored efficiently in memory, where each component
11(e.g. device drivers) can have one separate debug log.
12One purpose of this is to inspect the debug logs after a production system crash
13in order to analyze the reason for the crash.
14If the system still runs but only a subcomponent which uses dbf fails,
15it is possible to look at the debug logs on a live system via the Linux
16debugfs filesystem.
17The debug feature may also very useful for kernel and driver development.
18
19Design:
20-------
21Kernel components (e.g. device drivers) can register themselves at the debug
22feature with the function call debug_register(). This function initializes a
23debug log for the caller. For each debug log exists a number of debug areas
24where exactly one is active at one time.  Each debug area consists of contiguous
25pages in memory. In the debug areas there are stored debug entries (log records)
26which are written by event- and exception-calls.
27
28An event-call writes the specified debug entry to the active debug
29area and updates the log pointer for the active area. If the end
30of the active debug area is reached, a wrap around is done (ring buffer)
31and the next debug entry will be written at the beginning of the active
32debug area.
33
34An exception-call writes the specified debug entry to the log and
35switches to the next debug area. This is done in order to be sure
36that the records which describe the origin of the exception are not
37overwritten when a wrap around for the current area occurs.
38
39The debug areas themselves are also ordered in form of a ring buffer.
40When an exception is thrown in the last debug area, the following debug
41entries are then written again in the very first area.
42
43There are three versions for the event- and exception-calls: One for
44logging raw data, one for text and one for numbers.
45
46Each debug entry contains the following data:
47
48- Timestamp
49- Cpu-Number of calling task
50- Level of debug entry (0...6)
51- Return Address to caller
52- Flag, if entry is an exception or not
53
54The debug logs can be inspected in a live system through entries in
55the debugfs-filesystem. Under the toplevel directory "s390dbf" there is
56a directory for each registered component, which is named like the
57corresponding component. The debugfs normally should be mounted to
58/sys/kernel/debug therefore the debug feature can be accessed under
59/sys/kernel/debug/s390dbf.
60
61The content of the directories are files which represent different views
62to the debug log. Each component can decide which views should be
63used through registering them with the function debug_register_view().
64Predefined views for hex/ascii, sprintf and raw binary data are provided.
65It is also possible to define other views. The content of
66a view can be inspected simply by reading the corresponding debugfs file.
67
68All debug logs have an actual debug level (range from 0 to 6).
69The default level is 3. Event and Exception functions have a 'level'
70parameter. Only debug entries with a level that is lower or equal
71than the actual level are written to the log. This means, when
72writing events, high priority log entries should have a low level
73value whereas low priority entries should have a high one.
74The actual debug level can be changed with the help of the debugfs-filesystem
75through writing a number string "x" to the 'level' debugfs file which is
76provided for every debug log. Debugging can be switched off completely
77by using "-" on the 'level' debugfs file.
78
79Example:
80
81> echo "-" > /sys/kernel/debug/s390dbf/dasd/level
82
83It is also possible to deactivate the debug feature globally for every
84debug log. You can change the behavior using  2 sysctl parameters in
85/proc/sys/s390dbf:
86There are currently 2 possible triggers, which stop the debug feature
87globally. The first possibility is to use the "debug_active" sysctl. If
88set to 1 the debug feature is running. If "debug_active" is set to 0 the
89debug feature is turned off.
90The second trigger which stops the debug feature is a kernel oops.
91That prevents the debug feature from overwriting debug information that
92happened before the oops. After an oops you can reactivate the debug feature
93by piping 1 to /proc/sys/s390dbf/debug_active. Nevertheless, its not
94suggested to use an oopsed kernel in a production environment.
95If you want to disallow the deactivation of the debug feature, you can use
96the "debug_stoppable" sysctl. If you set "debug_stoppable" to 0 the debug
97feature cannot be stopped. If the debug feature is already stopped, it
98will stay deactivated.
99
100Kernel Interfaces:
101------------------
102
103----------------------------------------------------------------------------
104debug_info_t *debug_register(char *name, int pages, int nr_areas,
105                             int buf_size);
106
107Parameter:    name:        Name of debug log (e.g. used for debugfs entry)
108              pages:       number of pages, which will be allocated per area
109              nr_areas:    number of debug areas
110              buf_size:    size of data area in each debug entry
111
112Return Value: Handle for generated debug area
113              NULL if register failed
114
115Description:  Allocates memory for a debug log
116              Must not be called within an interrupt handler
117
118----------------------------------------------------------------------------
119debug_info_t *debug_register_mode(char *name, int pages, int nr_areas,
120				  int buf_size, mode_t mode, uid_t uid,
121				  gid_t gid);
122
123Parameter:    name:	   Name of debug log (e.g. used for debugfs entry)
124	      pages:	   Number of pages, which will be allocated per area
125	      nr_areas:    Number of debug areas
126	      buf_size:    Size of data area in each debug entry
127	      mode:	   File mode for debugfs files. E.g. S_IRWXUGO
128	      uid:	   User ID for debugfs files. Currently only 0 is
129			   supported.
130	      gid:	   Group ID for debugfs files. Currently only 0 is
131			   supported.
132
133Return Value: Handle for generated debug area
134	      NULL if register failed
135
136Description:  Allocates memory for a debug log
137	      Must not be called within an interrupt handler
138
139---------------------------------------------------------------------------
140void debug_unregister (debug_info_t * id);
141
142Parameter:     id:   handle for debug log
143
144Return Value:  none
145
146Description:   frees memory for a debug log
147               Must not be called within an interrupt handler
148
149---------------------------------------------------------------------------
150void debug_set_level (debug_info_t * id, int new_level);
151
152Parameter:     id:        handle for debug log
153               new_level: new debug level
154
155Return Value:  none
156
157Description:   Sets new actual debug level if new_level is valid.
158
159---------------------------------------------------------------------------
160void debug_stop_all(void);
161
162Parameter:     none
163
164Return Value:  none
165
166Description:   stops the debug feature if stopping is allowed. Currently
167               used in case of a kernel oops.
168
169---------------------------------------------------------------------------
170debug_entry_t* debug_event (debug_info_t* id, int level, void* data,
171                            int length);
172
173Parameter:     id:     handle for debug log
174               level:  debug level
175               data:   pointer to data for debug entry
176               length: length of data in bytes
177
178Return Value:  Address of written debug entry
179
180Description:   writes debug entry to active debug area (if level <= actual
181               debug level)
182
183---------------------------------------------------------------------------
184debug_entry_t* debug_int_event (debug_info_t * id, int level,
185                                unsigned int data);
186debug_entry_t* debug_long_event(debug_info_t * id, int level,
187                                unsigned long data);
188
189Parameter:     id:     handle for debug log
190               level:  debug level
191               data:   integer value for debug entry
192
193Return Value:  Address of written debug entry
194
195Description:   writes debug entry to active debug area (if level <= actual
196               debug level)
197
198---------------------------------------------------------------------------
199debug_entry_t* debug_text_event (debug_info_t * id, int level,
200                                 const char* data);
201
202Parameter:     id:     handle for debug log
203               level:  debug level
204               data:   string for debug entry
205
206Return Value:  Address of written debug entry
207
208Description:   writes debug entry in ascii format to active debug area
209               (if level <= actual debug level)
210
211---------------------------------------------------------------------------
212debug_entry_t* debug_sprintf_event (debug_info_t * id, int level,
213                                    char* string,...);
214
215Parameter:     id:    handle for debug log
216               level: debug level
217               string: format string for debug entry
218               ...: varargs used as in sprintf()
219
220Return Value:  Address of written debug entry
221
222Description:   writes debug entry with format string and varargs (longs) to
223               active debug area (if level $<=$ actual debug level).
224               floats and long long datatypes cannot be used as varargs.
225
226---------------------------------------------------------------------------
227
228debug_entry_t* debug_exception (debug_info_t* id, int level, void* data,
229                                int length);
230
231Parameter:     id:     handle for debug log
232               level:  debug level
233               data:   pointer to data for debug entry
234               length: length of data in bytes
235
236Return Value:  Address of written debug entry
237
238Description:   writes debug entry to active debug area (if level <= actual
239               debug level) and switches to next debug area
240
241---------------------------------------------------------------------------
242debug_entry_t* debug_int_exception (debug_info_t * id, int level,
243                                    unsigned int data);
244debug_entry_t* debug_long_exception(debug_info_t * id, int level,
245                                    unsigned long data);
246
247Parameter:     id:     handle for debug log
248               level:  debug level
249               data:   integer value for debug entry
250
251Return Value:  Address of written debug entry
252
253Description:   writes debug entry to active debug area (if level <= actual
254               debug level) and switches to next debug area
255
256---------------------------------------------------------------------------
257debug_entry_t* debug_text_exception (debug_info_t * id, int level,
258                                     const char* data);
259
260Parameter:     id:     handle for debug log
261               level:  debug level
262               data:   string for debug entry
263
264Return Value:  Address of written debug entry
265
266Description:   writes debug entry in ascii format to active debug area
267               (if level <= actual debug level) and switches to next debug
268               area
269
270---------------------------------------------------------------------------
271debug_entry_t* debug_sprintf_exception (debug_info_t * id, int level,
272                                        char* string,...);
273
274Parameter:     id:    handle for debug log
275               level: debug level
276               string: format string for debug entry
277               ...: varargs used as in sprintf()
278
279Return Value:  Address of written debug entry
280
281Description:   writes debug entry with format string and varargs (longs) to
282               active debug area (if level $<=$ actual debug level) and
283               switches to next debug area.
284               floats and long long datatypes cannot be used as varargs.
285
286---------------------------------------------------------------------------
287
288int debug_register_view (debug_info_t * id, struct debug_view *view);
289
290Parameter:     id:    handle for debug log
291               view:  pointer to debug view struct
292
293Return Value:  0  : ok
294               < 0: Error
295
296Description:   registers new debug view and creates debugfs dir entry
297
298---------------------------------------------------------------------------
299int debug_unregister_view (debug_info_t * id, struct debug_view *view);
300
301Parameter:     id:    handle for debug log
302               view:  pointer to debug view struct
303
304Return Value:  0  : ok
305               < 0: Error
306
307Description:   unregisters debug view and removes debugfs dir entry
308
309
310
311Predefined views:
312-----------------
313
314extern struct debug_view debug_hex_ascii_view;
315extern struct debug_view debug_raw_view;
316extern struct debug_view debug_sprintf_view;
317
318Examples
319--------
320
321/*
322 * hex_ascii- + raw-view Example
323 */
324
325#include <linux/init.h>
326#include <asm/debug.h>
327
328static debug_info_t* debug_info;
329
330static int init(void)
331{
332    /* register 4 debug areas with one page each and 4 byte data field */
333
334    debug_info = debug_register ("test", 1, 4, 4 );
335    debug_register_view(debug_info,&debug_hex_ascii_view);
336    debug_register_view(debug_info,&debug_raw_view);
337
338    debug_text_event(debug_info, 4 , "one ");
339    debug_int_exception(debug_info, 4, 4711);
340    debug_event(debug_info, 3, &debug_info, 4);
341
342    return 0;
343}
344
345static void cleanup(void)
346{
347    debug_unregister (debug_info);
348}
349
350module_init(init);
351module_exit(cleanup);
352
353---------------------------------------------------------------------------
354
355/*
356 * sprintf-view Example
357 */
358
359#include <linux/init.h>
360#include <asm/debug.h>
361
362static debug_info_t* debug_info;
363
364static int init(void)
365{
366    /* register 4 debug areas with one page each and data field for */
367    /* format string pointer + 2 varargs (= 3 * sizeof(long))       */
368
369    debug_info = debug_register ("test", 1, 4, sizeof(long) * 3);
370    debug_register_view(debug_info,&debug_sprintf_view);
371
372    debug_sprintf_event(debug_info, 2 , "first event in %s:%i\n",__FILE__,__LINE__);
373    debug_sprintf_exception(debug_info, 1, "pointer to debug info: %p\n",&debug_info);
374
375    return 0;
376}
377
378static void cleanup(void)
379{
380    debug_unregister (debug_info);
381}
382
383module_init(init);
384module_exit(cleanup);
385
386
387
388Debugfs Interface
389----------------
390Views to the debug logs can be investigated through reading the corresponding
391debugfs-files:
392
393Example:
394
395> ls /sys/kernel/debug/s390dbf/dasd
396flush  hex_ascii  level pages raw
397> cat /sys/kernel/debug/s390dbf/dasd/hex_ascii | sort +1
39800 00974733272:680099 2 - 02 0006ad7e  07 ea 4a 90 | ....
39900 00974733272:682210 2 - 02 0006ade6  46 52 45 45 | FREE
40000 00974733272:682213 2 - 02 0006adf6  07 ea 4a 90 | ....
40100 00974733272:682281 1 * 02 0006ab08  41 4c 4c 43 | EXCP
40201 00974733272:682284 2 - 02 0006ab16  45 43 4b 44 | ECKD
40301 00974733272:682287 2 - 02 0006ab28  00 00 00 04 | ....
40401 00974733272:682289 2 - 02 0006ab3e  00 00 00 20 | ...
40501 00974733272:682297 2 - 02 0006ad7e  07 ea 4a 90 | ....
40601 00974733272:684384 2 - 00 0006ade6  46 52 45 45 | FREE
40701 00974733272:684388 2 - 00 0006adf6  07 ea 4a 90 | ....
408
409See section about predefined views for explanation of the above output!
410
411Changing the debug level
412------------------------
413
414Example:
415
416
417> cat /sys/kernel/debug/s390dbf/dasd/level
4183
419> echo "5" > /sys/kernel/debug/s390dbf/dasd/level
420> cat /sys/kernel/debug/s390dbf/dasd/level
4215
422
423Flushing debug areas
424--------------------
425Debug areas can be flushed with piping the number of the desired
426area (0...n) to the debugfs file "flush". When using "-" all debug areas
427are flushed.
428
429Examples:
430
4311. Flush debug area 0:
432> echo "0" > /sys/kernel/debug/s390dbf/dasd/flush
433
4342. Flush all debug areas:
435> echo "-" > /sys/kernel/debug/s390dbf/dasd/flush
436
437Changing the size of debug areas
438------------------------------------
439It is possible the change the size of debug areas through piping
440the number of pages to the debugfs file "pages". The resize request will
441also flush the debug areas.
442
443Example:
444
445Define 4 pages for the debug areas of debug feature "dasd":
446> echo "4" > /sys/kernel/debug/s390dbf/dasd/pages
447
448Stooping the debug feature
449--------------------------
450Example:
451
4521. Check if stopping is allowed
453> cat /proc/sys/s390dbf/debug_stoppable
4542. Stop debug feature
455> echo 0 > /proc/sys/s390dbf/debug_active
456
457lcrash Interface
458----------------
459It is planned that the dump analysis tool lcrash gets an additional command
460's390dbf' to display all the debug logs. With this tool it will be possible
461to investigate the debug logs on a live system and with a memory dump after
462a system crash.
463
464Investigating raw memory
465------------------------
466One last possibility to investigate the debug logs at a live
467system and after a system crash is to look at the raw memory
468under VM or at the Service Element.
469It is possible to find the anker of the debug-logs through
470the 'debug_area_first' symbol in the System map. Then one has
471to follow the correct pointers of the data-structures defined
472in debug.h and find the debug-areas in memory.
473Normally modules which use the debug feature will also have
474a global variable with the pointer to the debug-logs. Following
475this pointer it will also be possible to find the debug logs in
476memory.
477
478For this method it is recommended to use '16 * x + 4' byte (x = 0..n)
479for the length of the data field in debug_register() in
480order to see the debug entries well formatted.
481
482
483Predefined Views
484----------------
485
486There are three predefined views: hex_ascii, raw and sprintf.
487The hex_ascii view shows the data field in hex and ascii representation
488(e.g. '45 43 4b 44 | ECKD').
489The raw view returns a bytestream as the debug areas are stored in memory.
490
491The sprintf view formats the debug entries in the same way as the sprintf
492function would do. The sprintf event/exception functions write to the
493debug entry a pointer to the format string (size = sizeof(long))
494and for each vararg a long value. So e.g. for a debug entry with a format
495string plus two varargs one would need to allocate a (3 * sizeof(long))
496byte data area in the debug_register() function.
497
498IMPORTANT: Using "%s" in sprintf event functions is dangerous. You can only
499use "%s" in the sprintf event functions, if the memory for the passed string is
500available as long as the debug feature exists. The reason behind this is that
501due to performance considerations only a pointer to the string is stored in
502the debug feature. If you log a string that is freed afterwards, you will get
503an OOPS when inspecting the debug feature, because then the debug feature will
504access the already freed memory.
505
506NOTE: If using the sprintf view do NOT use other event/exception functions
507than the sprintf-event and -exception functions.
508
509The format of the hex_ascii and sprintf view is as follows:
510- Number of area
511- Timestamp (formatted as seconds and microseconds since 00:00:00 Coordinated
512  Universal Time (UTC), January 1, 1970)
513- level of debug entry
514- Exception flag (* = Exception)
515- Cpu-Number of calling task
516- Return Address to caller
517- data field
518
519The format of the raw view is:
520- Header as described in debug.h
521- datafield
522
523A typical line of the hex_ascii view will look like the following (first line
524is only for explanation and will not be displayed when 'cating' the view):
525
526area  time           level exception cpu caller    data (hex + ascii)
527--------------------------------------------------------------------------
52800    00964419409:440690 1 -         00  88023fe
529
530
531Defining views
532--------------
533
534Views are specified with the 'debug_view' structure. There are defined
535callback functions which are used for reading and writing the debugfs files:
536
537struct debug_view {
538        char name[DEBUG_MAX_PROCF_LEN];
539        debug_prolog_proc_t* prolog_proc;
540        debug_header_proc_t* header_proc;
541        debug_format_proc_t* format_proc;
542        debug_input_proc_t*  input_proc;
543	void*                private_data;
544};
545
546where
547
548typedef int (debug_header_proc_t) (debug_info_t* id,
549                                   struct debug_view* view,
550                                   int area,
551                                   debug_entry_t* entry,
552                                   char* out_buf);
553
554typedef int (debug_format_proc_t) (debug_info_t* id,
555                                   struct debug_view* view, char* out_buf,
556                                   const char* in_buf);
557typedef int (debug_prolog_proc_t) (debug_info_t* id,
558                                   struct debug_view* view,
559                                   char* out_buf);
560typedef int (debug_input_proc_t) (debug_info_t* id,
561                                  struct debug_view* view,
562                                  struct file* file, const char* user_buf,
563                                  size_t in_buf_size, loff_t* offset);
564
565
566The "private_data" member can be used as pointer to view specific data.
567It is not used by the debug feature itself.
568
569The output when reading a debugfs file is structured like this:
570
571"prolog_proc output"
572
573"header_proc output 1"  "format_proc output 1"
574"header_proc output 2"  "format_proc output 2"
575"header_proc output 3"  "format_proc output 3"
576...
577
578When a view is read from the debugfs, the Debug Feature calls the
579'prolog_proc' once for writing the prolog.
580Then 'header_proc' and 'format_proc' are called for each
581existing debug entry.
582
583The input_proc can be used to implement functionality when it is written to
584the view (e.g. like with 'echo "0" > /sys/kernel/debug/s390dbf/dasd/level).
585
586For header_proc there can be used the default function
587debug_dflt_header_fn() which is defined in debug.h.
588and which produces the same header output as the predefined views.
589E.g:
59000 00964419409:440761 2 - 00 88023ec
591
592In order to see how to use the callback functions check the implementation
593of the default views!
594
595Example
596
597#include <asm/debug.h>
598
599#define UNKNOWNSTR "data: %08x"
600
601const char* messages[] =
602{"This error...........\n",
603 "That error...........\n",
604 "Problem..............\n",
605 "Something went wrong.\n",
606 "Everything ok........\n",
607 NULL
608};
609
610static int debug_test_format_fn(
611   debug_info_t * id, struct debug_view *view,
612   char *out_buf, const char *in_buf
613)
614{
615  int i, rc = 0;
616
617  if(id->buf_size >= 4) {
618     int msg_nr = *((int*)in_buf);
619     if(msg_nr < sizeof(messages)/sizeof(char*) - 1)
620        rc += sprintf(out_buf, "%s", messages[msg_nr]);
621     else
622        rc += sprintf(out_buf, UNKNOWNSTR, msg_nr);
623  }
624 out:
625   return rc;
626}
627
628struct debug_view debug_test_view = {
629  "myview",                 /* name of view */
630  NULL,                     /* no prolog */
631  &debug_dflt_header_fn,    /* default header for each entry */
632  &debug_test_format_fn,    /* our own format function */
633  NULL,                     /* no input function */
634  NULL                      /* no private data */
635};
636
637=====
638test:
639=====
640debug_info_t *debug_info;
641...
642debug_info = debug_register ("test", 0, 4, 4 ));
643debug_register_view(debug_info, &debug_test_view);
644for(i = 0; i < 10; i ++) debug_int_event(debug_info, 1, i);
645
646> cat /sys/kernel/debug/s390dbf/test/myview
64700 00964419734:611402 1 - 00 88042ca   This error...........
64800 00964419734:611405 1 - 00 88042ca   That error...........
64900 00964419734:611408 1 - 00 88042ca   Problem..............
65000 00964419734:611411 1 - 00 88042ca   Something went wrong.
65100 00964419734:611414 1 - 00 88042ca   Everything ok........
65200 00964419734:611417 1 - 00 88042ca   data: 00000005
65300 00964419734:611419 1 - 00 88042ca   data: 00000006
65400 00964419734:611422 1 - 00 88042ca   data: 00000007
65500 00964419734:611425 1 - 00 88042ca   data: 00000008
65600 00964419734:611428 1 - 00 88042ca   data: 00000009
657