string: on strstrip(), first remove leading spaces before running over str
[deliverable/linux.git] / arch / s390 / kernel / debug.c
CommitLineData
1da177e4
LT
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
c5612c19
MH
13#define KMSG_COMPONENT "s390dbf"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
1da177e4
LT
16#include <linux/stddef.h>
17#include <linux/kernel.h>
18#include <linux/errno.h>
19#include <linux/slab.h>
20#include <linux/ctype.h>
21#include <linux/sysctl.h>
22#include <asm/uaccess.h>
1da177e4
LT
23#include <linux/module.h>
24#include <linux/init.h>
66a464db
MH
25#include <linux/fs.h>
26#include <linux/debugfs.h>
1da177e4
LT
27
28#include <asm/debug.h>
29
30#define DEBUG_PROLOG_ENTRY -1
31
66a464db
MH
32#define ALL_AREAS 0 /* copy all debug areas */
33#define NO_AREAS 1 /* copy no debug areas */
34
1da177e4
LT
35/* typedefs */
36
37typedef struct file_private_info {
38 loff_t offset; /* offset of last read in file */
39 int act_area; /* number of last formated area */
66a464db 40 int act_page; /* act page in given area */
1da177e4
LT
41 int act_entry; /* last formated entry (offset */
42 /* relative to beginning of last */
66a464db 43 /* formated page) */
1da177e4
LT
44 size_t act_entry_offset; /* up to this offset we copied */
45 /* in last read the last formated */
46 /* entry to userland */
47 char temp_buf[2048]; /* buffer for output */
48 debug_info_t *debug_info_org; /* original debug information */
49 debug_info_t *debug_info_snap; /* snapshot of debug information */
50 struct debug_view *view; /* used view of debug info */
51} file_private_info_t;
52
53typedef struct
54{
55 char *string;
56 /*
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)
66a464db
MH
60 *
61 */
1da177e4
LT
62 long args[0];
63} debug_sprintf_entry_t;
64
65
1da177e4
LT
66/* internal function prototyes */
67
68static int debug_init(void);
69static ssize_t debug_output(struct file *file, char __user *user_buf,
66a464db 70 size_t user_len, loff_t * offset);
1da177e4 71static ssize_t debug_input(struct file *file, const char __user *user_buf,
66a464db 72 size_t user_len, loff_t * offset);
1da177e4
LT
73static int debug_open(struct inode *inode, struct file *file);
74static int debug_close(struct inode *inode, struct file *file);
5cbbf16a 75static debug_info_t *debug_info_create(const char *name, int pages_per_area,
9637c3f3 76 int nr_areas, int buf_size, mode_t mode);
1da177e4
LT
77static void debug_info_get(debug_info_t *);
78static void debug_info_put(debug_info_t *);
79static int debug_prolog_level_fn(debug_info_t * id,
66a464db 80 struct debug_view *view, char *out_buf);
1da177e4 81static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,
66a464db
MH
82 struct file *file, const char __user *user_buf,
83 size_t user_buf_size, loff_t * offset);
84static int debug_prolog_pages_fn(debug_info_t * id,
85 struct debug_view *view, char *out_buf);
86static 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);
1da177e4 89static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
66a464db
MH
90 struct file *file, const char __user *user_buf,
91 size_t user_buf_size, loff_t * offset);
1da177e4 92static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
66a464db 93 char *out_buf, const char *in_buf);
1da177e4 94static int debug_raw_format_fn(debug_info_t * id,
66a464db
MH
95 struct debug_view *view, char *out_buf,
96 const char *in_buf);
1da177e4 97static int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
66a464db 98 int area, debug_entry_t * entry, char *out_buf);
1da177e4
LT
99
100static int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
66a464db 101 char *out_buf, debug_sprintf_entry_t *curr_event);
1da177e4
LT
102
103/* globals */
104
105struct 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
114struct 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
2b67fc46 123static struct debug_view debug_level_view = {
1da177e4
LT
124 "level",
125 &debug_prolog_level_fn,
126 NULL,
127 NULL,
128 &debug_input_level_fn,
129 NULL
130};
131
2b67fc46 132static struct debug_view debug_pages_view = {
66a464db
MH
133 "pages",
134 &debug_prolog_pages_fn,
135 NULL,
136 NULL,
137 &debug_input_pages_fn,
138 NULL
139};
140
2b67fc46 141static struct debug_view debug_flush_view = {
1da177e4
LT
142 "flush",
143 NULL,
144 NULL,
145 NULL,
146 &debug_input_flush_fn,
147 NULL
148};
149
150struct 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
2b67fc46 159/* used by dump analysis tools to determine version of debug feature */
a806170e 160static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
1da177e4
LT
161
162/* static globals */
163
164static debug_info_t *debug_area_first = NULL;
165static debug_info_t *debug_area_last = NULL;
e11f0d04 166static DEFINE_MUTEX(debug_mutex);
1da177e4
LT
167
168static int initialized;
169
5dfe4c96 170static const struct file_operations debug_file_ops = {
66a464db 171 .owner = THIS_MODULE,
1da177e4 172 .read = debug_output,
66a464db 173 .write = debug_input,
1da177e4
LT
174 .open = debug_open,
175 .release = debug_close,
176};
177
66a464db 178static struct dentry *debug_debugfs_root_entry;
1da177e4
LT
179
180/* functions */
181
66a464db
MH
182/*
183 * debug_areas_alloc
184 * - Debug areas are implemented as a threedimensonal array:
185 * areas[areanumber][pagenumber][pageoffset]
186 */
187
188static debug_entry_t***
189debug_areas_alloc(int pages_per_area, int nr_areas)
190{
191 debug_entry_t*** areas;
192 int i,j;
193
5cbded58 194 areas = kmalloc(nr_areas *
66a464db
MH
195 sizeof(debug_entry_t**),
196 GFP_KERNEL);
197 if (!areas)
198 goto fail_malloc_areas;
199 for (i = 0; i < nr_areas; i++) {
5cbded58 200 areas[i] = kmalloc(pages_per_area *
66a464db
MH
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++) {
fb630517 206 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
66a464db
MH
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;
66a464db
MH
213 }
214 }
215 }
216 return areas;
217
218fail_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);
226fail_malloc_areas:
227 return NULL;
228
229}
230
231
1da177e4
LT
232/*
233 * debug_info_alloc
234 * - alloc new debug-info
235 */
236
66a464db 237static debug_info_t*
5cbbf16a
CH
238debug_info_alloc(const char *name, int pages_per_area, int nr_areas,
239 int buf_size, int level, int mode)
1da177e4
LT
240{
241 debug_info_t* rc;
1da177e4
LT
242
243 /* alloc everything */
244
5cbded58 245 rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
1da177e4
LT
246 if(!rc)
247 goto fail_malloc_rc;
fb630517 248 rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
66a464db
MH
249 if(!rc->active_entries)
250 goto fail_malloc_active_entries;
fb630517 251 rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
66a464db
MH
252 if(!rc->active_pages)
253 goto fail_malloc_active_pages;
66a464db
MH
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;
1da177e4
LT
260 }
261
262 /* initialize members */
263
264 spin_lock_init(&rc->lock);
66a464db
MH
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;
20cb9e79 271 strlcpy(rc->name, name, sizeof(rc->name));
1da177e4 272 memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
66a464db
MH
273 memset(rc->debugfs_entries, 0 ,DEBUG_MAX_VIEWS *
274 sizeof(struct dentry*));
1da177e4
LT
275 atomic_set(&(rc->ref_count), 0);
276
277 return rc;
278
1da177e4 279fail_malloc_areas:
66a464db
MH
280 kfree(rc->active_pages);
281fail_malloc_active_pages:
282 kfree(rc->active_entries);
283fail_malloc_active_entries:
1da177e4
LT
284 kfree(rc);
285fail_malloc_rc:
286 return NULL;
287}
288
289/*
66a464db
MH
290 * debug_areas_free
291 * - free all debug areas
1da177e4
LT
292 */
293
66a464db
MH
294static void
295debug_areas_free(debug_info_t* db_info)
296{
297 int i,j;
298
299 if(!db_info->areas)
300 return;
1da177e4 301 for (i = 0; i < db_info->nr_areas; i++) {
66a464db
MH
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]);
1da177e4
LT
306 }
307 kfree(db_info->areas);
66a464db
MH
308 db_info->areas = NULL;
309}
310
311/*
312 * debug_info_free
313 * - free memory debug-info
314 */
315
316static void
317debug_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);
1da177e4
LT
321 kfree(db_info);
322}
323
324/*
325 * debug_info_create
326 * - create new debug-info
327 */
328
66a464db 329static debug_info_t*
5cbbf16a
CH
330debug_info_create(const char *name, int pages_per_area, int nr_areas,
331 int buf_size, mode_t mode)
1da177e4
LT
332{
333 debug_info_t* rc;
334
66a464db
MH
335 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
336 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
1da177e4
LT
337 if(!rc)
338 goto out;
339
9637c3f3
MH
340 rc->mode = mode & ~S_IFMT;
341
66a464db
MH
342 /* create root directory */
343 rc->debugfs_root_entry = debugfs_create_dir(rc->name,
344 debug_debugfs_root_entry);
1da177e4
LT
345
346 /* append new element to linked list */
66a464db 347 if (!debug_area_first) {
1da177e4
LT
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);
360out:
361 return rc;
362}
363
364/*
365 * debug_info_copy
366 * - copy debug-info
367 */
368
66a464db
MH
369static debug_info_t*
370debug_info_copy(debug_info_t* in, int mode)
1da177e4 371{
66a464db 372 int i,j;
1da177e4 373 debug_info_t* rc;
942eaabd
MH
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);
66a464db 391
acfa922c 392 if (mode == NO_AREAS)
1da177e4
LT
393 goto out;
394
395 for(i = 0; i < in->nr_areas; i++){
66a464db
MH
396 for(j = 0; j < in->pages_per_area; j++) {
397 memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
398 }
1da177e4
LT
399 }
400out:
942eaabd 401 spin_unlock_irqrestore(&in->lock, flags);
1da177e4
LT
402 return rc;
403}
404
405/*
406 * debug_info_get
407 * - increments reference count for debug-info
408 */
409
66a464db
MH
410static void
411debug_info_get(debug_info_t * db_info)
1da177e4
LT
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
66a464db
MH
422static void
423debug_info_put(debug_info_t *db_info)
1da177e4
LT
424{
425 int i;
426
427 if (!db_info)
428 return;
429 if (atomic_dec_and_test(&db_info->ref_count)) {
1da177e4 430 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
66a464db 431 if (!db_info->views[i])
1da177e4 432 continue;
66a464db 433 debugfs_remove(db_info->debugfs_entries[i]);
1da177e4 434 }
66a464db 435 debugfs_remove(db_info->debugfs_root_entry);
1da177e4
LT
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
66a464db
MH
451static int
452debug_format_entry(file_private_info_t *p_info)
1da177e4 453{
1da177e4
LT
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)
66a464db 461 len += view->prolog_proc(id_snap,view,p_info->temp_buf);
1da177e4
LT
462 goto out;
463 }
66a464db
MH
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);
1da177e4
LT
468
469 if (act_entry->id.stck == 0LL)
470 goto out; /* empty entry */
471 if (view->header_proc)
66a464db 472 len += view->header_proc(id_snap, view, p_info->act_area,
1da177e4
LT
473 act_entry, p_info->temp_buf + len);
474 if (view->format_proc)
66a464db 475 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
1da177e4 476 DEBUG_DATA(act_entry));
66a464db 477out:
1da177e4
LT
478 return len;
479}
480
481/*
482 * debug_next_entry:
483 * - goto next entry in p_info
484 */
485
4448aaf0 486static inline int
66a464db 487debug_next_entry(file_private_info_t *p_info)
1da177e4 488{
66a464db
MH
489 debug_info_t *id;
490
491 id = p_info->debug_info_snap;
1da177e4
LT
492 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
493 p_info->act_entry = 0;
66a464db 494 p_info->act_page = 0;
1da177e4
LT
495 goto out;
496 }
66a464db
MH
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 */
1da177e4 503 p_info->act_entry = 0;
66a464db
MH
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 }
1da177e4
LT
510 if(p_info->act_area >= id->nr_areas)
511 return 1;
512 }
513out:
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
66a464db
MH
523static ssize_t
524debug_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 */
1da177e4
LT
528{
529 size_t count = 0;
66a464db 530 size_t entry_offset;
1da177e4
LT
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;
1da177e4 538 entry_offset = p_info->act_entry_offset;
1da177e4 539 while(count < len){
66a464db
MH
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;
1da177e4 555 }
66a464db
MH
556 if(copy_size == formatted_line_residue){
557 entry_offset = 0;
558 if(debug_next_entry(p_info))
1da177e4 559 goto out;
66a464db 560 }
1da177e4
LT
561 }
562out:
563 p_info->offset = *offset + count;
66a464db 564 p_info->act_entry_offset = entry_offset;
1da177e4
LT
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
66a464db
MH
575static ssize_t
576debug_input(struct file *file, const char __user *user_buf, size_t length,
577 loff_t *offset)
1da177e4
LT
578{
579 int rc = 0;
580 file_private_info_t *p_info;
581
e11f0d04 582 mutex_lock(&debug_mutex);
1da177e4
LT
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;
e11f0d04 590 mutex_unlock(&debug_mutex);
1da177e4
LT
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
66a464db
MH
601static int
602debug_open(struct inode *inode, struct file *file)
1da177e4 603{
a0fa8e37 604 int i, rc = 0;
1da177e4
LT
605 file_private_info_t *p_info;
606 debug_info_t *debug_info, *debug_info_snapshot;
607
e11f0d04 608 mutex_lock(&debug_mutex);
d20343e7 609 debug_info = file->f_path.dentry->d_inode->i_private;
942eaabd
MH
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] ==
d20343e7 615 file->f_path.dentry) {
942eaabd 616 goto found; /* found view ! */
1da177e4 617 }
1da177e4
LT
618 }
619 /* no entry found */
620 rc = -EINVAL;
621 goto out;
622
66a464db 623found:
1da177e4 624
66a464db
MH
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. */
1da177e4 628
66a464db
MH
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 }
1da177e4
LT
635
636 if(!debug_info_snapshot){
1da177e4
LT
637 rc = -ENOMEM;
638 goto out;
639 }
5cbded58 640 p_info = kmalloc(sizeof(file_private_info_t),
66a464db
MH
641 GFP_KERNEL);
642 if(!p_info){
58ace9f2 643 debug_info_free(debug_info_snapshot);
1da177e4
LT
644 rc = -ENOMEM;
645 goto out;
646 }
1da177e4
LT
647 p_info->offset = 0;
648 p_info->debug_info_snap = debug_info_snapshot;
649 p_info->debug_info_org = debug_info;
650 p_info->view = debug_info->views[i];
651 p_info->act_area = 0;
66a464db 652 p_info->act_page = 0;
1da177e4
LT
653 p_info->act_entry = DEBUG_PROLOG_ENTRY;
654 p_info->act_entry_offset = 0;
66a464db 655 file->private_data = p_info;
1da177e4 656 debug_info_get(debug_info);
66a464db 657out:
e11f0d04 658 mutex_unlock(&debug_mutex);
1da177e4
LT
659 return rc;
660}
661
662/*
663 * debug_close:
664 * - called for user close()
665 * - deletes private_data area of the file handle
666 */
667
66a464db
MH
668static int
669debug_close(struct inode *inode, struct file *file)
1da177e4
LT
670{
671 file_private_info_t *p_info;
1da177e4 672 p_info = (file_private_info_t *) file->private_data;
66a464db
MH
673 if(p_info->debug_info_snap)
674 debug_info_free(p_info->debug_info_snap);
1da177e4
LT
675 debug_info_put(p_info->debug_info_org);
676 kfree(file->private_data);
677 return 0; /* success */
678}
679
680/*
9637c3f3
MH
681 * debug_register_mode:
682 * - Creates and initializes debug area for the caller
683 * The mode parameter allows to specify access rights for the s390dbf files
684 * - Returns handle for debug area
1da177e4
LT
685 */
686
5cbbf16a
CH
687debug_info_t *debug_register_mode(const char *name, int pages_per_area,
688 int nr_areas, int buf_size, mode_t mode,
689 uid_t uid, gid_t gid)
1da177e4
LT
690{
691 debug_info_t *rc = NULL;
692
9637c3f3
MH
693 /* Since debugfs currently does not support uid/gid other than root, */
694 /* we do not allow gid/uid != 0 until we get support for that. */
695 if ((uid != 0) || (gid != 0))
c5612c19
MH
696 pr_warning("Root becomes the owner of all s390dbf files "
697 "in sysfs\n");
6aa0d3a9 698 BUG_ON(!initialized);
e11f0d04 699 mutex_lock(&debug_mutex);
1da177e4
LT
700
701 /* create new debug_info */
702
9637c3f3 703 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
1da177e4
LT
704 if(!rc)
705 goto out;
706 debug_register_view(rc, &debug_level_view);
707 debug_register_view(rc, &debug_flush_view);
66a464db
MH
708 debug_register_view(rc, &debug_pages_view);
709out:
710 if (!rc){
c5612c19 711 pr_err("Registering debug feature %s failed\n", name);
1da177e4 712 }
e11f0d04 713 mutex_unlock(&debug_mutex);
1da177e4
LT
714 return rc;
715}
9637c3f3
MH
716EXPORT_SYMBOL(debug_register_mode);
717
718/*
719 * debug_register:
720 * - creates and initializes debug area for the caller
721 * - returns handle for debug area
722 */
723
5cbbf16a
CH
724debug_info_t *debug_register(const char *name, int pages_per_area,
725 int nr_areas, int buf_size)
9637c3f3
MH
726{
727 return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
728 S_IRUSR | S_IWUSR, 0, 0);
729}
1da177e4
LT
730
731/*
732 * debug_unregister:
733 * - give back debug area
734 */
735
66a464db
MH
736void
737debug_unregister(debug_info_t * id)
1da177e4
LT
738{
739 if (!id)
740 goto out;
e11f0d04 741 mutex_lock(&debug_mutex);
1da177e4 742 debug_info_put(id);
e11f0d04 743 mutex_unlock(&debug_mutex);
1da177e4 744
66a464db 745out:
1da177e4
LT
746 return;
747}
748
66a464db
MH
749/*
750 * debug_set_size:
751 * - set area size (number of pages) and number of areas
752 */
753static int
754debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
755{
756 unsigned long flags;
757 debug_entry_t *** new_areas;
758 int rc=0;
759
760 if(!id || (nr_areas <= 0) || (pages_per_area < 0))
761 return -EINVAL;
762 if(pages_per_area > 0){
763 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
764 if(!new_areas) {
c5612c19
MH
765 pr_info("Allocating memory for %i pages failed\n",
766 pages_per_area);
66a464db
MH
767 rc = -ENOMEM;
768 goto out;
769 }
770 } else {
771 new_areas = NULL;
772 }
773 spin_lock_irqsave(&id->lock,flags);
774 debug_areas_free(id);
775 id->areas = new_areas;
776 id->nr_areas = nr_areas;
777 id->pages_per_area = pages_per_area;
778 id->active_area = 0;
779 memset(id->active_entries,0,sizeof(int)*id->nr_areas);
780 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
781 spin_unlock_irqrestore(&id->lock,flags);
c5612c19 782 pr_info("%s: set new size (%i pages)\n" ,id->name, pages_per_area);
66a464db
MH
783out:
784 return rc;
785}
786
1da177e4
LT
787/*
788 * debug_set_level:
789 * - set actual debug level
790 */
791
66a464db
MH
792void
793debug_set_level(debug_info_t* id, int new_level)
1da177e4
LT
794{
795 unsigned long flags;
796 if(!id)
797 return;
798 spin_lock_irqsave(&id->lock,flags);
799 if(new_level == DEBUG_OFF_LEVEL){
800 id->level = DEBUG_OFF_LEVEL;
c5612c19 801 pr_info("%s: switched off\n",id->name);
1da177e4 802 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
c5612c19 803 pr_info("%s: level %i is out of range (%i - %i)\n",
1da177e4
LT
804 id->name, new_level, 0, DEBUG_MAX_LEVEL);
805 } else {
806 id->level = new_level;
1da177e4
LT
807 }
808 spin_unlock_irqrestore(&id->lock,flags);
809}
810
811
812/*
813 * proceed_active_entry:
814 * - set active entry to next in the ring buffer
815 */
816
4448aaf0 817static inline void
66a464db 818proceed_active_entry(debug_info_t * id)
1da177e4 819{
66a464db
MH
820 if ((id->active_entries[id->active_area] += id->entry_size)
821 > (PAGE_SIZE - id->entry_size)){
822 id->active_entries[id->active_area] = 0;
823 id->active_pages[id->active_area] =
824 (id->active_pages[id->active_area] + 1) %
825 id->pages_per_area;
826 }
1da177e4
LT
827}
828
829/*
830 * proceed_active_area:
831 * - set active area to next in the ring buffer
832 */
833
4448aaf0 834static inline void
66a464db 835proceed_active_area(debug_info_t * id)
1da177e4
LT
836{
837 id->active_area++;
838 id->active_area = id->active_area % id->nr_areas;
839}
840
841/*
842 * get_active_entry:
843 */
844
4448aaf0 845static inline debug_entry_t*
66a464db 846get_active_entry(debug_info_t * id)
1da177e4 847{
66a464db
MH
848 return (debug_entry_t *) (((char *) id->areas[id->active_area]
849 [id->active_pages[id->active_area]]) +
850 id->active_entries[id->active_area]);
1da177e4
LT
851}
852
853/*
854 * debug_finish_entry:
855 * - set timestamp, caller address, cpu number etc.
856 */
857
4448aaf0 858static inline void
66a464db
MH
859debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
860 int exception)
1da177e4 861{
942eaabd 862 active->id.stck = get_clock();
1da177e4
LT
863 active->id.fields.cpuid = smp_processor_id();
864 active->caller = __builtin_return_address(0);
865 active->id.fields.exception = exception;
866 active->id.fields.level = level;
867 proceed_active_entry(id);
868 if(exception)
869 proceed_active_area(id);
870}
871
872static int debug_stoppable=1;
873static int debug_active=1;
874
1da177e4
LT
875#define CTL_S390DBF_STOPPABLE 5678
876#define CTL_S390DBF_ACTIVE 5679
877
878/*
879 * proc handler for the running debug_active sysctl
880 * always allow read, allow write only if debug_stoppable is set or
881 * if debug_active is already off
882 */
66a464db 883static int
8d65af78 884s390dbf_procactive(ctl_table *table, int write,
1da177e4
LT
885 void __user *buffer, size_t *lenp, loff_t *ppos)
886{
887 if (!write || debug_stoppable || !debug_active)
8d65af78 888 return proc_dointvec(table, write, buffer, lenp, ppos);
1da177e4
LT
889 else
890 return 0;
891}
892
893
894static struct ctl_table s390dbf_table[] = {
895 {
1da177e4
LT
896 .procname = "debug_stoppable",
897 .data = &debug_stoppable,
898 .maxlen = sizeof(int),
899 .mode = S_IRUGO | S_IWUSR,
6d456111 900 .proc_handler = proc_dointvec,
1da177e4
LT
901 },
902 {
1da177e4
LT
903 .procname = "debug_active",
904 .data = &debug_active,
905 .maxlen = sizeof(int),
906 .mode = S_IRUGO | S_IWUSR,
6d456111 907 .proc_handler = s390dbf_procactive,
1da177e4 908 },
b05fd35d 909 { }
1da177e4
LT
910};
911
912static struct ctl_table s390dbf_dir_table[] = {
913 {
1da177e4
LT
914 .procname = "s390dbf",
915 .maxlen = 0,
916 .mode = S_IRUGO | S_IXUGO,
917 .child = s390dbf_table,
918 },
b05fd35d 919 { }
1da177e4
LT
920};
921
2b67fc46 922static struct ctl_table_header *s390dbf_sysctl_header;
1da177e4 923
66a464db
MH
924void
925debug_stop_all(void)
1da177e4
LT
926{
927 if (debug_stoppable)
928 debug_active = 0;
929}
930
931
932/*
933 * debug_event_common:
934 * - write debug entry with given size
935 */
936
66a464db
MH
937debug_entry_t*
938debug_event_common(debug_info_t * id, int level, const void *buf, int len)
1da177e4
LT
939{
940 unsigned long flags;
941 debug_entry_t *active;
942
66a464db 943 if (!debug_active || !id->areas)
1da177e4
LT
944 return NULL;
945 spin_lock_irqsave(&id->lock, flags);
946 active = get_active_entry(id);
947 memset(DEBUG_DATA(active), 0, id->buf_size);
948 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
949 debug_finish_entry(id, active, level, 0);
950 spin_unlock_irqrestore(&id->lock, flags);
951
952 return active;
953}
954
955/*
956 * debug_exception_common:
957 * - write debug entry with given size and switch to next debug area
958 */
959
66a464db
MH
960debug_entry_t
961*debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
1da177e4
LT
962{
963 unsigned long flags;
964 debug_entry_t *active;
965
66a464db 966 if (!debug_active || !id->areas)
1da177e4
LT
967 return NULL;
968 spin_lock_irqsave(&id->lock, flags);
969 active = get_active_entry(id);
970 memset(DEBUG_DATA(active), 0, id->buf_size);
971 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
972 debug_finish_entry(id, active, level, 1);
973 spin_unlock_irqrestore(&id->lock, flags);
974
975 return active;
976}
977
978/*
979 * counts arguments in format string for sprintf view
980 */
981
4448aaf0 982static inline int
66a464db 983debug_count_numargs(char *string)
1da177e4
LT
984{
985 int numargs=0;
986
987 while(*string) {
988 if(*string++=='%')
989 numargs++;
990 }
991 return(numargs);
992}
993
994/*
995 * debug_sprintf_event:
996 */
997
66a464db
MH
998debug_entry_t*
999debug_sprintf_event(debug_info_t* id, int level,char *string,...)
1da177e4
LT
1000{
1001 va_list ap;
1002 int numargs,idx;
1003 unsigned long flags;
1004 debug_sprintf_entry_t *curr_event;
1005 debug_entry_t *active;
1006
1007 if((!id) || (level > id->level))
1008 return NULL;
66a464db 1009 if (!debug_active || !id->areas)
1da177e4
LT
1010 return NULL;
1011 numargs=debug_count_numargs(string);
1012
1013 spin_lock_irqsave(&id->lock, flags);
1014 active = get_active_entry(id);
1015 curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
1016 va_start(ap,string);
1017 curr_event->string=string;
1018 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1019 curr_event->args[idx]=va_arg(ap,long);
1020 va_end(ap);
1021 debug_finish_entry(id, active, level, 0);
1022 spin_unlock_irqrestore(&id->lock, flags);
1023
1024 return active;
1025}
1026
1027/*
1028 * debug_sprintf_exception:
1029 */
1030
66a464db
MH
1031debug_entry_t*
1032debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
1da177e4
LT
1033{
1034 va_list ap;
1035 int numargs,idx;
1036 unsigned long flags;
1037 debug_sprintf_entry_t *curr_event;
1038 debug_entry_t *active;
1039
1040 if((!id) || (level > id->level))
1041 return NULL;
66a464db 1042 if (!debug_active || !id->areas)
1da177e4
LT
1043 return NULL;
1044
1045 numargs=debug_count_numargs(string);
1046
1047 spin_lock_irqsave(&id->lock, flags);
1048 active = get_active_entry(id);
1049 curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
1050 va_start(ap,string);
1051 curr_event->string=string;
1052 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1053 curr_event->args[idx]=va_arg(ap,long);
1054 va_end(ap);
1055 debug_finish_entry(id, active, level, 1);
1056 spin_unlock_irqrestore(&id->lock, flags);
1057
1058 return active;
1059}
1060
1061/*
1062 * debug_init:
1063 * - is called exactly once to initialize the debug feature
1064 */
1065
66a464db
MH
1066static int
1067__init debug_init(void)
1da177e4
LT
1068{
1069 int rc = 0;
1070
0b4d4147 1071 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
e11f0d04 1072 mutex_lock(&debug_mutex);
66a464db 1073 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
1da177e4 1074 initialized = 1;
e11f0d04 1075 mutex_unlock(&debug_mutex);
1da177e4
LT
1076
1077 return rc;
1078}
1079
1080/*
1081 * debug_register_view:
1082 */
1083
66a464db
MH
1084int
1085debug_register_view(debug_info_t * id, struct debug_view *view)
1da177e4
LT
1086{
1087 int rc = 0;
1088 int i;
1089 unsigned long flags;
9637c3f3 1090 mode_t mode;
66a464db 1091 struct dentry *pde;
1da177e4
LT
1092
1093 if (!id)
1094 goto out;
9637c3f3
MH
1095 mode = (id->mode | S_IFREG) & ~S_IXUGO;
1096 if (!(view->prolog_proc || view->format_proc || view->header_proc))
1097 mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
1098 if (!view->input_proc)
1099 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
66a464db 1100 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
942eaabd 1101 id , &debug_file_ops);
1da177e4 1102 if (!pde){
c5612c19
MH
1103 pr_err("Registering view %s/%s failed due to out of "
1104 "memory\n", id->name,view->name);
1da177e4
LT
1105 rc = -1;
1106 goto out;
1107 }
1da177e4
LT
1108 spin_lock_irqsave(&id->lock, flags);
1109 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
66a464db 1110 if (!id->views[i])
1da177e4
LT
1111 break;
1112 }
1113 if (i == DEBUG_MAX_VIEWS) {
c5612c19
MH
1114 pr_err("Registering view %s/%s would exceed the maximum "
1115 "number of views %i\n", id->name, view->name, i);
66a464db 1116 debugfs_remove(pde);
1da177e4 1117 rc = -1;
66a464db 1118 } else {
1da177e4 1119 id->views[i] = view;
66a464db 1120 id->debugfs_entries[i] = pde;
1da177e4
LT
1121 }
1122 spin_unlock_irqrestore(&id->lock, flags);
66a464db 1123out:
1da177e4
LT
1124 return rc;
1125}
1126
1127/*
1128 * debug_unregister_view:
1129 */
1130
66a464db
MH
1131int
1132debug_unregister_view(debug_info_t * id, struct debug_view *view)
1da177e4
LT
1133{
1134 int rc = 0;
1135 int i;
1136 unsigned long flags;
1137
1138 if (!id)
1139 goto out;
1140 spin_lock_irqsave(&id->lock, flags);
1141 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1142 if (id->views[i] == view)
1143 break;
1144 }
1145 if (i == DEBUG_MAX_VIEWS)
1146 rc = -1;
1147 else {
66a464db 1148 debugfs_remove(id->debugfs_entries[i]);
1da177e4 1149 id->views[i] = NULL;
1da177e4
LT
1150 }
1151 spin_unlock_irqrestore(&id->lock, flags);
66a464db
MH
1152out:
1153 return rc;
1154}
1155
1156static inline char *
1157debug_get_user_string(const char __user *user_buf, size_t user_len)
1158{
1159 char* buffer;
1160
1161 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1162 if (!buffer)
1163 return ERR_PTR(-ENOMEM);
1164 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1165 kfree(buffer);
1166 return ERR_PTR(-EFAULT);
1167 }
1168 /* got the string, now strip linefeed. */
1169 if (buffer[user_len - 1] == '\n')
1170 buffer[user_len - 1] = 0;
1171 else
1172 buffer[user_len] = 0;
1173 return buffer;
1174}
1175
1176static inline int
1177debug_get_uint(char *buf)
1178{
1179 int rc;
1180
1181 for(; isspace(*buf); buf++);
1182 rc = simple_strtoul(buf, &buf, 10);
1183 if(*buf){
66a464db
MH
1184 rc = -EINVAL;
1185 }
1da177e4
LT
1186 return rc;
1187}
1188
1189/*
1190 * functions for debug-views
1191 ***********************************
1192*/
1193
1194/*
1195 * prints out actual debug level
1196 */
1197
66a464db
MH
1198static int
1199debug_prolog_pages_fn(debug_info_t * id,
1da177e4 1200 struct debug_view *view, char *out_buf)
66a464db
MH
1201{
1202 return sprintf(out_buf, "%i\n", id->pages_per_area);
1203}
1204
1205/*
1206 * reads new size (number of pages per debug area)
1207 */
1208
1209static int
1210debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
1211 struct file *file, const char __user *user_buf,
1212 size_t user_len, loff_t * offset)
1213{
1214 char *str;
1215 int rc,new_pages;
1216
1217 if (user_len > 0x10000)
1218 user_len = 0x10000;
1219 if (*offset != 0){
1220 rc = -EPIPE;
1221 goto out;
1222 }
1223 str = debug_get_user_string(user_buf,user_len);
1224 if(IS_ERR(str)){
1225 rc = PTR_ERR(str);
1226 goto out;
1227 }
1228 new_pages = debug_get_uint(str);
1229 if(new_pages < 0){
1230 rc = -EINVAL;
1231 goto free_str;
1232 }
1233 rc = debug_set_size(id,id->nr_areas, new_pages);
1234 if(rc != 0){
1235 rc = -EINVAL;
1236 goto free_str;
1237 }
1238 rc = user_len;
1239free_str:
1240 kfree(str);
1241out:
1242 *offset += user_len;
1243 return rc; /* number of input characters */
1244}
1245
1246/*
1247 * prints out actual debug level
1248 */
1249
1250static int
1251debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
1da177e4
LT
1252{
1253 int rc = 0;
1254
66a464db
MH
1255 if(id->level == DEBUG_OFF_LEVEL) {
1256 rc = sprintf(out_buf,"-\n");
1257 }
1258 else {
1259 rc = sprintf(out_buf, "%i\n", id->level);
1260 }
1da177e4
LT
1261 return rc;
1262}
1263
1264/*
1265 * reads new debug level
1266 */
1267
66a464db
MH
1268static int
1269debug_input_level_fn(debug_info_t * id, struct debug_view *view,
1270 struct file *file, const char __user *user_buf,
1271 size_t user_len, loff_t * offset)
1da177e4 1272{
66a464db
MH
1273 char *str;
1274 int rc,new_level;
1da177e4 1275
66a464db
MH
1276 if (user_len > 0x10000)
1277 user_len = 0x10000;
1278 if (*offset != 0){
1279 rc = -EPIPE;
1da177e4 1280 goto out;
66a464db
MH
1281 }
1282 str = debug_get_user_string(user_buf,user_len);
1283 if(IS_ERR(str)){
1284 rc = PTR_ERR(str);
1da177e4
LT
1285 goto out;
1286 }
66a464db 1287 if(str[0] == '-'){
1da177e4 1288 debug_set_level(id, DEBUG_OFF_LEVEL);
66a464db
MH
1289 rc = user_len;
1290 goto free_str;
1da177e4 1291 } else {
66a464db 1292 new_level = debug_get_uint(str);
1da177e4 1293 }
66a464db 1294 if(new_level < 0) {
c5612c19
MH
1295 pr_warning("%s is not a valid level for a debug "
1296 "feature\n", str);
66a464db
MH
1297 rc = -EINVAL;
1298 } else {
1299 debug_set_level(id, new_level);
1300 rc = user_len;
1301 }
1302free_str:
1303 kfree(str);
1304out:
1305 *offset += user_len;
1da177e4
LT
1306 return rc; /* number of input characters */
1307}
1308
1309
1310/*
1311 * flushes debug areas
1312 */
1313
2b67fc46 1314static void debug_flush(debug_info_t* id, int area)
1da177e4
LT
1315{
1316 unsigned long flags;
66a464db 1317 int i,j;
1da177e4 1318
66a464db 1319 if(!id || !id->areas)
1da177e4
LT
1320 return;
1321 spin_lock_irqsave(&id->lock,flags);
1322 if(area == DEBUG_FLUSH_ALL){
1323 id->active_area = 0;
66a464db
MH
1324 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1325 for (i = 0; i < id->nr_areas; i++) {
1326 id->active_pages[i] = 0;
1327 for(j = 0; j < id->pages_per_area; j++) {
1328 memset(id->areas[i][j], 0, PAGE_SIZE);
1329 }
1330 }
1da177e4 1331 } else if(area >= 0 && area < id->nr_areas) {
66a464db
MH
1332 id->active_entries[area] = 0;
1333 id->active_pages[area] = 0;
1334 for(i = 0; i < id->pages_per_area; i++) {
1335 memset(id->areas[area][i],0,PAGE_SIZE);
1336 }
1da177e4
LT
1337 }
1338 spin_unlock_irqrestore(&id->lock,flags);
1339}
1340
1341/*
1342 * view function: flushes debug areas
1343 */
1344
66a464db
MH
1345static int
1346debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
1347 struct file *file, const char __user *user_buf,
1348 size_t user_len, loff_t * offset)
1da177e4
LT
1349{
1350 char input_buf[1];
66a464db
MH
1351 int rc = user_len;
1352
1353 if (user_len > 0x10000)
1354 user_len = 0x10000;
1355 if (*offset != 0){
1356 rc = -EPIPE;
1da177e4 1357 goto out;
66a464db 1358 }
1da177e4
LT
1359 if (copy_from_user(input_buf, user_buf, 1)){
1360 rc = -EFAULT;
1361 goto out;
1362 }
1363 if(input_buf[0] == '-') {
1364 debug_flush(id, DEBUG_FLUSH_ALL);
1365 goto out;
1366 }
1367 if (isdigit(input_buf[0])) {
1368 int area = ((int) input_buf[0] - (int) '0');
1369 debug_flush(id, area);
1370 goto out;
1371 }
1372
c5612c19
MH
1373 pr_info("Flushing debug data failed because %c is not a valid "
1374 "area\n", input_buf[0]);
1da177e4 1375
66a464db
MH
1376out:
1377 *offset += user_len;
1da177e4
LT
1378 return rc; /* number of input characters */
1379}
1380
1381/*
1382 * prints debug header in raw format
1383 */
1384
66a464db
MH
1385static int
1386debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
1387 int area, debug_entry_t * entry, char *out_buf)
1da177e4
LT
1388{
1389 int rc;
1390
1391 rc = sizeof(debug_entry_t);
1392 memcpy(out_buf,entry,sizeof(debug_entry_t));
1393 return rc;
1394}
1395
1396/*
1397 * prints debug data in raw format
1398 */
1399
66a464db
MH
1400static int
1401debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
1da177e4
LT
1402 char *out_buf, const char *in_buf)
1403{
1404 int rc;
1405
1406 rc = id->buf_size;
1407 memcpy(out_buf, in_buf, id->buf_size);
1408 return rc;
1409}
1410
1411/*
1412 * prints debug data in hex/ascii format
1413 */
1414
66a464db
MH
1415static int
1416debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
1417 char *out_buf, const char *in_buf)
1da177e4
LT
1418{
1419 int i, rc = 0;
1420
1421 for (i = 0; i < id->buf_size; i++) {
1422 rc += sprintf(out_buf + rc, "%02x ",
1423 ((unsigned char *) in_buf)[i]);
1424 }
1425 rc += sprintf(out_buf + rc, "| ");
1426 for (i = 0; i < id->buf_size; i++) {
1427 unsigned char c = in_buf[i];
1428 if (!isprint(c))
1429 rc += sprintf(out_buf + rc, ".");
1430 else
1431 rc += sprintf(out_buf + rc, "%c", c);
1432 }
1433 rc += sprintf(out_buf + rc, "\n");
1434 return rc;
1435}
1436
1437/*
1438 * prints header for debug entry
1439 */
1440
66a464db
MH
1441int
1442debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
1da177e4
LT
1443 int area, debug_entry_t * entry, char *out_buf)
1444{
942eaabd 1445 struct timespec time_spec;
1da177e4
LT
1446 char *except_str;
1447 unsigned long caller;
1448 int rc = 0;
1449 unsigned int level;
1450
1451 level = entry->id.fields.level;
b592e89a 1452 stck_to_timespec(entry->id.stck, &time_spec);
1da177e4
LT
1453
1454 if (entry->id.fields.exception)
1455 except_str = "*";
1456 else
1457 except_str = "-";
1458 caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
1459 rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
942eaabd 1460 area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
1da177e4
LT
1461 except_str, entry->id.fields.cpuid, (void *) caller);
1462 return rc;
1463}
1464
1465/*
1466 * prints debug data sprintf-formated:
1467 * debug_sprinf_event/exception calls must be used together with this view
1468 */
1469
1470#define DEBUG_SPRINTF_MAX_ARGS 10
1471
66a464db
MH
1472static int
1473debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
1474 char *out_buf, debug_sprintf_entry_t *curr_event)
1da177e4
LT
1475{
1476 int num_longs, num_used_args = 0,i, rc = 0;
1477 int index[DEBUG_SPRINTF_MAX_ARGS];
1478
1479 /* count of longs fit into one entry */
1480 num_longs = id->buf_size / sizeof(long);
1481
1482 if(num_longs < 1)
1483 goto out; /* bufsize of entry too small */
1484 if(num_longs == 1) {
1485 /* no args, we use only the string */
1486 strcpy(out_buf, curr_event->string);
1487 rc = strlen(curr_event->string);
1488 goto out;
1489 }
1490
1491 /* number of arguments used for sprintf (without the format string) */
1492 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1493
1494 memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1495
1496 for(i = 0; i < num_used_args; i++)
1497 index[i] = i;
1498
1499 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1500 curr_event->args[index[1]], curr_event->args[index[2]],
1501 curr_event->args[index[3]], curr_event->args[index[4]],
1502 curr_event->args[index[5]], curr_event->args[index[6]],
1503 curr_event->args[index[7]], curr_event->args[index[8]],
1504 curr_event->args[index[9]]);
1505
1506out:
1507
1508 return rc;
1509}
1510
1511/*
1512 * clean up module
1513 */
2b67fc46 1514static void __exit debug_exit(void)
1da177e4 1515{
66a464db 1516 debugfs_remove(debug_debugfs_root_entry);
1da177e4
LT
1517 unregister_sysctl_table(s390dbf_sysctl_header);
1518 return;
1519}
1520
1521/*
1522 * module definitions
1523 */
66a464db 1524postcore_initcall(debug_init);
1da177e4
LT
1525module_exit(debug_exit);
1526MODULE_LICENSE("GPL");
1527
1528EXPORT_SYMBOL(debug_register);
1529EXPORT_SYMBOL(debug_unregister);
1530EXPORT_SYMBOL(debug_set_level);
1531EXPORT_SYMBOL(debug_stop_all);
1532EXPORT_SYMBOL(debug_register_view);
1533EXPORT_SYMBOL(debug_unregister_view);
1534EXPORT_SYMBOL(debug_event_common);
1535EXPORT_SYMBOL(debug_exception_common);
1536EXPORT_SYMBOL(debug_hex_ascii_view);
1537EXPORT_SYMBOL(debug_raw_view);
1538EXPORT_SYMBOL(debug_dflt_header_fn);
1539EXPORT_SYMBOL(debug_sprintf_view);
1540EXPORT_SYMBOL(debug_sprintf_exception);
1541EXPORT_SYMBOL(debug_sprintf_event);
This page took 0.504866 seconds and 5 git commands to generate.