2 * arch/s390/kernel/debug.c
5 * Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Michael Holzheu (holzheu@de.ibm.com),
8 * Holger Smolinski (Holger.Smolinski@de.ibm.com)
10 * Bugreports to: <Linux390@de.ibm.com>
13 #define KMSG_COMPONENT "s390dbf"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/stddef.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/ctype.h>
21 #include <linux/sysctl.h>
22 #include <asm/uaccess.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
26 #include <linux/debugfs.h>
28 #include <asm/debug.h>
30 #define DEBUG_PROLOG_ENTRY -1
32 #define ALL_AREAS 0 /* copy all debug areas */
33 #define NO_AREAS 1 /* copy no debug areas */
37 typedef struct file_private_info
{
38 loff_t offset
; /* offset of last read in file */
39 int act_area
; /* number of last formated area */
40 int act_page
; /* act page in given area */
41 int act_entry
; /* last formated entry (offset */
42 /* relative to beginning of last */
44 size_t act_entry_offset
; /* up to this offset we copied */
45 /* in last read the last formated */
46 /* entry to userland */
47 char temp_buf
[2048]; /* buffer for output */
48 debug_info_t
*debug_info_org
; /* original debug information */
49 debug_info_t
*debug_info_snap
; /* snapshot of debug information */
50 struct debug_view
*view
; /* used view of debug info */
51 } file_private_info_t
;
57 * This assumes that all args are converted into longs
58 * on L/390 this is the case for all types of parameter
59 * except of floats, and long long (32 bit)
63 } debug_sprintf_entry_t
;
66 /* internal function prototyes */
68 static int debug_init(void);
69 static ssize_t
debug_output(struct file
*file
, char __user
*user_buf
,
70 size_t user_len
, loff_t
* offset
);
71 static ssize_t
debug_input(struct file
*file
, const char __user
*user_buf
,
72 size_t user_len
, loff_t
* offset
);
73 static int debug_open(struct inode
*inode
, struct file
*file
);
74 static int debug_close(struct inode
*inode
, struct file
*file
);
75 static debug_info_t
*debug_info_create(const char *name
, int pages_per_area
,
76 int nr_areas
, int buf_size
, mode_t mode
);
77 static void debug_info_get(debug_info_t
*);
78 static void debug_info_put(debug_info_t
*);
79 static int debug_prolog_level_fn(debug_info_t
* id
,
80 struct debug_view
*view
, char *out_buf
);
81 static int debug_input_level_fn(debug_info_t
* id
, struct debug_view
*view
,
82 struct file
*file
, const char __user
*user_buf
,
83 size_t user_buf_size
, loff_t
* offset
);
84 static int debug_prolog_pages_fn(debug_info_t
* id
,
85 struct debug_view
*view
, char *out_buf
);
86 static int debug_input_pages_fn(debug_info_t
* id
, struct debug_view
*view
,
87 struct file
*file
, const char __user
*user_buf
,
88 size_t user_buf_size
, loff_t
* offset
);
89 static int debug_input_flush_fn(debug_info_t
* id
, struct debug_view
*view
,
90 struct file
*file
, const char __user
*user_buf
,
91 size_t user_buf_size
, loff_t
* offset
);
92 static int debug_hex_ascii_format_fn(debug_info_t
* id
, struct debug_view
*view
,
93 char *out_buf
, const char *in_buf
);
94 static int debug_raw_format_fn(debug_info_t
* id
,
95 struct debug_view
*view
, char *out_buf
,
97 static int debug_raw_header_fn(debug_info_t
* id
, struct debug_view
*view
,
98 int area
, debug_entry_t
* entry
, char *out_buf
);
100 static int debug_sprintf_format_fn(debug_info_t
* id
, struct debug_view
*view
,
101 char *out_buf
, debug_sprintf_entry_t
*curr_event
);
105 struct debug_view debug_raw_view
= {
108 &debug_raw_header_fn
,
109 &debug_raw_format_fn
,
114 struct debug_view debug_hex_ascii_view
= {
117 &debug_dflt_header_fn
,
118 &debug_hex_ascii_format_fn
,
123 static struct debug_view debug_level_view
= {
125 &debug_prolog_level_fn
,
128 &debug_input_level_fn
,
132 static struct debug_view debug_pages_view
= {
134 &debug_prolog_pages_fn
,
137 &debug_input_pages_fn
,
141 static struct debug_view debug_flush_view
= {
146 &debug_input_flush_fn
,
150 struct debug_view debug_sprintf_view
= {
153 &debug_dflt_header_fn
,
154 (debug_format_proc_t
*)&debug_sprintf_format_fn
,
159 /* used by dump analysis tools to determine version of debug feature */
160 static unsigned int __used debug_feature_version
= __DEBUG_FEATURE_VERSION
;
164 static debug_info_t
*debug_area_first
= NULL
;
165 static debug_info_t
*debug_area_last
= NULL
;
166 static DEFINE_MUTEX(debug_mutex
);
168 static int initialized
;
170 static const struct file_operations debug_file_ops
= {
171 .owner
= THIS_MODULE
,
172 .read
= debug_output
,
173 .write
= debug_input
,
175 .release
= debug_close
,
178 static struct dentry
*debug_debugfs_root_entry
;
184 * - Debug areas are implemented as a threedimensonal array:
185 * areas[areanumber][pagenumber][pageoffset]
188 static debug_entry_t
***
189 debug_areas_alloc(int pages_per_area
, int nr_areas
)
191 debug_entry_t
*** areas
;
194 areas
= kmalloc(nr_areas
*
195 sizeof(debug_entry_t
**),
198 goto fail_malloc_areas
;
199 for (i
= 0; i
< nr_areas
; i
++) {
200 areas
[i
] = kmalloc(pages_per_area
*
201 sizeof(debug_entry_t
*),GFP_KERNEL
);
203 goto fail_malloc_areas2
;
205 for(j
= 0; j
< pages_per_area
; j
++) {
206 areas
[i
][j
] = kzalloc(PAGE_SIZE
, GFP_KERNEL
);
208 for(j
--; j
>=0 ; j
--) {
212 goto fail_malloc_areas2
;
219 for(i
--; i
>= 0; i
--){
220 for(j
=0; j
< pages_per_area
;j
++){
234 * - alloc new debug-info
238 debug_info_alloc(const char *name
, int pages_per_area
, int nr_areas
,
239 int buf_size
, int level
, int mode
)
243 /* alloc everything */
245 rc
= kmalloc(sizeof(debug_info_t
), GFP_KERNEL
);
248 rc
->active_entries
= kcalloc(nr_areas
, sizeof(int), GFP_KERNEL
);
249 if(!rc
->active_entries
)
250 goto fail_malloc_active_entries
;
251 rc
->active_pages
= kcalloc(nr_areas
, sizeof(int), GFP_KERNEL
);
252 if(!rc
->active_pages
)
253 goto fail_malloc_active_pages
;
254 if((mode
== ALL_AREAS
) && (pages_per_area
!= 0)){
255 rc
->areas
= debug_areas_alloc(pages_per_area
, nr_areas
);
257 goto fail_malloc_areas
;
262 /* initialize members */
264 spin_lock_init(&rc
->lock
);
265 rc
->pages_per_area
= pages_per_area
;
266 rc
->nr_areas
= nr_areas
;
269 rc
->buf_size
= buf_size
;
270 rc
->entry_size
= sizeof(debug_entry_t
) + buf_size
;
271 strlcpy(rc
->name
, name
, sizeof(rc
->name
));
272 memset(rc
->views
, 0, DEBUG_MAX_VIEWS
* sizeof(struct debug_view
*));
273 memset(rc
->debugfs_entries
, 0 ,DEBUG_MAX_VIEWS
*
274 sizeof(struct dentry
*));
275 atomic_set(&(rc
->ref_count
), 0);
280 kfree(rc
->active_pages
);
281 fail_malloc_active_pages
:
282 kfree(rc
->active_entries
);
283 fail_malloc_active_entries
:
291 * - free all debug areas
295 debug_areas_free(debug_info_t
* db_info
)
301 for (i
= 0; i
< db_info
->nr_areas
; i
++) {
302 for(j
= 0; j
< db_info
->pages_per_area
; j
++) {
303 kfree(db_info
->areas
[i
][j
]);
305 kfree(db_info
->areas
[i
]);
307 kfree(db_info
->areas
);
308 db_info
->areas
= NULL
;
313 * - free memory debug-info
317 debug_info_free(debug_info_t
* db_info
){
318 debug_areas_free(db_info
);
319 kfree(db_info
->active_entries
);
320 kfree(db_info
->active_pages
);
326 * - create new debug-info
330 debug_info_create(const char *name
, int pages_per_area
, int nr_areas
,
331 int buf_size
, mode_t mode
)
335 rc
= debug_info_alloc(name
, pages_per_area
, nr_areas
, buf_size
,
336 DEBUG_DEFAULT_LEVEL
, ALL_AREAS
);
340 rc
->mode
= mode
& ~S_IFMT
;
342 /* create root directory */
343 rc
->debugfs_root_entry
= debugfs_create_dir(rc
->name
,
344 debug_debugfs_root_entry
);
346 /* append new element to linked list */
347 if (!debug_area_first
) {
348 /* first element in list */
349 debug_area_first
= rc
;
352 /* append element to end of list */
353 debug_area_last
->next
= rc
;
354 rc
->prev
= debug_area_last
;
356 debug_area_last
= rc
;
370 debug_info_copy(debug_info_t
* in
, int mode
)
376 /* get a consistent copy of the debug areas */
378 rc
= debug_info_alloc(in
->name
, in
->pages_per_area
,
379 in
->nr_areas
, in
->buf_size
, in
->level
, mode
);
380 spin_lock_irqsave(&in
->lock
, flags
);
383 /* has something changed in the meantime ? */
384 if((rc
->pages_per_area
== in
->pages_per_area
) &&
385 (rc
->nr_areas
== in
->nr_areas
)) {
388 spin_unlock_irqrestore(&in
->lock
, flags
);
392 if (mode
== NO_AREAS
)
395 for(i
= 0; i
< in
->nr_areas
; i
++){
396 for(j
= 0; j
< in
->pages_per_area
; j
++) {
397 memcpy(rc
->areas
[i
][j
], in
->areas
[i
][j
],PAGE_SIZE
);
401 spin_unlock_irqrestore(&in
->lock
, flags
);
407 * - increments reference count for debug-info
411 debug_info_get(debug_info_t
* db_info
)
414 atomic_inc(&db_info
->ref_count
);
419 * - decreases reference count for debug-info and frees it if necessary
423 debug_info_put(debug_info_t
*db_info
)
429 if (atomic_dec_and_test(&db_info
->ref_count
)) {
430 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
431 if (!db_info
->views
[i
])
433 debugfs_remove(db_info
->debugfs_entries
[i
]);
435 debugfs_remove(db_info
->debugfs_root_entry
);
436 if(db_info
== debug_area_first
)
437 debug_area_first
= db_info
->next
;
438 if(db_info
== debug_area_last
)
439 debug_area_last
= db_info
->prev
;
440 if(db_info
->prev
) db_info
->prev
->next
= db_info
->next
;
441 if(db_info
->next
) db_info
->next
->prev
= db_info
->prev
;
442 debug_info_free(db_info
);
447 * debug_format_entry:
448 * - format one debug entry and return size of formated data
452 debug_format_entry(file_private_info_t
*p_info
)
454 debug_info_t
*id_snap
= p_info
->debug_info_snap
;
455 struct debug_view
*view
= p_info
->view
;
456 debug_entry_t
*act_entry
;
458 if(p_info
->act_entry
== DEBUG_PROLOG_ENTRY
){
460 if (view
->prolog_proc
)
461 len
+= view
->prolog_proc(id_snap
,view
,p_info
->temp_buf
);
464 if (!id_snap
->areas
) /* this is true, if we have a prolog only view */
465 goto out
; /* or if 'pages_per_area' is 0 */
466 act_entry
= (debug_entry_t
*) ((char*)id_snap
->areas
[p_info
->act_area
]
467 [p_info
->act_page
] + p_info
->act_entry
);
469 if (act_entry
->id
.stck
== 0LL)
470 goto out
; /* empty entry */
471 if (view
->header_proc
)
472 len
+= view
->header_proc(id_snap
, view
, p_info
->act_area
,
473 act_entry
, p_info
->temp_buf
+ len
);
474 if (view
->format_proc
)
475 len
+= view
->format_proc(id_snap
, view
, p_info
->temp_buf
+ len
,
476 DEBUG_DATA(act_entry
));
483 * - goto next entry in p_info
487 debug_next_entry(file_private_info_t
*p_info
)
491 id
= p_info
->debug_info_snap
;
492 if(p_info
->act_entry
== DEBUG_PROLOG_ENTRY
){
493 p_info
->act_entry
= 0;
494 p_info
->act_page
= 0;
499 p_info
->act_entry
+= id
->entry_size
;
500 /* switch to next page, if we reached the end of the page */
501 if (p_info
->act_entry
> (PAGE_SIZE
- id
->entry_size
)){
503 p_info
->act_entry
= 0;
504 p_info
->act_page
+= 1;
505 if((p_info
->act_page
% id
->pages_per_area
) == 0) {
510 if(p_info
->act_area
>= id
->nr_areas
)
519 * - called for user read()
520 * - copies formated debug entries to the user buffer
524 debug_output(struct file
*file
, /* file descriptor */
525 char __user
*user_buf
, /* user buffer */
526 size_t len
, /* length of buffer */
527 loff_t
*offset
) /* offset in the file */
531 file_private_info_t
*p_info
;
533 p_info
= ((file_private_info_t
*) file
->private_data
);
534 if (*offset
!= p_info
->offset
)
536 if(p_info
->act_area
>= p_info
->debug_info_snap
->nr_areas
)
538 entry_offset
= p_info
->act_entry_offset
;
540 int formatted_line_size
;
541 int formatted_line_residue
;
542 int user_buf_residue
;
545 formatted_line_size
= debug_format_entry(p_info
);
546 formatted_line_residue
= formatted_line_size
- entry_offset
;
547 user_buf_residue
= len
-count
;
548 copy_size
= min(user_buf_residue
, formatted_line_residue
);
550 if (copy_to_user(user_buf
+ count
, p_info
->temp_buf
551 + entry_offset
, copy_size
))
554 entry_offset
+= copy_size
;
556 if(copy_size
== formatted_line_residue
){
558 if(debug_next_entry(p_info
))
563 p_info
->offset
= *offset
+ count
;
564 p_info
->act_entry_offset
= entry_offset
;
565 *offset
= p_info
->offset
;
571 * - called for user write()
572 * - calls input function of view
576 debug_input(struct file
*file
, const char __user
*user_buf
, size_t length
,
580 file_private_info_t
*p_info
;
582 mutex_lock(&debug_mutex
);
583 p_info
= ((file_private_info_t
*) file
->private_data
);
584 if (p_info
->view
->input_proc
)
585 rc
= p_info
->view
->input_proc(p_info
->debug_info_org
,
586 p_info
->view
, file
, user_buf
,
590 mutex_unlock(&debug_mutex
);
591 return rc
; /* number of input characters */
596 * - called for user open()
597 * - copies formated output to private_data area of the file
602 debug_open(struct inode
*inode
, struct file
*file
)
605 file_private_info_t
*p_info
;
606 debug_info_t
*debug_info
, *debug_info_snapshot
;
608 mutex_lock(&debug_mutex
);
609 debug_info
= file
->f_path
.dentry
->d_inode
->i_private
;
610 /* find debug view */
611 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
612 if (!debug_info
->views
[i
])
614 else if (debug_info
->debugfs_entries
[i
] ==
615 file
->f_path
.dentry
) {
616 goto found
; /* found view ! */
625 /* Make snapshot of current debug areas to get it consistent. */
626 /* To copy all the areas is only needed, if we have a view which */
627 /* formats the debug areas. */
629 if(!debug_info
->views
[i
]->format_proc
&&
630 !debug_info
->views
[i
]->header_proc
){
631 debug_info_snapshot
= debug_info_copy(debug_info
, NO_AREAS
);
633 debug_info_snapshot
= debug_info_copy(debug_info
, ALL_AREAS
);
636 if(!debug_info_snapshot
){
640 p_info
= kmalloc(sizeof(file_private_info_t
),
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(const char *name
, int pages_per_area
,
688 int nr_areas
, int buf_size
, mode_t mode
,
689 uid_t uid
, gid_t gid
)
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 pr_warning("Root becomes the owner of all s390dbf files "
698 BUG_ON(!initialized
);
699 mutex_lock(&debug_mutex
);
701 /* create new debug_info */
703 rc
= debug_info_create(name
, pages_per_area
, nr_areas
, buf_size
, mode
);
706 debug_register_view(rc
, &debug_level_view
);
707 debug_register_view(rc
, &debug_flush_view
);
708 debug_register_view(rc
, &debug_pages_view
);
711 pr_err("Registering debug feature %s failed\n", name
);
713 mutex_unlock(&debug_mutex
);
716 EXPORT_SYMBOL(debug_register_mode
);
720 * - creates and initializes debug area for the caller
721 * - returns handle for debug area
724 debug_info_t
*debug_register(const char *name
, int pages_per_area
,
725 int nr_areas
, int buf_size
)
727 return debug_register_mode(name
, pages_per_area
, nr_areas
, buf_size
,
728 S_IRUSR
| S_IWUSR
, 0, 0);
733 * - give back debug area
737 debug_unregister(debug_info_t
* id
)
741 mutex_lock(&debug_mutex
);
743 mutex_unlock(&debug_mutex
);
751 * - set area size (number of pages) and number of areas
754 debug_set_size(debug_info_t
* id
, int nr_areas
, int pages_per_area
)
757 debug_entry_t
*** new_areas
;
760 if(!id
|| (nr_areas
<= 0) || (pages_per_area
< 0))
762 if(pages_per_area
> 0){
763 new_areas
= debug_areas_alloc(pages_per_area
, nr_areas
);
765 pr_info("Allocating memory for %i pages failed\n",
773 spin_lock_irqsave(&id
->lock
,flags
);
774 debug_areas_free(id
);
775 id
->areas
= new_areas
;
776 id
->nr_areas
= nr_areas
;
777 id
->pages_per_area
= pages_per_area
;
779 memset(id
->active_entries
,0,sizeof(int)*id
->nr_areas
);
780 memset(id
->active_pages
, 0, sizeof(int)*id
->nr_areas
);
781 spin_unlock_irqrestore(&id
->lock
,flags
);
782 pr_info("%s: set new size (%i pages)\n" ,id
->name
, pages_per_area
);
789 * - set actual debug level
793 debug_set_level(debug_info_t
* id
, int new_level
)
798 spin_lock_irqsave(&id
->lock
,flags
);
799 if(new_level
== DEBUG_OFF_LEVEL
){
800 id
->level
= DEBUG_OFF_LEVEL
;
801 pr_info("%s: switched off\n",id
->name
);
802 } else if ((new_level
> DEBUG_MAX_LEVEL
) || (new_level
< 0)) {
803 pr_info("%s: level %i is out of range (%i - %i)\n",
804 id
->name
, new_level
, 0, DEBUG_MAX_LEVEL
);
806 id
->level
= new_level
;
808 spin_unlock_irqrestore(&id
->lock
,flags
);
813 * proceed_active_entry:
814 * - set active entry to next in the ring buffer
818 proceed_active_entry(debug_info_t
* id
)
820 if ((id
->active_entries
[id
->active_area
] += id
->entry_size
)
821 > (PAGE_SIZE
- id
->entry_size
)){
822 id
->active_entries
[id
->active_area
] = 0;
823 id
->active_pages
[id
->active_area
] =
824 (id
->active_pages
[id
->active_area
] + 1) %
830 * proceed_active_area:
831 * - set active area to next in the ring buffer
835 proceed_active_area(debug_info_t
* id
)
838 id
->active_area
= id
->active_area
% id
->nr_areas
;
845 static inline debug_entry_t
*
846 get_active_entry(debug_info_t
* id
)
848 return (debug_entry_t
*) (((char *) id
->areas
[id
->active_area
]
849 [id
->active_pages
[id
->active_area
]]) +
850 id
->active_entries
[id
->active_area
]);
854 * debug_finish_entry:
855 * - set timestamp, caller address, cpu number etc.
859 debug_finish_entry(debug_info_t
* id
, debug_entry_t
* active
, int level
,
862 active
->id
.stck
= get_clock();
863 active
->id
.fields
.cpuid
= smp_processor_id();
864 active
->caller
= __builtin_return_address(0);
865 active
->id
.fields
.exception
= exception
;
866 active
->id
.fields
.level
= level
;
867 proceed_active_entry(id
);
869 proceed_active_area(id
);
872 static int debug_stoppable
=1;
873 static int debug_active
=1;
875 #define CTL_S390DBF_STOPPABLE 5678
876 #define CTL_S390DBF_ACTIVE 5679
879 * proc handler for the running debug_active sysctl
880 * always allow read, allow write only if debug_stoppable is set or
881 * if debug_active is already off
884 s390dbf_procactive(ctl_table
*table
, int write
,
885 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
887 if (!write
|| debug_stoppable
|| !debug_active
)
888 return proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
894 static struct ctl_table s390dbf_table
[] = {
896 .ctl_name
= CTL_S390DBF_STOPPABLE
,
897 .procname
= "debug_stoppable",
898 .data
= &debug_stoppable
,
899 .maxlen
= sizeof(int),
900 .mode
= S_IRUGO
| S_IWUSR
,
901 .proc_handler
= &proc_dointvec
,
902 .strategy
= &sysctl_intvec
,
905 .ctl_name
= CTL_S390DBF_ACTIVE
,
906 .procname
= "debug_active",
907 .data
= &debug_active
,
908 .maxlen
= sizeof(int),
909 .mode
= S_IRUGO
| S_IWUSR
,
910 .proc_handler
= &s390dbf_procactive
,
911 .strategy
= &sysctl_intvec
,
916 static struct ctl_table s390dbf_dir_table
[] = {
918 .ctl_name
= CTL_S390DBF
,
919 .procname
= "s390dbf",
921 .mode
= S_IRUGO
| S_IXUGO
,
922 .child
= s390dbf_table
,
927 static struct ctl_table_header
*s390dbf_sysctl_header
;
938 * debug_event_common:
939 * - write debug entry with given size
943 debug_event_common(debug_info_t
* id
, int level
, const void *buf
, int len
)
946 debug_entry_t
*active
;
948 if (!debug_active
|| !id
->areas
)
950 spin_lock_irqsave(&id
->lock
, flags
);
951 active
= get_active_entry(id
);
952 memset(DEBUG_DATA(active
), 0, id
->buf_size
);
953 memcpy(DEBUG_DATA(active
), buf
, min(len
, id
->buf_size
));
954 debug_finish_entry(id
, active
, level
, 0);
955 spin_unlock_irqrestore(&id
->lock
, flags
);
961 * debug_exception_common:
962 * - write debug entry with given size and switch to next debug area
966 *debug_exception_common(debug_info_t
* id
, int level
, const void *buf
, int len
)
969 debug_entry_t
*active
;
971 if (!debug_active
|| !id
->areas
)
973 spin_lock_irqsave(&id
->lock
, flags
);
974 active
= get_active_entry(id
);
975 memset(DEBUG_DATA(active
), 0, id
->buf_size
);
976 memcpy(DEBUG_DATA(active
), buf
, min(len
, id
->buf_size
));
977 debug_finish_entry(id
, active
, level
, 1);
978 spin_unlock_irqrestore(&id
->lock
, flags
);
984 * counts arguments in format string for sprintf view
988 debug_count_numargs(char *string
)
1000 * debug_sprintf_event:
1004 debug_sprintf_event(debug_info_t
* id
, int level
,char *string
,...)
1008 unsigned long flags
;
1009 debug_sprintf_entry_t
*curr_event
;
1010 debug_entry_t
*active
;
1012 if((!id
) || (level
> id
->level
))
1014 if (!debug_active
|| !id
->areas
)
1016 numargs
=debug_count_numargs(string
);
1018 spin_lock_irqsave(&id
->lock
, flags
);
1019 active
= get_active_entry(id
);
1020 curr_event
=(debug_sprintf_entry_t
*) DEBUG_DATA(active
);
1021 va_start(ap
,string
);
1022 curr_event
->string
=string
;
1023 for(idx
=0;idx
<min(numargs
,(int)(id
->buf_size
/ sizeof(long))-1);idx
++)
1024 curr_event
->args
[idx
]=va_arg(ap
,long);
1026 debug_finish_entry(id
, active
, level
, 0);
1027 spin_unlock_irqrestore(&id
->lock
, flags
);
1033 * debug_sprintf_exception:
1037 debug_sprintf_exception(debug_info_t
* id
, int level
,char *string
,...)
1041 unsigned long flags
;
1042 debug_sprintf_entry_t
*curr_event
;
1043 debug_entry_t
*active
;
1045 if((!id
) || (level
> id
->level
))
1047 if (!debug_active
|| !id
->areas
)
1050 numargs
=debug_count_numargs(string
);
1052 spin_lock_irqsave(&id
->lock
, flags
);
1053 active
= get_active_entry(id
);
1054 curr_event
=(debug_sprintf_entry_t
*)DEBUG_DATA(active
);
1055 va_start(ap
,string
);
1056 curr_event
->string
=string
;
1057 for(idx
=0;idx
<min(numargs
,(int)(id
->buf_size
/ sizeof(long))-1);idx
++)
1058 curr_event
->args
[idx
]=va_arg(ap
,long);
1060 debug_finish_entry(id
, active
, level
, 1);
1061 spin_unlock_irqrestore(&id
->lock
, flags
);
1068 * - is called exactly once to initialize the debug feature
1072 __init
debug_init(void)
1076 s390dbf_sysctl_header
= register_sysctl_table(s390dbf_dir_table
);
1077 mutex_lock(&debug_mutex
);
1078 debug_debugfs_root_entry
= debugfs_create_dir(DEBUG_DIR_ROOT
,NULL
);
1080 mutex_unlock(&debug_mutex
);
1086 * debug_register_view:
1090 debug_register_view(debug_info_t
* id
, struct debug_view
*view
)
1094 unsigned long flags
;
1100 mode
= (id
->mode
| S_IFREG
) & ~S_IXUGO
;
1101 if (!(view
->prolog_proc
|| view
->format_proc
|| view
->header_proc
))
1102 mode
&= ~(S_IRUSR
| S_IRGRP
| S_IROTH
);
1103 if (!view
->input_proc
)
1104 mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
1105 pde
= debugfs_create_file(view
->name
, mode
, id
->debugfs_root_entry
,
1106 id
, &debug_file_ops
);
1108 pr_err("Registering view %s/%s failed due to out of "
1109 "memory\n", id
->name
,view
->name
);
1113 spin_lock_irqsave(&id
->lock
, flags
);
1114 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
1118 if (i
== DEBUG_MAX_VIEWS
) {
1119 pr_err("Registering view %s/%s would exceed the maximum "
1120 "number of views %i\n", id
->name
, view
->name
, i
);
1121 debugfs_remove(pde
);
1124 id
->views
[i
] = view
;
1125 id
->debugfs_entries
[i
] = pde
;
1127 spin_unlock_irqrestore(&id
->lock
, flags
);
1133 * debug_unregister_view:
1137 debug_unregister_view(debug_info_t
* id
, struct debug_view
*view
)
1141 unsigned long flags
;
1145 spin_lock_irqsave(&id
->lock
, flags
);
1146 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
1147 if (id
->views
[i
] == view
)
1150 if (i
== DEBUG_MAX_VIEWS
)
1153 debugfs_remove(id
->debugfs_entries
[i
]);
1154 id
->views
[i
] = NULL
;
1156 spin_unlock_irqrestore(&id
->lock
, flags
);
1161 static inline char *
1162 debug_get_user_string(const char __user
*user_buf
, size_t user_len
)
1166 buffer
= kmalloc(user_len
+ 1, GFP_KERNEL
);
1168 return ERR_PTR(-ENOMEM
);
1169 if (copy_from_user(buffer
, user_buf
, user_len
) != 0) {
1171 return ERR_PTR(-EFAULT
);
1173 /* got the string, now strip linefeed. */
1174 if (buffer
[user_len
- 1] == '\n')
1175 buffer
[user_len
- 1] = 0;
1177 buffer
[user_len
] = 0;
1182 debug_get_uint(char *buf
)
1186 for(; isspace(*buf
); buf
++);
1187 rc
= simple_strtoul(buf
, &buf
, 10);
1195 * functions for debug-views
1196 ***********************************
1200 * prints out actual debug level
1204 debug_prolog_pages_fn(debug_info_t
* id
,
1205 struct debug_view
*view
, char *out_buf
)
1207 return sprintf(out_buf
, "%i\n", id
->pages_per_area
);
1211 * reads new size (number of pages per debug area)
1215 debug_input_pages_fn(debug_info_t
* id
, struct debug_view
*view
,
1216 struct file
*file
, const char __user
*user_buf
,
1217 size_t user_len
, loff_t
* offset
)
1222 if (user_len
> 0x10000)
1228 str
= debug_get_user_string(user_buf
,user_len
);
1233 new_pages
= debug_get_uint(str
);
1238 rc
= debug_set_size(id
,id
->nr_areas
, new_pages
);
1247 *offset
+= user_len
;
1248 return rc
; /* number of input characters */
1252 * prints out actual debug level
1256 debug_prolog_level_fn(debug_info_t
* id
, struct debug_view
*view
, char *out_buf
)
1260 if(id
->level
== DEBUG_OFF_LEVEL
) {
1261 rc
= sprintf(out_buf
,"-\n");
1264 rc
= sprintf(out_buf
, "%i\n", id
->level
);
1270 * reads new debug level
1274 debug_input_level_fn(debug_info_t
* id
, struct debug_view
*view
,
1275 struct file
*file
, const char __user
*user_buf
,
1276 size_t user_len
, loff_t
* offset
)
1281 if (user_len
> 0x10000)
1287 str
= debug_get_user_string(user_buf
,user_len
);
1293 debug_set_level(id
, DEBUG_OFF_LEVEL
);
1297 new_level
= debug_get_uint(str
);
1300 pr_warning("%s is not a valid level for a debug "
1304 debug_set_level(id
, new_level
);
1310 *offset
+= user_len
;
1311 return rc
; /* number of input characters */
1316 * flushes debug areas
1319 static void debug_flush(debug_info_t
* id
, int area
)
1321 unsigned long flags
;
1324 if(!id
|| !id
->areas
)
1326 spin_lock_irqsave(&id
->lock
,flags
);
1327 if(area
== DEBUG_FLUSH_ALL
){
1328 id
->active_area
= 0;
1329 memset(id
->active_entries
, 0, id
->nr_areas
* sizeof(int));
1330 for (i
= 0; i
< id
->nr_areas
; i
++) {
1331 id
->active_pages
[i
] = 0;
1332 for(j
= 0; j
< id
->pages_per_area
; j
++) {
1333 memset(id
->areas
[i
][j
], 0, PAGE_SIZE
);
1336 } else if(area
>= 0 && area
< id
->nr_areas
) {
1337 id
->active_entries
[area
] = 0;
1338 id
->active_pages
[area
] = 0;
1339 for(i
= 0; i
< id
->pages_per_area
; i
++) {
1340 memset(id
->areas
[area
][i
],0,PAGE_SIZE
);
1343 spin_unlock_irqrestore(&id
->lock
,flags
);
1347 * view function: flushes debug areas
1351 debug_input_flush_fn(debug_info_t
* id
, struct debug_view
*view
,
1352 struct file
*file
, const char __user
*user_buf
,
1353 size_t user_len
, loff_t
* offset
)
1358 if (user_len
> 0x10000)
1364 if (copy_from_user(input_buf
, user_buf
, 1)){
1368 if(input_buf
[0] == '-') {
1369 debug_flush(id
, DEBUG_FLUSH_ALL
);
1372 if (isdigit(input_buf
[0])) {
1373 int area
= ((int) input_buf
[0] - (int) '0');
1374 debug_flush(id
, area
);
1378 pr_info("Flushing debug data failed because %c is not a valid "
1379 "area\n", input_buf
[0]);
1382 *offset
+= user_len
;
1383 return rc
; /* number of input characters */
1387 * prints debug header in raw format
1391 debug_raw_header_fn(debug_info_t
* id
, struct debug_view
*view
,
1392 int area
, debug_entry_t
* entry
, char *out_buf
)
1396 rc
= sizeof(debug_entry_t
);
1397 memcpy(out_buf
,entry
,sizeof(debug_entry_t
));
1402 * prints debug data in raw format
1406 debug_raw_format_fn(debug_info_t
* id
, struct debug_view
*view
,
1407 char *out_buf
, const char *in_buf
)
1412 memcpy(out_buf
, in_buf
, id
->buf_size
);
1417 * prints debug data in hex/ascii format
1421 debug_hex_ascii_format_fn(debug_info_t
* id
, struct debug_view
*view
,
1422 char *out_buf
, const char *in_buf
)
1426 for (i
= 0; i
< id
->buf_size
; i
++) {
1427 rc
+= sprintf(out_buf
+ rc
, "%02x ",
1428 ((unsigned char *) in_buf
)[i
]);
1430 rc
+= sprintf(out_buf
+ rc
, "| ");
1431 for (i
= 0; i
< id
->buf_size
; i
++) {
1432 unsigned char c
= in_buf
[i
];
1434 rc
+= sprintf(out_buf
+ rc
, ".");
1436 rc
+= sprintf(out_buf
+ rc
, "%c", c
);
1438 rc
+= sprintf(out_buf
+ rc
, "\n");
1443 * prints header for debug entry
1447 debug_dflt_header_fn(debug_info_t
* id
, struct debug_view
*view
,
1448 int area
, debug_entry_t
* entry
, char *out_buf
)
1450 struct timespec time_spec
;
1452 unsigned long caller
;
1456 level
= entry
->id
.fields
.level
;
1457 stck_to_timespec(entry
->id
.stck
, &time_spec
);
1459 if (entry
->id
.fields
.exception
)
1463 caller
= ((unsigned long) entry
->caller
) & PSW_ADDR_INSN
;
1464 rc
+= sprintf(out_buf
, "%02i %011lu:%06lu %1u %1s %02i %p ",
1465 area
, time_spec
.tv_sec
, time_spec
.tv_nsec
/ 1000, level
,
1466 except_str
, entry
->id
.fields
.cpuid
, (void *) caller
);
1471 * prints debug data sprintf-formated:
1472 * debug_sprinf_event/exception calls must be used together with this view
1475 #define DEBUG_SPRINTF_MAX_ARGS 10
1478 debug_sprintf_format_fn(debug_info_t
* id
, struct debug_view
*view
,
1479 char *out_buf
, debug_sprintf_entry_t
*curr_event
)
1481 int num_longs
, num_used_args
= 0,i
, rc
= 0;
1482 int index
[DEBUG_SPRINTF_MAX_ARGS
];
1484 /* count of longs fit into one entry */
1485 num_longs
= id
->buf_size
/ sizeof(long);
1488 goto out
; /* bufsize of entry too small */
1489 if(num_longs
== 1) {
1490 /* no args, we use only the string */
1491 strcpy(out_buf
, curr_event
->string
);
1492 rc
= strlen(curr_event
->string
);
1496 /* number of arguments used for sprintf (without the format string) */
1497 num_used_args
= min(DEBUG_SPRINTF_MAX_ARGS
, (num_longs
- 1));
1499 memset(index
,0, DEBUG_SPRINTF_MAX_ARGS
* sizeof(int));
1501 for(i
= 0; i
< num_used_args
; i
++)
1504 rc
= sprintf(out_buf
, curr_event
->string
, curr_event
->args
[index
[0]],
1505 curr_event
->args
[index
[1]], curr_event
->args
[index
[2]],
1506 curr_event
->args
[index
[3]], curr_event
->args
[index
[4]],
1507 curr_event
->args
[index
[5]], curr_event
->args
[index
[6]],
1508 curr_event
->args
[index
[7]], curr_event
->args
[index
[8]],
1509 curr_event
->args
[index
[9]]);
1519 static void __exit
debug_exit(void)
1521 debugfs_remove(debug_debugfs_root_entry
);
1522 unregister_sysctl_table(s390dbf_sysctl_header
);
1527 * module definitions
1529 postcore_initcall(debug_init
);
1530 module_exit(debug_exit
);
1531 MODULE_LICENSE("GPL");
1533 EXPORT_SYMBOL(debug_register
);
1534 EXPORT_SYMBOL(debug_unregister
);
1535 EXPORT_SYMBOL(debug_set_level
);
1536 EXPORT_SYMBOL(debug_stop_all
);
1537 EXPORT_SYMBOL(debug_register_view
);
1538 EXPORT_SYMBOL(debug_unregister_view
);
1539 EXPORT_SYMBOL(debug_event_common
);
1540 EXPORT_SYMBOL(debug_exception_common
);
1541 EXPORT_SYMBOL(debug_hex_ascii_view
);
1542 EXPORT_SYMBOL(debug_raw_view
);
1543 EXPORT_SYMBOL(debug_dflt_header_fn
);
1544 EXPORT_SYMBOL(debug_sprintf_view
);
1545 EXPORT_SYMBOL(debug_sprintf_exception
);
1546 EXPORT_SYMBOL(debug_sprintf_event
);