[S390] Fix a lot of sparse warnings.
[deliverable/linux.git] / arch / s390 / kernel / debug.c
1 /*
2 * arch/s390/kernel/debug.c
3 * S/390 debug facility
4 *
5 * Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Michael Holzheu (holzheu@de.ibm.com),
8 * Holger Smolinski (Holger.Smolinski@de.ibm.com)
9 *
10 * Bugreports to: <Linux390@de.ibm.com>
11 */
12
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>
23 #include <linux/fs.h>
24 #include <linux/debugfs.h>
25
26 #include <asm/debug.h>
27
28 #define DEBUG_PROLOG_ENTRY -1
29
30 #define ALL_AREAS 0 /* copy all debug areas */
31 #define NO_AREAS 1 /* copy no debug areas */
32
33 /* typedefs */
34
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 */
41 /* formated page) */
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;
50
51 typedef struct
52 {
53 char *string;
54 /*
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)
58 *
59 */
60 long args[0];
61 } debug_sprintf_entry_t;
62
63
64 extern void tod_to_timeval(uint64_t todval, struct timespec *xtime);
65
66 /* internal function prototyes */
67
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, 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,
96 const char *in_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);
99
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);
102
103 /* globals */
104
105 struct debug_view debug_raw_view = {
106 "raw",
107 NULL,
108 &debug_raw_header_fn,
109 &debug_raw_format_fn,
110 NULL,
111 NULL
112 };
113
114 struct debug_view debug_hex_ascii_view = {
115 "hex_ascii",
116 NULL,
117 &debug_dflt_header_fn,
118 &debug_hex_ascii_format_fn,
119 NULL,
120 NULL
121 };
122
123 static struct debug_view debug_level_view = {
124 "level",
125 &debug_prolog_level_fn,
126 NULL,
127 NULL,
128 &debug_input_level_fn,
129 NULL
130 };
131
132 static struct debug_view debug_pages_view = {
133 "pages",
134 &debug_prolog_pages_fn,
135 NULL,
136 NULL,
137 &debug_input_pages_fn,
138 NULL
139 };
140
141 static struct debug_view debug_flush_view = {
142 "flush",
143 NULL,
144 NULL,
145 NULL,
146 &debug_input_flush_fn,
147 NULL
148 };
149
150 struct debug_view debug_sprintf_view = {
151 "sprintf",
152 NULL,
153 &debug_dflt_header_fn,
154 (debug_format_proc_t*)&debug_sprintf_format_fn,
155 NULL,
156 NULL
157 };
158
159 /* used by dump analysis tools to determine version of debug feature */
160 static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
161
162 /* static globals */
163
164 static debug_info_t *debug_area_first = NULL;
165 static debug_info_t *debug_area_last = NULL;
166 static DEFINE_MUTEX(debug_mutex);
167
168 static int initialized;
169
170 static const struct file_operations debug_file_ops = {
171 .owner = THIS_MODULE,
172 .read = debug_output,
173 .write = debug_input,
174 .open = debug_open,
175 .release = debug_close,
176 };
177
178 static struct dentry *debug_debugfs_root_entry;
179
180 /* functions */
181
182 /*
183 * debug_areas_alloc
184 * - Debug areas are implemented as a threedimensonal array:
185 * areas[areanumber][pagenumber][pageoffset]
186 */
187
188 static debug_entry_t***
189 debug_areas_alloc(int pages_per_area, int nr_areas)
190 {
191 debug_entry_t*** areas;
192 int i,j;
193
194 areas = kmalloc(nr_areas *
195 sizeof(debug_entry_t**),
196 GFP_KERNEL);
197 if (!areas)
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);
202 if (!areas[i]) {
203 goto fail_malloc_areas2;
204 }
205 for(j = 0; j < pages_per_area; j++) {
206 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
207 if(!areas[i][j]) {
208 for(j--; j >=0 ; j--) {
209 kfree(areas[i][j]);
210 }
211 kfree(areas[i]);
212 goto fail_malloc_areas2;
213 }
214 }
215 }
216 return areas;
217
218 fail_malloc_areas2:
219 for(i--; i >= 0; i--){
220 for(j=0; j < pages_per_area;j++){
221 kfree(areas[i][j]);
222 }
223 kfree(areas[i]);
224 }
225 kfree(areas);
226 fail_malloc_areas:
227 return NULL;
228
229 }
230
231
232 /*
233 * debug_info_alloc
234 * - alloc new debug-info
235 */
236
237 static debug_info_t*
238 debug_info_alloc(char *name, int pages_per_area, int nr_areas, int buf_size,
239 int level, int mode)
240 {
241 debug_info_t* rc;
242
243 /* alloc everything */
244
245 rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
246 if(!rc)
247 goto fail_malloc_rc;
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);
256 if(!rc->areas)
257 goto fail_malloc_areas;
258 } else {
259 rc->areas = NULL;
260 }
261
262 /* initialize members */
263
264 spin_lock_init(&rc->lock);
265 rc->pages_per_area = pages_per_area;
266 rc->nr_areas = nr_areas;
267 rc->active_area = 0;
268 rc->level = level;
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);
276
277 return rc;
278
279 fail_malloc_areas:
280 kfree(rc->active_pages);
281 fail_malloc_active_pages:
282 kfree(rc->active_entries);
283 fail_malloc_active_entries:
284 kfree(rc);
285 fail_malloc_rc:
286 return NULL;
287 }
288
289 /*
290 * debug_areas_free
291 * - free all debug areas
292 */
293
294 static void
295 debug_areas_free(debug_info_t* db_info)
296 {
297 int i,j;
298
299 if(!db_info->areas)
300 return;
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]);
304 }
305 kfree(db_info->areas[i]);
306 }
307 kfree(db_info->areas);
308 db_info->areas = NULL;
309 }
310
311 /*
312 * debug_info_free
313 * - free memory debug-info
314 */
315
316 static void
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);
321 kfree(db_info);
322 }
323
324 /*
325 * debug_info_create
326 * - create new debug-info
327 */
328
329 static debug_info_t*
330 debug_info_create(char *name, int pages_per_area, int nr_areas, int buf_size,
331 mode_t mode)
332 {
333 debug_info_t* rc;
334
335 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
336 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
337 if(!rc)
338 goto out;
339
340 rc->mode = mode & ~S_IFMT;
341
342 /* create root directory */
343 rc->debugfs_root_entry = debugfs_create_dir(rc->name,
344 debug_debugfs_root_entry);
345
346 /* append new element to linked list */
347 if (!debug_area_first) {
348 /* first element in list */
349 debug_area_first = rc;
350 rc->prev = NULL;
351 } else {
352 /* append element to end of list */
353 debug_area_last->next = rc;
354 rc->prev = debug_area_last;
355 }
356 debug_area_last = rc;
357 rc->next = NULL;
358
359 debug_info_get(rc);
360 out:
361 return rc;
362 }
363
364 /*
365 * debug_info_copy
366 * - copy debug-info
367 */
368
369 static debug_info_t*
370 debug_info_copy(debug_info_t* in, int mode)
371 {
372 int i,j;
373 debug_info_t* rc;
374 unsigned long flags;
375
376 /* get a consistent copy of the debug areas */
377 do {
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);
381 if(!rc)
382 goto out;
383 /* has something changed in the meantime ? */
384 if((rc->pages_per_area == in->pages_per_area) &&
385 (rc->nr_areas == in->nr_areas)) {
386 break;
387 }
388 spin_unlock_irqrestore(&in->lock, flags);
389 debug_info_free(rc);
390 } while (1);
391
392 if(!rc || (mode == NO_AREAS))
393 goto out;
394
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);
398 }
399 }
400 out:
401 spin_unlock_irqrestore(&in->lock, flags);
402 return rc;
403 }
404
405 /*
406 * debug_info_get
407 * - increments reference count for debug-info
408 */
409
410 static void
411 debug_info_get(debug_info_t * db_info)
412 {
413 if (db_info)
414 atomic_inc(&db_info->ref_count);
415 }
416
417 /*
418 * debug_info_put:
419 * - decreases reference count for debug-info and frees it if necessary
420 */
421
422 static void
423 debug_info_put(debug_info_t *db_info)
424 {
425 int i;
426
427 if (!db_info)
428 return;
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])
432 continue;
433 debugfs_remove(db_info->debugfs_entries[i]);
434 }
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);
443 }
444 }
445
446 /*
447 * debug_format_entry:
448 * - format one debug entry and return size of formated data
449 */
450
451 static int
452 debug_format_entry(file_private_info_t *p_info)
453 {
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;
457 size_t len = 0;
458 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
459 /* print prolog */
460 if (view->prolog_proc)
461 len += view->prolog_proc(id_snap,view,p_info->temp_buf);
462 goto out;
463 }
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);
468
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));
477 out:
478 return len;
479 }
480
481 /*
482 * debug_next_entry:
483 * - goto next entry in p_info
484 */
485
486 static inline int
487 debug_next_entry(file_private_info_t *p_info)
488 {
489 debug_info_t *id;
490
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;
495 goto out;
496 }
497 if(!id->areas)
498 return 1;
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)){
502 /* next page */
503 p_info->act_entry = 0;
504 p_info->act_page += 1;
505 if((p_info->act_page % id->pages_per_area) == 0) {
506 /* next area */
507 p_info->act_area++;
508 p_info->act_page=0;
509 }
510 if(p_info->act_area >= id->nr_areas)
511 return 1;
512 }
513 out:
514 return 0;
515 }
516
517 /*
518 * debug_output:
519 * - called for user read()
520 * - copies formated debug entries to the user buffer
521 */
522
523 static ssize_t
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 */
528 {
529 size_t count = 0;
530 size_t entry_offset;
531 file_private_info_t *p_info;
532
533 p_info = ((file_private_info_t *) file->private_data);
534 if (*offset != p_info->offset)
535 return -EPIPE;
536 if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
537 return 0;
538 entry_offset = p_info->act_entry_offset;
539 while(count < len){
540 int formatted_line_size;
541 int formatted_line_residue;
542 int user_buf_residue;
543 size_t copy_size;
544
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);
549 if(copy_size){
550 if (copy_to_user(user_buf + count, p_info->temp_buf
551 + entry_offset, copy_size))
552 return -EFAULT;
553 count += copy_size;
554 entry_offset += copy_size;
555 }
556 if(copy_size == formatted_line_residue){
557 entry_offset = 0;
558 if(debug_next_entry(p_info))
559 goto out;
560 }
561 }
562 out:
563 p_info->offset = *offset + count;
564 p_info->act_entry_offset = entry_offset;
565 *offset = p_info->offset;
566 return count;
567 }
568
569 /*
570 * debug_input:
571 * - called for user write()
572 * - calls input function of view
573 */
574
575 static ssize_t
576 debug_input(struct file *file, const char __user *user_buf, size_t length,
577 loff_t *offset)
578 {
579 int rc = 0;
580 file_private_info_t *p_info;
581
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,
587 length, offset);
588 else
589 rc = -EPERM;
590 mutex_unlock(&debug_mutex);
591 return rc; /* number of input characters */
592 }
593
594 /*
595 * debug_open:
596 * - called for user open()
597 * - copies formated output to private_data area of the file
598 * handle
599 */
600
601 static int
602 debug_open(struct inode *inode, struct file *file)
603 {
604 int i = 0, rc = 0;
605 file_private_info_t *p_info;
606 debug_info_t *debug_info, *debug_info_snapshot;
607
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])
613 continue;
614 else if (debug_info->debugfs_entries[i] ==
615 file->f_path.dentry) {
616 goto found; /* found view ! */
617 }
618 }
619 /* no entry found */
620 rc = -EINVAL;
621 goto out;
622
623 found:
624
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. */
628
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);
632 } else {
633 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
634 }
635
636 if(!debug_info_snapshot){
637 rc = -ENOMEM;
638 goto out;
639 }
640 p_info = kmalloc(sizeof(file_private_info_t),
641 GFP_KERNEL);
642 if(!p_info){
643 if(debug_info_snapshot)
644 debug_info_free(debug_info_snapshot);
645 rc = -ENOMEM;
646 goto out;
647 }
648 p_info->offset = 0;
649 p_info->debug_info_snap = debug_info_snapshot;
650 p_info->debug_info_org = debug_info;
651 p_info->view = debug_info->views[i];
652 p_info->act_area = 0;
653 p_info->act_page = 0;
654 p_info->act_entry = DEBUG_PROLOG_ENTRY;
655 p_info->act_entry_offset = 0;
656 file->private_data = p_info;
657 debug_info_get(debug_info);
658 out:
659 mutex_unlock(&debug_mutex);
660 return rc;
661 }
662
663 /*
664 * debug_close:
665 * - called for user close()
666 * - deletes private_data area of the file handle
667 */
668
669 static int
670 debug_close(struct inode *inode, struct file *file)
671 {
672 file_private_info_t *p_info;
673 p_info = (file_private_info_t *) file->private_data;
674 if(p_info->debug_info_snap)
675 debug_info_free(p_info->debug_info_snap);
676 debug_info_put(p_info->debug_info_org);
677 kfree(file->private_data);
678 return 0; /* success */
679 }
680
681 /*
682 * debug_register_mode:
683 * - Creates and initializes debug area for the caller
684 * The mode parameter allows to specify access rights for the s390dbf files
685 * - Returns handle for debug area
686 */
687
688 debug_info_t *debug_register_mode(char *name, int pages_per_area, int nr_areas,
689 int buf_size, mode_t mode, uid_t uid,
690 gid_t gid)
691 {
692 debug_info_t *rc = NULL;
693
694 /* Since debugfs currently does not support uid/gid other than root, */
695 /* we do not allow gid/uid != 0 until we get support for that. */
696 if ((uid != 0) || (gid != 0))
697 printk(KERN_WARNING "debug: Warning - Currently only uid/gid "
698 "= 0 are supported. Using root as owner now!");
699 if (!initialized)
700 BUG();
701 mutex_lock(&debug_mutex);
702
703 /* create new debug_info */
704
705 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
706 if(!rc)
707 goto out;
708 debug_register_view(rc, &debug_level_view);
709 debug_register_view(rc, &debug_flush_view);
710 debug_register_view(rc, &debug_pages_view);
711 out:
712 if (!rc){
713 printk(KERN_ERR "debug: debug_register failed for %s\n",name);
714 }
715 mutex_unlock(&debug_mutex);
716 return rc;
717 }
718 EXPORT_SYMBOL(debug_register_mode);
719
720 /*
721 * debug_register:
722 * - creates and initializes debug area for the caller
723 * - returns handle for debug area
724 */
725
726 debug_info_t *debug_register(char *name, int pages_per_area, int nr_areas,
727 int buf_size)
728 {
729 return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
730 S_IRUSR | S_IWUSR, 0, 0);
731 }
732
733 /*
734 * debug_unregister:
735 * - give back debug area
736 */
737
738 void
739 debug_unregister(debug_info_t * id)
740 {
741 if (!id)
742 goto out;
743 mutex_lock(&debug_mutex);
744 debug_info_put(id);
745 mutex_unlock(&debug_mutex);
746
747 out:
748 return;
749 }
750
751 /*
752 * debug_set_size:
753 * - set area size (number of pages) and number of areas
754 */
755 static int
756 debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
757 {
758 unsigned long flags;
759 debug_entry_t *** new_areas;
760 int rc=0;
761
762 if(!id || (nr_areas <= 0) || (pages_per_area < 0))
763 return -EINVAL;
764 if(pages_per_area > 0){
765 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
766 if(!new_areas) {
767 printk(KERN_WARNING "debug: could not allocate memory "\
768 "for pagenumber: %i\n",pages_per_area);
769 rc = -ENOMEM;
770 goto out;
771 }
772 } else {
773 new_areas = NULL;
774 }
775 spin_lock_irqsave(&id->lock,flags);
776 debug_areas_free(id);
777 id->areas = new_areas;
778 id->nr_areas = nr_areas;
779 id->pages_per_area = pages_per_area;
780 id->active_area = 0;
781 memset(id->active_entries,0,sizeof(int)*id->nr_areas);
782 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
783 spin_unlock_irqrestore(&id->lock,flags);
784 printk(KERN_INFO "debug: %s: set new size (%i pages)\n"\
785 ,id->name, pages_per_area);
786 out:
787 return rc;
788 }
789
790 /*
791 * debug_set_level:
792 * - set actual debug level
793 */
794
795 void
796 debug_set_level(debug_info_t* id, int new_level)
797 {
798 unsigned long flags;
799 if(!id)
800 return;
801 spin_lock_irqsave(&id->lock,flags);
802 if(new_level == DEBUG_OFF_LEVEL){
803 id->level = DEBUG_OFF_LEVEL;
804 printk(KERN_INFO "debug: %s: switched off\n",id->name);
805 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
806 printk(KERN_INFO
807 "debug: %s: level %i is out of range (%i - %i)\n",
808 id->name, new_level, 0, DEBUG_MAX_LEVEL);
809 } else {
810 id->level = new_level;
811 }
812 spin_unlock_irqrestore(&id->lock,flags);
813 }
814
815
816 /*
817 * proceed_active_entry:
818 * - set active entry to next in the ring buffer
819 */
820
821 static inline void
822 proceed_active_entry(debug_info_t * id)
823 {
824 if ((id->active_entries[id->active_area] += id->entry_size)
825 > (PAGE_SIZE - id->entry_size)){
826 id->active_entries[id->active_area] = 0;
827 id->active_pages[id->active_area] =
828 (id->active_pages[id->active_area] + 1) %
829 id->pages_per_area;
830 }
831 }
832
833 /*
834 * proceed_active_area:
835 * - set active area to next in the ring buffer
836 */
837
838 static inline void
839 proceed_active_area(debug_info_t * id)
840 {
841 id->active_area++;
842 id->active_area = id->active_area % id->nr_areas;
843 }
844
845 /*
846 * get_active_entry:
847 */
848
849 static inline debug_entry_t*
850 get_active_entry(debug_info_t * id)
851 {
852 return (debug_entry_t *) (((char *) id->areas[id->active_area]
853 [id->active_pages[id->active_area]]) +
854 id->active_entries[id->active_area]);
855 }
856
857 /*
858 * debug_finish_entry:
859 * - set timestamp, caller address, cpu number etc.
860 */
861
862 static inline void
863 debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
864 int exception)
865 {
866 active->id.stck = get_clock();
867 active->id.fields.cpuid = smp_processor_id();
868 active->caller = __builtin_return_address(0);
869 active->id.fields.exception = exception;
870 active->id.fields.level = level;
871 proceed_active_entry(id);
872 if(exception)
873 proceed_active_area(id);
874 }
875
876 static int debug_stoppable=1;
877 static int debug_active=1;
878
879 #define CTL_S390DBF_STOPPABLE 5678
880 #define CTL_S390DBF_ACTIVE 5679
881
882 /*
883 * proc handler for the running debug_active sysctl
884 * always allow read, allow write only if debug_stoppable is set or
885 * if debug_active is already off
886 */
887 static int
888 s390dbf_procactive(ctl_table *table, int write, struct file *filp,
889 void __user *buffer, size_t *lenp, loff_t *ppos)
890 {
891 if (!write || debug_stoppable || !debug_active)
892 return proc_dointvec(table, write, filp, buffer, lenp, ppos);
893 else
894 return 0;
895 }
896
897
898 static struct ctl_table s390dbf_table[] = {
899 {
900 .ctl_name = CTL_S390DBF_STOPPABLE,
901 .procname = "debug_stoppable",
902 .data = &debug_stoppable,
903 .maxlen = sizeof(int),
904 .mode = S_IRUGO | S_IWUSR,
905 .proc_handler = &proc_dointvec,
906 .strategy = &sysctl_intvec,
907 },
908 {
909 .ctl_name = CTL_S390DBF_ACTIVE,
910 .procname = "debug_active",
911 .data = &debug_active,
912 .maxlen = sizeof(int),
913 .mode = S_IRUGO | S_IWUSR,
914 .proc_handler = &s390dbf_procactive,
915 .strategy = &sysctl_intvec,
916 },
917 { .ctl_name = 0 }
918 };
919
920 static struct ctl_table s390dbf_dir_table[] = {
921 {
922 .ctl_name = CTL_S390DBF,
923 .procname = "s390dbf",
924 .maxlen = 0,
925 .mode = S_IRUGO | S_IXUGO,
926 .child = s390dbf_table,
927 },
928 { .ctl_name = 0 }
929 };
930
931 static struct ctl_table_header *s390dbf_sysctl_header;
932
933 void
934 debug_stop_all(void)
935 {
936 if (debug_stoppable)
937 debug_active = 0;
938 }
939
940
941 /*
942 * debug_event_common:
943 * - write debug entry with given size
944 */
945
946 debug_entry_t*
947 debug_event_common(debug_info_t * id, int level, const void *buf, int len)
948 {
949 unsigned long flags;
950 debug_entry_t *active;
951
952 if (!debug_active || !id->areas)
953 return NULL;
954 spin_lock_irqsave(&id->lock, flags);
955 active = get_active_entry(id);
956 memset(DEBUG_DATA(active), 0, id->buf_size);
957 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
958 debug_finish_entry(id, active, level, 0);
959 spin_unlock_irqrestore(&id->lock, flags);
960
961 return active;
962 }
963
964 /*
965 * debug_exception_common:
966 * - write debug entry with given size and switch to next debug area
967 */
968
969 debug_entry_t
970 *debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
971 {
972 unsigned long flags;
973 debug_entry_t *active;
974
975 if (!debug_active || !id->areas)
976 return NULL;
977 spin_lock_irqsave(&id->lock, flags);
978 active = get_active_entry(id);
979 memset(DEBUG_DATA(active), 0, id->buf_size);
980 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
981 debug_finish_entry(id, active, level, 1);
982 spin_unlock_irqrestore(&id->lock, flags);
983
984 return active;
985 }
986
987 /*
988 * counts arguments in format string for sprintf view
989 */
990
991 static inline int
992 debug_count_numargs(char *string)
993 {
994 int numargs=0;
995
996 while(*string) {
997 if(*string++=='%')
998 numargs++;
999 }
1000 return(numargs);
1001 }
1002
1003 /*
1004 * debug_sprintf_event:
1005 */
1006
1007 debug_entry_t*
1008 debug_sprintf_event(debug_info_t* id, int level,char *string,...)
1009 {
1010 va_list ap;
1011 int numargs,idx;
1012 unsigned long flags;
1013 debug_sprintf_entry_t *curr_event;
1014 debug_entry_t *active;
1015
1016 if((!id) || (level > id->level))
1017 return NULL;
1018 if (!debug_active || !id->areas)
1019 return NULL;
1020 numargs=debug_count_numargs(string);
1021
1022 spin_lock_irqsave(&id->lock, flags);
1023 active = get_active_entry(id);
1024 curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
1025 va_start(ap,string);
1026 curr_event->string=string;
1027 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1028 curr_event->args[idx]=va_arg(ap,long);
1029 va_end(ap);
1030 debug_finish_entry(id, active, level, 0);
1031 spin_unlock_irqrestore(&id->lock, flags);
1032
1033 return active;
1034 }
1035
1036 /*
1037 * debug_sprintf_exception:
1038 */
1039
1040 debug_entry_t*
1041 debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
1042 {
1043 va_list ap;
1044 int numargs,idx;
1045 unsigned long flags;
1046 debug_sprintf_entry_t *curr_event;
1047 debug_entry_t *active;
1048
1049 if((!id) || (level > id->level))
1050 return NULL;
1051 if (!debug_active || !id->areas)
1052 return NULL;
1053
1054 numargs=debug_count_numargs(string);
1055
1056 spin_lock_irqsave(&id->lock, flags);
1057 active = get_active_entry(id);
1058 curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
1059 va_start(ap,string);
1060 curr_event->string=string;
1061 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1062 curr_event->args[idx]=va_arg(ap,long);
1063 va_end(ap);
1064 debug_finish_entry(id, active, level, 1);
1065 spin_unlock_irqrestore(&id->lock, flags);
1066
1067 return active;
1068 }
1069
1070 /*
1071 * debug_init:
1072 * - is called exactly once to initialize the debug feature
1073 */
1074
1075 static int
1076 __init debug_init(void)
1077 {
1078 int rc = 0;
1079
1080 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
1081 mutex_lock(&debug_mutex);
1082 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
1083 printk(KERN_INFO "debug: Initialization complete\n");
1084 initialized = 1;
1085 mutex_unlock(&debug_mutex);
1086
1087 return rc;
1088 }
1089
1090 /*
1091 * debug_register_view:
1092 */
1093
1094 int
1095 debug_register_view(debug_info_t * id, struct debug_view *view)
1096 {
1097 int rc = 0;
1098 int i;
1099 unsigned long flags;
1100 mode_t mode;
1101 struct dentry *pde;
1102
1103 if (!id)
1104 goto out;
1105 mode = (id->mode | S_IFREG) & ~S_IXUGO;
1106 if (!(view->prolog_proc || view->format_proc || view->header_proc))
1107 mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
1108 if (!view->input_proc)
1109 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1110 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
1111 id , &debug_file_ops);
1112 if (!pde){
1113 printk(KERN_WARNING "debug: debugfs_create_file() failed!"\
1114 " Cannot register view %s/%s\n", id->name,view->name);
1115 rc = -1;
1116 goto out;
1117 }
1118 spin_lock_irqsave(&id->lock, flags);
1119 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1120 if (!id->views[i])
1121 break;
1122 }
1123 if (i == DEBUG_MAX_VIEWS) {
1124 printk(KERN_WARNING "debug: cannot register view %s/%s\n",
1125 id->name,view->name);
1126 printk(KERN_WARNING
1127 "debug: maximum number of views reached (%i)!\n", i);
1128 debugfs_remove(pde);
1129 rc = -1;
1130 } else {
1131 id->views[i] = view;
1132 id->debugfs_entries[i] = pde;
1133 }
1134 spin_unlock_irqrestore(&id->lock, flags);
1135 out:
1136 return rc;
1137 }
1138
1139 /*
1140 * debug_unregister_view:
1141 */
1142
1143 int
1144 debug_unregister_view(debug_info_t * id, struct debug_view *view)
1145 {
1146 int rc = 0;
1147 int i;
1148 unsigned long flags;
1149
1150 if (!id)
1151 goto out;
1152 spin_lock_irqsave(&id->lock, flags);
1153 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1154 if (id->views[i] == view)
1155 break;
1156 }
1157 if (i == DEBUG_MAX_VIEWS)
1158 rc = -1;
1159 else {
1160 debugfs_remove(id->debugfs_entries[i]);
1161 id->views[i] = NULL;
1162 rc = 0;
1163 }
1164 spin_unlock_irqrestore(&id->lock, flags);
1165 out:
1166 return rc;
1167 }
1168
1169 static inline char *
1170 debug_get_user_string(const char __user *user_buf, size_t user_len)
1171 {
1172 char* buffer;
1173
1174 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1175 if (!buffer)
1176 return ERR_PTR(-ENOMEM);
1177 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1178 kfree(buffer);
1179 return ERR_PTR(-EFAULT);
1180 }
1181 /* got the string, now strip linefeed. */
1182 if (buffer[user_len - 1] == '\n')
1183 buffer[user_len - 1] = 0;
1184 else
1185 buffer[user_len] = 0;
1186 return buffer;
1187 }
1188
1189 static inline int
1190 debug_get_uint(char *buf)
1191 {
1192 int rc;
1193
1194 for(; isspace(*buf); buf++);
1195 rc = simple_strtoul(buf, &buf, 10);
1196 if(*buf){
1197 printk("debug: no integer specified!\n");
1198 rc = -EINVAL;
1199 }
1200 return rc;
1201 }
1202
1203 /*
1204 * functions for debug-views
1205 ***********************************
1206 */
1207
1208 /*
1209 * prints out actual debug level
1210 */
1211
1212 static int
1213 debug_prolog_pages_fn(debug_info_t * id,
1214 struct debug_view *view, char *out_buf)
1215 {
1216 return sprintf(out_buf, "%i\n", id->pages_per_area);
1217 }
1218
1219 /*
1220 * reads new size (number of pages per debug area)
1221 */
1222
1223 static int
1224 debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
1225 struct file *file, const char __user *user_buf,
1226 size_t user_len, loff_t * offset)
1227 {
1228 char *str;
1229 int rc,new_pages;
1230
1231 if (user_len > 0x10000)
1232 user_len = 0x10000;
1233 if (*offset != 0){
1234 rc = -EPIPE;
1235 goto out;
1236 }
1237 str = debug_get_user_string(user_buf,user_len);
1238 if(IS_ERR(str)){
1239 rc = PTR_ERR(str);
1240 goto out;
1241 }
1242 new_pages = debug_get_uint(str);
1243 if(new_pages < 0){
1244 rc = -EINVAL;
1245 goto free_str;
1246 }
1247 rc = debug_set_size(id,id->nr_areas, new_pages);
1248 if(rc != 0){
1249 rc = -EINVAL;
1250 goto free_str;
1251 }
1252 rc = user_len;
1253 free_str:
1254 kfree(str);
1255 out:
1256 *offset += user_len;
1257 return rc; /* number of input characters */
1258 }
1259
1260 /*
1261 * prints out actual debug level
1262 */
1263
1264 static int
1265 debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
1266 {
1267 int rc = 0;
1268
1269 if(id->level == DEBUG_OFF_LEVEL) {
1270 rc = sprintf(out_buf,"-\n");
1271 }
1272 else {
1273 rc = sprintf(out_buf, "%i\n", id->level);
1274 }
1275 return rc;
1276 }
1277
1278 /*
1279 * reads new debug level
1280 */
1281
1282 static int
1283 debug_input_level_fn(debug_info_t * id, struct debug_view *view,
1284 struct file *file, const char __user *user_buf,
1285 size_t user_len, loff_t * offset)
1286 {
1287 char *str;
1288 int rc,new_level;
1289
1290 if (user_len > 0x10000)
1291 user_len = 0x10000;
1292 if (*offset != 0){
1293 rc = -EPIPE;
1294 goto out;
1295 }
1296 str = debug_get_user_string(user_buf,user_len);
1297 if(IS_ERR(str)){
1298 rc = PTR_ERR(str);
1299 goto out;
1300 }
1301 if(str[0] == '-'){
1302 debug_set_level(id, DEBUG_OFF_LEVEL);
1303 rc = user_len;
1304 goto free_str;
1305 } else {
1306 new_level = debug_get_uint(str);
1307 }
1308 if(new_level < 0) {
1309 printk(KERN_INFO "debug: level `%s` is not valid\n", str);
1310 rc = -EINVAL;
1311 } else {
1312 debug_set_level(id, new_level);
1313 rc = user_len;
1314 }
1315 free_str:
1316 kfree(str);
1317 out:
1318 *offset += user_len;
1319 return rc; /* number of input characters */
1320 }
1321
1322
1323 /*
1324 * flushes debug areas
1325 */
1326
1327 static void debug_flush(debug_info_t* id, int area)
1328 {
1329 unsigned long flags;
1330 int i,j;
1331
1332 if(!id || !id->areas)
1333 return;
1334 spin_lock_irqsave(&id->lock,flags);
1335 if(area == DEBUG_FLUSH_ALL){
1336 id->active_area = 0;
1337 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1338 for (i = 0; i < id->nr_areas; i++) {
1339 id->active_pages[i] = 0;
1340 for(j = 0; j < id->pages_per_area; j++) {
1341 memset(id->areas[i][j], 0, PAGE_SIZE);
1342 }
1343 }
1344 printk(KERN_INFO "debug: %s: all areas flushed\n",id->name);
1345 } else if(area >= 0 && area < id->nr_areas) {
1346 id->active_entries[area] = 0;
1347 id->active_pages[area] = 0;
1348 for(i = 0; i < id->pages_per_area; i++) {
1349 memset(id->areas[area][i],0,PAGE_SIZE);
1350 }
1351 printk(KERN_INFO "debug: %s: area %i has been flushed\n",
1352 id->name, area);
1353 } else {
1354 printk(KERN_INFO
1355 "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
1356 id->name, area, 0, id->nr_areas-1);
1357 }
1358 spin_unlock_irqrestore(&id->lock,flags);
1359 }
1360
1361 /*
1362 * view function: flushes debug areas
1363 */
1364
1365 static int
1366 debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
1367 struct file *file, const char __user *user_buf,
1368 size_t user_len, loff_t * offset)
1369 {
1370 char input_buf[1];
1371 int rc = user_len;
1372
1373 if (user_len > 0x10000)
1374 user_len = 0x10000;
1375 if (*offset != 0){
1376 rc = -EPIPE;
1377 goto out;
1378 }
1379 if (copy_from_user(input_buf, user_buf, 1)){
1380 rc = -EFAULT;
1381 goto out;
1382 }
1383 if(input_buf[0] == '-') {
1384 debug_flush(id, DEBUG_FLUSH_ALL);
1385 goto out;
1386 }
1387 if (isdigit(input_buf[0])) {
1388 int area = ((int) input_buf[0] - (int) '0');
1389 debug_flush(id, area);
1390 goto out;
1391 }
1392
1393 printk(KERN_INFO "debug: area `%c` is not valid\n", input_buf[0]);
1394
1395 out:
1396 *offset += user_len;
1397 return rc; /* number of input characters */
1398 }
1399
1400 /*
1401 * prints debug header in raw format
1402 */
1403
1404 static int
1405 debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
1406 int area, debug_entry_t * entry, char *out_buf)
1407 {
1408 int rc;
1409
1410 rc = sizeof(debug_entry_t);
1411 memcpy(out_buf,entry,sizeof(debug_entry_t));
1412 return rc;
1413 }
1414
1415 /*
1416 * prints debug data in raw format
1417 */
1418
1419 static int
1420 debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
1421 char *out_buf, const char *in_buf)
1422 {
1423 int rc;
1424
1425 rc = id->buf_size;
1426 memcpy(out_buf, in_buf, id->buf_size);
1427 return rc;
1428 }
1429
1430 /*
1431 * prints debug data in hex/ascii format
1432 */
1433
1434 static int
1435 debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
1436 char *out_buf, const char *in_buf)
1437 {
1438 int i, rc = 0;
1439
1440 for (i = 0; i < id->buf_size; i++) {
1441 rc += sprintf(out_buf + rc, "%02x ",
1442 ((unsigned char *) in_buf)[i]);
1443 }
1444 rc += sprintf(out_buf + rc, "| ");
1445 for (i = 0; i < id->buf_size; i++) {
1446 unsigned char c = in_buf[i];
1447 if (!isprint(c))
1448 rc += sprintf(out_buf + rc, ".");
1449 else
1450 rc += sprintf(out_buf + rc, "%c", c);
1451 }
1452 rc += sprintf(out_buf + rc, "\n");
1453 return rc;
1454 }
1455
1456 /*
1457 * prints header for debug entry
1458 */
1459
1460 int
1461 debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
1462 int area, debug_entry_t * entry, char *out_buf)
1463 {
1464 struct timespec time_spec;
1465 unsigned long long time;
1466 char *except_str;
1467 unsigned long caller;
1468 int rc = 0;
1469 unsigned int level;
1470
1471 level = entry->id.fields.level;
1472 time = entry->id.stck;
1473 /* adjust todclock to 1970 */
1474 time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
1475 tod_to_timeval(time, &time_spec);
1476
1477 if (entry->id.fields.exception)
1478 except_str = "*";
1479 else
1480 except_str = "-";
1481 caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
1482 rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
1483 area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
1484 except_str, entry->id.fields.cpuid, (void *) caller);
1485 return rc;
1486 }
1487
1488 /*
1489 * prints debug data sprintf-formated:
1490 * debug_sprinf_event/exception calls must be used together with this view
1491 */
1492
1493 #define DEBUG_SPRINTF_MAX_ARGS 10
1494
1495 static int
1496 debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
1497 char *out_buf, debug_sprintf_entry_t *curr_event)
1498 {
1499 int num_longs, num_used_args = 0,i, rc = 0;
1500 int index[DEBUG_SPRINTF_MAX_ARGS];
1501
1502 /* count of longs fit into one entry */
1503 num_longs = id->buf_size / sizeof(long);
1504
1505 if(num_longs < 1)
1506 goto out; /* bufsize of entry too small */
1507 if(num_longs == 1) {
1508 /* no args, we use only the string */
1509 strcpy(out_buf, curr_event->string);
1510 rc = strlen(curr_event->string);
1511 goto out;
1512 }
1513
1514 /* number of arguments used for sprintf (without the format string) */
1515 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1516
1517 memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1518
1519 for(i = 0; i < num_used_args; i++)
1520 index[i] = i;
1521
1522 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1523 curr_event->args[index[1]], curr_event->args[index[2]],
1524 curr_event->args[index[3]], curr_event->args[index[4]],
1525 curr_event->args[index[5]], curr_event->args[index[6]],
1526 curr_event->args[index[7]], curr_event->args[index[8]],
1527 curr_event->args[index[9]]);
1528
1529 out:
1530
1531 return rc;
1532 }
1533
1534 /*
1535 * clean up module
1536 */
1537 static void __exit debug_exit(void)
1538 {
1539 debugfs_remove(debug_debugfs_root_entry);
1540 unregister_sysctl_table(s390dbf_sysctl_header);
1541 return;
1542 }
1543
1544 /*
1545 * module definitions
1546 */
1547 postcore_initcall(debug_init);
1548 module_exit(debug_exit);
1549 MODULE_LICENSE("GPL");
1550
1551 EXPORT_SYMBOL(debug_register);
1552 EXPORT_SYMBOL(debug_unregister);
1553 EXPORT_SYMBOL(debug_set_level);
1554 EXPORT_SYMBOL(debug_stop_all);
1555 EXPORT_SYMBOL(debug_register_view);
1556 EXPORT_SYMBOL(debug_unregister_view);
1557 EXPORT_SYMBOL(debug_event_common);
1558 EXPORT_SYMBOL(debug_exception_common);
1559 EXPORT_SYMBOL(debug_hex_ascii_view);
1560 EXPORT_SYMBOL(debug_raw_view);
1561 EXPORT_SYMBOL(debug_dflt_header_fn);
1562 EXPORT_SYMBOL(debug_sprintf_view);
1563 EXPORT_SYMBOL(debug_sprintf_exception);
1564 EXPORT_SYMBOL(debug_sprintf_event);
This page took 0.087428 seconds and 5 git commands to generate.