2 * arch/s390/kernel/debug.c
5 * Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Michael Holzheu (holzheu@de.ibm.com),
8 * Holger Smolinski (Holger.Smolinski@de.ibm.com)
10 * Bugreports to: <Linux390@de.ibm.com>
13 #define KMSG_COMPONENT "s390dbf"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/stddef.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/ctype.h>
21 #include <linux/string.h>
22 #include <linux/sysctl.h>
23 #include <asm/uaccess.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
27 #include <linux/debugfs.h>
29 #include <asm/debug.h>
31 #define DEBUG_PROLOG_ENTRY -1
33 #define ALL_AREAS 0 /* copy all debug areas */
34 #define NO_AREAS 1 /* copy no debug areas */
38 typedef struct file_private_info {
39 loff_t offset; /* offset of last read in file */
40 int act_area; /* number of last formated area */
41 int act_page; /* act page in given area */
42 int act_entry; /* last formated entry (offset */
43 /* relative to beginning of last */
45 size_t act_entry_offset; /* up to this offset we copied */
46 /* in last read the last formated */
47 /* entry to userland */
48 char temp_buf[2048]; /* buffer for output */
49 debug_info_t *debug_info_org; /* original debug information */
50 debug_info_t *debug_info_snap; /* snapshot of debug information */
51 struct debug_view *view; /* used view of debug info */
52 } file_private_info_t;
58 * This assumes that all args are converted into longs
59 * on L/390 this is the case for all types of parameter
60 * except of floats, and long long (32 bit)
64 } debug_sprintf_entry_t;
67 /* internal function prototyes */
69 static int debug_init(void);
70 static ssize_t debug_output(struct file *file, char __user *user_buf,
71 size_t user_len, loff_t * offset);
72 static ssize_t debug_input(struct file *file, const char __user *user_buf,
73 size_t user_len, loff_t * offset);
74 static int debug_open(struct inode *inode, struct file *file);
75 static int debug_close(struct inode *inode, struct file *file);
76 static debug_info_t *debug_info_create(const char *name, int pages_per_area,
77 int nr_areas, int buf_size, mode_t mode);
78 static void debug_info_get(debug_info_t *);
79 static void debug_info_put(debug_info_t *);
80 static int debug_prolog_level_fn(debug_info_t * id,
81 struct debug_view *view, char *out_buf);
82 static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,
83 struct file *file, const char __user *user_buf,
84 size_t user_buf_size, loff_t * offset);
85 static int debug_prolog_pages_fn(debug_info_t * id,
86 struct debug_view *view, char *out_buf);
87 static int debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
88 struct file *file, const char __user *user_buf,
89 size_t user_buf_size, loff_t * offset);
90 static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
91 struct file *file, const char __user *user_buf,
92 size_t user_buf_size, loff_t * offset);
93 static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
94 char *out_buf, const char *in_buf);
95 static int debug_raw_format_fn(debug_info_t * id,
96 struct debug_view *view, char *out_buf,
98 static int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
99 int area, debug_entry_t * entry, char *out_buf);
101 static int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
102 char *out_buf, debug_sprintf_entry_t *curr_event);
106 struct debug_view debug_raw_view = {
109 &debug_raw_header_fn,
110 &debug_raw_format_fn,
115 struct debug_view debug_hex_ascii_view = {
118 &debug_dflt_header_fn,
119 &debug_hex_ascii_format_fn,
124 static struct debug_view debug_level_view = {
126 &debug_prolog_level_fn,
129 &debug_input_level_fn,
133 static struct debug_view debug_pages_view = {
135 &debug_prolog_pages_fn,
138 &debug_input_pages_fn,
142 static struct debug_view debug_flush_view = {
147 &debug_input_flush_fn,
151 struct debug_view debug_sprintf_view = {
154 &debug_dflt_header_fn,
155 (debug_format_proc_t*)&debug_sprintf_format_fn,
160 /* used by dump analysis tools to determine version of debug feature */
161 static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
165 static debug_info_t *debug_area_first = NULL;
166 static debug_info_t *debug_area_last = NULL;
167 static DEFINE_MUTEX(debug_mutex);
169 static int initialized;
171 static const struct file_operations debug_file_ops = {
172 .owner = THIS_MODULE,
173 .read = debug_output,
174 .write = debug_input,
176 .release = debug_close,
179 static struct dentry *debug_debugfs_root_entry;
185 * - Debug areas are implemented as a threedimensonal array:
186 * areas[areanumber][pagenumber][pageoffset]
189 static debug_entry_t***
190 debug_areas_alloc(int pages_per_area, int nr_areas)
192 debug_entry_t*** areas;
195 areas = kmalloc(nr_areas *
196 sizeof(debug_entry_t**),
199 goto fail_malloc_areas;
200 for (i = 0; i < nr_areas; i++) {
201 areas[i] = kmalloc(pages_per_area *
202 sizeof(debug_entry_t*),GFP_KERNEL);
204 goto fail_malloc_areas2;
206 for(j = 0; j < pages_per_area; j++) {
207 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
209 for(j--; j >=0 ; j--) {
213 goto fail_malloc_areas2;
220 for(i--; i >= 0; i--){
221 for(j=0; j < pages_per_area;j++){
235 * - alloc new debug-info
239 debug_info_alloc(const char *name, int pages_per_area, int nr_areas,
240 int buf_size, int level, int mode)
244 /* alloc everything */
246 rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
249 rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
250 if(!rc->active_entries)
251 goto fail_malloc_active_entries;
252 rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
253 if(!rc->active_pages)
254 goto fail_malloc_active_pages;
255 if((mode == ALL_AREAS) && (pages_per_area != 0)){
256 rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
258 goto fail_malloc_areas;
263 /* initialize members */
265 spin_lock_init(&rc->lock);
266 rc->pages_per_area = pages_per_area;
267 rc->nr_areas = nr_areas;
270 rc->buf_size = buf_size;
271 rc->entry_size = sizeof(debug_entry_t) + buf_size;
272 strlcpy(rc->name, name, sizeof(rc->name));
273 memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
274 memset(rc->debugfs_entries, 0 ,DEBUG_MAX_VIEWS *
275 sizeof(struct dentry*));
276 atomic_set(&(rc->ref_count), 0);
281 kfree(rc->active_pages);
282 fail_malloc_active_pages:
283 kfree(rc->active_entries);
284 fail_malloc_active_entries:
292 * - free all debug areas
296 debug_areas_free(debug_info_t* db_info)
302 for (i = 0; i < db_info->nr_areas; i++) {
303 for(j = 0; j < db_info->pages_per_area; j++) {
304 kfree(db_info->areas[i][j]);
306 kfree(db_info->areas[i]);
308 kfree(db_info->areas);
309 db_info->areas = NULL;
314 * - free memory debug-info
318 debug_info_free(debug_info_t* db_info){
319 debug_areas_free(db_info);
320 kfree(db_info->active_entries);
321 kfree(db_info->active_pages);
327 * - create new debug-info
331 debug_info_create(const char *name, int pages_per_area, int nr_areas,
332 int buf_size, mode_t mode)
336 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
337 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
341 rc->mode = mode & ~S_IFMT;
343 /* create root directory */
344 rc->debugfs_root_entry = debugfs_create_dir(rc->name,
345 debug_debugfs_root_entry);
347 /* append new element to linked list */
348 if (!debug_area_first) {
349 /* first element in list */
350 debug_area_first = rc;
353 /* append element to end of list */
354 debug_area_last->next = rc;
355 rc->prev = debug_area_last;
357 debug_area_last = rc;
371 debug_info_copy(debug_info_t* in, int mode)
377 /* get a consistent copy of the debug areas */
379 rc = debug_info_alloc(in->name, in->pages_per_area,
380 in->nr_areas, in->buf_size, in->level, mode);
381 spin_lock_irqsave(&in->lock, flags);
384 /* has something changed in the meantime ? */
385 if((rc->pages_per_area == in->pages_per_area) &&
386 (rc->nr_areas == in->nr_areas)) {
389 spin_unlock_irqrestore(&in->lock, flags);
393 if (mode == NO_AREAS)
396 for(i = 0; i < in->nr_areas; i++){
397 for(j = 0; j < in->pages_per_area; j++) {
398 memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
402 spin_unlock_irqrestore(&in->lock, flags);
408 * - increments reference count for debug-info
412 debug_info_get(debug_info_t * db_info)
415 atomic_inc(&db_info->ref_count);
420 * - decreases reference count for debug-info and frees it if necessary
424 debug_info_put(debug_info_t *db_info)
430 if (atomic_dec_and_test(&db_info->ref_count)) {
431 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
432 if (!db_info->views[i])
434 debugfs_remove(db_info->debugfs_entries[i]);
436 debugfs_remove(db_info->debugfs_root_entry);
437 if(db_info == debug_area_first)
438 debug_area_first = db_info->next;
439 if(db_info == debug_area_last)
440 debug_area_last = db_info->prev;
441 if(db_info->prev) db_info->prev->next = db_info->next;
442 if(db_info->next) db_info->next->prev = db_info->prev;
443 debug_info_free(db_info);
448 * debug_format_entry:
449 * - format one debug entry and return size of formated data
453 debug_format_entry(file_private_info_t *p_info)
455 debug_info_t *id_snap = p_info->debug_info_snap;
456 struct debug_view *view = p_info->view;
457 debug_entry_t *act_entry;
459 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
461 if (view->prolog_proc)
462 len += view->prolog_proc(id_snap,view,p_info->temp_buf);
465 if (!id_snap->areas) /* this is true, if we have a prolog only view */
466 goto out; /* or if 'pages_per_area' is 0 */
467 act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area]
468 [p_info->act_page] + p_info->act_entry);
470 if (act_entry->id.stck == 0LL)
471 goto out; /* empty entry */
472 if (view->header_proc)
473 len += view->header_proc(id_snap, view, p_info->act_area,
474 act_entry, p_info->temp_buf + len);
475 if (view->format_proc)
476 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
477 DEBUG_DATA(act_entry));
484 * - goto next entry in p_info
488 debug_next_entry(file_private_info_t *p_info)
492 id = p_info->debug_info_snap;
493 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
494 p_info->act_entry = 0;
495 p_info->act_page = 0;
500 p_info->act_entry += id->entry_size;
501 /* switch to next page, if we reached the end of the page */
502 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)){
504 p_info->act_entry = 0;
505 p_info->act_page += 1;
506 if((p_info->act_page % id->pages_per_area) == 0) {
511 if(p_info->act_area >= id->nr_areas)
520 * - called for user read()
521 * - copies formated debug entries to the user buffer
525 debug_output(struct file *file, /* file descriptor */
526 char __user *user_buf, /* user buffer */
527 size_t len, /* length of buffer */
528 loff_t *offset) /* offset in the file */
532 file_private_info_t *p_info;
534 p_info = ((file_private_info_t *) file->private_data);
535 if (*offset != p_info->offset)
537 if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
539 entry_offset = p_info->act_entry_offset;
541 int formatted_line_size;
542 int formatted_line_residue;
543 int user_buf_residue;
546 formatted_line_size = debug_format_entry(p_info);
547 formatted_line_residue = formatted_line_size - entry_offset;
548 user_buf_residue = len-count;
549 copy_size = min(user_buf_residue, formatted_line_residue);
551 if (copy_to_user(user_buf + count, p_info->temp_buf
552 + entry_offset, copy_size))
555 entry_offset += copy_size;
557 if(copy_size == formatted_line_residue){
559 if(debug_next_entry(p_info))
564 p_info->offset = *offset + count;
565 p_info->act_entry_offset = entry_offset;
566 *offset = p_info->offset;
572 * - called for user write()
573 * - calls input function of view
577 debug_input(struct file *file, const char __user *user_buf, size_t length,
581 file_private_info_t *p_info;
583 mutex_lock(&debug_mutex);
584 p_info = ((file_private_info_t *) file->private_data);
585 if (p_info->view->input_proc)
586 rc = p_info->view->input_proc(p_info->debug_info_org,
587 p_info->view, file, user_buf,
591 mutex_unlock(&debug_mutex);
592 return rc; /* number of input characters */
597 * - called for user open()
598 * - copies formated output to private_data area of the file
603 debug_open(struct inode *inode, struct file *file)
606 file_private_info_t *p_info;
607 debug_info_t *debug_info, *debug_info_snapshot;
609 mutex_lock(&debug_mutex);
610 debug_info = file->f_path.dentry->d_inode->i_private;
611 /* find debug view */
612 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
613 if (!debug_info->views[i])
615 else if (debug_info->debugfs_entries[i] ==
616 file->f_path.dentry) {
617 goto found; /* found view ! */
626 /* Make snapshot of current debug areas to get it consistent. */
627 /* To copy all the areas is only needed, if we have a view which */
628 /* formats the debug areas. */
630 if(!debug_info->views[i]->format_proc &&
631 !debug_info->views[i]->header_proc){
632 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
634 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
637 if(!debug_info_snapshot){
641 p_info = kmalloc(sizeof(file_private_info_t),
644 debug_info_free(debug_info_snapshot);
649 p_info->debug_info_snap = debug_info_snapshot;
650 p_info->debug_info_org = debug_info;
651 p_info->view = debug_info->views[i];
652 p_info->act_area = 0;
653 p_info->act_page = 0;
654 p_info->act_entry = DEBUG_PROLOG_ENTRY;
655 p_info->act_entry_offset = 0;
656 file->private_data = p_info;
657 debug_info_get(debug_info);
658 nonseekable_open(inode, file);
660 mutex_unlock(&debug_mutex);
666 * - called for user close()
667 * - deletes private_data area of the file handle
671 debug_close(struct inode *inode, struct file *file)
673 file_private_info_t *p_info;
674 p_info = (file_private_info_t *) file->private_data;
675 if(p_info->debug_info_snap)
676 debug_info_free(p_info->debug_info_snap);
677 debug_info_put(p_info->debug_info_org);
678 kfree(file->private_data);
679 return 0; /* success */
683 * debug_register_mode:
684 * - Creates and initializes debug area for the caller
685 * The mode parameter allows to specify access rights for the s390dbf files
686 * - Returns handle for debug area
689 debug_info_t *debug_register_mode(const char *name, int pages_per_area,
690 int nr_areas, int buf_size, mode_t mode,
691 uid_t uid, gid_t gid)
693 debug_info_t *rc = NULL;
695 /* Since debugfs currently does not support uid/gid other than root, */
696 /* we do not allow gid/uid != 0 until we get support for that. */
697 if ((uid != 0) || (gid != 0))
698 pr_warning("Root becomes the owner of all s390dbf files "
700 BUG_ON(!initialized);
701 mutex_lock(&debug_mutex);
703 /* create new debug_info */
705 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
708 debug_register_view(rc, &debug_level_view);
709 debug_register_view(rc, &debug_flush_view);
710 debug_register_view(rc, &debug_pages_view);
713 pr_err("Registering debug feature %s failed\n", name);
715 mutex_unlock(&debug_mutex);
718 EXPORT_SYMBOL(debug_register_mode);
722 * - creates and initializes debug area for the caller
723 * - returns handle for debug area
726 debug_info_t *debug_register(const char *name, int pages_per_area,
727 int nr_areas, int buf_size)
729 return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
730 S_IRUSR | S_IWUSR, 0, 0);
735 * - give back debug area
739 debug_unregister(debug_info_t * id)
743 mutex_lock(&debug_mutex);
745 mutex_unlock(&debug_mutex);
753 * - set area size (number of pages) and number of areas
756 debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
759 debug_entry_t *** new_areas;
762 if(!id || (nr_areas <= 0) || (pages_per_area < 0))
764 if(pages_per_area > 0){
765 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
767 pr_info("Allocating memory for %i pages failed\n",
775 spin_lock_irqsave(&id->lock,flags);
776 debug_areas_free(id);
777 id->areas = new_areas;
778 id->nr_areas = nr_areas;
779 id->pages_per_area = pages_per_area;
781 memset(id->active_entries,0,sizeof(int)*id->nr_areas);
782 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
783 spin_unlock_irqrestore(&id->lock,flags);
784 pr_info("%s: set new size (%i pages)\n" ,id->name, pages_per_area);
791 * - set actual debug level
795 debug_set_level(debug_info_t* id, int new_level)
800 spin_lock_irqsave(&id->lock,flags);
801 if(new_level == DEBUG_OFF_LEVEL){
802 id->level = DEBUG_OFF_LEVEL;
803 pr_info("%s: switched off\n",id->name);
804 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
805 pr_info("%s: level %i is out of range (%i - %i)\n",
806 id->name, new_level, 0, DEBUG_MAX_LEVEL);
808 id->level = new_level;
810 spin_unlock_irqrestore(&id->lock,flags);
815 * proceed_active_entry:
816 * - set active entry to next in the ring buffer
820 proceed_active_entry(debug_info_t * id)
822 if ((id->active_entries[id->active_area] += id->entry_size)
823 > (PAGE_SIZE - id->entry_size)){
824 id->active_entries[id->active_area] = 0;
825 id->active_pages[id->active_area] =
826 (id->active_pages[id->active_area] + 1) %
832 * proceed_active_area:
833 * - set active area to next in the ring buffer
837 proceed_active_area(debug_info_t * id)
840 id->active_area = id->active_area % id->nr_areas;
847 static inline debug_entry_t*
848 get_active_entry(debug_info_t * id)
850 return (debug_entry_t *) (((char *) id->areas[id->active_area]
851 [id->active_pages[id->active_area]]) +
852 id->active_entries[id->active_area]);
856 * debug_finish_entry:
857 * - set timestamp, caller address, cpu number etc.
861 debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
864 active->id.stck = get_clock();
865 active->id.fields.cpuid = smp_processor_id();
866 active->caller = __builtin_return_address(0);
867 active->id.fields.exception = exception;
868 active->id.fields.level = level;
869 proceed_active_entry(id);
871 proceed_active_area(id);
874 static int debug_stoppable=1;
875 static int debug_active=1;
877 #define CTL_S390DBF_STOPPABLE 5678
878 #define CTL_S390DBF_ACTIVE 5679
881 * proc handler for the running debug_active sysctl
882 * always allow read, allow write only if debug_stoppable is set or
883 * if debug_active is already off
886 s390dbf_procactive(ctl_table *table, int write,
887 void __user *buffer, size_t *lenp, loff_t *ppos)
889 if (!write || debug_stoppable || !debug_active)
890 return proc_dointvec(table, write, buffer, lenp, ppos);
896 static struct ctl_table s390dbf_table[] = {
898 .procname = "debug_stoppable",
899 .data = &debug_stoppable,
900 .maxlen = sizeof(int),
901 .mode = S_IRUGO | S_IWUSR,
902 .proc_handler = proc_dointvec,
905 .procname = "debug_active",
906 .data = &debug_active,
907 .maxlen = sizeof(int),
908 .mode = S_IRUGO | S_IWUSR,
909 .proc_handler = s390dbf_procactive,
914 static struct ctl_table s390dbf_dir_table[] = {
916 .procname = "s390dbf",
918 .mode = S_IRUGO | S_IXUGO,
919 .child = s390dbf_table,
924 static struct ctl_table_header *s390dbf_sysctl_header;
935 * debug_event_common:
936 * - write debug entry with given size
940 debug_event_common(debug_info_t * id, int level, const void *buf, int len)
943 debug_entry_t *active;
945 if (!debug_active || !id->areas)
947 spin_lock_irqsave(&id->lock, flags);
948 active = get_active_entry(id);
949 memset(DEBUG_DATA(active), 0, id->buf_size);
950 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
951 debug_finish_entry(id, active, level, 0);
952 spin_unlock_irqrestore(&id->lock, flags);
958 * debug_exception_common:
959 * - write debug entry with given size and switch to next debug area
963 *debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
966 debug_entry_t *active;
968 if (!debug_active || !id->areas)
970 spin_lock_irqsave(&id->lock, flags);
971 active = get_active_entry(id);
972 memset(DEBUG_DATA(active), 0, id->buf_size);
973 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
974 debug_finish_entry(id, active, level, 1);
975 spin_unlock_irqrestore(&id->lock, flags);
981 * counts arguments in format string for sprintf view
985 debug_count_numargs(char *string)
997 * debug_sprintf_event:
1001 debug_sprintf_event(debug_info_t* id, int level,char *string,...)
1005 unsigned long flags;
1006 debug_sprintf_entry_t *curr_event;
1007 debug_entry_t *active;
1009 if((!id) || (level > id->level))
1011 if (!debug_active || !id->areas)
1013 numargs=debug_count_numargs(string);
1015 spin_lock_irqsave(&id->lock, flags);
1016 active = get_active_entry(id);
1017 curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
1018 va_start(ap,string);
1019 curr_event->string=string;
1020 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1021 curr_event->args[idx]=va_arg(ap,long);
1023 debug_finish_entry(id, active, level, 0);
1024 spin_unlock_irqrestore(&id->lock, flags);
1030 * debug_sprintf_exception:
1034 debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
1038 unsigned long flags;
1039 debug_sprintf_entry_t *curr_event;
1040 debug_entry_t *active;
1042 if((!id) || (level > id->level))
1044 if (!debug_active || !id->areas)
1047 numargs=debug_count_numargs(string);
1049 spin_lock_irqsave(&id->lock, flags);
1050 active = get_active_entry(id);
1051 curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
1052 va_start(ap,string);
1053 curr_event->string=string;
1054 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1055 curr_event->args[idx]=va_arg(ap,long);
1057 debug_finish_entry(id, active, level, 1);
1058 spin_unlock_irqrestore(&id->lock, flags);
1065 * - is called exactly once to initialize the debug feature
1069 __init debug_init(void)
1073 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
1074 mutex_lock(&debug_mutex);
1075 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
1077 mutex_unlock(&debug_mutex);
1083 * debug_register_view:
1087 debug_register_view(debug_info_t * id, struct debug_view *view)
1091 unsigned long flags;
1097 mode = (id->mode | S_IFREG) & ~S_IXUGO;
1098 if (!(view->prolog_proc || view->format_proc || view->header_proc))
1099 mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
1100 if (!view->input_proc)
1101 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1102 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
1103 id , &debug_file_ops);
1105 pr_err("Registering view %s/%s failed due to out of "
1106 "memory\n", id->name,view->name);
1110 spin_lock_irqsave(&id->lock, flags);
1111 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1115 if (i == DEBUG_MAX_VIEWS) {
1116 pr_err("Registering view %s/%s would exceed the maximum "
1117 "number of views %i\n", id->name, view->name, i);
1118 debugfs_remove(pde);
1121 id->views[i] = view;
1122 id->debugfs_entries[i] = pde;
1124 spin_unlock_irqrestore(&id->lock, flags);
1130 * debug_unregister_view:
1134 debug_unregister_view(debug_info_t * id, struct debug_view *view)
1138 unsigned long flags;
1142 spin_lock_irqsave(&id->lock, flags);
1143 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1144 if (id->views[i] == view)
1147 if (i == DEBUG_MAX_VIEWS)
1150 debugfs_remove(id->debugfs_entries[i]);
1151 id->views[i] = NULL;
1153 spin_unlock_irqrestore(&id->lock, flags);
1158 static inline char *
1159 debug_get_user_string(const char __user *user_buf, size_t user_len)
1163 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1165 return ERR_PTR(-ENOMEM);
1166 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1168 return ERR_PTR(-EFAULT);
1170 /* got the string, now strip linefeed. */
1171 if (buffer[user_len - 1] == '\n')
1172 buffer[user_len - 1] = 0;
1174 buffer[user_len] = 0;
1179 debug_get_uint(char *buf)
1183 buf = skip_spaces(buf);
1184 rc = simple_strtoul(buf, &buf, 10);
1192 * functions for debug-views
1193 ***********************************
1197 * prints out actual debug level
1201 debug_prolog_pages_fn(debug_info_t * id,
1202 struct debug_view *view, char *out_buf)
1204 return sprintf(out_buf, "%i\n", id->pages_per_area);
1208 * reads new size (number of pages per debug area)
1212 debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
1213 struct file *file, const char __user *user_buf,
1214 size_t user_len, loff_t * offset)
1219 if (user_len > 0x10000)
1225 str = debug_get_user_string(user_buf,user_len);
1230 new_pages = debug_get_uint(str);
1235 rc = debug_set_size(id,id->nr_areas, new_pages);
1244 *offset += user_len;
1245 return rc; /* number of input characters */
1249 * prints out actual debug level
1253 debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
1257 if(id->level == DEBUG_OFF_LEVEL) {
1258 rc = sprintf(out_buf,"-\n");
1261 rc = sprintf(out_buf, "%i\n", id->level);
1267 * reads new debug level
1271 debug_input_level_fn(debug_info_t * id, struct debug_view *view,
1272 struct file *file, const char __user *user_buf,
1273 size_t user_len, loff_t * offset)
1278 if (user_len > 0x10000)
1284 str = debug_get_user_string(user_buf,user_len);
1290 debug_set_level(id, DEBUG_OFF_LEVEL);
1294 new_level = debug_get_uint(str);
1297 pr_warning("%s is not a valid level for a debug "
1301 debug_set_level(id, new_level);
1307 *offset += user_len;
1308 return rc; /* number of input characters */
1313 * flushes debug areas
1316 static void debug_flush(debug_info_t* id, int area)
1318 unsigned long flags;
1321 if(!id || !id->areas)
1323 spin_lock_irqsave(&id->lock,flags);
1324 if(area == DEBUG_FLUSH_ALL){
1325 id->active_area = 0;
1326 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1327 for (i = 0; i < id->nr_areas; i++) {
1328 id->active_pages[i] = 0;
1329 for(j = 0; j < id->pages_per_area; j++) {
1330 memset(id->areas[i][j], 0, PAGE_SIZE);
1333 } else if(area >= 0 && area < id->nr_areas) {
1334 id->active_entries[area] = 0;
1335 id->active_pages[area] = 0;
1336 for(i = 0; i < id->pages_per_area; i++) {
1337 memset(id->areas[area][i],0,PAGE_SIZE);
1340 spin_unlock_irqrestore(&id->lock,flags);
1344 * view function: flushes debug areas
1348 debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
1349 struct file *file, const char __user *user_buf,
1350 size_t user_len, loff_t * offset)
1355 if (user_len > 0x10000)
1361 if (copy_from_user(input_buf, user_buf, 1)){
1365 if(input_buf[0] == '-') {
1366 debug_flush(id, DEBUG_FLUSH_ALL);
1369 if (isdigit(input_buf[0])) {
1370 int area = ((int) input_buf[0] - (int) '0');
1371 debug_flush(id, area);
1375 pr_info("Flushing debug data failed because %c is not a valid "
1376 "area\n", input_buf[0]);
1379 *offset += user_len;
1380 return rc; /* number of input characters */
1384 * prints debug header in raw format
1388 debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
1389 int area, debug_entry_t * entry, char *out_buf)
1393 rc = sizeof(debug_entry_t);
1394 memcpy(out_buf,entry,sizeof(debug_entry_t));
1399 * prints debug data in raw format
1403 debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
1404 char *out_buf, const char *in_buf)
1409 memcpy(out_buf, in_buf, id->buf_size);
1414 * prints debug data in hex/ascii format
1418 debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
1419 char *out_buf, const char *in_buf)
1423 for (i = 0; i < id->buf_size; i++) {
1424 rc += sprintf(out_buf + rc, "%02x ",
1425 ((unsigned char *) in_buf)[i]);
1427 rc += sprintf(out_buf + rc, "| ");
1428 for (i = 0; i < id->buf_size; i++) {
1429 unsigned char c = in_buf[i];
1431 rc += sprintf(out_buf + rc, ".");
1433 rc += sprintf(out_buf + rc, "%c", c);
1435 rc += sprintf(out_buf + rc, "\n");
1440 * prints header for debug entry
1444 debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
1445 int area, debug_entry_t * entry, char *out_buf)
1447 struct timespec time_spec;
1449 unsigned long caller;
1453 level = entry->id.fields.level;
1454 stck_to_timespec(entry->id.stck, &time_spec);
1456 if (entry->id.fields.exception)
1460 caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
1461 rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
1462 area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
1463 except_str, entry->id.fields.cpuid, (void *) caller);
1468 * prints debug data sprintf-formated:
1469 * debug_sprinf_event/exception calls must be used together with this view
1472 #define DEBUG_SPRINTF_MAX_ARGS 10
1475 debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
1476 char *out_buf, debug_sprintf_entry_t *curr_event)
1478 int num_longs, num_used_args = 0,i, rc = 0;
1479 int index[DEBUG_SPRINTF_MAX_ARGS];
1481 /* count of longs fit into one entry */
1482 num_longs = id->buf_size / sizeof(long);
1485 goto out; /* bufsize of entry too small */
1486 if(num_longs == 1) {
1487 /* no args, we use only the string */
1488 strcpy(out_buf, curr_event->string);
1489 rc = strlen(curr_event->string);
1493 /* number of arguments used for sprintf (without the format string) */
1494 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1496 memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1498 for(i = 0; i < num_used_args; i++)
1501 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1502 curr_event->args[index[1]], curr_event->args[index[2]],
1503 curr_event->args[index[3]], curr_event->args[index[4]],
1504 curr_event->args[index[5]], curr_event->args[index[6]],
1505 curr_event->args[index[7]], curr_event->args[index[8]],
1506 curr_event->args[index[9]]);
1516 static void __exit debug_exit(void)
1518 debugfs_remove(debug_debugfs_root_entry);
1519 unregister_sysctl_table(s390dbf_sysctl_header);
1524 * module definitions
1526 postcore_initcall(debug_init);
1527 module_exit(debug_exit);
1528 MODULE_LICENSE("GPL");
1530 EXPORT_SYMBOL(debug_register);
1531 EXPORT_SYMBOL(debug_unregister);
1532 EXPORT_SYMBOL(debug_set_level);
1533 EXPORT_SYMBOL(debug_stop_all);
1534 EXPORT_SYMBOL(debug_register_view);
1535 EXPORT_SYMBOL(debug_unregister_view);
1536 EXPORT_SYMBOL(debug_event_common);
1537 EXPORT_SYMBOL(debug_exception_common);
1538 EXPORT_SYMBOL(debug_hex_ascii_view);
1539 EXPORT_SYMBOL(debug_raw_view);
1540 EXPORT_SYMBOL(debug_dflt_header_fn);
1541 EXPORT_SYMBOL(debug_sprintf_view);
1542 EXPORT_SYMBOL(debug_sprintf_exception);
1543 EXPORT_SYMBOL(debug_sprintf_event);