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/stddef.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/ctype.h>
18 #include <linux/sysctl.h>
19 #include <asm/uaccess.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
23 #include <linux/debugfs.h>
25 #include <asm/debug.h>
27 #define DEBUG_PROLOG_ENTRY -1
29 #define ALL_AREAS 0 /* copy all debug areas */
30 #define NO_AREAS 1 /* copy no debug areas */
34 typedef struct file_private_info
{
35 loff_t offset
; /* offset of last read in file */
36 int act_area
; /* number of last formated area */
37 int act_page
; /* act page in given area */
38 int act_entry
; /* last formated entry (offset */
39 /* relative to beginning of last */
41 size_t act_entry_offset
; /* up to this offset we copied */
42 /* in last read the last formated */
43 /* entry to userland */
44 char temp_buf
[2048]; /* buffer for output */
45 debug_info_t
*debug_info_org
; /* original debug information */
46 debug_info_t
*debug_info_snap
; /* snapshot of debug information */
47 struct debug_view
*view
; /* used view of debug info */
48 } file_private_info_t
;
54 * This assumes that all args are converted into longs
55 * on L/390 this is the case for all types of parameter
56 * except of floats, and long long (32 bit)
60 } debug_sprintf_entry_t
;
63 extern void tod_to_timeval(uint64_t todval
, struct timespec
*xtime
);
65 /* internal function prototyes */
67 static int debug_init(void);
68 static ssize_t
debug_output(struct file
*file
, char __user
*user_buf
,
69 size_t user_len
, loff_t
* offset
);
70 static ssize_t
debug_input(struct file
*file
, const char __user
*user_buf
,
71 size_t user_len
, loff_t
* offset
);
72 static int debug_open(struct inode
*inode
, struct file
*file
);
73 static int debug_close(struct inode
*inode
, struct file
*file
);
74 static debug_info_t
* debug_info_create(char *name
, int pages_per_area
,
75 int nr_areas
, int buf_size
, mode_t mode
);
76 static void debug_info_get(debug_info_t
*);
77 static void debug_info_put(debug_info_t
*);
78 static int debug_prolog_level_fn(debug_info_t
* id
,
79 struct debug_view
*view
, char *out_buf
);
80 static int debug_input_level_fn(debug_info_t
* id
, struct debug_view
*view
,
81 struct file
*file
, const char __user
*user_buf
,
82 size_t user_buf_size
, loff_t
* offset
);
83 static int debug_prolog_pages_fn(debug_info_t
* id
,
84 struct debug_view
*view
, char *out_buf
);
85 static int debug_input_pages_fn(debug_info_t
* id
, struct debug_view
*view
,
86 struct file
*file
, const char __user
*user_buf
,
87 size_t user_buf_size
, loff_t
* offset
);
88 static int debug_input_flush_fn(debug_info_t
* id
, struct debug_view
*view
,
89 struct file
*file
, const char __user
*user_buf
,
90 size_t user_buf_size
, loff_t
* offset
);
91 static int debug_hex_ascii_format_fn(debug_info_t
* id
, struct debug_view
*view
,
92 char *out_buf
, const char *in_buf
);
93 static int debug_raw_format_fn(debug_info_t
* id
,
94 struct debug_view
*view
, char *out_buf
,
96 static int debug_raw_header_fn(debug_info_t
* id
, struct debug_view
*view
,
97 int area
, debug_entry_t
* entry
, char *out_buf
);
99 static int debug_sprintf_format_fn(debug_info_t
* id
, struct debug_view
*view
,
100 char *out_buf
, debug_sprintf_entry_t
*curr_event
);
104 struct debug_view debug_raw_view
= {
107 &debug_raw_header_fn
,
108 &debug_raw_format_fn
,
113 struct debug_view debug_hex_ascii_view
= {
116 &debug_dflt_header_fn
,
117 &debug_hex_ascii_format_fn
,
122 static struct debug_view debug_level_view
= {
124 &debug_prolog_level_fn
,
127 &debug_input_level_fn
,
131 static struct debug_view debug_pages_view
= {
133 &debug_prolog_pages_fn
,
136 &debug_input_pages_fn
,
140 static struct debug_view debug_flush_view
= {
145 &debug_input_flush_fn
,
149 struct debug_view debug_sprintf_view
= {
152 &debug_dflt_header_fn
,
153 (debug_format_proc_t
*)&debug_sprintf_format_fn
,
158 /* used by dump analysis tools to determine version of debug feature */
159 static unsigned int __used debug_feature_version
= __DEBUG_FEATURE_VERSION
;
163 static debug_info_t
*debug_area_first
= NULL
;
164 static debug_info_t
*debug_area_last
= NULL
;
165 static DEFINE_MUTEX(debug_mutex
);
167 static int initialized
;
169 static const struct file_operations debug_file_ops
= {
170 .owner
= THIS_MODULE
,
171 .read
= debug_output
,
172 .write
= debug_input
,
174 .release
= debug_close
,
177 static struct dentry
*debug_debugfs_root_entry
;
183 * - Debug areas are implemented as a threedimensonal array:
184 * areas[areanumber][pagenumber][pageoffset]
187 static debug_entry_t
***
188 debug_areas_alloc(int pages_per_area
, int nr_areas
)
190 debug_entry_t
*** areas
;
193 areas
= kmalloc(nr_areas
*
194 sizeof(debug_entry_t
**),
197 goto fail_malloc_areas
;
198 for (i
= 0; i
< nr_areas
; i
++) {
199 areas
[i
] = kmalloc(pages_per_area
*
200 sizeof(debug_entry_t
*),GFP_KERNEL
);
202 goto fail_malloc_areas2
;
204 for(j
= 0; j
< pages_per_area
; j
++) {
205 areas
[i
][j
] = kzalloc(PAGE_SIZE
, GFP_KERNEL
);
207 for(j
--; j
>=0 ; j
--) {
211 goto fail_malloc_areas2
;
218 for(i
--; i
>= 0; i
--){
219 for(j
=0; j
< pages_per_area
;j
++){
233 * - alloc new debug-info
237 debug_info_alloc(char *name
, int pages_per_area
, int nr_areas
, int buf_size
,
242 /* alloc everything */
244 rc
= kmalloc(sizeof(debug_info_t
), GFP_KERNEL
);
247 rc
->active_entries
= kcalloc(nr_areas
, sizeof(int), GFP_KERNEL
);
248 if(!rc
->active_entries
)
249 goto fail_malloc_active_entries
;
250 rc
->active_pages
= kcalloc(nr_areas
, sizeof(int), GFP_KERNEL
);
251 if(!rc
->active_pages
)
252 goto fail_malloc_active_pages
;
253 if((mode
== ALL_AREAS
) && (pages_per_area
!= 0)){
254 rc
->areas
= debug_areas_alloc(pages_per_area
, nr_areas
);
256 goto fail_malloc_areas
;
261 /* initialize members */
263 spin_lock_init(&rc
->lock
);
264 rc
->pages_per_area
= pages_per_area
;
265 rc
->nr_areas
= nr_areas
;
268 rc
->buf_size
= buf_size
;
269 rc
->entry_size
= sizeof(debug_entry_t
) + buf_size
;
270 strlcpy(rc
->name
, name
, sizeof(rc
->name
));
271 memset(rc
->views
, 0, DEBUG_MAX_VIEWS
* sizeof(struct debug_view
*));
272 memset(rc
->debugfs_entries
, 0 ,DEBUG_MAX_VIEWS
*
273 sizeof(struct dentry
*));
274 atomic_set(&(rc
->ref_count
), 0);
279 kfree(rc
->active_pages
);
280 fail_malloc_active_pages
:
281 kfree(rc
->active_entries
);
282 fail_malloc_active_entries
:
290 * - free all debug areas
294 debug_areas_free(debug_info_t
* db_info
)
300 for (i
= 0; i
< db_info
->nr_areas
; i
++) {
301 for(j
= 0; j
< db_info
->pages_per_area
; j
++) {
302 kfree(db_info
->areas
[i
][j
]);
304 kfree(db_info
->areas
[i
]);
306 kfree(db_info
->areas
);
307 db_info
->areas
= NULL
;
312 * - free memory debug-info
316 debug_info_free(debug_info_t
* db_info
){
317 debug_areas_free(db_info
);
318 kfree(db_info
->active_entries
);
319 kfree(db_info
->active_pages
);
325 * - create new debug-info
329 debug_info_create(char *name
, int pages_per_area
, int nr_areas
, int buf_size
,
334 rc
= debug_info_alloc(name
, pages_per_area
, nr_areas
, buf_size
,
335 DEBUG_DEFAULT_LEVEL
, ALL_AREAS
);
339 rc
->mode
= mode
& ~S_IFMT
;
341 /* create root directory */
342 rc
->debugfs_root_entry
= debugfs_create_dir(rc
->name
,
343 debug_debugfs_root_entry
);
345 /* append new element to linked list */
346 if (!debug_area_first
) {
347 /* first element in list */
348 debug_area_first
= rc
;
351 /* append element to end of list */
352 debug_area_last
->next
= rc
;
353 rc
->prev
= debug_area_last
;
355 debug_area_last
= rc
;
369 debug_info_copy(debug_info_t
* in
, int mode
)
375 /* get a consistent copy of the debug areas */
377 rc
= debug_info_alloc(in
->name
, in
->pages_per_area
,
378 in
->nr_areas
, in
->buf_size
, in
->level
, mode
);
379 spin_lock_irqsave(&in
->lock
, flags
);
382 /* has something changed in the meantime ? */
383 if((rc
->pages_per_area
== in
->pages_per_area
) &&
384 (rc
->nr_areas
== in
->nr_areas
)) {
387 spin_unlock_irqrestore(&in
->lock
, flags
);
391 if(!rc
|| (mode
== NO_AREAS
))
394 for(i
= 0; i
< in
->nr_areas
; i
++){
395 for(j
= 0; j
< in
->pages_per_area
; j
++) {
396 memcpy(rc
->areas
[i
][j
], in
->areas
[i
][j
],PAGE_SIZE
);
400 spin_unlock_irqrestore(&in
->lock
, flags
);
406 * - increments reference count for debug-info
410 debug_info_get(debug_info_t
* db_info
)
413 atomic_inc(&db_info
->ref_count
);
418 * - decreases reference count for debug-info and frees it if necessary
422 debug_info_put(debug_info_t
*db_info
)
428 if (atomic_dec_and_test(&db_info
->ref_count
)) {
429 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
430 if (!db_info
->views
[i
])
432 debugfs_remove(db_info
->debugfs_entries
[i
]);
434 debugfs_remove(db_info
->debugfs_root_entry
);
435 if(db_info
== debug_area_first
)
436 debug_area_first
= db_info
->next
;
437 if(db_info
== debug_area_last
)
438 debug_area_last
= db_info
->prev
;
439 if(db_info
->prev
) db_info
->prev
->next
= db_info
->next
;
440 if(db_info
->next
) db_info
->next
->prev
= db_info
->prev
;
441 debug_info_free(db_info
);
446 * debug_format_entry:
447 * - format one debug entry and return size of formated data
451 debug_format_entry(file_private_info_t
*p_info
)
453 debug_info_t
*id_snap
= p_info
->debug_info_snap
;
454 struct debug_view
*view
= p_info
->view
;
455 debug_entry_t
*act_entry
;
457 if(p_info
->act_entry
== DEBUG_PROLOG_ENTRY
){
459 if (view
->prolog_proc
)
460 len
+= view
->prolog_proc(id_snap
,view
,p_info
->temp_buf
);
463 if (!id_snap
->areas
) /* this is true, if we have a prolog only view */
464 goto out
; /* or if 'pages_per_area' is 0 */
465 act_entry
= (debug_entry_t
*) ((char*)id_snap
->areas
[p_info
->act_area
]
466 [p_info
->act_page
] + p_info
->act_entry
);
468 if (act_entry
->id
.stck
== 0LL)
469 goto out
; /* empty entry */
470 if (view
->header_proc
)
471 len
+= view
->header_proc(id_snap
, view
, p_info
->act_area
,
472 act_entry
, p_info
->temp_buf
+ len
);
473 if (view
->format_proc
)
474 len
+= view
->format_proc(id_snap
, view
, p_info
->temp_buf
+ len
,
475 DEBUG_DATA(act_entry
));
482 * - goto next entry in p_info
486 debug_next_entry(file_private_info_t
*p_info
)
490 id
= p_info
->debug_info_snap
;
491 if(p_info
->act_entry
== DEBUG_PROLOG_ENTRY
){
492 p_info
->act_entry
= 0;
493 p_info
->act_page
= 0;
498 p_info
->act_entry
+= id
->entry_size
;
499 /* switch to next page, if we reached the end of the page */
500 if (p_info
->act_entry
> (PAGE_SIZE
- id
->entry_size
)){
502 p_info
->act_entry
= 0;
503 p_info
->act_page
+= 1;
504 if((p_info
->act_page
% id
->pages_per_area
) == 0) {
509 if(p_info
->act_area
>= id
->nr_areas
)
518 * - called for user read()
519 * - copies formated debug entries to the user buffer
523 debug_output(struct file
*file
, /* file descriptor */
524 char __user
*user_buf
, /* user buffer */
525 size_t len
, /* length of buffer */
526 loff_t
*offset
) /* offset in the file */
530 file_private_info_t
*p_info
;
532 p_info
= ((file_private_info_t
*) file
->private_data
);
533 if (*offset
!= p_info
->offset
)
535 if(p_info
->act_area
>= p_info
->debug_info_snap
->nr_areas
)
537 entry_offset
= p_info
->act_entry_offset
;
539 int formatted_line_size
;
540 int formatted_line_residue
;
541 int user_buf_residue
;
544 formatted_line_size
= debug_format_entry(p_info
);
545 formatted_line_residue
= formatted_line_size
- entry_offset
;
546 user_buf_residue
= len
-count
;
547 copy_size
= min(user_buf_residue
, formatted_line_residue
);
549 if (copy_to_user(user_buf
+ count
, p_info
->temp_buf
550 + entry_offset
, copy_size
))
553 entry_offset
+= copy_size
;
555 if(copy_size
== formatted_line_residue
){
557 if(debug_next_entry(p_info
))
562 p_info
->offset
= *offset
+ count
;
563 p_info
->act_entry_offset
= entry_offset
;
564 *offset
= p_info
->offset
;
570 * - called for user write()
571 * - calls input function of view
575 debug_input(struct file
*file
, const char __user
*user_buf
, size_t length
,
579 file_private_info_t
*p_info
;
581 mutex_lock(&debug_mutex
);
582 p_info
= ((file_private_info_t
*) file
->private_data
);
583 if (p_info
->view
->input_proc
)
584 rc
= p_info
->view
->input_proc(p_info
->debug_info_org
,
585 p_info
->view
, file
, user_buf
,
589 mutex_unlock(&debug_mutex
);
590 return rc
; /* number of input characters */
595 * - called for user open()
596 * - copies formated output to private_data area of the file
601 debug_open(struct inode
*inode
, struct file
*file
)
604 file_private_info_t
*p_info
;
605 debug_info_t
*debug_info
, *debug_info_snapshot
;
607 mutex_lock(&debug_mutex
);
608 debug_info
= file
->f_path
.dentry
->d_inode
->i_private
;
609 /* find debug view */
610 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
611 if (!debug_info
->views
[i
])
613 else if (debug_info
->debugfs_entries
[i
] ==
614 file
->f_path
.dentry
) {
615 goto found
; /* found view ! */
624 /* Make snapshot of current debug areas to get it consistent. */
625 /* To copy all the areas is only needed, if we have a view which */
626 /* formats the debug areas. */
628 if(!debug_info
->views
[i
]->format_proc
&&
629 !debug_info
->views
[i
]->header_proc
){
630 debug_info_snapshot
= debug_info_copy(debug_info
, NO_AREAS
);
632 debug_info_snapshot
= debug_info_copy(debug_info
, ALL_AREAS
);
635 if(!debug_info_snapshot
){
639 p_info
= kmalloc(sizeof(file_private_info_t
),
642 if(debug_info_snapshot
)
643 debug_info_free(debug_info_snapshot
);
648 p_info
->debug_info_snap
= debug_info_snapshot
;
649 p_info
->debug_info_org
= debug_info
;
650 p_info
->view
= debug_info
->views
[i
];
651 p_info
->act_area
= 0;
652 p_info
->act_page
= 0;
653 p_info
->act_entry
= DEBUG_PROLOG_ENTRY
;
654 p_info
->act_entry_offset
= 0;
655 file
->private_data
= p_info
;
656 debug_info_get(debug_info
);
658 mutex_unlock(&debug_mutex
);
664 * - called for user close()
665 * - deletes private_data area of the file handle
669 debug_close(struct inode
*inode
, struct file
*file
)
671 file_private_info_t
*p_info
;
672 p_info
= (file_private_info_t
*) file
->private_data
;
673 if(p_info
->debug_info_snap
)
674 debug_info_free(p_info
->debug_info_snap
);
675 debug_info_put(p_info
->debug_info_org
);
676 kfree(file
->private_data
);
677 return 0; /* success */
681 * debug_register_mode:
682 * - Creates and initializes debug area for the caller
683 * The mode parameter allows to specify access rights for the s390dbf files
684 * - Returns handle for debug area
687 debug_info_t
*debug_register_mode(char *name
, int pages_per_area
, int nr_areas
,
688 int buf_size
, mode_t mode
, uid_t uid
,
691 debug_info_t
*rc
= NULL
;
693 /* Since debugfs currently does not support uid/gid other than root, */
694 /* we do not allow gid/uid != 0 until we get support for that. */
695 if ((uid
!= 0) || (gid
!= 0))
696 printk(KERN_WARNING
"debug: Warning - Currently only uid/gid "
697 "= 0 are supported. Using root as owner now!");
700 mutex_lock(&debug_mutex
);
702 /* create new debug_info */
704 rc
= debug_info_create(name
, pages_per_area
, nr_areas
, buf_size
, mode
);
707 debug_register_view(rc
, &debug_level_view
);
708 debug_register_view(rc
, &debug_flush_view
);
709 debug_register_view(rc
, &debug_pages_view
);
712 printk(KERN_ERR
"debug: debug_register failed for %s\n",name
);
714 mutex_unlock(&debug_mutex
);
717 EXPORT_SYMBOL(debug_register_mode
);
721 * - creates and initializes debug area for the caller
722 * - returns handle for debug area
725 debug_info_t
*debug_register(char *name
, int pages_per_area
, int nr_areas
,
728 return debug_register_mode(name
, pages_per_area
, nr_areas
, buf_size
,
729 S_IRUSR
| S_IWUSR
, 0, 0);
734 * - give back debug area
738 debug_unregister(debug_info_t
* id
)
742 mutex_lock(&debug_mutex
);
744 mutex_unlock(&debug_mutex
);
752 * - set area size (number of pages) and number of areas
755 debug_set_size(debug_info_t
* id
, int nr_areas
, int pages_per_area
)
758 debug_entry_t
*** new_areas
;
761 if(!id
|| (nr_areas
<= 0) || (pages_per_area
< 0))
763 if(pages_per_area
> 0){
764 new_areas
= debug_areas_alloc(pages_per_area
, nr_areas
);
766 printk(KERN_WARNING
"debug: could not allocate memory "\
767 "for pagenumber: %i\n",pages_per_area
);
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
;
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 printk(KERN_INFO
"debug: %s: set new size (%i pages)\n"\
784 ,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 printk(KERN_INFO
"debug: %s: switched off\n",id
->name
);
804 } else if ((new_level
> DEBUG_MAX_LEVEL
) || (new_level
< 0)) {
806 "debug: %s: level %i is out of range (%i - %i)\n",
807 id
->name
, new_level
, 0, DEBUG_MAX_LEVEL
);
809 id
->level
= new_level
;
811 spin_unlock_irqrestore(&id
->lock
,flags
);
816 * proceed_active_entry:
817 * - set active entry to next in the ring buffer
821 proceed_active_entry(debug_info_t
* id
)
823 if ((id
->active_entries
[id
->active_area
] += id
->entry_size
)
824 > (PAGE_SIZE
- id
->entry_size
)){
825 id
->active_entries
[id
->active_area
] = 0;
826 id
->active_pages
[id
->active_area
] =
827 (id
->active_pages
[id
->active_area
] + 1) %
833 * proceed_active_area:
834 * - set active area to next in the ring buffer
838 proceed_active_area(debug_info_t
* id
)
841 id
->active_area
= id
->active_area
% id
->nr_areas
;
848 static inline debug_entry_t
*
849 get_active_entry(debug_info_t
* id
)
851 return (debug_entry_t
*) (((char *) id
->areas
[id
->active_area
]
852 [id
->active_pages
[id
->active_area
]]) +
853 id
->active_entries
[id
->active_area
]);
857 * debug_finish_entry:
858 * - set timestamp, caller address, cpu number etc.
862 debug_finish_entry(debug_info_t
* id
, debug_entry_t
* active
, int level
,
865 active
->id
.stck
= get_clock();
866 active
->id
.fields
.cpuid
= smp_processor_id();
867 active
->caller
= __builtin_return_address(0);
868 active
->id
.fields
.exception
= exception
;
869 active
->id
.fields
.level
= level
;
870 proceed_active_entry(id
);
872 proceed_active_area(id
);
875 static int debug_stoppable
=1;
876 static int debug_active
=1;
878 #define CTL_S390DBF_STOPPABLE 5678
879 #define CTL_S390DBF_ACTIVE 5679
882 * proc handler for the running debug_active sysctl
883 * always allow read, allow write only if debug_stoppable is set or
884 * if debug_active is already off
887 s390dbf_procactive(ctl_table
*table
, int write
, struct file
*filp
,
888 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
890 if (!write
|| debug_stoppable
|| !debug_active
)
891 return proc_dointvec(table
, write
, filp
, buffer
, lenp
, ppos
);
897 static struct ctl_table s390dbf_table
[] = {
899 .ctl_name
= CTL_S390DBF_STOPPABLE
,
900 .procname
= "debug_stoppable",
901 .data
= &debug_stoppable
,
902 .maxlen
= sizeof(int),
903 .mode
= S_IRUGO
| S_IWUSR
,
904 .proc_handler
= &proc_dointvec
,
905 .strategy
= &sysctl_intvec
,
908 .ctl_name
= CTL_S390DBF_ACTIVE
,
909 .procname
= "debug_active",
910 .data
= &debug_active
,
911 .maxlen
= sizeof(int),
912 .mode
= S_IRUGO
| S_IWUSR
,
913 .proc_handler
= &s390dbf_procactive
,
914 .strategy
= &sysctl_intvec
,
919 static struct ctl_table s390dbf_dir_table
[] = {
921 .ctl_name
= CTL_S390DBF
,
922 .procname
= "s390dbf",
924 .mode
= S_IRUGO
| S_IXUGO
,
925 .child
= s390dbf_table
,
930 static struct ctl_table_header
*s390dbf_sysctl_header
;
941 * debug_event_common:
942 * - write debug entry with given size
946 debug_event_common(debug_info_t
* id
, int level
, const void *buf
, int len
)
949 debug_entry_t
*active
;
951 if (!debug_active
|| !id
->areas
)
953 spin_lock_irqsave(&id
->lock
, flags
);
954 active
= get_active_entry(id
);
955 memset(DEBUG_DATA(active
), 0, id
->buf_size
);
956 memcpy(DEBUG_DATA(active
), buf
, min(len
, id
->buf_size
));
957 debug_finish_entry(id
, active
, level
, 0);
958 spin_unlock_irqrestore(&id
->lock
, flags
);
964 * debug_exception_common:
965 * - write debug entry with given size and switch to next debug area
969 *debug_exception_common(debug_info_t
* id
, int level
, const void *buf
, int len
)
972 debug_entry_t
*active
;
974 if (!debug_active
|| !id
->areas
)
976 spin_lock_irqsave(&id
->lock
, flags
);
977 active
= get_active_entry(id
);
978 memset(DEBUG_DATA(active
), 0, id
->buf_size
);
979 memcpy(DEBUG_DATA(active
), buf
, min(len
, id
->buf_size
));
980 debug_finish_entry(id
, active
, level
, 1);
981 spin_unlock_irqrestore(&id
->lock
, flags
);
987 * counts arguments in format string for sprintf view
991 debug_count_numargs(char *string
)
1003 * debug_sprintf_event:
1007 debug_sprintf_event(debug_info_t
* id
, int level
,char *string
,...)
1011 unsigned long flags
;
1012 debug_sprintf_entry_t
*curr_event
;
1013 debug_entry_t
*active
;
1015 if((!id
) || (level
> id
->level
))
1017 if (!debug_active
|| !id
->areas
)
1019 numargs
=debug_count_numargs(string
);
1021 spin_lock_irqsave(&id
->lock
, flags
);
1022 active
= get_active_entry(id
);
1023 curr_event
=(debug_sprintf_entry_t
*) DEBUG_DATA(active
);
1024 va_start(ap
,string
);
1025 curr_event
->string
=string
;
1026 for(idx
=0;idx
<min(numargs
,(int)(id
->buf_size
/ sizeof(long))-1);idx
++)
1027 curr_event
->args
[idx
]=va_arg(ap
,long);
1029 debug_finish_entry(id
, active
, level
, 0);
1030 spin_unlock_irqrestore(&id
->lock
, flags
);
1036 * debug_sprintf_exception:
1040 debug_sprintf_exception(debug_info_t
* id
, int level
,char *string
,...)
1044 unsigned long flags
;
1045 debug_sprintf_entry_t
*curr_event
;
1046 debug_entry_t
*active
;
1048 if((!id
) || (level
> id
->level
))
1050 if (!debug_active
|| !id
->areas
)
1053 numargs
=debug_count_numargs(string
);
1055 spin_lock_irqsave(&id
->lock
, flags
);
1056 active
= get_active_entry(id
);
1057 curr_event
=(debug_sprintf_entry_t
*)DEBUG_DATA(active
);
1058 va_start(ap
,string
);
1059 curr_event
->string
=string
;
1060 for(idx
=0;idx
<min(numargs
,(int)(id
->buf_size
/ sizeof(long))-1);idx
++)
1061 curr_event
->args
[idx
]=va_arg(ap
,long);
1063 debug_finish_entry(id
, active
, level
, 1);
1064 spin_unlock_irqrestore(&id
->lock
, flags
);
1071 * - is called exactly once to initialize the debug feature
1075 __init
debug_init(void)
1079 s390dbf_sysctl_header
= register_sysctl_table(s390dbf_dir_table
);
1080 mutex_lock(&debug_mutex
);
1081 debug_debugfs_root_entry
= debugfs_create_dir(DEBUG_DIR_ROOT
,NULL
);
1082 printk(KERN_INFO
"debug: Initialization complete\n");
1084 mutex_unlock(&debug_mutex
);
1090 * debug_register_view:
1094 debug_register_view(debug_info_t
* id
, struct debug_view
*view
)
1098 unsigned long flags
;
1104 mode
= (id
->mode
| S_IFREG
) & ~S_IXUGO
;
1105 if (!(view
->prolog_proc
|| view
->format_proc
|| view
->header_proc
))
1106 mode
&= ~(S_IRUSR
| S_IRGRP
| S_IROTH
);
1107 if (!view
->input_proc
)
1108 mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
1109 pde
= debugfs_create_file(view
->name
, mode
, id
->debugfs_root_entry
,
1110 id
, &debug_file_ops
);
1112 printk(KERN_WARNING
"debug: debugfs_create_file() failed!"\
1113 " Cannot register view %s/%s\n", id
->name
,view
->name
);
1117 spin_lock_irqsave(&id
->lock
, flags
);
1118 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
1122 if (i
== DEBUG_MAX_VIEWS
) {
1123 printk(KERN_WARNING
"debug: cannot register view %s/%s\n",
1124 id
->name
,view
->name
);
1126 "debug: maximum number of views reached (%i)!\n", i
);
1127 debugfs_remove(pde
);
1130 id
->views
[i
] = view
;
1131 id
->debugfs_entries
[i
] = pde
;
1133 spin_unlock_irqrestore(&id
->lock
, flags
);
1139 * debug_unregister_view:
1143 debug_unregister_view(debug_info_t
* id
, struct debug_view
*view
)
1147 unsigned long flags
;
1151 spin_lock_irqsave(&id
->lock
, flags
);
1152 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
1153 if (id
->views
[i
] == view
)
1156 if (i
== DEBUG_MAX_VIEWS
)
1159 debugfs_remove(id
->debugfs_entries
[i
]);
1160 id
->views
[i
] = NULL
;
1163 spin_unlock_irqrestore(&id
->lock
, flags
);
1168 static inline char *
1169 debug_get_user_string(const char __user
*user_buf
, size_t user_len
)
1173 buffer
= kmalloc(user_len
+ 1, GFP_KERNEL
);
1175 return ERR_PTR(-ENOMEM
);
1176 if (copy_from_user(buffer
, user_buf
, user_len
) != 0) {
1178 return ERR_PTR(-EFAULT
);
1180 /* got the string, now strip linefeed. */
1181 if (buffer
[user_len
- 1] == '\n')
1182 buffer
[user_len
- 1] = 0;
1184 buffer
[user_len
] = 0;
1189 debug_get_uint(char *buf
)
1193 for(; isspace(*buf
); buf
++);
1194 rc
= simple_strtoul(buf
, &buf
, 10);
1196 printk("debug: no integer specified!\n");
1203 * functions for debug-views
1204 ***********************************
1208 * prints out actual debug level
1212 debug_prolog_pages_fn(debug_info_t
* id
,
1213 struct debug_view
*view
, char *out_buf
)
1215 return sprintf(out_buf
, "%i\n", id
->pages_per_area
);
1219 * reads new size (number of pages per debug area)
1223 debug_input_pages_fn(debug_info_t
* id
, struct debug_view
*view
,
1224 struct file
*file
, const char __user
*user_buf
,
1225 size_t user_len
, loff_t
* offset
)
1230 if (user_len
> 0x10000)
1236 str
= debug_get_user_string(user_buf
,user_len
);
1241 new_pages
= debug_get_uint(str
);
1246 rc
= debug_set_size(id
,id
->nr_areas
, new_pages
);
1255 *offset
+= user_len
;
1256 return rc
; /* number of input characters */
1260 * prints out actual debug level
1264 debug_prolog_level_fn(debug_info_t
* id
, struct debug_view
*view
, char *out_buf
)
1268 if(id
->level
== DEBUG_OFF_LEVEL
) {
1269 rc
= sprintf(out_buf
,"-\n");
1272 rc
= sprintf(out_buf
, "%i\n", id
->level
);
1278 * reads new debug level
1282 debug_input_level_fn(debug_info_t
* id
, struct debug_view
*view
,
1283 struct file
*file
, const char __user
*user_buf
,
1284 size_t user_len
, loff_t
* offset
)
1289 if (user_len
> 0x10000)
1295 str
= debug_get_user_string(user_buf
,user_len
);
1301 debug_set_level(id
, DEBUG_OFF_LEVEL
);
1305 new_level
= debug_get_uint(str
);
1308 printk(KERN_INFO
"debug: level `%s` is not valid\n", str
);
1311 debug_set_level(id
, new_level
);
1317 *offset
+= user_len
;
1318 return rc
; /* number of input characters */
1323 * flushes debug areas
1326 static void debug_flush(debug_info_t
* id
, int area
)
1328 unsigned long flags
;
1331 if(!id
|| !id
->areas
)
1333 spin_lock_irqsave(&id
->lock
,flags
);
1334 if(area
== DEBUG_FLUSH_ALL
){
1335 id
->active_area
= 0;
1336 memset(id
->active_entries
, 0, id
->nr_areas
* sizeof(int));
1337 for (i
= 0; i
< id
->nr_areas
; i
++) {
1338 id
->active_pages
[i
] = 0;
1339 for(j
= 0; j
< id
->pages_per_area
; j
++) {
1340 memset(id
->areas
[i
][j
], 0, PAGE_SIZE
);
1343 printk(KERN_INFO
"debug: %s: all areas flushed\n",id
->name
);
1344 } else if(area
>= 0 && area
< id
->nr_areas
) {
1345 id
->active_entries
[area
] = 0;
1346 id
->active_pages
[area
] = 0;
1347 for(i
= 0; i
< id
->pages_per_area
; i
++) {
1348 memset(id
->areas
[area
][i
],0,PAGE_SIZE
);
1350 printk(KERN_INFO
"debug: %s: area %i has been flushed\n",
1354 "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
1355 id
->name
, area
, 0, id
->nr_areas
-1);
1357 spin_unlock_irqrestore(&id
->lock
,flags
);
1361 * view function: flushes debug areas
1365 debug_input_flush_fn(debug_info_t
* id
, struct debug_view
*view
,
1366 struct file
*file
, const char __user
*user_buf
,
1367 size_t user_len
, loff_t
* offset
)
1372 if (user_len
> 0x10000)
1378 if (copy_from_user(input_buf
, user_buf
, 1)){
1382 if(input_buf
[0] == '-') {
1383 debug_flush(id
, DEBUG_FLUSH_ALL
);
1386 if (isdigit(input_buf
[0])) {
1387 int area
= ((int) input_buf
[0] - (int) '0');
1388 debug_flush(id
, area
);
1392 printk(KERN_INFO
"debug: area `%c` is not valid\n", input_buf
[0]);
1395 *offset
+= user_len
;
1396 return rc
; /* number of input characters */
1400 * prints debug header in raw format
1404 debug_raw_header_fn(debug_info_t
* id
, struct debug_view
*view
,
1405 int area
, debug_entry_t
* entry
, char *out_buf
)
1409 rc
= sizeof(debug_entry_t
);
1410 memcpy(out_buf
,entry
,sizeof(debug_entry_t
));
1415 * prints debug data in raw format
1419 debug_raw_format_fn(debug_info_t
* id
, struct debug_view
*view
,
1420 char *out_buf
, const char *in_buf
)
1425 memcpy(out_buf
, in_buf
, id
->buf_size
);
1430 * prints debug data in hex/ascii format
1434 debug_hex_ascii_format_fn(debug_info_t
* id
, struct debug_view
*view
,
1435 char *out_buf
, const char *in_buf
)
1439 for (i
= 0; i
< id
->buf_size
; i
++) {
1440 rc
+= sprintf(out_buf
+ rc
, "%02x ",
1441 ((unsigned char *) in_buf
)[i
]);
1443 rc
+= sprintf(out_buf
+ rc
, "| ");
1444 for (i
= 0; i
< id
->buf_size
; i
++) {
1445 unsigned char c
= in_buf
[i
];
1447 rc
+= sprintf(out_buf
+ rc
, ".");
1449 rc
+= sprintf(out_buf
+ rc
, "%c", c
);
1451 rc
+= sprintf(out_buf
+ rc
, "\n");
1456 * prints header for debug entry
1460 debug_dflt_header_fn(debug_info_t
* id
, struct debug_view
*view
,
1461 int area
, debug_entry_t
* entry
, char *out_buf
)
1463 struct timespec time_spec
;
1464 unsigned long long time
;
1466 unsigned long caller
;
1470 level
= entry
->id
.fields
.level
;
1471 time
= entry
->id
.stck
;
1472 /* adjust todclock to 1970 */
1473 time
-= 0x8126d60e46000000LL
- (0x3c26700LL
* 1000000 * 4096);
1474 tod_to_timeval(time
, &time_spec
);
1476 if (entry
->id
.fields
.exception
)
1480 caller
= ((unsigned long) entry
->caller
) & PSW_ADDR_INSN
;
1481 rc
+= sprintf(out_buf
, "%02i %011lu:%06lu %1u %1s %02i %p ",
1482 area
, time_spec
.tv_sec
, time_spec
.tv_nsec
/ 1000, level
,
1483 except_str
, entry
->id
.fields
.cpuid
, (void *) caller
);
1488 * prints debug data sprintf-formated:
1489 * debug_sprinf_event/exception calls must be used together with this view
1492 #define DEBUG_SPRINTF_MAX_ARGS 10
1495 debug_sprintf_format_fn(debug_info_t
* id
, struct debug_view
*view
,
1496 char *out_buf
, debug_sprintf_entry_t
*curr_event
)
1498 int num_longs
, num_used_args
= 0,i
, rc
= 0;
1499 int index
[DEBUG_SPRINTF_MAX_ARGS
];
1501 /* count of longs fit into one entry */
1502 num_longs
= id
->buf_size
/ sizeof(long);
1505 goto out
; /* bufsize of entry too small */
1506 if(num_longs
== 1) {
1507 /* no args, we use only the string */
1508 strcpy(out_buf
, curr_event
->string
);
1509 rc
= strlen(curr_event
->string
);
1513 /* number of arguments used for sprintf (without the format string) */
1514 num_used_args
= min(DEBUG_SPRINTF_MAX_ARGS
, (num_longs
- 1));
1516 memset(index
,0, DEBUG_SPRINTF_MAX_ARGS
* sizeof(int));
1518 for(i
= 0; i
< num_used_args
; i
++)
1521 rc
= sprintf(out_buf
, curr_event
->string
, curr_event
->args
[index
[0]],
1522 curr_event
->args
[index
[1]], curr_event
->args
[index
[2]],
1523 curr_event
->args
[index
[3]], curr_event
->args
[index
[4]],
1524 curr_event
->args
[index
[5]], curr_event
->args
[index
[6]],
1525 curr_event
->args
[index
[7]], curr_event
->args
[index
[8]],
1526 curr_event
->args
[index
[9]]);
1536 static void __exit
debug_exit(void)
1538 debugfs_remove(debug_debugfs_root_entry
);
1539 unregister_sysctl_table(s390dbf_sysctl_header
);
1544 * module definitions
1546 postcore_initcall(debug_init
);
1547 module_exit(debug_exit
);
1548 MODULE_LICENSE("GPL");
1550 EXPORT_SYMBOL(debug_register
);
1551 EXPORT_SYMBOL(debug_unregister
);
1552 EXPORT_SYMBOL(debug_set_level
);
1553 EXPORT_SYMBOL(debug_stop_all
);
1554 EXPORT_SYMBOL(debug_register_view
);
1555 EXPORT_SYMBOL(debug_unregister_view
);
1556 EXPORT_SYMBOL(debug_event_common
);
1557 EXPORT_SYMBOL(debug_exception_common
);
1558 EXPORT_SYMBOL(debug_hex_ascii_view
);
1559 EXPORT_SYMBOL(debug_raw_view
);
1560 EXPORT_SYMBOL(debug_dflt_header_fn
);
1561 EXPORT_SYMBOL(debug_sprintf_view
);
1562 EXPORT_SYMBOL(debug_sprintf_exception
);
1563 EXPORT_SYMBOL(debug_sprintf_event
);