S390 Debug Feature¶
- files:
- arch/s390/kernel/debug.c
- arch/s390/include/asm/debug.h
Description:¶
The goal of this feature is to provide a kernel debug logging APIwhere log records can be stored efficiently in memory, where each component(e.g. device drivers) can have one separate debug log.One purpose of this is to inspect the debug logs after a production system crashin order to analyze the reason for the crash.
If the system still runs but only a subcomponent which uses dbf fails,it is possible to look at the debug logs on a live system via the Linuxdebugfs filesystem.
The debug feature may also very useful for kernel and driver development.
Design:¶
Kernel components (e.g. device drivers) can register themselves at the debugfeature with the function calldebug_register().This function initializes adebug log for the caller. For each debug log exists a number of debug areaswhere exactly one is active at one time. Each debug area consists of contiguouspages in memory. In the debug areas there are stored debug entries (log records)which are written by event- and exception-calls.
An event-call writes the specified debug entry to the active debugarea and updates the log pointer for the active area. If the endof the active debug area is reached, a wrap around is done (ring buffer)and the next debug entry will be written at the beginning of the activedebug area.
An exception-call writes the specified debug entry to the log andswitches to the next debug area. This is done in order to be surethat the records which describe the origin of the exception are notoverwritten when a wrap around for the current area occurs.
The debug areas themselves are also ordered in form of a ring buffer.When an exception is thrown in the last debug area, the following debugentries are then written again in the very first area.
There are four versions for the event- and exception-calls: One forlogging raw data, one for text, one for numbers (unsigned int and long),and one for sprintf-like formatted strings.
Each debug entry contains the following data:
- Timestamp
- Cpu-Number of calling task
- Level of debug entry (0…6)
- Return Address to caller
- Flag, if entry is an exception or not
The debug logs can be inspected in a live system through entries inthe debugfs-filesystem. Under the toplevel directory “s390dbf” there isa directory for each registered component, which is named like thecorresponding component. The debugfs normally should be mounted to/sys/kernel/debug therefore the debug feature can be accessed under/sys/kernel/debug/s390dbf.
The content of the directories are files which represent different viewsto the debug log. Each component can decide which views should beused through registering them with the functiondebug_register_view().Predefined views for hex/ascii and sprintf data are provided.It is also possible to define other views. The content ofa view can be inspected simply by reading the corresponding debugfs file.
All debug logs have an actual debug level (range from 0 to 6).The default level is 3. Event and Exception functions have alevelparameter. Only debug entries with a level that is lower or equalthan the actual level are written to the log. This means, whenwriting events, high priority log entries should have a low levelvalue whereas low priority entries should have a high one.The actual debug level can be changed with the help of the debugfs-filesystemthrough writing a number string “x” to thelevel debugfs file which isprovided for every debug log. Debugging can be switched off completelyby using “-” on thelevel debugfs file.
Example:
> echo "-" > /sys/kernel/debug/s390dbf/dasd/level
It is also possible to deactivate the debug feature globally for everydebug log. You can change the behavior using 2 sysctl parameters in/proc/sys/s390dbf:
There are currently 2 possible triggers, which stop the debug featureglobally. The first possibility is to use thedebug_active sysctl. Ifset to 1 the debug feature is running. Ifdebug_active is set to 0 thedebug feature is turned off.
The second trigger which stops the debug feature is a kernel oops.That prevents the debug feature from overwriting debug information thathappened before the oops. After an oops you can reactivate the debug featureby piping 1 to/proc/sys/s390dbf/debug_active. Nevertheless, it’s notsuggested to use an oopsed kernel in a production environment.
If you want to disallow the deactivation of the debug feature, you can usethedebug_stoppable sysctl. If you setdebug_stoppable to 0 the debugfeature cannot be stopped. If the debug feature is already stopped, itwill stay deactivated.
Kernel Interfaces:¶
- debug_info_t *
debug_register_mode(const char * name, int pages_per_area, int nr_areas, int buf_size, umode_t mode, uid_t uid, gid_t gid)¶ creates and initializes debug area.
Parameters
constchar*name- Name of debug log (e.g. used for debugfs entry)
intpages_per_area- Number of pages, which will be allocated per area
intnr_areas- Number of debug areas
intbuf_size- Size of data area in each debug entry
umode_tmode- File mode for debugfs files. E.g. S_IRWXUGO
uid_tuid- User ID for debugfs files. Currently only 0 is supported.
gid_tgid- Group ID for debugfs files. Currently only 0 is supported.
Return
- Handle for generated debug area
NULLif register failed
Description
Allocates memory for a debug log.Must not be called within an interrupt handler.
- debug_info_t *
debug_register(const char * name, int pages_per_area, int nr_areas, int buf_size)¶ creates and initializes debug area with default file mode.
Parameters
constchar*name- Name of debug log (e.g. used for debugfs entry)
intpages_per_area- Number of pages, which will be allocated per area
intnr_areas- Number of debug areas
intbuf_size- Size of data area in each debug entry
Return
- Handle for generated debug area
NULLif register failed
Description
Allocates memory for a debug log.The debugfs file mode access permissions are read and write for user.Must not be called within an interrupt handler.
- void
debug_unregister(debug_info_t * id)¶ give back debug area.
Parameters
debug_info_t*id- handle for debug log
Return
none
- void
debug_set_level(debug_info_t * id, int new_level)¶ Sets new actual debug level if new_level is valid.
Parameters
debug_info_t*id- handle for debug log
intnew_level- new debug level
Return
none
- void
debug_stop_all(void)¶ stops the debug feature if stopping is allowed.
Parameters
void- no arguments
Return
- none
Description
Currently used in case of a kernel oops.
- void
debug_set_critical(void)¶ event/exception functions try lock instead of spin.
Parameters
void- no arguments
Return
- none
Description
Currently used in case of stopping all CPUs but the current one.Once in this state, functions to write a debug entry for anevent or exception no longer spin on the debug area lock,but only try to get it and fail if they do not get the lock.
- int
debug_register_view(debug_info_t * id, struct debug_view * view)¶ registers new debug view and creates debugfs dir entry
Parameters
debug_info_t*id- handle for debug log
structdebug_view*view- pointer to debug view struct
Return
- 0 : ok
- < 0: Error
- int
debug_unregister_view(debug_info_t * id, struct debug_view * view)¶ unregisters debug view and removes debugfs dir entry
Parameters
debug_info_t*id- handle for debug log
structdebug_view*view- pointer to debug view struct
Return
- 0 : ok
- < 0: Error
- bool
debug_level_enabled(debug_info_t * id, int level)¶ Returns true if debug events for the specified level would be logged. Otherwise returns false.
Parameters
debug_info_t*id- handle for debug log
intlevel- debug level
Return
trueif level is less or equal to the current debug level.
- debug_entry_t *
debug_event(debug_info_t * id, int level, void * data, int length)¶ writes binary debug entry to active debug area (if level <= actual debug level)
Parameters
debug_info_t*id- handle for debug log
intlevel- debug level
void*data- pointer to data for debug entry
intlength- length of data in bytes
Return
- Address of written debug entry
NULLif error
- debug_entry_t *
debug_int_event(debug_info_t * id, int level, unsigned int tag)¶ writes unsigned integer debug entry to active debug area (if level <= actual debug level)
Parameters
debug_info_t*id- handle for debug log
intlevel- debug level
unsignedinttag- integer value for debug entry
Return
- Address of written debug entry
NULLif error
- debug_entry_t *
debug_long_event(debug_info_t * id, int level, unsigned long tag)¶ writes unsigned long debug entry to active debug area (if level <= actual debug level)
Parameters
debug_info_t*id- handle for debug log
intlevel- debug level
unsignedlongtag- long integer value for debug entry
Return
- Address of written debug entry
NULLif error
- debug_entry_t *
debug_text_event(debug_info_t * id, int level, const char * txt)¶ writes string debug entry in ascii format to active debug area (if level <= actual debug level)
Parameters
debug_info_t*id- handle for debug log
intlevel- debug level
constchar*txt- string for debug entry
Return
- Address of written debug entry
NULLif error
debug_sprintf_event(_id,_level,_fmt,…)¶writes debug entry with format string and varargs (longs) to active debug area (if level $<=$ actual debug level).
Parameters
_id- handle for debug log
_level- debug level
_fmt- format string for debug entry
...- varargs used as in
sprintf()
Return
- Address of written debug entry
NULLif error
Description
floats and long long datatypes cannot be used as varargs.
- debug_entry_t *
debug_exception(debug_info_t * id, int level, void * data, int length)¶ writes binary debug entry to active debug area (if level <= actual debug level) and switches to next debug area
Parameters
debug_info_t*id- handle for debug log
intlevel- debug level
void*data- pointer to data for debug entry
intlength- length of data in bytes
Return
- Address of written debug entry
NULLif error
- debug_entry_t *
debug_int_exception(debug_info_t * id, int level, unsigned int tag)¶ writes unsigned int debug entry to active debug area (if level <= actual debug level) and switches to next debug area
Parameters
debug_info_t*id- handle for debug log
intlevel- debug level
unsignedinttag- integer value for debug entry
Return
- Address of written debug entry
NULLif error
- debug_entry_t *
debug_long_exception(debug_info_t * id, int level, unsigned long tag)¶ writes long debug entry to active debug area (if level <= actual debug level) and switches to next debug area
Parameters
debug_info_t*id- handle for debug log
intlevel- debug level
unsignedlongtag- long integer value for debug entry
Return
- Address of written debug entry
NULLif error
- debug_entry_t *
debug_text_exception(debug_info_t * id, int level, const char * txt)¶ writes string debug entry in ascii format to active debug area (if level <= actual debug level) and switches to next debug area area
Parameters
debug_info_t*id- handle for debug log
intlevel- debug level
constchar*txt- string for debug entry
Return
- Address of written debug entry
NULLif error
debug_sprintf_exception(_id,_level,_fmt,…)¶writes debug entry with format string and varargs (longs) to active debug area (if level <= actual debug level) and switches to next debug area.
Parameters
_id- handle for debug log
_level- debug level
_fmt- format string for debug entry
...- varargs used as in
sprintf()
Return
- Address of written debug entry
NULLif error
Description
floats and long long datatypes cannot be used as varargs.
Predefined views:¶
externstructdebug_viewdebug_hex_ascii_view;externstructdebug_viewdebug_sprintf_view;
Examples¶
/* * hex_ascii-view Example */#include<linux/init.h>#include<asm/debug.h>staticdebug_info_t*debug_info;staticintinit(void){/* register 4 debug areas with one page each and 4 byte data field */debug_info=debug_register("test",1,4,4);debug_register_view(debug_info,&debug_hex_ascii_view);debug_text_event(debug_info,4,"one ");debug_int_exception(debug_info,4,4711);debug_event(debug_info,3,&debug_info,4);return0;}staticvoidcleanup(void){debug_unregister(debug_info);}module_init(init);module_exit(cleanup);
/* * sprintf-view Example */#include<linux/init.h>#include<asm/debug.h>staticdebug_info_t*debug_info;staticintinit(void){/* register 4 debug areas with one page each and data field for *//* format string pointer + 2 varargs (= 3 * sizeof(long)) */debug_info=debug_register("test",1,4,sizeof(long)*3);debug_register_view(debug_info,&debug_sprintf_view);debug_sprintf_event(debug_info,2,"first event in %s:%i\n",__FILE__,__LINE__);debug_sprintf_exception(debug_info,1,"pointer to debug info: %p\n",&debug_info);return0;}staticvoidcleanup(void){debug_unregister(debug_info);}module_init(init);module_exit(cleanup);
Debugfs Interface¶
Views to the debug logs can be investigated through reading the correspondingdebugfs-files:
Example:
> ls /sys/kernel/debug/s390dbf/dasdflush hex_ascii level pages> cat /sys/kernel/debug/s390dbf/dasd/hex_ascii | sort -k2,2 -s00 00974733272:680099 2 - 02 0006ad7e 07 ea 4a 90 | ....00 00974733272:682210 2 - 02 0006ade6 46 52 45 45 | FREE00 00974733272:682213 2 - 02 0006adf6 07 ea 4a 90 | ....00 00974733272:682281 1 * 02 0006ab08 41 4c 4c 43 | EXCP01 00974733272:682284 2 - 02 0006ab16 45 43 4b 44 | ECKD01 00974733272:682287 2 - 02 0006ab28 00 00 00 04 | ....01 00974733272:682289 2 - 02 0006ab3e 00 00 00 20 | ...01 00974733272:682297 2 - 02 0006ad7e 07 ea 4a 90 | ....01 00974733272:684384 2 - 00 0006ade6 46 52 45 45 | FREE01 00974733272:684388 2 - 00 0006adf6 07 ea 4a 90 | ....
See section about predefined views for explanation of the above output!
Changing the debug level¶
Example:
> cat /sys/kernel/debug/s390dbf/dasd/level3> echo "5" > /sys/kernel/debug/s390dbf/dasd/level> cat /sys/kernel/debug/s390dbf/dasd/level5
Flushing debug areas¶
Debug areas can be flushed with piping the number of the desiredarea (0…n) to the debugfs file “flush”. When using “-” all debug areasare flushed.
Examples:
Flush debug area 0:
> echo "0" > /sys/kernel/debug/s390dbf/dasd/flush
Flush all debug areas:
> echo "-" > /sys/kernel/debug/s390dbf/dasd/flush
Changing the size of debug areas¶
It is possible the change the size of debug areas through pipingthe number of pages to the debugfs file “pages”. The resize request willalso flush the debug areas.
Example:
Define 4 pages for the debug areas of debug feature “dasd”:
> echo "4" > /sys/kernel/debug/s390dbf/dasd/pages
Stopping the debug feature¶
Example:
Check if stopping is allowed:
> cat /proc/sys/s390dbf/debug_stoppable
Stop debug feature:
> echo 0 > /proc/sys/s390dbf/debug_active
crash Interface¶
Thecrash tool since v5.1.0 has a built-in commands390dbf to display all the debug logs or export them to the file system.With this tool it is possibleto investigate the debug logs on a live system and with a memory dump aftera system crash.
Investigating raw memory¶
One last possibility to investigate the debug logs at a livesystem and after a system crash is to look at the raw memoryunder VM or at the Service Element.It is possible to find the anchor of the debug-logs throughthedebug_area_first symbol in the System map. Then one hasto follow the correct pointers of the data-structures definedin debug.h and find the debug-areas in memory.Normally modules which use the debug feature will also havea global variable with the pointer to the debug-logs. Followingthis pointer it will also be possible to find the debug logs inmemory.
For this method it is recommended to use ‘16 * x + 4’ byte (x = 0..n)for the length of the data field indebug_register() inorder to see the debug entries well formatted.
Predefined Views¶
There are two predefined views: hex_ascii and sprintf.The hex_ascii view shows the data field in hex and ascii representation(e.g.45434b44|ECKD).
The sprintf view formats the debug entries in the same way as the sprintffunction would do. The sprintf event/exception functions write to thedebug entry a pointer to the format string (size = sizeof(long))and for each vararg a long value. So e.g. for a debug entry with a formatstring plus two varargs one would need to allocate a (3 * sizeof(long))byte data area in thedebug_register() function.
- IMPORTANT:
- Using “%s” in sprintf event functions is dangerous. You can onlyuse “%s” in the sprintf event functions, if the memory for the passed stringis available as long as the debug feature exists. The reason behind this isthat due to performance considerations only a pointer to the string is storedin the debug feature. If you log a string that is freed afterwards, you willget an OOPS when inspecting the debug feature, because then the debug featurewill access the already freed memory.
- NOTE:
- If using the sprintf view do NOT use other event/exception functionsthan the sprintf-event and -exception functions.
The format of the hex_ascii and sprintf view is as follows:
- Number of area
- Timestamp (formatted as seconds and microseconds since 00:00:00 CoordinatedUniversal Time (UTC), January 1, 1970)
- level of debug entry
- Exception flag (* = Exception)
- Cpu-Number of calling task
- Return Address to caller
- data field
A typical line of the hex_ascii view will look like the following (first lineis only for explanation and will not be displayed when ‘cating’ the view):
area time level exception cpu caller data (hex + ascii)--------------------------------------------------------------------------00 00964419409:440690 1 - 00 88023fe
Defining views¶
Views are specified with the ‘debug_view’ structure. There are definedcallback functions which are used for reading and writing the debugfs files:
structdebug_view{charname[DEBUG_MAX_PROCF_LEN];debug_prolog_proc_t*prolog_proc;debug_header_proc_t*header_proc;debug_format_proc_t*format_proc;debug_input_proc_t*input_proc;void*private_data;};
where:
typedefint(debug_header_proc_t)(debug_info_t*id,structdebug_view*view,intarea,debug_entry_t*entry,char*out_buf);typedefint(debug_format_proc_t)(debug_info_t*id,structdebug_view*view,char*out_buf,constchar*in_buf);typedefint(debug_prolog_proc_t)(debug_info_t*id,structdebug_view*view,char*out_buf);typedefint(debug_input_proc_t)(debug_info_t*id,structdebug_view*view,structfile*file,constchar*user_buf,size_tin_buf_size,loff_t*offset);
The “private_data” member can be used as pointer to view specific data.It is not used by the debug feature itself.
The output when reading a debugfs file is structured like this:
"prolog_proc output""header_proc output 1" "format_proc output 1""header_proc output 2" "format_proc output 2""header_proc output 3" "format_proc output 3"...
When a view is read from the debugfs, the Debug Feature calls the‘prolog_proc’ once for writing the prolog.Then ‘header_proc’ and ‘format_proc’ are called for eachexisting debug entry.
The input_proc can be used to implement functionality when it is written tothe view (e.g. like withecho"0">/sys/kernel/debug/s390dbf/dasd/level).
For header_proc there can be used the default functiondebug_dflt_header_fn() which is defined in debug.h.and which produces the same header output as the predefined views.E.g:
00 00964419409:440761 2 - 00 88023ec
In order to see how to use the callback functions check the implementationof the default views!
Example:
#include<asm/debug.h>#define UNKNOWNSTR "data: %08x"constchar*messages[]={"This error...........\n","That error...........\n","Problem..............\n","Something went wrong.\n","Everything ok........\n",NULL};staticintdebug_test_format_fn(debug_info_t*id,structdebug_view*view,char*out_buf,constchar*in_buf){inti,rc=0;if(id->buf_size>=4){intmsg_nr=*((int*)in_buf);if(msg_nr<sizeof(messages)/sizeof(char*)-1)rc+=sprintf(out_buf,"%s",messages[msg_nr]);elserc+=sprintf(out_buf,UNKNOWNSTR,msg_nr);}returnrc;}structdebug_viewdebug_test_view={"myview",/* name of view */NULL,/* no prolog */&debug_dflt_header_fn,/* default header for each entry */&debug_test_format_fn,/* our own format function */NULL,/* no input function */NULL/* no private data */};
test:¶
debug_info_t*debug_info;inti;...debug_info=debug_register("test",0,4,4);debug_register_view(debug_info,&debug_test_view);for(i=0;i<10;i++)debug_int_event(debug_info,1,i);
> cat /sys/kernel/debug/s390dbf/test/myview00 00964419734:611402 1 - 00 88042ca This error...........00 00964419734:611405 1 - 00 88042ca That error...........00 00964419734:611408 1 - 00 88042ca Problem..............00 00964419734:611411 1 - 00 88042ca Something went wrong.00 00964419734:611414 1 - 00 88042ca Everything ok........00 00964419734:611417 1 - 00 88042ca data: 0000000500 00964419734:611419 1 - 00 88042ca data: 0000000600 00964419734:611422 1 - 00 88042ca data: 0000000700 00964419734:611425 1 - 00 88042ca data: 0000000800 00964419734:611428 1 - 00 88042ca data: 00000009