1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright IBM Corp. 1999, 2012
7 * Author(s): Michael Holzheu (holzheu@de.ibm.com),
8 * Holger Smolinski (Holger.Smolinski@de.ibm.com)
10 * Bugreports to: <Linux390@de.ibm.com>
13 #define KMSG_COMPONENT "s390dbf"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/stddef.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/ctype.h>
21 #include <linux/string.h>
22 #include <linux/sysctl.h>
23 #include <linux/uaccess.h>
24 #include <linux/export.h>
25 #include <linux/init.h>
27 #include <linux/debugfs.h>
29 #include <asm/debug.h>
31 #define DEBUG_PROLOG_ENTRY -1
33 #define ALL_AREAS 0 /* copy all debug areas */
34 #define NO_AREAS 1 /* copy no debug areas */
38 typedef struct file_private_info {
39 loff_t offset; /* offset of last read in file */
40 int act_area; /* number of last formated area */
41 int act_page; /* act page in given area */
42 int act_entry; /* last formated entry (offset */
43 /* relative to beginning of last */
45 size_t act_entry_offset; /* up to this offset we copied */
46 /* in last read the last formated */
47 /* entry to userland */
48 char temp_buf[2048]; /* buffer for output */
49 debug_info_t *debug_info_org; /* original debug information */
50 debug_info_t *debug_info_snap; /* snapshot of debug information */
51 struct debug_view *view; /* used view of debug info */
52 } file_private_info_t;
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;
65 /* internal function prototyes */
67 static int debug_init(void);
68 static ssize_t debug_output(struct file *file, char __user *user_buf,
69 size_t user_len, loff_t *offset);
70 static ssize_t debug_input(struct file *file, const char __user *user_buf,
71 size_t user_len, loff_t *offset);
72 static int debug_open(struct inode *inode, struct file *file);
73 static int debug_close(struct inode *inode, struct file *file);
74 static debug_info_t *debug_info_create(const char *name, int pages_per_area,
75 int nr_areas, int buf_size, umode_t mode);
76 static void debug_info_get(debug_info_t *);
77 static void debug_info_put(debug_info_t *);
78 static int debug_prolog_level_fn(debug_info_t *id,
79 struct debug_view *view, char *out_buf);
80 static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
81 struct file *file, const char __user *user_buf,
82 size_t user_buf_size, loff_t *offset);
83 static int debug_prolog_pages_fn(debug_info_t *id,
84 struct debug_view *view, char *out_buf);
85 static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
86 struct file *file, const char __user *user_buf,
87 size_t user_buf_size, loff_t *offset);
88 static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
89 struct file *file, const char __user *user_buf,
90 size_t user_buf_size, loff_t *offset);
91 static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
92 char *out_buf, const char *in_buf);
93 static int debug_raw_format_fn(debug_info_t *id,
94 struct debug_view *view, char *out_buf,
96 static int debug_raw_header_fn(debug_info_t *id, struct debug_view *view,
97 int area, debug_entry_t *entry, char *out_buf);
99 static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
100 char *out_buf, debug_sprintf_entry_t *curr_event);
104 struct debug_view debug_raw_view = {
107 &debug_raw_header_fn,
108 &debug_raw_format_fn,
112 EXPORT_SYMBOL(debug_raw_view);
114 struct debug_view debug_hex_ascii_view = {
117 &debug_dflt_header_fn,
118 &debug_hex_ascii_format_fn,
122 EXPORT_SYMBOL(debug_hex_ascii_view);
124 static struct debug_view debug_level_view = {
126 &debug_prolog_level_fn,
129 &debug_input_level_fn,
133 static struct debug_view debug_pages_view = {
135 &debug_prolog_pages_fn,
138 &debug_input_pages_fn,
142 static struct debug_view debug_flush_view = {
147 &debug_input_flush_fn,
151 struct debug_view debug_sprintf_view = {
154 &debug_dflt_header_fn,
155 (debug_format_proc_t *)&debug_sprintf_format_fn,
159 EXPORT_SYMBOL(debug_sprintf_view);
161 /* used by dump analysis tools to determine version of debug feature */
162 static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
166 static debug_info_t *debug_area_first;
167 static debug_info_t *debug_area_last;
168 static DEFINE_MUTEX(debug_mutex);
170 static int initialized;
171 static int debug_critical;
173 static const struct file_operations debug_file_ops = {
174 .owner = THIS_MODULE,
175 .read = debug_output,
176 .write = debug_input,
178 .release = debug_close,
182 static struct dentry *debug_debugfs_root_entry;
188 * - Debug areas are implemented as a threedimensonal array:
189 * areas[areanumber][pagenumber][pageoffset]
192 static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas)
194 debug_entry_t ***areas;
197 areas = kmalloc_array(nr_areas, sizeof(debug_entry_t **), GFP_KERNEL);
199 goto fail_malloc_areas;
200 for (i = 0; i < nr_areas; i++) {
201 /* GFP_NOWARN to avoid user triggerable WARN, we handle fails */
202 areas[i] = kmalloc_array(pages_per_area,
203 sizeof(debug_entry_t *),
204 GFP_KERNEL | __GFP_NOWARN);
206 goto fail_malloc_areas2;
207 for (j = 0; j < pages_per_area; j++) {
208 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
210 for (j--; j >= 0 ; j--)
213 goto fail_malloc_areas2;
220 for (i--; i >= 0; i--) {
221 for (j = 0; j < pages_per_area; j++)
232 * - alloc new debug-info
234 static debug_info_t *debug_info_alloc(const char *name, int pages_per_area,
235 int nr_areas, int buf_size, int level,
240 /* alloc everything */
241 rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
244 rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
245 if (!rc->active_entries)
246 goto fail_malloc_active_entries;
247 rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
248 if (!rc->active_pages)
249 goto fail_malloc_active_pages;
250 if ((mode == ALL_AREAS) && (pages_per_area != 0)) {
251 rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
253 goto fail_malloc_areas;
258 /* initialize members */
259 spin_lock_init(&rc->lock);
260 rc->pages_per_area = pages_per_area;
261 rc->nr_areas = nr_areas;
264 rc->buf_size = buf_size;
265 rc->entry_size = sizeof(debug_entry_t) + buf_size;
266 strlcpy(rc->name, name, sizeof(rc->name));
267 memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
268 memset(rc->debugfs_entries, 0, DEBUG_MAX_VIEWS * sizeof(struct dentry *));
269 refcount_set(&(rc->ref_count), 0);
274 kfree(rc->active_pages);
275 fail_malloc_active_pages:
276 kfree(rc->active_entries);
277 fail_malloc_active_entries:
285 * - free all debug areas
287 static void debug_areas_free(debug_info_t *db_info)
293 for (i = 0; i < db_info->nr_areas; i++) {
294 for (j = 0; j < db_info->pages_per_area; j++)
295 kfree(db_info->areas[i][j]);
296 kfree(db_info->areas[i]);
298 kfree(db_info->areas);
299 db_info->areas = NULL;
304 * - free memory debug-info
306 static void debug_info_free(debug_info_t *db_info)
308 debug_areas_free(db_info);
309 kfree(db_info->active_entries);
310 kfree(db_info->active_pages);
316 * - create new debug-info
319 static debug_info_t *debug_info_create(const char *name, int pages_per_area,
320 int nr_areas, int buf_size, umode_t mode)
324 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
325 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
329 rc->mode = mode & ~S_IFMT;
331 /* create root directory */
332 rc->debugfs_root_entry = debugfs_create_dir(rc->name,
333 debug_debugfs_root_entry);
335 /* append new element to linked list */
336 if (!debug_area_first) {
337 /* first element in list */
338 debug_area_first = rc;
341 /* append element to end of list */
342 debug_area_last->next = rc;
343 rc->prev = debug_area_last;
345 debug_area_last = rc;
348 refcount_set(&rc->ref_count, 1);
357 static debug_info_t *debug_info_copy(debug_info_t *in, int mode)
363 /* get a consistent copy of the debug areas */
365 rc = debug_info_alloc(in->name, in->pages_per_area,
366 in->nr_areas, in->buf_size, in->level, mode);
367 spin_lock_irqsave(&in->lock, flags);
370 /* has something changed in the meantime ? */
371 if ((rc->pages_per_area == in->pages_per_area) &&
372 (rc->nr_areas == in->nr_areas)) {
375 spin_unlock_irqrestore(&in->lock, flags);
379 if (mode == NO_AREAS)
382 for (i = 0; i < in->nr_areas; i++) {
383 for (j = 0; j < in->pages_per_area; j++)
384 memcpy(rc->areas[i][j], in->areas[i][j], PAGE_SIZE);
387 spin_unlock_irqrestore(&in->lock, flags);
393 * - increments reference count for debug-info
395 static void debug_info_get(debug_info_t *db_info)
398 refcount_inc(&db_info->ref_count);
403 * - decreases reference count for debug-info and frees it if necessary
405 static void debug_info_put(debug_info_t *db_info)
411 if (refcount_dec_and_test(&db_info->ref_count)) {
412 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
413 if (!db_info->views[i])
415 debugfs_remove(db_info->debugfs_entries[i]);
417 debugfs_remove(db_info->debugfs_root_entry);
418 if (db_info == debug_area_first)
419 debug_area_first = db_info->next;
420 if (db_info == debug_area_last)
421 debug_area_last = db_info->prev;
423 db_info->prev->next = db_info->next;
425 db_info->next->prev = db_info->prev;
426 debug_info_free(db_info);
431 * debug_format_entry:
432 * - format one debug entry and return size of formated data
434 static int debug_format_entry(file_private_info_t *p_info)
436 debug_info_t *id_snap = p_info->debug_info_snap;
437 struct debug_view *view = p_info->view;
438 debug_entry_t *act_entry;
441 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
443 if (view->prolog_proc)
444 len += view->prolog_proc(id_snap, view, p_info->temp_buf);
447 if (!id_snap->areas) /* this is true, if we have a prolog only view */
448 goto out; /* or if 'pages_per_area' is 0 */
449 act_entry = (debug_entry_t *) ((char *)id_snap->areas[p_info->act_area]
450 [p_info->act_page] + p_info->act_entry);
452 if (act_entry->id.stck == 0LL)
453 goto out; /* empty entry */
454 if (view->header_proc)
455 len += view->header_proc(id_snap, view, p_info->act_area,
456 act_entry, p_info->temp_buf + len);
457 if (view->format_proc)
458 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
459 DEBUG_DATA(act_entry));
466 * - goto next entry in p_info
468 static inline int debug_next_entry(file_private_info_t *p_info)
472 id = p_info->debug_info_snap;
473 if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
474 p_info->act_entry = 0;
475 p_info->act_page = 0;
480 p_info->act_entry += id->entry_size;
481 /* switch to next page, if we reached the end of the page */
482 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)) {
484 p_info->act_entry = 0;
485 p_info->act_page += 1;
486 if ((p_info->act_page % id->pages_per_area) == 0) {
489 p_info->act_page = 0;
491 if (p_info->act_area >= id->nr_areas)
500 * - called for user read()
501 * - copies formated debug entries to the user buffer
503 static ssize_t debug_output(struct file *file, /* file descriptor */
504 char __user *user_buf, /* user buffer */
505 size_t len, /* length of buffer */
506 loff_t *offset) /* offset in the file */
510 file_private_info_t *p_info;
512 p_info = (file_private_info_t *) file->private_data;
513 if (*offset != p_info->offset)
515 if (p_info->act_area >= p_info->debug_info_snap->nr_areas)
517 entry_offset = p_info->act_entry_offset;
518 while (count < len) {
519 int formatted_line_residue;
520 int formatted_line_size;
521 int user_buf_residue;
524 formatted_line_size = debug_format_entry(p_info);
525 formatted_line_residue = formatted_line_size - entry_offset;
526 user_buf_residue = len-count;
527 copy_size = min(user_buf_residue, formatted_line_residue);
529 if (copy_to_user(user_buf + count, p_info->temp_buf
530 + entry_offset, copy_size))
533 entry_offset += copy_size;
535 if (copy_size == formatted_line_residue) {
537 if (debug_next_entry(p_info))
542 p_info->offset = *offset + count;
543 p_info->act_entry_offset = entry_offset;
544 *offset = p_info->offset;
550 * - called for user write()
551 * - calls input function of view
553 static ssize_t debug_input(struct file *file, const char __user *user_buf,
554 size_t length, loff_t *offset)
556 file_private_info_t *p_info;
559 mutex_lock(&debug_mutex);
560 p_info = ((file_private_info_t *) file->private_data);
561 if (p_info->view->input_proc) {
562 rc = p_info->view->input_proc(p_info->debug_info_org,
563 p_info->view, file, user_buf,
568 mutex_unlock(&debug_mutex);
569 return rc; /* number of input characters */
574 * - called for user open()
575 * - copies formated output to private_data area of the file
578 static int debug_open(struct inode *inode, struct file *file)
580 debug_info_t *debug_info, *debug_info_snapshot;
581 file_private_info_t *p_info;
584 mutex_lock(&debug_mutex);
585 debug_info = file_inode(file)->i_private;
586 /* find debug view */
587 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
588 if (!debug_info->views[i])
590 else if (debug_info->debugfs_entries[i] == file->f_path.dentry)
591 goto found; /* found view ! */
599 /* Make snapshot of current debug areas to get it consistent. */
600 /* To copy all the areas is only needed, if we have a view which */
601 /* formats the debug areas. */
603 if (!debug_info->views[i]->format_proc && !debug_info->views[i]->header_proc)
604 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
606 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
608 if (!debug_info_snapshot) {
612 p_info = kmalloc(sizeof(file_private_info_t), GFP_KERNEL);
614 debug_info_free(debug_info_snapshot);
619 p_info->debug_info_snap = debug_info_snapshot;
620 p_info->debug_info_org = debug_info;
621 p_info->view = debug_info->views[i];
622 p_info->act_area = 0;
623 p_info->act_page = 0;
624 p_info->act_entry = DEBUG_PROLOG_ENTRY;
625 p_info->act_entry_offset = 0;
626 file->private_data = p_info;
627 debug_info_get(debug_info);
628 nonseekable_open(inode, file);
630 mutex_unlock(&debug_mutex);
636 * - called for user close()
637 * - deletes private_data area of the file handle
639 static int debug_close(struct inode *inode, struct file *file)
641 file_private_info_t *p_info;
643 p_info = (file_private_info_t *) file->private_data;
644 if (p_info->debug_info_snap)
645 debug_info_free(p_info->debug_info_snap);
646 debug_info_put(p_info->debug_info_org);
647 kfree(file->private_data);
648 return 0; /* success */
652 * debug_register_mode:
653 * - Creates and initializes debug area for the caller
654 * The mode parameter allows to specify access rights for the s390dbf files
655 * - Returns handle for debug area
657 debug_info_t *debug_register_mode(const char *name, int pages_per_area,
658 int nr_areas, int buf_size, umode_t mode,
659 uid_t uid, gid_t gid)
661 debug_info_t *rc = NULL;
663 /* Since debugfs currently does not support uid/gid other than root, */
664 /* we do not allow gid/uid != 0 until we get support for that. */
665 if ((uid != 0) || (gid != 0))
666 pr_warn("Root becomes the owner of all s390dbf files in sysfs\n");
667 BUG_ON(!initialized);
668 mutex_lock(&debug_mutex);
670 /* create new debug_info */
671 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
674 debug_register_view(rc, &debug_level_view);
675 debug_register_view(rc, &debug_flush_view);
676 debug_register_view(rc, &debug_pages_view);
679 pr_err("Registering debug feature %s failed\n", name);
680 mutex_unlock(&debug_mutex);
683 EXPORT_SYMBOL(debug_register_mode);
687 * - creates and initializes debug area for the caller
688 * - returns handle for debug area
690 debug_info_t *debug_register(const char *name, int pages_per_area,
691 int nr_areas, int buf_size)
693 return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
694 S_IRUSR | S_IWUSR, 0, 0);
696 EXPORT_SYMBOL(debug_register);
700 * - give back debug area
702 void debug_unregister(debug_info_t *id)
706 mutex_lock(&debug_mutex);
708 mutex_unlock(&debug_mutex);
710 EXPORT_SYMBOL(debug_unregister);
714 * - set area size (number of pages) and number of areas
716 static int debug_set_size(debug_info_t *id, int nr_areas, int pages_per_area)
718 debug_entry_t ***new_areas;
722 if (!id || (nr_areas <= 0) || (pages_per_area < 0))
724 if (pages_per_area > 0) {
725 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
727 pr_info("Allocating memory for %i pages failed\n",
735 spin_lock_irqsave(&id->lock, flags);
736 debug_areas_free(id);
737 id->areas = new_areas;
738 id->nr_areas = nr_areas;
739 id->pages_per_area = pages_per_area;
741 memset(id->active_entries, 0, sizeof(int)*id->nr_areas);
742 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
743 spin_unlock_irqrestore(&id->lock, flags);
744 pr_info("%s: set new size (%i pages)\n", id->name, pages_per_area);
751 * - set actual debug level
753 void debug_set_level(debug_info_t *id, int new_level)
759 spin_lock_irqsave(&id->lock, flags);
760 if (new_level == DEBUG_OFF_LEVEL) {
761 id->level = DEBUG_OFF_LEVEL;
762 pr_info("%s: switched off\n", id->name);
763 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
764 pr_info("%s: level %i is out of range (%i - %i)\n",
765 id->name, new_level, 0, DEBUG_MAX_LEVEL);
767 id->level = new_level;
769 spin_unlock_irqrestore(&id->lock, flags);
771 EXPORT_SYMBOL(debug_set_level);
774 * proceed_active_entry:
775 * - set active entry to next in the ring buffer
777 static inline void proceed_active_entry(debug_info_t *id)
779 if ((id->active_entries[id->active_area] += id->entry_size)
780 > (PAGE_SIZE - id->entry_size)) {
781 id->active_entries[id->active_area] = 0;
782 id->active_pages[id->active_area] =
783 (id->active_pages[id->active_area] + 1) %
789 * proceed_active_area:
790 * - set active area to next in the ring buffer
792 static inline void proceed_active_area(debug_info_t *id)
795 id->active_area = id->active_area % id->nr_areas;
801 static inline debug_entry_t *get_active_entry(debug_info_t *id)
803 return (debug_entry_t *) (((char *) id->areas[id->active_area]
804 [id->active_pages[id->active_area]]) +
805 id->active_entries[id->active_area]);
809 * debug_finish_entry:
810 * - set timestamp, caller address, cpu number etc.
813 static inline void debug_finish_entry(debug_info_t *id, debug_entry_t *active,
814 int level, int exception)
816 active->id.stck = get_tod_clock_fast() -
817 *(unsigned long long *) &tod_clock_base[1];
818 active->id.fields.cpuid = smp_processor_id();
819 active->caller = __builtin_return_address(0);
820 active->id.fields.exception = exception;
821 active->id.fields.level = level;
822 proceed_active_entry(id);
824 proceed_active_area(id);
827 static int debug_stoppable = 1;
828 static int debug_active = 1;
830 #define CTL_S390DBF_STOPPABLE 5678
831 #define CTL_S390DBF_ACTIVE 5679
834 * proc handler for the running debug_active sysctl
835 * always allow read, allow write only if debug_stoppable is set or
836 * if debug_active is already off
838 static int s390dbf_procactive(struct ctl_table *table, int write,
839 void __user *buffer, size_t *lenp, loff_t *ppos)
841 if (!write || debug_stoppable || !debug_active)
842 return proc_dointvec(table, write, buffer, lenp, ppos);
847 static struct ctl_table s390dbf_table[] = {
849 .procname = "debug_stoppable",
850 .data = &debug_stoppable,
851 .maxlen = sizeof(int),
852 .mode = S_IRUGO | S_IWUSR,
853 .proc_handler = proc_dointvec,
856 .procname = "debug_active",
857 .data = &debug_active,
858 .maxlen = sizeof(int),
859 .mode = S_IRUGO | S_IWUSR,
860 .proc_handler = s390dbf_procactive,
865 static struct ctl_table s390dbf_dir_table[] = {
867 .procname = "s390dbf",
869 .mode = S_IRUGO | S_IXUGO,
870 .child = s390dbf_table,
875 static struct ctl_table_header *s390dbf_sysctl_header;
877 void debug_stop_all(void)
882 EXPORT_SYMBOL(debug_stop_all);
884 void debug_set_critical(void)
890 * debug_event_common:
891 * - write debug entry with given size
893 debug_entry_t *debug_event_common(debug_info_t *id, int level, const void *buf,
896 debug_entry_t *active;
899 if (!debug_active || !id->areas)
901 if (debug_critical) {
902 if (!spin_trylock_irqsave(&id->lock, flags))
905 spin_lock_irqsave(&id->lock, flags);
908 active = get_active_entry(id);
909 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
910 if (len < id->buf_size)
911 memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
912 debug_finish_entry(id, active, level, 0);
917 spin_unlock_irqrestore(&id->lock, flags);
920 EXPORT_SYMBOL(debug_event_common);
923 * debug_exception_common:
924 * - write debug entry with given size and switch to next debug area
926 debug_entry_t *debug_exception_common(debug_info_t *id, int level,
927 const void *buf, int len)
929 debug_entry_t *active;
932 if (!debug_active || !id->areas)
934 if (debug_critical) {
935 if (!spin_trylock_irqsave(&id->lock, flags))
938 spin_lock_irqsave(&id->lock, flags);
941 active = get_active_entry(id);
942 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
943 if (len < id->buf_size)
944 memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
945 debug_finish_entry(id, active, level, len <= id->buf_size);
950 spin_unlock_irqrestore(&id->lock, flags);
953 EXPORT_SYMBOL(debug_exception_common);
956 * counts arguments in format string for sprintf view
958 static inline int debug_count_numargs(char *string)
963 if (*string++ == '%')
970 * debug_sprintf_event:
972 debug_entry_t *__debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
974 debug_sprintf_entry_t *curr_event;
975 debug_entry_t *active;
980 if (!debug_active || !id->areas)
982 numargs = debug_count_numargs(string);
984 if (debug_critical) {
985 if (!spin_trylock_irqsave(&id->lock, flags))
988 spin_lock_irqsave(&id->lock, flags);
990 active = get_active_entry(id);
991 curr_event = (debug_sprintf_entry_t *) DEBUG_DATA(active);
992 va_start(ap, string);
993 curr_event->string = string;
994 for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
995 curr_event->args[idx] = va_arg(ap, long);
997 debug_finish_entry(id, active, level, 0);
998 spin_unlock_irqrestore(&id->lock, flags);
1002 EXPORT_SYMBOL(__debug_sprintf_event);
1005 * debug_sprintf_exception:
1007 debug_entry_t *__debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
1009 debug_sprintf_entry_t *curr_event;
1010 debug_entry_t *active;
1011 unsigned long flags;
1015 if (!debug_active || !id->areas)
1018 numargs = debug_count_numargs(string);
1020 if (debug_critical) {
1021 if (!spin_trylock_irqsave(&id->lock, flags))
1024 spin_lock_irqsave(&id->lock, flags);
1026 active = get_active_entry(id);
1027 curr_event = (debug_sprintf_entry_t *)DEBUG_DATA(active);
1028 va_start(ap, string);
1029 curr_event->string = string;
1030 for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
1031 curr_event->args[idx] = va_arg(ap, long);
1033 debug_finish_entry(id, active, level, 1);
1034 spin_unlock_irqrestore(&id->lock, flags);
1038 EXPORT_SYMBOL(__debug_sprintf_exception);
1041 * debug_register_view:
1043 int debug_register_view(debug_info_t *id, struct debug_view *view)
1045 unsigned long flags;
1053 mode = (id->mode | S_IFREG) & ~S_IXUGO;
1054 if (!(view->prolog_proc || view->format_proc || view->header_proc))
1055 mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
1056 if (!view->input_proc)
1057 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1058 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
1059 id, &debug_file_ops);
1061 pr_err("Registering view %s/%s failed due to out of "
1062 "memory\n", id->name, view->name);
1066 spin_lock_irqsave(&id->lock, flags);
1067 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1071 if (i == DEBUG_MAX_VIEWS) {
1072 pr_err("Registering view %s/%s would exceed the maximum "
1073 "number of views %i\n", id->name, view->name, i);
1076 id->views[i] = view;
1077 id->debugfs_entries[i] = pde;
1079 spin_unlock_irqrestore(&id->lock, flags);
1081 debugfs_remove(pde);
1085 EXPORT_SYMBOL(debug_register_view);
1088 * debug_unregister_view:
1090 int debug_unregister_view(debug_info_t *id, struct debug_view *view)
1092 struct dentry *dentry = NULL;
1093 unsigned long flags;
1098 spin_lock_irqsave(&id->lock, flags);
1099 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1100 if (id->views[i] == view)
1103 if (i == DEBUG_MAX_VIEWS) {
1106 dentry = id->debugfs_entries[i];
1107 id->views[i] = NULL;
1108 id->debugfs_entries[i] = NULL;
1110 spin_unlock_irqrestore(&id->lock, flags);
1111 debugfs_remove(dentry);
1115 EXPORT_SYMBOL(debug_unregister_view);
1117 static inline char *debug_get_user_string(const char __user *user_buf,
1122 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1124 return ERR_PTR(-ENOMEM);
1125 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1127 return ERR_PTR(-EFAULT);
1129 /* got the string, now strip linefeed. */
1130 if (buffer[user_len - 1] == '\n')
1131 buffer[user_len - 1] = 0;
1133 buffer[user_len] = 0;
1137 static inline int debug_get_uint(char *buf)
1141 buf = skip_spaces(buf);
1142 rc = simple_strtoul(buf, &buf, 10);
1149 * functions for debug-views
1150 ***********************************
1154 * prints out actual debug level
1157 static int debug_prolog_pages_fn(debug_info_t *id, struct debug_view *view,
1160 return sprintf(out_buf, "%i\n", id->pages_per_area);
1164 * reads new size (number of pages per debug area)
1167 static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
1168 struct file *file, const char __user *user_buf,
1169 size_t user_len, loff_t *offset)
1174 if (user_len > 0x10000)
1180 str = debug_get_user_string(user_buf, user_len);
1185 new_pages = debug_get_uint(str);
1186 if (new_pages < 0) {
1190 rc = debug_set_size(id, id->nr_areas, new_pages);
1199 *offset += user_len;
1200 return rc; /* number of input characters */
1204 * prints out actual debug level
1206 static int debug_prolog_level_fn(debug_info_t *id, struct debug_view *view,
1211 if (id->level == DEBUG_OFF_LEVEL)
1212 rc = sprintf(out_buf, "-\n");
1214 rc = sprintf(out_buf, "%i\n", id->level);
1219 * reads new debug level
1221 static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
1222 struct file *file, const char __user *user_buf,
1223 size_t user_len, loff_t *offset)
1228 if (user_len > 0x10000)
1234 str = debug_get_user_string(user_buf, user_len);
1239 if (str[0] == '-') {
1240 debug_set_level(id, DEBUG_OFF_LEVEL);
1244 new_level = debug_get_uint(str);
1246 if (new_level < 0) {
1247 pr_warn("%s is not a valid level for a debug feature\n", str);
1250 debug_set_level(id, new_level);
1256 *offset += user_len;
1257 return rc; /* number of input characters */
1261 * flushes debug areas
1263 static void debug_flush(debug_info_t *id, int area)
1265 unsigned long flags;
1268 if (!id || !id->areas)
1270 spin_lock_irqsave(&id->lock, flags);
1271 if (area == DEBUG_FLUSH_ALL) {
1272 id->active_area = 0;
1273 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1274 for (i = 0; i < id->nr_areas; i++) {
1275 id->active_pages[i] = 0;
1276 for (j = 0; j < id->pages_per_area; j++)
1277 memset(id->areas[i][j], 0, PAGE_SIZE);
1279 } else if (area >= 0 && area < id->nr_areas) {
1280 id->active_entries[area] = 0;
1281 id->active_pages[area] = 0;
1282 for (i = 0; i < id->pages_per_area; i++)
1283 memset(id->areas[area][i], 0, PAGE_SIZE);
1285 spin_unlock_irqrestore(&id->lock, flags);
1289 * view function: flushes debug areas
1291 static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
1292 struct file *file, const char __user *user_buf,
1293 size_t user_len, loff_t *offset)
1298 if (user_len > 0x10000)
1304 if (copy_from_user(input_buf, user_buf, 1)) {
1308 if (input_buf[0] == '-') {
1309 debug_flush(id, DEBUG_FLUSH_ALL);
1312 if (isdigit(input_buf[0])) {
1313 int area = ((int) input_buf[0] - (int) '0');
1315 debug_flush(id, area);
1319 pr_info("Flushing debug data failed because %c is not a valid "
1320 "area\n", input_buf[0]);
1323 *offset += user_len;
1324 return rc; /* number of input characters */
1328 * prints debug header in raw format
1330 static int debug_raw_header_fn(debug_info_t *id, struct debug_view *view,
1331 int area, debug_entry_t *entry, char *out_buf)
1335 rc = sizeof(debug_entry_t);
1336 memcpy(out_buf, entry, sizeof(debug_entry_t));
1341 * prints debug data in raw format
1343 static int debug_raw_format_fn(debug_info_t *id, struct debug_view *view,
1344 char *out_buf, const char *in_buf)
1349 memcpy(out_buf, in_buf, id->buf_size);
1354 * prints debug data in hex/ascii format
1356 static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
1357 char *out_buf, const char *in_buf)
1361 for (i = 0; i < id->buf_size; i++)
1362 rc += sprintf(out_buf + rc, "%02x ", ((unsigned char *) in_buf)[i]);
1363 rc += sprintf(out_buf + rc, "| ");
1364 for (i = 0; i < id->buf_size; i++) {
1365 unsigned char c = in_buf[i];
1367 if (isascii(c) && isprint(c))
1368 rc += sprintf(out_buf + rc, "%c", c);
1370 rc += sprintf(out_buf + rc, ".");
1372 rc += sprintf(out_buf + rc, "\n");
1377 * prints header for debug entry
1379 int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view,
1380 int area, debug_entry_t *entry, char *out_buf)
1382 unsigned long base, sec, usec;
1383 unsigned long caller;
1388 level = entry->id.fields.level;
1389 base = (*(unsigned long *) &tod_clock_base[0]) >> 4;
1390 sec = (entry->id.stck >> 12) + base - (TOD_UNIX_EPOCH >> 12);
1391 usec = do_div(sec, USEC_PER_SEC);
1393 if (entry->id.fields.exception)
1397 caller = (unsigned long) entry->caller;
1398 rc += sprintf(out_buf, "%02i %011ld:%06lu %1u %1s %02i %pK ",
1399 area, sec, usec, level, except_str,
1400 entry->id.fields.cpuid, (void *)caller);
1403 EXPORT_SYMBOL(debug_dflt_header_fn);
1406 * prints debug data sprintf-formated:
1407 * debug_sprinf_event/exception calls must be used together with this view
1410 #define DEBUG_SPRINTF_MAX_ARGS 10
1412 static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
1413 char *out_buf, debug_sprintf_entry_t *curr_event)
1415 int num_longs, num_used_args = 0, i, rc = 0;
1416 int index[DEBUG_SPRINTF_MAX_ARGS];
1418 /* count of longs fit into one entry */
1419 num_longs = id->buf_size / sizeof(long);
1422 goto out; /* bufsize of entry too small */
1423 if (num_longs == 1) {
1424 /* no args, we use only the string */
1425 strcpy(out_buf, curr_event->string);
1426 rc = strlen(curr_event->string);
1430 /* number of arguments used for sprintf (without the format string) */
1431 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1433 memset(index, 0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1435 for (i = 0; i < num_used_args; i++)
1438 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1439 curr_event->args[index[1]], curr_event->args[index[2]],
1440 curr_event->args[index[3]], curr_event->args[index[4]],
1441 curr_event->args[index[5]], curr_event->args[index[6]],
1442 curr_event->args[index[7]], curr_event->args[index[8]],
1443 curr_event->args[index[9]]);
1450 * - is called exactly once to initialize the debug feature
1452 static int __init debug_init(void)
1454 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
1455 mutex_lock(&debug_mutex);
1456 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT, NULL);
1458 mutex_unlock(&debug_mutex);
1461 postcore_initcall(debug_init);