Merge branch 'master' of /home/gregkh/linux/git/torvalds-2.6
[wandboard.git] / arch / s390 / kernel / debug.c
blob0168472b2fdfb66ed122e4ebaabf7a7a2d415efd
1 /*
2 * arch/s390/kernel/debug.c
3 * S/390 debug facility
5 * Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
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>
26 #include <linux/fs.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 */
36 /* typedefs */
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 */
44 /* formated page) */
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;
54 typedef struct
56 char *string;
57 /*
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)
63 long args[0];
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,
97 const char *in_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);
104 /* globals */
106 struct debug_view debug_raw_view = {
107 "raw",
108 NULL,
109 &debug_raw_header_fn,
110 &debug_raw_format_fn,
111 NULL,
112 NULL
115 struct debug_view debug_hex_ascii_view = {
116 "hex_ascii",
117 NULL,
118 &debug_dflt_header_fn,
119 &debug_hex_ascii_format_fn,
120 NULL,
121 NULL
124 static struct debug_view debug_level_view = {
125 "level",
126 &debug_prolog_level_fn,
127 NULL,
128 NULL,
129 &debug_input_level_fn,
130 NULL
133 static struct debug_view debug_pages_view = {
134 "pages",
135 &debug_prolog_pages_fn,
136 NULL,
137 NULL,
138 &debug_input_pages_fn,
139 NULL
142 static struct debug_view debug_flush_view = {
143 "flush",
144 NULL,
145 NULL,
146 NULL,
147 &debug_input_flush_fn,
148 NULL
151 struct debug_view debug_sprintf_view = {
152 "sprintf",
153 NULL,
154 &debug_dflt_header_fn,
155 (debug_format_proc_t*)&debug_sprintf_format_fn,
156 NULL,
157 NULL
160 /* used by dump analysis tools to determine version of debug feature */
161 static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
163 /* static globals */
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,
175 .open = debug_open,
176 .release = debug_close,
179 static struct dentry *debug_debugfs_root_entry;
181 /* functions */
184 * debug_areas_alloc
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;
193 int i,j;
195 areas = kmalloc(nr_areas *
196 sizeof(debug_entry_t**),
197 GFP_KERNEL);
198 if (!areas)
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);
203 if (!areas[i]) {
204 goto fail_malloc_areas2;
206 for(j = 0; j < pages_per_area; j++) {
207 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
208 if(!areas[i][j]) {
209 for(j--; j >=0 ; j--) {
210 kfree(areas[i][j]);
212 kfree(areas[i]);
213 goto fail_malloc_areas2;
217 return areas;
219 fail_malloc_areas2:
220 for(i--; i >= 0; i--){
221 for(j=0; j < pages_per_area;j++){
222 kfree(areas[i][j]);
224 kfree(areas[i]);
226 kfree(areas);
227 fail_malloc_areas:
228 return NULL;
234 * debug_info_alloc
235 * - alloc new debug-info
238 static debug_info_t*
239 debug_info_alloc(const char *name, int pages_per_area, int nr_areas,
240 int buf_size, int level, int mode)
242 debug_info_t* rc;
244 /* alloc everything */
246 rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
247 if(!rc)
248 goto fail_malloc_rc;
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);
257 if(!rc->areas)
258 goto fail_malloc_areas;
259 } else {
260 rc->areas = NULL;
263 /* initialize members */
265 spin_lock_init(&rc->lock);
266 rc->pages_per_area = pages_per_area;
267 rc->nr_areas = nr_areas;
268 rc->active_area = 0;
269 rc->level = level;
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);
278 return rc;
280 fail_malloc_areas:
281 kfree(rc->active_pages);
282 fail_malloc_active_pages:
283 kfree(rc->active_entries);
284 fail_malloc_active_entries:
285 kfree(rc);
286 fail_malloc_rc:
287 return NULL;
291 * debug_areas_free
292 * - free all debug areas
295 static void
296 debug_areas_free(debug_info_t* db_info)
298 int i,j;
300 if(!db_info->areas)
301 return;
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;
313 * debug_info_free
314 * - free memory debug-info
317 static void
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);
322 kfree(db_info);
326 * debug_info_create
327 * - create new debug-info
330 static debug_info_t*
331 debug_info_create(const char *name, int pages_per_area, int nr_areas,
332 int buf_size, mode_t mode)
334 debug_info_t* rc;
336 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
337 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
338 if(!rc)
339 goto out;
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;
351 rc->prev = NULL;
352 } else {
353 /* append element to end of list */
354 debug_area_last->next = rc;
355 rc->prev = debug_area_last;
357 debug_area_last = rc;
358 rc->next = NULL;
360 debug_info_get(rc);
361 out:
362 return rc;
366 * debug_info_copy
367 * - copy debug-info
370 static debug_info_t*
371 debug_info_copy(debug_info_t* in, int mode)
373 int i,j;
374 debug_info_t* rc;
375 unsigned long flags;
377 /* get a consistent copy of the debug areas */
378 do {
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);
382 if(!rc)
383 goto out;
384 /* has something changed in the meantime ? */
385 if((rc->pages_per_area == in->pages_per_area) &&
386 (rc->nr_areas == in->nr_areas)) {
387 break;
389 spin_unlock_irqrestore(&in->lock, flags);
390 debug_info_free(rc);
391 } while (1);
393 if (mode == NO_AREAS)
394 goto out;
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);
401 out:
402 spin_unlock_irqrestore(&in->lock, flags);
403 return rc;
407 * debug_info_get
408 * - increments reference count for debug-info
411 static void
412 debug_info_get(debug_info_t * db_info)
414 if (db_info)
415 atomic_inc(&db_info->ref_count);
419 * debug_info_put:
420 * - decreases reference count for debug-info and frees it if necessary
423 static void
424 debug_info_put(debug_info_t *db_info)
426 int i;
428 if (!db_info)
429 return;
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])
433 continue;
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
452 static int
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;
458 size_t len = 0;
459 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
460 /* print prolog */
461 if (view->prolog_proc)
462 len += view->prolog_proc(id_snap,view,p_info->temp_buf);
463 goto out;
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));
478 out:
479 return len;
483 * debug_next_entry:
484 * - goto next entry in p_info
487 static inline int
488 debug_next_entry(file_private_info_t *p_info)
490 debug_info_t *id;
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;
496 goto out;
498 if(!id->areas)
499 return 1;
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)){
503 /* next page */
504 p_info->act_entry = 0;
505 p_info->act_page += 1;
506 if((p_info->act_page % id->pages_per_area) == 0) {
507 /* next area */
508 p_info->act_area++;
509 p_info->act_page=0;
511 if(p_info->act_area >= id->nr_areas)
512 return 1;
514 out:
515 return 0;
519 * debug_output:
520 * - called for user read()
521 * - copies formated debug entries to the user buffer
524 static ssize_t
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 */
530 size_t count = 0;
531 size_t entry_offset;
532 file_private_info_t *p_info;
534 p_info = ((file_private_info_t *) file->private_data);
535 if (*offset != p_info->offset)
536 return -EPIPE;
537 if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
538 return 0;
539 entry_offset = p_info->act_entry_offset;
540 while(count < len){
541 int formatted_line_size;
542 int formatted_line_residue;
543 int user_buf_residue;
544 size_t copy_size;
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);
550 if(copy_size){
551 if (copy_to_user(user_buf + count, p_info->temp_buf
552 + entry_offset, copy_size))
553 return -EFAULT;
554 count += copy_size;
555 entry_offset += copy_size;
557 if(copy_size == formatted_line_residue){
558 entry_offset = 0;
559 if(debug_next_entry(p_info))
560 goto out;
563 out:
564 p_info->offset = *offset + count;
565 p_info->act_entry_offset = entry_offset;
566 *offset = p_info->offset;
567 return count;
571 * debug_input:
572 * - called for user write()
573 * - calls input function of view
576 static ssize_t
577 debug_input(struct file *file, const char __user *user_buf, size_t length,
578 loff_t *offset)
580 int rc = 0;
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,
588 length, offset);
589 else
590 rc = -EPERM;
591 mutex_unlock(&debug_mutex);
592 return rc; /* number of input characters */
596 * debug_open:
597 * - called for user open()
598 * - copies formated output to private_data area of the file
599 * handle
602 static int
603 debug_open(struct inode *inode, struct file *file)
605 int i, rc = 0;
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])
614 continue;
615 else if (debug_info->debugfs_entries[i] ==
616 file->f_path.dentry) {
617 goto found; /* found view ! */
620 /* no entry found */
621 rc = -EINVAL;
622 goto out;
624 found:
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);
633 } else {
634 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
637 if(!debug_info_snapshot){
638 rc = -ENOMEM;
639 goto out;
641 p_info = kmalloc(sizeof(file_private_info_t),
642 GFP_KERNEL);
643 if(!p_info){
644 debug_info_free(debug_info_snapshot);
645 rc = -ENOMEM;
646 goto out;
648 p_info->offset = 0;
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 out:
659 mutex_unlock(&debug_mutex);
660 return rc;
664 * debug_close:
665 * - called for user close()
666 * - deletes private_data area of the file handle
669 static int
670 debug_close(struct inode *inode, struct file *file)
672 file_private_info_t *p_info;
673 p_info = (file_private_info_t *) file->private_data;
674 if(p_info->debug_info_snap)
675 debug_info_free(p_info->debug_info_snap);
676 debug_info_put(p_info->debug_info_org);
677 kfree(file->private_data);
678 return 0; /* success */
682 * debug_register_mode:
683 * - Creates and initializes debug area for the caller
684 * The mode parameter allows to specify access rights for the s390dbf files
685 * - Returns handle for debug area
688 debug_info_t *debug_register_mode(const char *name, int pages_per_area,
689 int nr_areas, int buf_size, mode_t mode,
690 uid_t uid, gid_t gid)
692 debug_info_t *rc = NULL;
694 /* Since debugfs currently does not support uid/gid other than root, */
695 /* we do not allow gid/uid != 0 until we get support for that. */
696 if ((uid != 0) || (gid != 0))
697 pr_warning("Root becomes the owner of all s390dbf files "
698 "in sysfs\n");
699 BUG_ON(!initialized);
700 mutex_lock(&debug_mutex);
702 /* create new debug_info */
704 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
705 if(!rc)
706 goto out;
707 debug_register_view(rc, &debug_level_view);
708 debug_register_view(rc, &debug_flush_view);
709 debug_register_view(rc, &debug_pages_view);
710 out:
711 if (!rc){
712 pr_err("Registering debug feature %s failed\n", name);
714 mutex_unlock(&debug_mutex);
715 return rc;
717 EXPORT_SYMBOL(debug_register_mode);
720 * debug_register:
721 * - creates and initializes debug area for the caller
722 * - returns handle for debug area
725 debug_info_t *debug_register(const char *name, int pages_per_area,
726 int nr_areas, int buf_size)
728 return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
729 S_IRUSR | S_IWUSR, 0, 0);
733 * debug_unregister:
734 * - give back debug area
737 void
738 debug_unregister(debug_info_t * id)
740 if (!id)
741 goto out;
742 mutex_lock(&debug_mutex);
743 debug_info_put(id);
744 mutex_unlock(&debug_mutex);
746 out:
747 return;
751 * debug_set_size:
752 * - set area size (number of pages) and number of areas
754 static int
755 debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
757 unsigned long flags;
758 debug_entry_t *** new_areas;
759 int rc=0;
761 if(!id || (nr_areas <= 0) || (pages_per_area < 0))
762 return -EINVAL;
763 if(pages_per_area > 0){
764 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
765 if(!new_areas) {
766 pr_info("Allocating memory for %i pages failed\n",
767 pages_per_area);
768 rc = -ENOMEM;
769 goto out;
771 } else {
772 new_areas = NULL;
774 spin_lock_irqsave(&id->lock,flags);
775 debug_areas_free(id);
776 id->areas = new_areas;
777 id->nr_areas = nr_areas;
778 id->pages_per_area = pages_per_area;
779 id->active_area = 0;
780 memset(id->active_entries,0,sizeof(int)*id->nr_areas);
781 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
782 spin_unlock_irqrestore(&id->lock,flags);
783 pr_info("%s: set new size (%i pages)\n" ,id->name, pages_per_area);
784 out:
785 return rc;
789 * debug_set_level:
790 * - set actual debug level
793 void
794 debug_set_level(debug_info_t* id, int new_level)
796 unsigned long flags;
797 if(!id)
798 return;
799 spin_lock_irqsave(&id->lock,flags);
800 if(new_level == DEBUG_OFF_LEVEL){
801 id->level = DEBUG_OFF_LEVEL;
802 pr_info("%s: switched off\n",id->name);
803 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
804 pr_info("%s: level %i is out of range (%i - %i)\n",
805 id->name, new_level, 0, DEBUG_MAX_LEVEL);
806 } else {
807 id->level = new_level;
809 spin_unlock_irqrestore(&id->lock,flags);
814 * proceed_active_entry:
815 * - set active entry to next in the ring buffer
818 static inline void
819 proceed_active_entry(debug_info_t * id)
821 if ((id->active_entries[id->active_area] += id->entry_size)
822 > (PAGE_SIZE - id->entry_size)){
823 id->active_entries[id->active_area] = 0;
824 id->active_pages[id->active_area] =
825 (id->active_pages[id->active_area] + 1) %
826 id->pages_per_area;
831 * proceed_active_area:
832 * - set active area to next in the ring buffer
835 static inline void
836 proceed_active_area(debug_info_t * id)
838 id->active_area++;
839 id->active_area = id->active_area % id->nr_areas;
843 * get_active_entry:
846 static inline debug_entry_t*
847 get_active_entry(debug_info_t * id)
849 return (debug_entry_t *) (((char *) id->areas[id->active_area]
850 [id->active_pages[id->active_area]]) +
851 id->active_entries[id->active_area]);
855 * debug_finish_entry:
856 * - set timestamp, caller address, cpu number etc.
859 static inline void
860 debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
861 int exception)
863 active->id.stck = get_clock();
864 active->id.fields.cpuid = smp_processor_id();
865 active->caller = __builtin_return_address(0);
866 active->id.fields.exception = exception;
867 active->id.fields.level = level;
868 proceed_active_entry(id);
869 if(exception)
870 proceed_active_area(id);
873 static int debug_stoppable=1;
874 static int debug_active=1;
876 #define CTL_S390DBF_STOPPABLE 5678
877 #define CTL_S390DBF_ACTIVE 5679
880 * proc handler for the running debug_active sysctl
881 * always allow read, allow write only if debug_stoppable is set or
882 * if debug_active is already off
884 static int
885 s390dbf_procactive(ctl_table *table, int write,
886 void __user *buffer, size_t *lenp, loff_t *ppos)
888 if (!write || debug_stoppable || !debug_active)
889 return proc_dointvec(table, write, buffer, lenp, ppos);
890 else
891 return 0;
895 static struct ctl_table s390dbf_table[] = {
897 .procname = "debug_stoppable",
898 .data = &debug_stoppable,
899 .maxlen = sizeof(int),
900 .mode = S_IRUGO | S_IWUSR,
901 .proc_handler = proc_dointvec,
904 .procname = "debug_active",
905 .data = &debug_active,
906 .maxlen = sizeof(int),
907 .mode = S_IRUGO | S_IWUSR,
908 .proc_handler = s390dbf_procactive,
913 static struct ctl_table s390dbf_dir_table[] = {
915 .procname = "s390dbf",
916 .maxlen = 0,
917 .mode = S_IRUGO | S_IXUGO,
918 .child = s390dbf_table,
923 static struct ctl_table_header *s390dbf_sysctl_header;
925 void
926 debug_stop_all(void)
928 if (debug_stoppable)
929 debug_active = 0;
934 * debug_event_common:
935 * - write debug entry with given size
938 debug_entry_t*
939 debug_event_common(debug_info_t * id, int level, const void *buf, int len)
941 unsigned long flags;
942 debug_entry_t *active;
944 if (!debug_active || !id->areas)
945 return NULL;
946 spin_lock_irqsave(&id->lock, flags);
947 active = get_active_entry(id);
948 memset(DEBUG_DATA(active), 0, id->buf_size);
949 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
950 debug_finish_entry(id, active, level, 0);
951 spin_unlock_irqrestore(&id->lock, flags);
953 return active;
957 * debug_exception_common:
958 * - write debug entry with given size and switch to next debug area
961 debug_entry_t
962 *debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
964 unsigned long flags;
965 debug_entry_t *active;
967 if (!debug_active || !id->areas)
968 return NULL;
969 spin_lock_irqsave(&id->lock, flags);
970 active = get_active_entry(id);
971 memset(DEBUG_DATA(active), 0, id->buf_size);
972 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
973 debug_finish_entry(id, active, level, 1);
974 spin_unlock_irqrestore(&id->lock, flags);
976 return active;
980 * counts arguments in format string for sprintf view
983 static inline int
984 debug_count_numargs(char *string)
986 int numargs=0;
988 while(*string) {
989 if(*string++=='%')
990 numargs++;
992 return(numargs);
996 * debug_sprintf_event:
999 debug_entry_t*
1000 debug_sprintf_event(debug_info_t* id, int level,char *string,...)
1002 va_list ap;
1003 int numargs,idx;
1004 unsigned long flags;
1005 debug_sprintf_entry_t *curr_event;
1006 debug_entry_t *active;
1008 if((!id) || (level > id->level))
1009 return NULL;
1010 if (!debug_active || !id->areas)
1011 return NULL;
1012 numargs=debug_count_numargs(string);
1014 spin_lock_irqsave(&id->lock, flags);
1015 active = get_active_entry(id);
1016 curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
1017 va_start(ap,string);
1018 curr_event->string=string;
1019 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1020 curr_event->args[idx]=va_arg(ap,long);
1021 va_end(ap);
1022 debug_finish_entry(id, active, level, 0);
1023 spin_unlock_irqrestore(&id->lock, flags);
1025 return active;
1029 * debug_sprintf_exception:
1032 debug_entry_t*
1033 debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
1035 va_list ap;
1036 int numargs,idx;
1037 unsigned long flags;
1038 debug_sprintf_entry_t *curr_event;
1039 debug_entry_t *active;
1041 if((!id) || (level > id->level))
1042 return NULL;
1043 if (!debug_active || !id->areas)
1044 return NULL;
1046 numargs=debug_count_numargs(string);
1048 spin_lock_irqsave(&id->lock, flags);
1049 active = get_active_entry(id);
1050 curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
1051 va_start(ap,string);
1052 curr_event->string=string;
1053 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1054 curr_event->args[idx]=va_arg(ap,long);
1055 va_end(ap);
1056 debug_finish_entry(id, active, level, 1);
1057 spin_unlock_irqrestore(&id->lock, flags);
1059 return active;
1063 * debug_init:
1064 * - is called exactly once to initialize the debug feature
1067 static int
1068 __init debug_init(void)
1070 int rc = 0;
1072 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
1073 mutex_lock(&debug_mutex);
1074 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
1075 initialized = 1;
1076 mutex_unlock(&debug_mutex);
1078 return rc;
1082 * debug_register_view:
1086 debug_register_view(debug_info_t * id, struct debug_view *view)
1088 int rc = 0;
1089 int i;
1090 unsigned long flags;
1091 mode_t mode;
1092 struct dentry *pde;
1094 if (!id)
1095 goto out;
1096 mode = (id->mode | S_IFREG) & ~S_IXUGO;
1097 if (!(view->prolog_proc || view->format_proc || view->header_proc))
1098 mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
1099 if (!view->input_proc)
1100 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1101 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
1102 id , &debug_file_ops);
1103 if (!pde){
1104 pr_err("Registering view %s/%s failed due to out of "
1105 "memory\n", id->name,view->name);
1106 rc = -1;
1107 goto out;
1109 spin_lock_irqsave(&id->lock, flags);
1110 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1111 if (!id->views[i])
1112 break;
1114 if (i == DEBUG_MAX_VIEWS) {
1115 pr_err("Registering view %s/%s would exceed the maximum "
1116 "number of views %i\n", id->name, view->name, i);
1117 debugfs_remove(pde);
1118 rc = -1;
1119 } else {
1120 id->views[i] = view;
1121 id->debugfs_entries[i] = pde;
1123 spin_unlock_irqrestore(&id->lock, flags);
1124 out:
1125 return rc;
1129 * debug_unregister_view:
1133 debug_unregister_view(debug_info_t * id, struct debug_view *view)
1135 int rc = 0;
1136 int i;
1137 unsigned long flags;
1139 if (!id)
1140 goto out;
1141 spin_lock_irqsave(&id->lock, flags);
1142 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1143 if (id->views[i] == view)
1144 break;
1146 if (i == DEBUG_MAX_VIEWS)
1147 rc = -1;
1148 else {
1149 debugfs_remove(id->debugfs_entries[i]);
1150 id->views[i] = NULL;
1152 spin_unlock_irqrestore(&id->lock, flags);
1153 out:
1154 return rc;
1157 static inline char *
1158 debug_get_user_string(const char __user *user_buf, size_t user_len)
1160 char* buffer;
1162 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1163 if (!buffer)
1164 return ERR_PTR(-ENOMEM);
1165 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1166 kfree(buffer);
1167 return ERR_PTR(-EFAULT);
1169 /* got the string, now strip linefeed. */
1170 if (buffer[user_len - 1] == '\n')
1171 buffer[user_len - 1] = 0;
1172 else
1173 buffer[user_len] = 0;
1174 return buffer;
1177 static inline int
1178 debug_get_uint(char *buf)
1180 int rc;
1182 buf = skip_spaces(buf);
1183 rc = simple_strtoul(buf, &buf, 10);
1184 if(*buf){
1185 rc = -EINVAL;
1187 return rc;
1191 * functions for debug-views
1192 ***********************************
1196 * prints out actual debug level
1199 static int
1200 debug_prolog_pages_fn(debug_info_t * id,
1201 struct debug_view *view, char *out_buf)
1203 return sprintf(out_buf, "%i\n", id->pages_per_area);
1207 * reads new size (number of pages per debug area)
1210 static int
1211 debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
1212 struct file *file, const char __user *user_buf,
1213 size_t user_len, loff_t * offset)
1215 char *str;
1216 int rc,new_pages;
1218 if (user_len > 0x10000)
1219 user_len = 0x10000;
1220 if (*offset != 0){
1221 rc = -EPIPE;
1222 goto out;
1224 str = debug_get_user_string(user_buf,user_len);
1225 if(IS_ERR(str)){
1226 rc = PTR_ERR(str);
1227 goto out;
1229 new_pages = debug_get_uint(str);
1230 if(new_pages < 0){
1231 rc = -EINVAL;
1232 goto free_str;
1234 rc = debug_set_size(id,id->nr_areas, new_pages);
1235 if(rc != 0){
1236 rc = -EINVAL;
1237 goto free_str;
1239 rc = user_len;
1240 free_str:
1241 kfree(str);
1242 out:
1243 *offset += user_len;
1244 return rc; /* number of input characters */
1248 * prints out actual debug level
1251 static int
1252 debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
1254 int rc = 0;
1256 if(id->level == DEBUG_OFF_LEVEL) {
1257 rc = sprintf(out_buf,"-\n");
1259 else {
1260 rc = sprintf(out_buf, "%i\n", id->level);
1262 return rc;
1266 * reads new debug level
1269 static int
1270 debug_input_level_fn(debug_info_t * id, struct debug_view *view,
1271 struct file *file, const char __user *user_buf,
1272 size_t user_len, loff_t * offset)
1274 char *str;
1275 int rc,new_level;
1277 if (user_len > 0x10000)
1278 user_len = 0x10000;
1279 if (*offset != 0){
1280 rc = -EPIPE;
1281 goto out;
1283 str = debug_get_user_string(user_buf,user_len);
1284 if(IS_ERR(str)){
1285 rc = PTR_ERR(str);
1286 goto out;
1288 if(str[0] == '-'){
1289 debug_set_level(id, DEBUG_OFF_LEVEL);
1290 rc = user_len;
1291 goto free_str;
1292 } else {
1293 new_level = debug_get_uint(str);
1295 if(new_level < 0) {
1296 pr_warning("%s is not a valid level for a debug "
1297 "feature\n", str);
1298 rc = -EINVAL;
1299 } else {
1300 debug_set_level(id, new_level);
1301 rc = user_len;
1303 free_str:
1304 kfree(str);
1305 out:
1306 *offset += user_len;
1307 return rc; /* number of input characters */
1312 * flushes debug areas
1315 static void debug_flush(debug_info_t* id, int area)
1317 unsigned long flags;
1318 int i,j;
1320 if(!id || !id->areas)
1321 return;
1322 spin_lock_irqsave(&id->lock,flags);
1323 if(area == DEBUG_FLUSH_ALL){
1324 id->active_area = 0;
1325 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1326 for (i = 0; i < id->nr_areas; i++) {
1327 id->active_pages[i] = 0;
1328 for(j = 0; j < id->pages_per_area; j++) {
1329 memset(id->areas[i][j], 0, PAGE_SIZE);
1332 } else if(area >= 0 && area < id->nr_areas) {
1333 id->active_entries[area] = 0;
1334 id->active_pages[area] = 0;
1335 for(i = 0; i < id->pages_per_area; i++) {
1336 memset(id->areas[area][i],0,PAGE_SIZE);
1339 spin_unlock_irqrestore(&id->lock,flags);
1343 * view function: flushes debug areas
1346 static int
1347 debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
1348 struct file *file, const char __user *user_buf,
1349 size_t user_len, loff_t * offset)
1351 char input_buf[1];
1352 int rc = user_len;
1354 if (user_len > 0x10000)
1355 user_len = 0x10000;
1356 if (*offset != 0){
1357 rc = -EPIPE;
1358 goto out;
1360 if (copy_from_user(input_buf, user_buf, 1)){
1361 rc = -EFAULT;
1362 goto out;
1364 if(input_buf[0] == '-') {
1365 debug_flush(id, DEBUG_FLUSH_ALL);
1366 goto out;
1368 if (isdigit(input_buf[0])) {
1369 int area = ((int) input_buf[0] - (int) '0');
1370 debug_flush(id, area);
1371 goto out;
1374 pr_info("Flushing debug data failed because %c is not a valid "
1375 "area\n", input_buf[0]);
1377 out:
1378 *offset += user_len;
1379 return rc; /* number of input characters */
1383 * prints debug header in raw format
1386 static int
1387 debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
1388 int area, debug_entry_t * entry, char *out_buf)
1390 int rc;
1392 rc = sizeof(debug_entry_t);
1393 memcpy(out_buf,entry,sizeof(debug_entry_t));
1394 return rc;
1398 * prints debug data in raw format
1401 static int
1402 debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
1403 char *out_buf, const char *in_buf)
1405 int rc;
1407 rc = id->buf_size;
1408 memcpy(out_buf, in_buf, id->buf_size);
1409 return rc;
1413 * prints debug data in hex/ascii format
1416 static int
1417 debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
1418 char *out_buf, const char *in_buf)
1420 int i, rc = 0;
1422 for (i = 0; i < id->buf_size; i++) {
1423 rc += sprintf(out_buf + rc, "%02x ",
1424 ((unsigned char *) in_buf)[i]);
1426 rc += sprintf(out_buf + rc, "| ");
1427 for (i = 0; i < id->buf_size; i++) {
1428 unsigned char c = in_buf[i];
1429 if (!isprint(c))
1430 rc += sprintf(out_buf + rc, ".");
1431 else
1432 rc += sprintf(out_buf + rc, "%c", c);
1434 rc += sprintf(out_buf + rc, "\n");
1435 return rc;
1439 * prints header for debug entry
1443 debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
1444 int area, debug_entry_t * entry, char *out_buf)
1446 struct timespec time_spec;
1447 char *except_str;
1448 unsigned long caller;
1449 int rc = 0;
1450 unsigned int level;
1452 level = entry->id.fields.level;
1453 stck_to_timespec(entry->id.stck, &time_spec);
1455 if (entry->id.fields.exception)
1456 except_str = "*";
1457 else
1458 except_str = "-";
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);
1463 return rc;
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
1473 static int
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);
1483 if(num_longs < 1)
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);
1489 goto out;
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++)
1498 index[i] = 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]]);
1507 out:
1509 return rc;
1513 * clean up module
1515 static void __exit debug_exit(void)
1517 debugfs_remove(debug_debugfs_root_entry);
1518 unregister_sysctl_table(s390dbf_sysctl_header);
1519 return;
1523 * module definitions
1525 postcore_initcall(debug_init);
1526 module_exit(debug_exit);
1527 MODULE_LICENSE("GPL");
1529 EXPORT_SYMBOL(debug_register);
1530 EXPORT_SYMBOL(debug_unregister);
1531 EXPORT_SYMBOL(debug_set_level);
1532 EXPORT_SYMBOL(debug_stop_all);
1533 EXPORT_SYMBOL(debug_register_view);
1534 EXPORT_SYMBOL(debug_unregister_view);
1535 EXPORT_SYMBOL(debug_event_common);
1536 EXPORT_SYMBOL(debug_exception_common);
1537 EXPORT_SYMBOL(debug_hex_ascii_view);
1538 EXPORT_SYMBOL(debug_raw_view);
1539 EXPORT_SYMBOL(debug_dflt_header_fn);
1540 EXPORT_SYMBOL(debug_sprintf_view);
1541 EXPORT_SYMBOL(debug_sprintf_exception);
1542 EXPORT_SYMBOL(debug_sprintf_event);