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