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