1================== 2S390 Debug Feature 3================== 4 5files: 6 - arch/s390/kernel/debug.c 7 - arch/s390/include/asm/debug.h 8 9Description: 10------------ 11The goal of this feature is to provide a kernel debug logging API 12where log records can be stored efficiently in memory, where each component 13(e.g. device drivers) can have one separate debug log. 14One purpose of this is to inspect the debug logs after a production system crash 15in order to analyze the reason for the crash. 16 17If the system still runs but only a subcomponent which uses dbf fails, 18it is possible to look at the debug logs on a live system via the Linux 19debugfs filesystem. 20 21The debug feature may also very useful for kernel and driver development. 22 23Design: 24------- 25Kernel components (e.g. device drivers) can register themselves at the debug 26feature with the function call :c:func:`debug_register()`. 27This function initializes a 28debug log for the caller. For each debug log exists a number of debug areas 29where exactly one is active at one time. Each debug area consists of contiguous 30pages in memory. In the debug areas there are stored debug entries (log records) 31which are written by event- and exception-calls. 32 33An event-call writes the specified debug entry to the active debug 34area and updates the log pointer for the active area. If the end 35of the active debug area is reached, a wrap around is done (ring buffer) 36and the next debug entry will be written at the beginning of the active 37debug area. 38 39An exception-call writes the specified debug entry to the log and 40switches to the next debug area. This is done in order to be sure 41that the records which describe the origin of the exception are not 42overwritten when a wrap around for the current area occurs. 43 44The debug areas themselves are also ordered in form of a ring buffer. 45When an exception is thrown in the last debug area, the following debug 46entries are then written again in the very first area. 47 48There are four versions for the event- and exception-calls: One for 49logging raw data, one for text, one for numbers (unsigned int and long), 50and one for sprintf-like formatted strings. 51 52Each debug entry contains the following data: 53 54- Timestamp 55- Cpu-Number of calling task 56- Level of debug entry (0...6) 57- Return Address to caller 58- Flag, if entry is an exception or not 59 60The debug logs can be inspected in a live system through entries in 61the debugfs-filesystem. Under the toplevel directory "``s390dbf``" there is 62a directory for each registered component, which is named like the 63corresponding component. The debugfs normally should be mounted to 64``/sys/kernel/debug`` therefore the debug feature can be accessed under 65``/sys/kernel/debug/s390dbf``. 66 67The content of the directories are files which represent different views 68to the debug log. Each component can decide which views should be 69used through registering them with the function :c:func:`debug_register_view()`. 70Predefined views for hex/ascii and sprintf data are provided. 71It is also possible to define other views. The content of 72a view can be inspected simply by reading the corresponding debugfs file. 73 74All debug logs have an actual debug level (range from 0 to 6). 75The default level is 3. Event and Exception functions have a :c:data:`level` 76parameter. Only debug entries with a level that is lower or equal 77than the actual level are written to the log. This means, when 78writing events, high priority log entries should have a low level 79value whereas low priority entries should have a high one. 80The actual debug level can be changed with the help of the debugfs-filesystem 81through writing a number string "x" to the ``level`` debugfs file which is 82provided for every debug log. Debugging can be switched off completely 83by using "-" on the ``level`` debugfs file. 84 85Example:: 86 87 > echo "-" > /sys/kernel/debug/s390dbf/dasd/level 88 89It is also possible to deactivate the debug feature globally for every 90debug log. You can change the behavior using 2 sysctl parameters in 91``/proc/sys/s390dbf``: 92 93There are currently 2 possible triggers, which stop the debug feature 94globally. The first possibility is to use the ``debug_active`` sysctl. If 95set to 1 the debug feature is running. If ``debug_active`` is set to 0 the 96debug feature is turned off. 97 98The second trigger which stops the debug feature is a kernel oops. 99That prevents the debug feature from overwriting debug information that 100happened before the oops. After an oops you can reactivate the debug feature 101by piping 1 to ``/proc/sys/s390dbf/debug_active``. Nevertheless, it's not 102suggested to use an oopsed kernel in a production environment. 103 104If you want to disallow the deactivation of the debug feature, you can use 105the ``debug_stoppable`` sysctl. If you set ``debug_stoppable`` to 0 the debug 106feature cannot be stopped. If the debug feature is already stopped, it 107will stay deactivated. 108 109Kernel parameters 110----------------- 111The size and log levels of debug logs can be configured early during boot using 112the ``s390dbf`` kernel parameter. The parameter accepts a debug log name, a log 113level, and a log size, separated by colon characters (``:``). To configure only 114a single attribute, either the log level or the log size may be omitted. 115 116To configure multiple debug logs, the parameter may be specified multiple times, 117or multiple parameter sets may be provided in a single instance, separated by 118commas. 119 120Parameter format:: 121 122 s390dbf=<name|pattern>:[<level>|-]:[<pages>][,...] 123 124where: 125 126- ``name`` specifies either an exact debug log name or a shell-style wildcard 127 pattern 128- ``level`` specifies the log level, or ``-`` to completely deactivate the log 129- ``pages`` specifies the debug area size in pages 130 131Example:: 132 133 s390dbf=cio*:6:128,sclp_err::2 134 135This example sets the log level to 6 and the log size to 128 pages for all debug 136logs whose names start with ``cio``. It also sets the log level of the 137``sclp_err`` debug log to 2. 138 139Kernel Interfaces: 140------------------ 141 142.. kernel-doc:: arch/s390/kernel/debug.c 143.. kernel-doc:: arch/s390/include/asm/debug.h 144 145Predefined views: 146----------------- 147 148.. code-block:: c 149 150 extern struct debug_view debug_hex_ascii_view; 151 152 extern struct debug_view debug_sprintf_view; 153 154Examples 155-------- 156 157.. code-block:: c 158 159 /* 160 * hex_ascii-view Example 161 */ 162 163 #include <linux/init.h> 164 #include <asm/debug.h> 165 166 static debug_info_t *debug_info; 167 168 static int init(void) 169 { 170 /* register 4 debug areas with one page each and 4 byte data field */ 171 172 debug_info = debug_register("test", 1, 4, 4 ); 173 debug_register_view(debug_info, &debug_hex_ascii_view); 174 175 debug_text_event(debug_info, 4 , "one "); 176 debug_int_exception(debug_info, 4, 4711); 177 debug_event(debug_info, 3, &debug_info, 4); 178 179 return 0; 180 } 181 182 static void cleanup(void) 183 { 184 debug_unregister(debug_info); 185 } 186 187 module_init(init); 188 module_exit(cleanup); 189 190.. code-block:: c 191 192 /* 193 * sprintf-view Example 194 */ 195 196 #include <linux/init.h> 197 #include <asm/debug.h> 198 199 static debug_info_t *debug_info; 200 201 static int init(void) 202 { 203 /* register 4 debug areas with one page each and data field for */ 204 /* format string pointer + 2 varargs (= 3 * sizeof(long)) */ 205 206 debug_info = debug_register("test", 1, 4, sizeof(long) * 3); 207 debug_register_view(debug_info, &debug_sprintf_view); 208 209 debug_sprintf_event(debug_info, 2 , "first event in %s:%i\n",__FILE__,__LINE__); 210 debug_sprintf_exception(debug_info, 1, "pointer to debug info: %p\n",&debug_info); 211 212 return 0; 213 } 214 215 static void cleanup(void) 216 { 217 debug_unregister(debug_info); 218 } 219 220 module_init(init); 221 module_exit(cleanup); 222 223Debugfs Interface 224----------------- 225Views to the debug logs can be investigated through reading the corresponding 226debugfs-files: 227 228Example:: 229 230 > ls /sys/kernel/debug/s390dbf/dasd 231 flush hex_ascii level pages 232 > cat /sys/kernel/debug/s390dbf/dasd/hex_ascii | sort -k2,2 -s 233 00 00974733272:680099 2 - 02 0006ad7e 07 ea 4a 90 | .... 234 00 00974733272:682210 2 - 02 0006ade6 46 52 45 45 | FREE 235 00 00974733272:682213 2 - 02 0006adf6 07 ea 4a 90 | .... 236 00 00974733272:682281 1 * 02 0006ab08 41 4c 4c 43 | EXCP 237 01 00974733272:682284 2 - 02 0006ab16 45 43 4b 44 | ECKD 238 01 00974733272:682287 2 - 02 0006ab28 00 00 00 04 | .... 239 01 00974733272:682289 2 - 02 0006ab3e 00 00 00 20 | ... 240 01 00974733272:682297 2 - 02 0006ad7e 07 ea 4a 90 | .... 241 01 00974733272:684384 2 - 00 0006ade6 46 52 45 45 | FREE 242 01 00974733272:684388 2 - 00 0006adf6 07 ea 4a 90 | .... 243 244See section about predefined views for explanation of the above output! 245 246Changing the debug level 247------------------------ 248 249Example:: 250 251 252 > cat /sys/kernel/debug/s390dbf/dasd/level 253 3 254 > echo "5" > /sys/kernel/debug/s390dbf/dasd/level 255 > cat /sys/kernel/debug/s390dbf/dasd/level 256 5 257 258Flushing debug areas 259-------------------- 260Debug areas can be flushed with piping the number of the desired 261area (0...n) to the debugfs file "flush". When using "-" all debug areas 262are flushed. 263 264Examples: 265 2661. Flush debug area 0:: 267 268 > echo "0" > /sys/kernel/debug/s390dbf/dasd/flush 269 2702. Flush all debug areas:: 271 272 > echo "-" > /sys/kernel/debug/s390dbf/dasd/flush 273 274Changing the size of debug areas 275------------------------------------ 276To resize a debug area, write the desired page count to the "pages" file. 277Existing data is preserved if it fits; otherwise, oldest entries are dropped. 278 279Example: 280 281Define 4 pages for the debug areas of debug feature "dasd":: 282 283 > echo "4" > /sys/kernel/debug/s390dbf/dasd/pages 284 285Stopping the debug feature 286-------------------------- 287Example: 288 2891. Check if stopping is allowed:: 290 291 > cat /proc/sys/s390dbf/debug_stoppable 292 2932. Stop debug feature:: 294 295 > echo 0 > /proc/sys/s390dbf/debug_active 296 297crash Interface 298---------------- 299The ``crash`` tool since v5.1.0 has a built-in command 300``s390dbf`` to display all the debug logs or export them to the file system. 301With this tool it is possible 302to investigate the debug logs on a live system and with a memory dump after 303a system crash. 304 305Investigating raw memory 306------------------------ 307One last possibility to investigate the debug logs at a live 308system and after a system crash is to look at the raw memory 309under VM or at the Service Element. 310It is possible to find the anchor of the debug-logs through 311the ``debug_area_first`` symbol in the System map. Then one has 312to follow the correct pointers of the data-structures defined 313in debug.h and find the debug-areas in memory. 314Normally modules which use the debug feature will also have 315a global variable with the pointer to the debug-logs. Following 316this pointer it will also be possible to find the debug logs in 317memory. 318 319For this method it is recommended to use '16 * x + 4' byte (x = 0..n) 320for the length of the data field in :c:func:`debug_register()` in 321order to see the debug entries well formatted. 322 323 324Predefined Views 325---------------- 326 327There are two predefined views: hex_ascii and sprintf. 328The hex_ascii view shows the data field in hex and ascii representation 329(e.g. ``45 43 4b 44 | ECKD``). 330 331The sprintf view formats the debug entries in the same way as the sprintf 332function would do. The sprintf event/exception functions write to the 333debug entry a pointer to the format string (size = sizeof(long)) 334and for each vararg a long value. So e.g. for a debug entry with a format 335string plus two varargs one would need to allocate a (3 * sizeof(long)) 336byte data area in the debug_register() function. 337 338IMPORTANT: 339 Using "%s" in sprintf event functions is dangerous. You can only 340 use "%s" in the sprintf event functions, if the memory for the passed string 341 is available as long as the debug feature exists. The reason behind this is 342 that due to performance considerations only a pointer to the string is stored 343 in the debug feature. If you log a string that is freed afterwards, you will 344 get an OOPS when inspecting the debug feature, because then the debug feature 345 will access the already freed memory. 346 347NOTE: 348 If using the sprintf view do NOT use other event/exception functions 349 than the sprintf-event and -exception functions. 350 351The format of the hex_ascii and sprintf view is as follows: 352 353- Number of area 354- Timestamp (formatted as seconds and microseconds since 00:00:00 Coordinated 355 Universal Time (UTC), January 1, 1970) 356- level of debug entry 357- Exception flag (* = Exception) 358- Cpu-Number of calling task 359- Return Address to caller 360- data field 361 362A typical line of the hex_ascii view will look like the following (first line 363is only for explanation and will not be displayed when 'cating' the view):: 364 365 area time level exception cpu caller data (hex + ascii) 366 -------------------------------------------------------------------------- 367 00 00964419409:440690 1 - 00 88023fe 368 369 370Defining views 371-------------- 372 373Views are specified with the 'debug_view' structure. There are defined 374callback functions which are used for reading and writing the debugfs files: 375 376.. code-block:: c 377 378 struct debug_view { 379 char name[DEBUG_MAX_PROCF_LEN]; 380 debug_prolog_proc_t* prolog_proc; 381 debug_header_proc_t* header_proc; 382 debug_format_proc_t* format_proc; 383 debug_input_proc_t* input_proc; 384 void* private_data; 385 }; 386 387where: 388 389.. code-block:: c 390 391 typedef int (debug_header_proc_t) (debug_info_t* id, 392 struct debug_view* view, 393 int area, 394 debug_entry_t* entry, 395 char* out_buf); 396 397 typedef int (debug_format_proc_t) (debug_info_t* id, 398 struct debug_view* view, char* out_buf, 399 const char* in_buf); 400 typedef int (debug_prolog_proc_t) (debug_info_t* id, 401 struct debug_view* view, 402 char* out_buf); 403 typedef int (debug_input_proc_t) (debug_info_t* id, 404 struct debug_view* view, 405 struct file* file, const char* user_buf, 406 size_t in_buf_size, loff_t* offset); 407 408 409The "private_data" member can be used as pointer to view specific data. 410It is not used by the debug feature itself. 411 412The output when reading a debugfs file is structured like this:: 413 414 "prolog_proc output" 415 416 "header_proc output 1" "format_proc output 1" 417 "header_proc output 2" "format_proc output 2" 418 "header_proc output 3" "format_proc output 3" 419 ... 420 421When a view is read from the debugfs, the Debug Feature calls the 422'prolog_proc' once for writing the prolog. 423Then 'header_proc' and 'format_proc' are called for each 424existing debug entry. 425 426The input_proc can be used to implement functionality when it is written to 427the view (e.g. like with ``echo "0" > /sys/kernel/debug/s390dbf/dasd/level``). 428 429For header_proc there can be used the default function 430:c:func:`debug_dflt_header_fn()` which is defined in debug.h. 431and which produces the same header output as the predefined views. 432E.g:: 433 434 00 00964419409:440761 2 - 00 88023ec 435 436In order to see how to use the callback functions check the implementation 437of the default views! 438 439Example: 440 441.. code-block:: c 442 443 #include <asm/debug.h> 444 445 #define UNKNOWNSTR "data: %08x" 446 447 const char* messages[] = 448 {"This error...........\n", 449 "That error...........\n", 450 "Problem..............\n", 451 "Something went wrong.\n", 452 "Everything ok........\n", 453 NULL 454 }; 455 456 static int debug_test_format_fn( 457 debug_info_t *id, struct debug_view *view, 458 char *out_buf, const char *in_buf 459 ) 460 { 461 int i, rc = 0; 462 463 if (id->buf_size >= 4) { 464 int msg_nr = *((int*)in_buf); 465 if (msg_nr < sizeof(messages) / sizeof(char*) - 1) 466 rc += sprintf(out_buf, "%s", messages[msg_nr]); 467 else 468 rc += sprintf(out_buf, UNKNOWNSTR, msg_nr); 469 } 470 return rc; 471 } 472 473 struct debug_view debug_test_view = { 474 "myview", /* name of view */ 475 NULL, /* no prolog */ 476 &debug_dflt_header_fn, /* default header for each entry */ 477 &debug_test_format_fn, /* our own format function */ 478 NULL, /* no input function */ 479 NULL /* no private data */ 480 }; 481 482test: 483===== 484 485.. code-block:: c 486 487 debug_info_t *debug_info; 488 int i; 489 ... 490 debug_info = debug_register("test", 0, 4, 4); 491 debug_register_view(debug_info, &debug_test_view); 492 for (i = 0; i < 10; i ++) 493 debug_int_event(debug_info, 1, i); 494 495:: 496 497 > cat /sys/kernel/debug/s390dbf/test/myview 498 00 00964419734:611402 1 - 00 88042ca This error........... 499 00 00964419734:611405 1 - 00 88042ca That error........... 500 00 00964419734:611408 1 - 00 88042ca Problem.............. 501 00 00964419734:611411 1 - 00 88042ca Something went wrong. 502 00 00964419734:611414 1 - 00 88042ca Everything ok........ 503 00 00964419734:611417 1 - 00 88042ca data: 00000005 504 00 00964419734:611419 1 - 00 88042ca data: 00000006 505 00 00964419734:611422 1 - 00 88042ca data: 00000007 506 00 00964419734:611425 1 - 00 88042ca data: 00000008 507 00 00964419734:611428 1 - 00 88042ca data: 00000009 508