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 <asm/semaphore.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/debugfs.h>
26 #include <asm/debug.h>
28 #define DEBUG_PROLOG_ENTRY -1
30 #define ALL_AREAS 0 /* copy all debug areas */
31 #define NO_AREAS 1 /* copy no debug areas */
35 typedef struct file_private_info
{
36 loff_t offset
; /* offset of last read in file */
37 int act_area
; /* number of last formated area */
38 int act_page
; /* act page in given area */
39 int act_entry
; /* last formated entry (offset */
40 /* relative to beginning of last */
42 size_t act_entry_offset
; /* up to this offset we copied */
43 /* in last read the last formated */
44 /* entry to userland */
45 char temp_buf
[2048]; /* buffer for output */
46 debug_info_t
*debug_info_org
; /* original debug information */
47 debug_info_t
*debug_info_snap
; /* snapshot of debug information */
48 struct debug_view
*view
; /* used view of debug info */
49 } file_private_info_t
;
55 * This assumes that all args are converted into longs
56 * on L/390 this is the case for all types of parameter
57 * except of floats, and long long (32 bit)
61 } debug_sprintf_entry_t
;
64 extern void tod_to_timeval(uint64_t todval
, struct timespec
*xtime
);
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(char *name
, int pages_per_area
,
76 int nr_areas
, int buf_size
);
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 unsigned int 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(char *name
, int pages_per_area
, int nr_areas
, int buf_size
,
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(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 /* create root directory */
340 rc
->debugfs_root_entry
= debugfs_create_dir(rc
->name
,
341 debug_debugfs_root_entry
);
343 /* append new element to linked list */
344 if (!debug_area_first
) {
345 /* first element in list */
346 debug_area_first
= rc
;
349 /* append element to end of list */
350 debug_area_last
->next
= rc
;
351 rc
->prev
= debug_area_last
;
353 debug_area_last
= rc
;
367 debug_info_copy(debug_info_t
* in
, int mode
)
373 /* get a consistent copy of the debug areas */
375 rc
= debug_info_alloc(in
->name
, in
->pages_per_area
,
376 in
->nr_areas
, in
->buf_size
, in
->level
, mode
);
377 spin_lock_irqsave(&in
->lock
, flags
);
380 /* has something changed in the meantime ? */
381 if((rc
->pages_per_area
== in
->pages_per_area
) &&
382 (rc
->nr_areas
== in
->nr_areas
)) {
385 spin_unlock_irqrestore(&in
->lock
, flags
);
389 if(!rc
|| (mode
== NO_AREAS
))
392 for(i
= 0; i
< in
->nr_areas
; i
++){
393 for(j
= 0; j
< in
->pages_per_area
; j
++) {
394 memcpy(rc
->areas
[i
][j
], in
->areas
[i
][j
],PAGE_SIZE
);
398 spin_unlock_irqrestore(&in
->lock
, flags
);
404 * - increments reference count for debug-info
408 debug_info_get(debug_info_t
* db_info
)
411 atomic_inc(&db_info
->ref_count
);
416 * - decreases reference count for debug-info and frees it if necessary
420 debug_info_put(debug_info_t
*db_info
)
426 if (atomic_dec_and_test(&db_info
->ref_count
)) {
427 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
428 if (!db_info
->views
[i
])
430 debugfs_remove(db_info
->debugfs_entries
[i
]);
432 debugfs_remove(db_info
->debugfs_root_entry
);
433 if(db_info
== debug_area_first
)
434 debug_area_first
= db_info
->next
;
435 if(db_info
== debug_area_last
)
436 debug_area_last
= db_info
->prev
;
437 if(db_info
->prev
) db_info
->prev
->next
= db_info
->next
;
438 if(db_info
->next
) db_info
->next
->prev
= db_info
->prev
;
439 debug_info_free(db_info
);
444 * debug_format_entry:
445 * - format one debug entry and return size of formated data
449 debug_format_entry(file_private_info_t
*p_info
)
451 debug_info_t
*id_snap
= p_info
->debug_info_snap
;
452 struct debug_view
*view
= p_info
->view
;
453 debug_entry_t
*act_entry
;
455 if(p_info
->act_entry
== DEBUG_PROLOG_ENTRY
){
457 if (view
->prolog_proc
)
458 len
+= view
->prolog_proc(id_snap
,view
,p_info
->temp_buf
);
461 if (!id_snap
->areas
) /* this is true, if we have a prolog only view */
462 goto out
; /* or if 'pages_per_area' is 0 */
463 act_entry
= (debug_entry_t
*) ((char*)id_snap
->areas
[p_info
->act_area
]
464 [p_info
->act_page
] + p_info
->act_entry
);
466 if (act_entry
->id
.stck
== 0LL)
467 goto out
; /* empty entry */
468 if (view
->header_proc
)
469 len
+= view
->header_proc(id_snap
, view
, p_info
->act_area
,
470 act_entry
, p_info
->temp_buf
+ len
);
471 if (view
->format_proc
)
472 len
+= view
->format_proc(id_snap
, view
, p_info
->temp_buf
+ len
,
473 DEBUG_DATA(act_entry
));
480 * - goto next entry in p_info
484 debug_next_entry(file_private_info_t
*p_info
)
488 id
= p_info
->debug_info_snap
;
489 if(p_info
->act_entry
== DEBUG_PROLOG_ENTRY
){
490 p_info
->act_entry
= 0;
491 p_info
->act_page
= 0;
496 p_info
->act_entry
+= id
->entry_size
;
497 /* switch to next page, if we reached the end of the page */
498 if (p_info
->act_entry
> (PAGE_SIZE
- id
->entry_size
)){
500 p_info
->act_entry
= 0;
501 p_info
->act_page
+= 1;
502 if((p_info
->act_page
% id
->pages_per_area
) == 0) {
507 if(p_info
->act_area
>= id
->nr_areas
)
516 * - called for user read()
517 * - copies formated debug entries to the user buffer
521 debug_output(struct file
*file
, /* file descriptor */
522 char __user
*user_buf
, /* user buffer */
523 size_t len
, /* length of buffer */
524 loff_t
*offset
) /* offset in the file */
528 file_private_info_t
*p_info
;
530 p_info
= ((file_private_info_t
*) file
->private_data
);
531 if (*offset
!= p_info
->offset
)
533 if(p_info
->act_area
>= p_info
->debug_info_snap
->nr_areas
)
535 entry_offset
= p_info
->act_entry_offset
;
537 int formatted_line_size
;
538 int formatted_line_residue
;
539 int user_buf_residue
;
542 formatted_line_size
= debug_format_entry(p_info
);
543 formatted_line_residue
= formatted_line_size
- entry_offset
;
544 user_buf_residue
= len
-count
;
545 copy_size
= min(user_buf_residue
, formatted_line_residue
);
547 if (copy_to_user(user_buf
+ count
, p_info
->temp_buf
548 + entry_offset
, copy_size
))
551 entry_offset
+= copy_size
;
553 if(copy_size
== formatted_line_residue
){
555 if(debug_next_entry(p_info
))
560 p_info
->offset
= *offset
+ count
;
561 p_info
->act_entry_offset
= entry_offset
;
562 *offset
= p_info
->offset
;
568 * - called for user write()
569 * - calls input function of view
573 debug_input(struct file
*file
, const char __user
*user_buf
, size_t length
,
577 file_private_info_t
*p_info
;
579 mutex_lock(&debug_mutex
);
580 p_info
= ((file_private_info_t
*) file
->private_data
);
581 if (p_info
->view
->input_proc
)
582 rc
= p_info
->view
->input_proc(p_info
->debug_info_org
,
583 p_info
->view
, file
, user_buf
,
587 mutex_unlock(&debug_mutex
);
588 return rc
; /* number of input characters */
593 * - called for user open()
594 * - copies formated output to private_data area of the file
599 debug_open(struct inode
*inode
, struct file
*file
)
602 file_private_info_t
*p_info
;
603 debug_info_t
*debug_info
, *debug_info_snapshot
;
605 mutex_lock(&debug_mutex
);
606 debug_info
= file
->f_path
.dentry
->d_inode
->i_private
;
607 /* find debug view */
608 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
609 if (!debug_info
->views
[i
])
611 else if (debug_info
->debugfs_entries
[i
] ==
612 file
->f_path
.dentry
) {
613 goto found
; /* found view ! */
622 /* Make snapshot of current debug areas to get it consistent. */
623 /* To copy all the areas is only needed, if we have a view which */
624 /* formats the debug areas. */
626 if(!debug_info
->views
[i
]->format_proc
&&
627 !debug_info
->views
[i
]->header_proc
){
628 debug_info_snapshot
= debug_info_copy(debug_info
, NO_AREAS
);
630 debug_info_snapshot
= debug_info_copy(debug_info
, ALL_AREAS
);
633 if(!debug_info_snapshot
){
637 p_info
= kmalloc(sizeof(file_private_info_t
),
640 if(debug_info_snapshot
)
641 debug_info_free(debug_info_snapshot
);
646 p_info
->debug_info_snap
= debug_info_snapshot
;
647 p_info
->debug_info_org
= debug_info
;
648 p_info
->view
= debug_info
->views
[i
];
649 p_info
->act_area
= 0;
650 p_info
->act_page
= 0;
651 p_info
->act_entry
= DEBUG_PROLOG_ENTRY
;
652 p_info
->act_entry_offset
= 0;
653 file
->private_data
= p_info
;
654 debug_info_get(debug_info
);
656 mutex_unlock(&debug_mutex
);
662 * - called for user close()
663 * - deletes private_data area of the file handle
667 debug_close(struct inode
*inode
, struct file
*file
)
669 file_private_info_t
*p_info
;
670 p_info
= (file_private_info_t
*) file
->private_data
;
671 if(p_info
->debug_info_snap
)
672 debug_info_free(p_info
->debug_info_snap
);
673 debug_info_put(p_info
->debug_info_org
);
674 kfree(file
->private_data
);
675 return 0; /* success */
680 * - creates and initializes debug area for the caller
681 * - returns handle for debug area
685 debug_register (char *name
, int pages_per_area
, int nr_areas
, int buf_size
)
687 debug_info_t
*rc
= NULL
;
691 mutex_lock(&debug_mutex
);
693 /* create new debug_info */
695 rc
= debug_info_create(name
, pages_per_area
, nr_areas
, buf_size
);
698 debug_register_view(rc
, &debug_level_view
);
699 debug_register_view(rc
, &debug_flush_view
);
700 debug_register_view(rc
, &debug_pages_view
);
703 printk(KERN_ERR
"debug: debug_register failed for %s\n",name
);
705 mutex_unlock(&debug_mutex
);
711 * - give back debug area
715 debug_unregister(debug_info_t
* id
)
719 mutex_lock(&debug_mutex
);
721 mutex_unlock(&debug_mutex
);
729 * - set area size (number of pages) and number of areas
732 debug_set_size(debug_info_t
* id
, int nr_areas
, int pages_per_area
)
735 debug_entry_t
*** new_areas
;
738 if(!id
|| (nr_areas
<= 0) || (pages_per_area
< 0))
740 if(pages_per_area
> 0){
741 new_areas
= debug_areas_alloc(pages_per_area
, nr_areas
);
743 printk(KERN_WARNING
"debug: could not allocate memory "\
744 "for pagenumber: %i\n",pages_per_area
);
751 spin_lock_irqsave(&id
->lock
,flags
);
752 debug_areas_free(id
);
753 id
->areas
= new_areas
;
754 id
->nr_areas
= nr_areas
;
755 id
->pages_per_area
= pages_per_area
;
757 memset(id
->active_entries
,0,sizeof(int)*id
->nr_areas
);
758 memset(id
->active_pages
, 0, sizeof(int)*id
->nr_areas
);
759 spin_unlock_irqrestore(&id
->lock
,flags
);
760 printk(KERN_INFO
"debug: %s: set new size (%i pages)\n"\
761 ,id
->name
, pages_per_area
);
768 * - set actual debug level
772 debug_set_level(debug_info_t
* id
, int new_level
)
777 spin_lock_irqsave(&id
->lock
,flags
);
778 if(new_level
== DEBUG_OFF_LEVEL
){
779 id
->level
= DEBUG_OFF_LEVEL
;
780 printk(KERN_INFO
"debug: %s: switched off\n",id
->name
);
781 } else if ((new_level
> DEBUG_MAX_LEVEL
) || (new_level
< 0)) {
783 "debug: %s: level %i is out of range (%i - %i)\n",
784 id
->name
, new_level
, 0, DEBUG_MAX_LEVEL
);
786 id
->level
= new_level
;
788 spin_unlock_irqrestore(&id
->lock
,flags
);
793 * proceed_active_entry:
794 * - set active entry to next in the ring buffer
798 proceed_active_entry(debug_info_t
* id
)
800 if ((id
->active_entries
[id
->active_area
] += id
->entry_size
)
801 > (PAGE_SIZE
- id
->entry_size
)){
802 id
->active_entries
[id
->active_area
] = 0;
803 id
->active_pages
[id
->active_area
] =
804 (id
->active_pages
[id
->active_area
] + 1) %
810 * proceed_active_area:
811 * - set active area to next in the ring buffer
815 proceed_active_area(debug_info_t
* id
)
818 id
->active_area
= id
->active_area
% id
->nr_areas
;
825 static inline debug_entry_t
*
826 get_active_entry(debug_info_t
* id
)
828 return (debug_entry_t
*) (((char *) id
->areas
[id
->active_area
]
829 [id
->active_pages
[id
->active_area
]]) +
830 id
->active_entries
[id
->active_area
]);
834 * debug_finish_entry:
835 * - set timestamp, caller address, cpu number etc.
839 debug_finish_entry(debug_info_t
* id
, debug_entry_t
* active
, int level
,
842 active
->id
.stck
= get_clock();
843 active
->id
.fields
.cpuid
= smp_processor_id();
844 active
->caller
= __builtin_return_address(0);
845 active
->id
.fields
.exception
= exception
;
846 active
->id
.fields
.level
= level
;
847 proceed_active_entry(id
);
849 proceed_active_area(id
);
852 static int debug_stoppable
=1;
853 static int debug_active
=1;
855 #define CTL_S390DBF_STOPPABLE 5678
856 #define CTL_S390DBF_ACTIVE 5679
859 * proc handler for the running debug_active sysctl
860 * always allow read, allow write only if debug_stoppable is set or
861 * if debug_active is already off
864 s390dbf_procactive(ctl_table
*table
, int write
, struct file
*filp
,
865 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
867 if (!write
|| debug_stoppable
|| !debug_active
)
868 return proc_dointvec(table
, write
, filp
, buffer
, lenp
, ppos
);
874 static struct ctl_table s390dbf_table
[] = {
876 .ctl_name
= CTL_S390DBF_STOPPABLE
,
877 .procname
= "debug_stoppable",
878 .data
= &debug_stoppable
,
879 .maxlen
= sizeof(int),
880 .mode
= S_IRUGO
| S_IWUSR
,
881 .proc_handler
= &proc_dointvec
,
882 .strategy
= &sysctl_intvec
,
885 .ctl_name
= CTL_S390DBF_ACTIVE
,
886 .procname
= "debug_active",
887 .data
= &debug_active
,
888 .maxlen
= sizeof(int),
889 .mode
= S_IRUGO
| S_IWUSR
,
890 .proc_handler
= &s390dbf_procactive
,
891 .strategy
= &sysctl_intvec
,
896 static struct ctl_table s390dbf_dir_table
[] = {
898 .ctl_name
= CTL_S390DBF
,
899 .procname
= "s390dbf",
901 .mode
= S_IRUGO
| S_IXUGO
,
902 .child
= s390dbf_table
,
907 static struct ctl_table_header
*s390dbf_sysctl_header
;
918 * debug_event_common:
919 * - write debug entry with given size
923 debug_event_common(debug_info_t
* id
, int level
, const void *buf
, int len
)
926 debug_entry_t
*active
;
928 if (!debug_active
|| !id
->areas
)
930 spin_lock_irqsave(&id
->lock
, flags
);
931 active
= get_active_entry(id
);
932 memset(DEBUG_DATA(active
), 0, id
->buf_size
);
933 memcpy(DEBUG_DATA(active
), buf
, min(len
, id
->buf_size
));
934 debug_finish_entry(id
, active
, level
, 0);
935 spin_unlock_irqrestore(&id
->lock
, flags
);
941 * debug_exception_common:
942 * - write debug entry with given size and switch to next debug area
946 *debug_exception_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
, 1);
958 spin_unlock_irqrestore(&id
->lock
, flags
);
964 * counts arguments in format string for sprintf view
968 debug_count_numargs(char *string
)
980 * debug_sprintf_event:
984 debug_sprintf_event(debug_info_t
* id
, int level
,char *string
,...)
989 debug_sprintf_entry_t
*curr_event
;
990 debug_entry_t
*active
;
992 if((!id
) || (level
> id
->level
))
994 if (!debug_active
|| !id
->areas
)
996 numargs
=debug_count_numargs(string
);
998 spin_lock_irqsave(&id
->lock
, flags
);
999 active
= get_active_entry(id
);
1000 curr_event
=(debug_sprintf_entry_t
*) DEBUG_DATA(active
);
1001 va_start(ap
,string
);
1002 curr_event
->string
=string
;
1003 for(idx
=0;idx
<min(numargs
,(int)(id
->buf_size
/ sizeof(long))-1);idx
++)
1004 curr_event
->args
[idx
]=va_arg(ap
,long);
1006 debug_finish_entry(id
, active
, level
, 0);
1007 spin_unlock_irqrestore(&id
->lock
, flags
);
1013 * debug_sprintf_exception:
1017 debug_sprintf_exception(debug_info_t
* id
, int level
,char *string
,...)
1021 unsigned long flags
;
1022 debug_sprintf_entry_t
*curr_event
;
1023 debug_entry_t
*active
;
1025 if((!id
) || (level
> id
->level
))
1027 if (!debug_active
|| !id
->areas
)
1030 numargs
=debug_count_numargs(string
);
1032 spin_lock_irqsave(&id
->lock
, flags
);
1033 active
= get_active_entry(id
);
1034 curr_event
=(debug_sprintf_entry_t
*)DEBUG_DATA(active
);
1035 va_start(ap
,string
);
1036 curr_event
->string
=string
;
1037 for(idx
=0;idx
<min(numargs
,(int)(id
->buf_size
/ sizeof(long))-1);idx
++)
1038 curr_event
->args
[idx
]=va_arg(ap
,long);
1040 debug_finish_entry(id
, active
, level
, 1);
1041 spin_unlock_irqrestore(&id
->lock
, flags
);
1048 * - is called exactly once to initialize the debug feature
1052 __init
debug_init(void)
1056 s390dbf_sysctl_header
= register_sysctl_table(s390dbf_dir_table
);
1057 mutex_lock(&debug_mutex
);
1058 debug_debugfs_root_entry
= debugfs_create_dir(DEBUG_DIR_ROOT
,NULL
);
1059 printk(KERN_INFO
"debug: Initialization complete\n");
1061 mutex_unlock(&debug_mutex
);
1067 * debug_register_view:
1071 debug_register_view(debug_info_t
* id
, struct debug_view
*view
)
1075 unsigned long flags
;
1076 mode_t mode
= S_IFREG
;
1081 if (view
->prolog_proc
|| view
->format_proc
|| view
->header_proc
)
1083 if (view
->input_proc
)
1085 pde
= debugfs_create_file(view
->name
, mode
, id
->debugfs_root_entry
,
1086 id
, &debug_file_ops
);
1088 printk(KERN_WARNING
"debug: debugfs_create_file() failed!"\
1089 " Cannot register view %s/%s\n", id
->name
,view
->name
);
1093 spin_lock_irqsave(&id
->lock
, flags
);
1094 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
1098 if (i
== DEBUG_MAX_VIEWS
) {
1099 printk(KERN_WARNING
"debug: cannot register view %s/%s\n",
1100 id
->name
,view
->name
);
1102 "debug: maximum number of views reached (%i)!\n", i
);
1103 debugfs_remove(pde
);
1106 id
->views
[i
] = view
;
1107 id
->debugfs_entries
[i
] = pde
;
1109 spin_unlock_irqrestore(&id
->lock
, flags
);
1115 * debug_unregister_view:
1119 debug_unregister_view(debug_info_t
* id
, struct debug_view
*view
)
1123 unsigned long flags
;
1127 spin_lock_irqsave(&id
->lock
, flags
);
1128 for (i
= 0; i
< DEBUG_MAX_VIEWS
; i
++) {
1129 if (id
->views
[i
] == view
)
1132 if (i
== DEBUG_MAX_VIEWS
)
1135 debugfs_remove(id
->debugfs_entries
[i
]);
1136 id
->views
[i
] = NULL
;
1139 spin_unlock_irqrestore(&id
->lock
, flags
);
1144 static inline char *
1145 debug_get_user_string(const char __user
*user_buf
, size_t user_len
)
1149 buffer
= kmalloc(user_len
+ 1, GFP_KERNEL
);
1151 return ERR_PTR(-ENOMEM
);
1152 if (copy_from_user(buffer
, user_buf
, user_len
) != 0) {
1154 return ERR_PTR(-EFAULT
);
1156 /* got the string, now strip linefeed. */
1157 if (buffer
[user_len
- 1] == '\n')
1158 buffer
[user_len
- 1] = 0;
1160 buffer
[user_len
] = 0;
1165 debug_get_uint(char *buf
)
1169 for(; isspace(*buf
); buf
++);
1170 rc
= simple_strtoul(buf
, &buf
, 10);
1172 printk("debug: no integer specified!\n");
1179 * functions for debug-views
1180 ***********************************
1184 * prints out actual debug level
1188 debug_prolog_pages_fn(debug_info_t
* id
,
1189 struct debug_view
*view
, char *out_buf
)
1191 return sprintf(out_buf
, "%i\n", id
->pages_per_area
);
1195 * reads new size (number of pages per debug area)
1199 debug_input_pages_fn(debug_info_t
* id
, struct debug_view
*view
,
1200 struct file
*file
, const char __user
*user_buf
,
1201 size_t user_len
, loff_t
* offset
)
1206 if (user_len
> 0x10000)
1212 str
= debug_get_user_string(user_buf
,user_len
);
1217 new_pages
= debug_get_uint(str
);
1222 rc
= debug_set_size(id
,id
->nr_areas
, new_pages
);
1231 *offset
+= user_len
;
1232 return rc
; /* number of input characters */
1236 * prints out actual debug level
1240 debug_prolog_level_fn(debug_info_t
* id
, struct debug_view
*view
, char *out_buf
)
1244 if(id
->level
== DEBUG_OFF_LEVEL
) {
1245 rc
= sprintf(out_buf
,"-\n");
1248 rc
= sprintf(out_buf
, "%i\n", id
->level
);
1254 * reads new debug level
1258 debug_input_level_fn(debug_info_t
* id
, struct debug_view
*view
,
1259 struct file
*file
, const char __user
*user_buf
,
1260 size_t user_len
, loff_t
* offset
)
1265 if (user_len
> 0x10000)
1271 str
= debug_get_user_string(user_buf
,user_len
);
1277 debug_set_level(id
, DEBUG_OFF_LEVEL
);
1281 new_level
= debug_get_uint(str
);
1284 printk(KERN_INFO
"debug: level `%s` is not valid\n", str
);
1287 debug_set_level(id
, new_level
);
1293 *offset
+= user_len
;
1294 return rc
; /* number of input characters */
1299 * flushes debug areas
1302 static void debug_flush(debug_info_t
* id
, int area
)
1304 unsigned long flags
;
1307 if(!id
|| !id
->areas
)
1309 spin_lock_irqsave(&id
->lock
,flags
);
1310 if(area
== DEBUG_FLUSH_ALL
){
1311 id
->active_area
= 0;
1312 memset(id
->active_entries
, 0, id
->nr_areas
* sizeof(int));
1313 for (i
= 0; i
< id
->nr_areas
; i
++) {
1314 id
->active_pages
[i
] = 0;
1315 for(j
= 0; j
< id
->pages_per_area
; j
++) {
1316 memset(id
->areas
[i
][j
], 0, PAGE_SIZE
);
1319 printk(KERN_INFO
"debug: %s: all areas flushed\n",id
->name
);
1320 } else if(area
>= 0 && area
< id
->nr_areas
) {
1321 id
->active_entries
[area
] = 0;
1322 id
->active_pages
[area
] = 0;
1323 for(i
= 0; i
< id
->pages_per_area
; i
++) {
1324 memset(id
->areas
[area
][i
],0,PAGE_SIZE
);
1326 printk(KERN_INFO
"debug: %s: area %i has been flushed\n",
1330 "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
1331 id
->name
, area
, 0, id
->nr_areas
-1);
1333 spin_unlock_irqrestore(&id
->lock
,flags
);
1337 * view function: flushes debug areas
1341 debug_input_flush_fn(debug_info_t
* id
, struct debug_view
*view
,
1342 struct file
*file
, const char __user
*user_buf
,
1343 size_t user_len
, loff_t
* offset
)
1348 if (user_len
> 0x10000)
1354 if (copy_from_user(input_buf
, user_buf
, 1)){
1358 if(input_buf
[0] == '-') {
1359 debug_flush(id
, DEBUG_FLUSH_ALL
);
1362 if (isdigit(input_buf
[0])) {
1363 int area
= ((int) input_buf
[0] - (int) '0');
1364 debug_flush(id
, area
);
1368 printk(KERN_INFO
"debug: area `%c` is not valid\n", input_buf
[0]);
1371 *offset
+= user_len
;
1372 return rc
; /* number of input characters */
1376 * prints debug header in raw format
1380 debug_raw_header_fn(debug_info_t
* id
, struct debug_view
*view
,
1381 int area
, debug_entry_t
* entry
, char *out_buf
)
1385 rc
= sizeof(debug_entry_t
);
1386 memcpy(out_buf
,entry
,sizeof(debug_entry_t
));
1391 * prints debug data in raw format
1395 debug_raw_format_fn(debug_info_t
* id
, struct debug_view
*view
,
1396 char *out_buf
, const char *in_buf
)
1401 memcpy(out_buf
, in_buf
, id
->buf_size
);
1406 * prints debug data in hex/ascii format
1410 debug_hex_ascii_format_fn(debug_info_t
* id
, struct debug_view
*view
,
1411 char *out_buf
, const char *in_buf
)
1415 for (i
= 0; i
< id
->buf_size
; i
++) {
1416 rc
+= sprintf(out_buf
+ rc
, "%02x ",
1417 ((unsigned char *) in_buf
)[i
]);
1419 rc
+= sprintf(out_buf
+ rc
, "| ");
1420 for (i
= 0; i
< id
->buf_size
; i
++) {
1421 unsigned char c
= in_buf
[i
];
1423 rc
+= sprintf(out_buf
+ rc
, ".");
1425 rc
+= sprintf(out_buf
+ rc
, "%c", c
);
1427 rc
+= sprintf(out_buf
+ rc
, "\n");
1432 * prints header for debug entry
1436 debug_dflt_header_fn(debug_info_t
* id
, struct debug_view
*view
,
1437 int area
, debug_entry_t
* entry
, char *out_buf
)
1439 struct timespec time_spec
;
1440 unsigned long long time
;
1442 unsigned long caller
;
1446 level
= entry
->id
.fields
.level
;
1447 time
= entry
->id
.stck
;
1448 /* adjust todclock to 1970 */
1449 time
-= 0x8126d60e46000000LL
- (0x3c26700LL
* 1000000 * 4096);
1450 tod_to_timeval(time
, &time_spec
);
1452 if (entry
->id
.fields
.exception
)
1456 caller
= ((unsigned long) entry
->caller
) & PSW_ADDR_INSN
;
1457 rc
+= sprintf(out_buf
, "%02i %011lu:%06lu %1u %1s %02i %p ",
1458 area
, time_spec
.tv_sec
, time_spec
.tv_nsec
/ 1000, level
,
1459 except_str
, entry
->id
.fields
.cpuid
, (void *) caller
);
1464 * prints debug data sprintf-formated:
1465 * debug_sprinf_event/exception calls must be used together with this view
1468 #define DEBUG_SPRINTF_MAX_ARGS 10
1471 debug_sprintf_format_fn(debug_info_t
* id
, struct debug_view
*view
,
1472 char *out_buf
, debug_sprintf_entry_t
*curr_event
)
1474 int num_longs
, num_used_args
= 0,i
, rc
= 0;
1475 int index
[DEBUG_SPRINTF_MAX_ARGS
];
1477 /* count of longs fit into one entry */
1478 num_longs
= id
->buf_size
/ sizeof(long);
1481 goto out
; /* bufsize of entry too small */
1482 if(num_longs
== 1) {
1483 /* no args, we use only the string */
1484 strcpy(out_buf
, curr_event
->string
);
1485 rc
= strlen(curr_event
->string
);
1489 /* number of arguments used for sprintf (without the format string) */
1490 num_used_args
= min(DEBUG_SPRINTF_MAX_ARGS
, (num_longs
- 1));
1492 memset(index
,0, DEBUG_SPRINTF_MAX_ARGS
* sizeof(int));
1494 for(i
= 0; i
< num_used_args
; i
++)
1497 rc
= sprintf(out_buf
, curr_event
->string
, curr_event
->args
[index
[0]],
1498 curr_event
->args
[index
[1]], curr_event
->args
[index
[2]],
1499 curr_event
->args
[index
[3]], curr_event
->args
[index
[4]],
1500 curr_event
->args
[index
[5]], curr_event
->args
[index
[6]],
1501 curr_event
->args
[index
[7]], curr_event
->args
[index
[8]],
1502 curr_event
->args
[index
[9]]);
1512 static void __exit
debug_exit(void)
1514 debugfs_remove(debug_debugfs_root_entry
);
1515 unregister_sysctl_table(s390dbf_sysctl_header
);
1520 * module definitions
1522 postcore_initcall(debug_init
);
1523 module_exit(debug_exit
);
1524 MODULE_LICENSE("GPL");
1526 EXPORT_SYMBOL(debug_register
);
1527 EXPORT_SYMBOL(debug_unregister
);
1528 EXPORT_SYMBOL(debug_set_level
);
1529 EXPORT_SYMBOL(debug_stop_all
);
1530 EXPORT_SYMBOL(debug_register_view
);
1531 EXPORT_SYMBOL(debug_unregister_view
);
1532 EXPORT_SYMBOL(debug_event_common
);
1533 EXPORT_SYMBOL(debug_exception_common
);
1534 EXPORT_SYMBOL(debug_hex_ascii_view
);
1535 EXPORT_SYMBOL(debug_raw_view
);
1536 EXPORT_SYMBOL(debug_dflt_header_fn
);
1537 EXPORT_SYMBOL(debug_sprintf_view
);
1538 EXPORT_SYMBOL(debug_sprintf_exception
);
1539 EXPORT_SYMBOL(debug_sprintf_event
);