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 #include <linux/config.h>
14 #include <linux/stddef.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/ctype.h>
19 #include <linux/sysctl.h>
20 #include <asm/uaccess.h>
21 #include <asm/semaphore.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
25 #include <linux/debugfs.h>
27 #include <asm/debug.h>
29 #define DEBUG_PROLOG_ENTRY -1
31 #define ALL_AREAS 0 /* copy all debug areas */
32 #define NO_AREAS 1 /* copy no debug areas */
36 typedef struct file_private_info {
37 loff_t offset; /* offset of last read in file */
38 int act_area; /* number of last formated area */
39 int act_page; /* act page in given area */
40 int act_entry; /* last formated entry (offset */
41 /* relative to beginning of last */
43 size_t act_entry_offset; /* up to this offset we copied */
44 /* in last read the last formated */
45 /* entry to userland */
46 char temp_buf[2048]; /* buffer for output */
47 debug_info_t *debug_info_org; /* original debug information */
48 debug_info_t *debug_info_snap; /* snapshot of debug information */
49 struct debug_view *view; /* used view of debug info */
50 } file_private_info_t;
56 * This assumes that all args are converted into longs
57 * on L/390 this is the case for all types of parameter
58 * except of floats, and long long (32 bit)
62 } debug_sprintf_entry_t;
65 extern void tod_to_timeval(uint64_t todval, struct timespec *xtime);
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(char *name, int pages_per_area,
77 int nr_areas, int buf_size);
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 struct debug_view debug_level_view = {
126 &debug_prolog_level_fn,
129 &debug_input_level_fn,
133 struct debug_view debug_pages_view = {
135 &debug_prolog_pages_fn,
138 &debug_input_pages_fn,
142 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,
161 unsigned int 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 DECLARE_MUTEX(debug_lock);
169 static int initialized;
171 static 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 = (debug_entry_t ***) kmalloc(nr_areas *
196 sizeof(debug_entry_t**),
199 goto fail_malloc_areas;
200 for (i = 0; i < nr_areas; i++) {
201 areas[i] = (debug_entry_t**) 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(char *name, int pages_per_area, int nr_areas, int buf_size,
244 /* alloc everything */
246 rc = (debug_info_t*) 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)-1);
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(char *name, int pages_per_area, int nr_areas, int buf_size)
335 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
336 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
340 /* create root directory */
341 rc->debugfs_root_entry = debugfs_create_dir(rc->name,
342 debug_debugfs_root_entry);
344 /* append new element to linked list */
345 if (!debug_area_first) {
346 /* first element in list */
347 debug_area_first = rc;
350 /* append element to end of list */
351 debug_area_last->next = rc;
352 rc->prev = debug_area_last;
354 debug_area_last = rc;
368 debug_info_copy(debug_info_t* in, int mode)
374 /* get a consistent copy of the debug areas */
376 rc = debug_info_alloc(in->name, in->pages_per_area,
377 in->nr_areas, in->buf_size, in->level, mode);
378 spin_lock_irqsave(&in->lock, flags);
381 /* has something changed in the meantime ? */
382 if((rc->pages_per_area == in->pages_per_area) &&
383 (rc->nr_areas == in->nr_areas)) {
386 spin_unlock_irqrestore(&in->lock, flags);
390 if(!rc || (mode == NO_AREAS))
393 for(i = 0; i < in->nr_areas; i++){
394 for(j = 0; j < in->pages_per_area; j++) {
395 memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
399 spin_unlock_irqrestore(&in->lock, flags);
405 * - increments reference count for debug-info
409 debug_info_get(debug_info_t * db_info)
412 atomic_inc(&db_info->ref_count);
417 * - decreases reference count for debug-info and frees it if necessary
421 debug_info_put(debug_info_t *db_info)
427 if (atomic_dec_and_test(&db_info->ref_count)) {
428 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
429 if (!db_info->views[i])
431 debugfs_remove(db_info->debugfs_entries[i]);
433 debugfs_remove(db_info->debugfs_root_entry);
434 if(db_info == debug_area_first)
435 debug_area_first = db_info->next;
436 if(db_info == debug_area_last)
437 debug_area_last = db_info->prev;
438 if(db_info->prev) db_info->prev->next = db_info->next;
439 if(db_info->next) db_info->next->prev = db_info->prev;
440 debug_info_free(db_info);
445 * debug_format_entry:
446 * - format one debug entry and return size of formated data
450 debug_format_entry(file_private_info_t *p_info)
452 debug_info_t *id_snap = p_info->debug_info_snap;
453 struct debug_view *view = p_info->view;
454 debug_entry_t *act_entry;
456 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
458 if (view->prolog_proc)
459 len += view->prolog_proc(id_snap,view,p_info->temp_buf);
462 if (!id_snap->areas) /* this is true, if we have a prolog only view */
463 goto out; /* or if 'pages_per_area' is 0 */
464 act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area]
465 [p_info->act_page] + p_info->act_entry);
467 if (act_entry->id.stck == 0LL)
468 goto out; /* empty entry */
469 if (view->header_proc)
470 len += view->header_proc(id_snap, view, p_info->act_area,
471 act_entry, p_info->temp_buf + len);
472 if (view->format_proc)
473 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
474 DEBUG_DATA(act_entry));
481 * - goto next entry in p_info
485 debug_next_entry(file_private_info_t *p_info)
489 id = p_info->debug_info_snap;
490 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
491 p_info->act_entry = 0;
492 p_info->act_page = 0;
497 p_info->act_entry += id->entry_size;
498 /* switch to next page, if we reached the end of the page */
499 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)){
501 p_info->act_entry = 0;
502 p_info->act_page += 1;
503 if((p_info->act_page % id->pages_per_area) == 0) {
508 if(p_info->act_area >= id->nr_areas)
517 * - called for user read()
518 * - copies formated debug entries to the user buffer
522 debug_output(struct file *file, /* file descriptor */
523 char __user *user_buf, /* user buffer */
524 size_t len, /* length of buffer */
525 loff_t *offset) /* offset in the file */
529 file_private_info_t *p_info;
531 p_info = ((file_private_info_t *) file->private_data);
532 if (*offset != p_info->offset)
534 if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
536 entry_offset = p_info->act_entry_offset;
538 int formatted_line_size;
539 int formatted_line_residue;
540 int user_buf_residue;
543 formatted_line_size = debug_format_entry(p_info);
544 formatted_line_residue = formatted_line_size - entry_offset;
545 user_buf_residue = len-count;
546 copy_size = min(user_buf_residue, formatted_line_residue);
548 if (copy_to_user(user_buf + count, p_info->temp_buf
549 + entry_offset, copy_size))
552 entry_offset += copy_size;
554 if(copy_size == formatted_line_residue){
556 if(debug_next_entry(p_info))
561 p_info->offset = *offset + count;
562 p_info->act_entry_offset = entry_offset;
563 *offset = p_info->offset;
569 * - called for user write()
570 * - calls input function of view
574 debug_input(struct file *file, const char __user *user_buf, size_t length,
578 file_private_info_t *p_info;
581 p_info = ((file_private_info_t *) file->private_data);
582 if (p_info->view->input_proc)
583 rc = p_info->view->input_proc(p_info->debug_info_org,
584 p_info->view, file, user_buf,
589 return rc; /* number of input characters */
594 * - called for user open()
595 * - copies formated output to private_data area of the file
600 debug_open(struct inode *inode, struct file *file)
603 file_private_info_t *p_info;
604 debug_info_t *debug_info, *debug_info_snapshot;
607 debug_info = (struct debug_info*)file->f_dentry->d_inode->u.generic_ip;
608 /* find debug view */
609 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
610 if (!debug_info->views[i])
612 else if (debug_info->debugfs_entries[i] ==
614 goto found; /* found view ! */
623 /* Make snapshot of current debug areas to get it consistent. */
624 /* To copy all the areas is only needed, if we have a view which */
625 /* formats the debug areas. */
627 if(!debug_info->views[i]->format_proc &&
628 !debug_info->views[i]->header_proc){
629 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
631 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
634 if(!debug_info_snapshot){
638 p_info = (file_private_info_t *) kmalloc(sizeof(file_private_info_t),
641 if(debug_info_snapshot)
642 debug_info_free(debug_info_snapshot);
647 p_info->debug_info_snap = debug_info_snapshot;
648 p_info->debug_info_org = debug_info;
649 p_info->view = debug_info->views[i];
650 p_info->act_area = 0;
651 p_info->act_page = 0;
652 p_info->act_entry = DEBUG_PROLOG_ENTRY;
653 p_info->act_entry_offset = 0;
654 file->private_data = p_info;
655 debug_info_get(debug_info);
663 * - called for user close()
664 * - deletes private_data area of the file handle
668 debug_close(struct inode *inode, struct file *file)
670 file_private_info_t *p_info;
671 p_info = (file_private_info_t *) file->private_data;
672 if(p_info->debug_info_snap)
673 debug_info_free(p_info->debug_info_snap);
674 debug_info_put(p_info->debug_info_org);
675 kfree(file->private_data);
676 return 0; /* success */
681 * - creates and initializes debug area for the caller
682 * - returns handle for debug area
686 debug_register (char *name, int pages_per_area, int nr_areas, int buf_size)
688 debug_info_t *rc = NULL;
694 /* create new debug_info */
696 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size);
699 debug_register_view(rc, &debug_level_view);
700 debug_register_view(rc, &debug_flush_view);
701 debug_register_view(rc, &debug_pages_view);
704 printk(KERN_ERR "debug: debug_register failed for %s\n",name);
712 * - give back debug area
716 debug_unregister(debug_info_t * id)
730 * - set area size (number of pages) and number of areas
733 debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
736 debug_entry_t *** new_areas;
739 if(!id || (nr_areas <= 0) || (pages_per_area < 0))
741 if(pages_per_area > 0){
742 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
744 printk(KERN_WARNING "debug: could not allocate memory "\
745 "for pagenumber: %i\n",pages_per_area);
752 spin_lock_irqsave(&id->lock,flags);
753 debug_areas_free(id);
754 id->areas = new_areas;
755 id->nr_areas = nr_areas;
756 id->pages_per_area = pages_per_area;
758 memset(id->active_entries,0,sizeof(int)*id->nr_areas);
759 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
760 spin_unlock_irqrestore(&id->lock,flags);
761 printk(KERN_INFO "debug: %s: set new size (%i pages)\n"\
762 ,id->name, pages_per_area);
769 * - set actual debug level
773 debug_set_level(debug_info_t* id, int new_level)
778 spin_lock_irqsave(&id->lock,flags);
779 if(new_level == DEBUG_OFF_LEVEL){
780 id->level = DEBUG_OFF_LEVEL;
781 printk(KERN_INFO "debug: %s: switched off\n",id->name);
782 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
784 "debug: %s: level %i is out of range (%i - %i)\n",
785 id->name, new_level, 0, DEBUG_MAX_LEVEL);
787 id->level = new_level;
789 spin_unlock_irqrestore(&id->lock,flags);
794 * proceed_active_entry:
795 * - set active entry to next in the ring buffer
799 proceed_active_entry(debug_info_t * id)
801 if ((id->active_entries[id->active_area] += id->entry_size)
802 > (PAGE_SIZE - id->entry_size)){
803 id->active_entries[id->active_area] = 0;
804 id->active_pages[id->active_area] =
805 (id->active_pages[id->active_area] + 1) %
811 * proceed_active_area:
812 * - set active area to next in the ring buffer
816 proceed_active_area(debug_info_t * id)
819 id->active_area = id->active_area % id->nr_areas;
826 static inline debug_entry_t*
827 get_active_entry(debug_info_t * id)
829 return (debug_entry_t *) (((char *) id->areas[id->active_area]
830 [id->active_pages[id->active_area]]) +
831 id->active_entries[id->active_area]);
835 * debug_finish_entry:
836 * - set timestamp, caller address, cpu number etc.
840 debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
843 active->id.stck = get_clock();
844 active->id.fields.cpuid = smp_processor_id();
845 active->caller = __builtin_return_address(0);
846 active->id.fields.exception = exception;
847 active->id.fields.level = level;
848 proceed_active_entry(id);
850 proceed_active_area(id);
853 static int debug_stoppable=1;
854 static int debug_active=1;
856 #define CTL_S390DBF 5677
857 #define CTL_S390DBF_STOPPABLE 5678
858 #define CTL_S390DBF_ACTIVE 5679
861 * proc handler for the running debug_active sysctl
862 * always allow read, allow write only if debug_stoppable is set or
863 * if debug_active is already off
866 s390dbf_procactive(ctl_table *table, int write, struct file *filp,
867 void __user *buffer, size_t *lenp, loff_t *ppos)
869 if (!write || debug_stoppable || !debug_active)
870 return proc_dointvec(table, write, filp, buffer, lenp, ppos);
876 static struct ctl_table s390dbf_table[] = {
878 .ctl_name = CTL_S390DBF_STOPPABLE,
879 .procname = "debug_stoppable",
880 .data = &debug_stoppable,
881 .maxlen = sizeof(int),
882 .mode = S_IRUGO | S_IWUSR,
883 .proc_handler = &proc_dointvec,
884 .strategy = &sysctl_intvec,
887 .ctl_name = CTL_S390DBF_ACTIVE,
888 .procname = "debug_active",
889 .data = &debug_active,
890 .maxlen = sizeof(int),
891 .mode = S_IRUGO | S_IWUSR,
892 .proc_handler = &s390dbf_procactive,
893 .strategy = &sysctl_intvec,
898 static struct ctl_table s390dbf_dir_table[] = {
900 .ctl_name = CTL_S390DBF,
901 .procname = "s390dbf",
903 .mode = S_IRUGO | S_IXUGO,
904 .child = s390dbf_table,
909 struct ctl_table_header *s390dbf_sysctl_header;
920 * debug_event_common:
921 * - write debug entry with given size
925 debug_event_common(debug_info_t * id, int level, const void *buf, int len)
928 debug_entry_t *active;
930 if (!debug_active || !id->areas)
932 spin_lock_irqsave(&id->lock, flags);
933 active = get_active_entry(id);
934 memset(DEBUG_DATA(active), 0, id->buf_size);
935 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
936 debug_finish_entry(id, active, level, 0);
937 spin_unlock_irqrestore(&id->lock, flags);
943 * debug_exception_common:
944 * - write debug entry with given size and switch to next debug area
948 *debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
951 debug_entry_t *active;
953 if (!debug_active || !id->areas)
955 spin_lock_irqsave(&id->lock, flags);
956 active = get_active_entry(id);
957 memset(DEBUG_DATA(active), 0, id->buf_size);
958 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
959 debug_finish_entry(id, active, level, 1);
960 spin_unlock_irqrestore(&id->lock, flags);
966 * counts arguments in format string for sprintf view
970 debug_count_numargs(char *string)
982 * debug_sprintf_event:
986 debug_sprintf_event(debug_info_t* id, int level,char *string,...)
991 debug_sprintf_entry_t *curr_event;
992 debug_entry_t *active;
994 if((!id) || (level > id->level))
996 if (!debug_active || !id->areas)
998 numargs=debug_count_numargs(string);
1000 spin_lock_irqsave(&id->lock, flags);
1001 active = get_active_entry(id);
1002 curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
1003 va_start(ap,string);
1004 curr_event->string=string;
1005 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1006 curr_event->args[idx]=va_arg(ap,long);
1008 debug_finish_entry(id, active, level, 0);
1009 spin_unlock_irqrestore(&id->lock, flags);
1015 * debug_sprintf_exception:
1019 debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
1023 unsigned long flags;
1024 debug_sprintf_entry_t *curr_event;
1025 debug_entry_t *active;
1027 if((!id) || (level > id->level))
1029 if (!debug_active || !id->areas)
1032 numargs=debug_count_numargs(string);
1034 spin_lock_irqsave(&id->lock, flags);
1035 active = get_active_entry(id);
1036 curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
1037 va_start(ap,string);
1038 curr_event->string=string;
1039 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1040 curr_event->args[idx]=va_arg(ap,long);
1042 debug_finish_entry(id, active, level, 1);
1043 spin_unlock_irqrestore(&id->lock, flags);
1050 * - is called exactly once to initialize the debug feature
1054 __init debug_init(void)
1058 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table, 1);
1060 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
1061 printk(KERN_INFO "debug: Initialization complete\n");
1069 * debug_register_view:
1073 debug_register_view(debug_info_t * id, struct debug_view *view)
1077 unsigned long flags;
1078 mode_t mode = S_IFREG;
1083 if (view->prolog_proc || view->format_proc || view->header_proc)
1085 if (view->input_proc)
1087 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
1088 id , &debug_file_ops);
1090 printk(KERN_WARNING "debug: debugfs_create_file() failed!"\
1091 " Cannot register view %s/%s\n", id->name,view->name);
1095 spin_lock_irqsave(&id->lock, flags);
1096 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1100 if (i == DEBUG_MAX_VIEWS) {
1101 printk(KERN_WARNING "debug: cannot register view %s/%s\n",
1102 id->name,view->name);
1104 "debug: maximum number of views reached (%i)!\n", i);
1105 debugfs_remove(pde);
1108 id->views[i] = view;
1109 id->debugfs_entries[i] = pde;
1111 spin_unlock_irqrestore(&id->lock, flags);
1117 * debug_unregister_view:
1121 debug_unregister_view(debug_info_t * id, struct debug_view *view)
1125 unsigned long flags;
1129 spin_lock_irqsave(&id->lock, flags);
1130 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1131 if (id->views[i] == view)
1134 if (i == DEBUG_MAX_VIEWS)
1137 debugfs_remove(id->debugfs_entries[i]);
1138 id->views[i] = NULL;
1141 spin_unlock_irqrestore(&id->lock, flags);
1146 static inline char *
1147 debug_get_user_string(const char __user *user_buf, size_t user_len)
1151 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1153 return ERR_PTR(-ENOMEM);
1154 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1156 return ERR_PTR(-EFAULT);
1158 /* got the string, now strip linefeed. */
1159 if (buffer[user_len - 1] == '\n')
1160 buffer[user_len - 1] = 0;
1162 buffer[user_len] = 0;
1167 debug_get_uint(char *buf)
1171 for(; isspace(*buf); buf++);
1172 rc = simple_strtoul(buf, &buf, 10);
1174 printk("debug: no integer specified!\n");
1181 * functions for debug-views
1182 ***********************************
1186 * prints out actual debug level
1190 debug_prolog_pages_fn(debug_info_t * id,
1191 struct debug_view *view, char *out_buf)
1193 return sprintf(out_buf, "%i\n", id->pages_per_area);
1197 * reads new size (number of pages per debug area)
1201 debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
1202 struct file *file, const char __user *user_buf,
1203 size_t user_len, loff_t * offset)
1208 if (user_len > 0x10000)
1214 str = debug_get_user_string(user_buf,user_len);
1219 new_pages = debug_get_uint(str);
1224 rc = debug_set_size(id,id->nr_areas, new_pages);
1233 *offset += user_len;
1234 return rc; /* number of input characters */
1238 * prints out actual debug level
1242 debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
1246 if(id->level == DEBUG_OFF_LEVEL) {
1247 rc = sprintf(out_buf,"-\n");
1250 rc = sprintf(out_buf, "%i\n", id->level);
1256 * reads new debug level
1260 debug_input_level_fn(debug_info_t * id, struct debug_view *view,
1261 struct file *file, const char __user *user_buf,
1262 size_t user_len, loff_t * offset)
1267 if (user_len > 0x10000)
1273 str = debug_get_user_string(user_buf,user_len);
1279 debug_set_level(id, DEBUG_OFF_LEVEL);
1283 new_level = debug_get_uint(str);
1286 printk(KERN_INFO "debug: level `%s` is not valid\n", str);
1289 debug_set_level(id, new_level);
1295 *offset += user_len;
1296 return rc; /* number of input characters */
1301 * flushes debug areas
1305 debug_flush(debug_info_t* id, int area)
1307 unsigned long flags;
1310 if(!id || !id->areas)
1312 spin_lock_irqsave(&id->lock,flags);
1313 if(area == DEBUG_FLUSH_ALL){
1314 id->active_area = 0;
1315 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1316 for (i = 0; i < id->nr_areas; i++) {
1317 id->active_pages[i] = 0;
1318 for(j = 0; j < id->pages_per_area; j++) {
1319 memset(id->areas[i][j], 0, PAGE_SIZE);
1322 printk(KERN_INFO "debug: %s: all areas flushed\n",id->name);
1323 } else if(area >= 0 && area < id->nr_areas) {
1324 id->active_entries[area] = 0;
1325 id->active_pages[area] = 0;
1326 for(i = 0; i < id->pages_per_area; i++) {
1327 memset(id->areas[area][i],0,PAGE_SIZE);
1329 printk(KERN_INFO "debug: %s: area %i has been flushed\n",
1333 "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
1334 id->name, area, 0, id->nr_areas-1);
1336 spin_unlock_irqrestore(&id->lock,flags);
1340 * view function: flushes debug areas
1344 debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
1345 struct file *file, const char __user *user_buf,
1346 size_t user_len, loff_t * offset)
1351 if (user_len > 0x10000)
1357 if (copy_from_user(input_buf, user_buf, 1)){
1361 if(input_buf[0] == '-') {
1362 debug_flush(id, DEBUG_FLUSH_ALL);
1365 if (isdigit(input_buf[0])) {
1366 int area = ((int) input_buf[0] - (int) '0');
1367 debug_flush(id, area);
1371 printk(KERN_INFO "debug: area `%c` is not valid\n", input_buf[0]);
1374 *offset += user_len;
1375 return rc; /* number of input characters */
1379 * prints debug header in raw format
1383 debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
1384 int area, debug_entry_t * entry, char *out_buf)
1388 rc = sizeof(debug_entry_t);
1389 memcpy(out_buf,entry,sizeof(debug_entry_t));
1394 * prints debug data in raw format
1398 debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
1399 char *out_buf, const char *in_buf)
1404 memcpy(out_buf, in_buf, id->buf_size);
1409 * prints debug data in hex/ascii format
1413 debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
1414 char *out_buf, const char *in_buf)
1418 for (i = 0; i < id->buf_size; i++) {
1419 rc += sprintf(out_buf + rc, "%02x ",
1420 ((unsigned char *) in_buf)[i]);
1422 rc += sprintf(out_buf + rc, "| ");
1423 for (i = 0; i < id->buf_size; i++) {
1424 unsigned char c = in_buf[i];
1426 rc += sprintf(out_buf + rc, ".");
1428 rc += sprintf(out_buf + rc, "%c", c);
1430 rc += sprintf(out_buf + rc, "\n");
1435 * prints header for debug entry
1439 debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
1440 int area, debug_entry_t * entry, char *out_buf)
1442 struct timespec time_spec;
1443 unsigned long long time;
1445 unsigned long caller;
1449 level = entry->id.fields.level;
1450 time = entry->id.stck;
1451 /* adjust todclock to 1970 */
1452 time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
1453 tod_to_timeval(time, &time_spec);
1455 if (entry->id.fields.exception)
1459 caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
1460 rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
1461 area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
1462 except_str, entry->id.fields.cpuid, (void *) caller);
1467 * prints debug data sprintf-formated:
1468 * debug_sprinf_event/exception calls must be used together with this view
1471 #define DEBUG_SPRINTF_MAX_ARGS 10
1474 debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
1475 char *out_buf, debug_sprintf_entry_t *curr_event)
1477 int num_longs, num_used_args = 0,i, rc = 0;
1478 int index[DEBUG_SPRINTF_MAX_ARGS];
1480 /* count of longs fit into one entry */
1481 num_longs = id->buf_size / sizeof(long);
1484 goto out; /* bufsize of entry too small */
1485 if(num_longs == 1) {
1486 /* no args, we use only the string */
1487 strcpy(out_buf, curr_event->string);
1488 rc = strlen(curr_event->string);
1492 /* number of arguments used for sprintf (without the format string) */
1493 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1495 memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1497 for(i = 0; i < num_used_args; i++)
1500 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1501 curr_event->args[index[1]], curr_event->args[index[2]],
1502 curr_event->args[index[3]], curr_event->args[index[4]],
1503 curr_event->args[index[5]], curr_event->args[index[6]],
1504 curr_event->args[index[7]], curr_event->args[index[8]],
1505 curr_event->args[index[9]]);
1516 __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);