debug.c 35.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  arch/s390/kernel/debug.c
 *   S/390 debug facility
 *
 *    Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH,
 *                             IBM Corporation
 *    Author(s): Michael Holzheu (holzheu@de.ibm.com),
 *               Holger Smolinski (Holger.Smolinski@de.ibm.com)
 *
 *    Bugreports to: <Linux390@de.ibm.com>
 */

#include <linux/stddef.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/ctype.h>
#include <linux/sysctl.h>
#include <asm/uaccess.h>
#include <asm/semaphore.h>
#include <linux/module.h>
#include <linux/init.h>
23 24
#include <linux/fs.h>
#include <linux/debugfs.h>
L
Linus Torvalds 已提交
25 26 27 28 29

#include <asm/debug.h>

#define DEBUG_PROLOG_ENTRY -1

30 31 32
#define ALL_AREAS 0 /* copy all debug areas */
#define NO_AREAS  1 /* copy no debug areas */

L
Linus Torvalds 已提交
33 34 35 36 37
/* typedefs */

typedef struct file_private_info {
	loff_t offset;			/* offset of last read in file */
	int    act_area;                /* number of last formated area */
38
	int    act_page;                /* act page in given area */
L
Linus Torvalds 已提交
39 40
	int    act_entry;               /* last formated entry (offset */
                                        /* relative to beginning of last */
41
                                        /* formated page) */
L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	size_t act_entry_offset;        /* up to this offset we copied */
					/* in last read the last formated */
					/* entry to userland */
	char   temp_buf[2048];		/* buffer for output */
	debug_info_t *debug_info_org;   /* original debug information */
	debug_info_t *debug_info_snap;	/* snapshot of debug information */
	struct debug_view *view;	/* used view of debug info */
} file_private_info_t;

typedef struct
{
	char *string;
	/* 
	 * This assumes that all args are converted into longs 
	 * on L/390 this is the case for all types of parameter 
	 * except of floats, and long long (32 bit) 
58 59
	 *
	 */
L
Linus Torvalds 已提交
60 61 62 63
	long args[0];
} debug_sprintf_entry_t;


64
extern void tod_to_timeval(uint64_t todval, struct timespec *xtime);
L
Linus Torvalds 已提交
65 66 67 68 69

/* internal function prototyes */

static int debug_init(void);
static ssize_t debug_output(struct file *file, char __user *user_buf,
70
			size_t user_len, loff_t * offset);
L
Linus Torvalds 已提交
71
static ssize_t debug_input(struct file *file, const char __user *user_buf,
72
			size_t user_len, loff_t * offset);
L
Linus Torvalds 已提交
73 74
static int debug_open(struct inode *inode, struct file *file);
static int debug_close(struct inode *inode, struct file *file);
75 76
static debug_info_t*  debug_info_create(char *name, int pages_per_area,
			int nr_areas, int buf_size);
L
Linus Torvalds 已提交
77 78 79
static void debug_info_get(debug_info_t *);
static void debug_info_put(debug_info_t *);
static int debug_prolog_level_fn(debug_info_t * id,
80
			struct debug_view *view, char *out_buf);
L
Linus Torvalds 已提交
81
static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,
82 83 84 85 86 87 88
			struct file *file, const char __user *user_buf,
			size_t user_buf_size, loff_t * offset);
static int debug_prolog_pages_fn(debug_info_t * id,
			struct debug_view *view, char *out_buf);
static int debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
			struct file *file, const char __user *user_buf,
			size_t user_buf_size, loff_t * offset);
L
Linus Torvalds 已提交
89
static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
90 91
			struct file *file, const char __user *user_buf,
			size_t user_buf_size, loff_t * offset);
L
Linus Torvalds 已提交
92
static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
93
			char *out_buf, const char *in_buf);
L
Linus Torvalds 已提交
94
static int debug_raw_format_fn(debug_info_t * id,
95 96
			struct debug_view *view, char *out_buf,
			const char *in_buf);
L
Linus Torvalds 已提交
97
static int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
98
			int area, debug_entry_t * entry, char *out_buf);
L
Linus Torvalds 已提交
99 100

static int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
101
			char *out_buf, debug_sprintf_entry_t *curr_event);
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

/* globals */

struct debug_view debug_raw_view = {
	"raw",
	NULL,
	&debug_raw_header_fn,
	&debug_raw_format_fn,
	NULL,
	NULL
};

struct debug_view debug_hex_ascii_view = {
	"hex_ascii",
	NULL,
	&debug_dflt_header_fn,
	&debug_hex_ascii_format_fn,
	NULL,
	NULL
};

123
static struct debug_view debug_level_view = {
L
Linus Torvalds 已提交
124 125 126 127 128 129 130 131
	"level",
	&debug_prolog_level_fn,
	NULL,
	NULL,
	&debug_input_level_fn,
	NULL
};

132
static struct debug_view debug_pages_view = {
133 134 135 136 137 138 139 140
	"pages",
	&debug_prolog_pages_fn,
	NULL,
	NULL,
	&debug_input_pages_fn,
	NULL
};

141
static struct debug_view debug_flush_view = {
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        "flush",
        NULL,
        NULL,
        NULL,
        &debug_input_flush_fn,
        NULL
};

struct debug_view debug_sprintf_view = {
	"sprintf",
	NULL,
	&debug_dflt_header_fn,
	(debug_format_proc_t*)&debug_sprintf_format_fn,
	NULL,
	NULL
};

159
/* used by dump analysis tools to determine version of debug feature */
L
Linus Torvalds 已提交
160 161 162 163 164 165
unsigned int debug_feature_version = __DEBUG_FEATURE_VERSION;

/* static globals */

static debug_info_t *debug_area_first = NULL;
static debug_info_t *debug_area_last = NULL;
166
static DEFINE_MUTEX(debug_mutex);
L
Linus Torvalds 已提交
167 168 169

static int initialized;

170
static const struct file_operations debug_file_ops = {
171
	.owner   = THIS_MODULE,
L
Linus Torvalds 已提交
172
	.read    = debug_output,
173
	.write   = debug_input,
L
Linus Torvalds 已提交
174 175 176 177
	.open    = debug_open,
	.release = debug_close,
};

178
static struct dentry *debug_debugfs_root_entry;
L
Linus Torvalds 已提交
179 180 181

/* functions */

182 183 184 185 186 187 188 189 190 191 192 193
/*
 * debug_areas_alloc
 * - Debug areas are implemented as a threedimensonal array:
 *   areas[areanumber][pagenumber][pageoffset]
 */

static debug_entry_t***
debug_areas_alloc(int pages_per_area, int nr_areas)
{
	debug_entry_t*** areas;
	int i,j;

194
	areas = kmalloc(nr_areas *
195 196 197 198 199
					sizeof(debug_entry_t**),
					GFP_KERNEL);
	if (!areas)
		goto fail_malloc_areas;
	for (i = 0; i < nr_areas; i++) {
200
		areas[i] = kmalloc(pages_per_area *
201 202 203 204 205
				sizeof(debug_entry_t*),GFP_KERNEL);
		if (!areas[i]) {
			goto fail_malloc_areas2;
		}
		for(j = 0; j < pages_per_area; j++) {
206
			areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
			if(!areas[i][j]) {
				for(j--; j >=0 ; j--) {
					kfree(areas[i][j]);
				}
				kfree(areas[i]);
				goto fail_malloc_areas2;
			}
		}
	}
	return areas;

fail_malloc_areas2:
	for(i--; i >= 0; i--){
		for(j=0; j < pages_per_area;j++){
			kfree(areas[i][j]);
		}
		kfree(areas[i]);
	}
	kfree(areas);
fail_malloc_areas:
	return NULL;

}


L
Linus Torvalds 已提交
232 233 234 235 236
/*
 * debug_info_alloc
 * - alloc new debug-info
 */

237 238 239
static debug_info_t*
debug_info_alloc(char *name, int pages_per_area, int nr_areas, int buf_size,
		int level, int mode)
L
Linus Torvalds 已提交
240 241 242 243 244
{
	debug_info_t* rc;

	/* alloc everything */

245
	rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
L
Linus Torvalds 已提交
246 247
	if(!rc)
		goto fail_malloc_rc;
248
	rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
249 250
	if(!rc->active_entries)
		goto fail_malloc_active_entries;
251
	rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
252 253 254 255 256 257 258 259
	if(!rc->active_pages)
		goto fail_malloc_active_pages;
	if((mode == ALL_AREAS) && (pages_per_area != 0)){
		rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
		if(!rc->areas)
			goto fail_malloc_areas;
	} else {
		rc->areas = NULL;
L
Linus Torvalds 已提交
260 261 262 263 264
	}

	/* initialize members */

	spin_lock_init(&rc->lock);
265 266 267 268 269 270
	rc->pages_per_area = pages_per_area;
	rc->nr_areas       = nr_areas;
	rc->active_area    = 0;
	rc->level          = level;
	rc->buf_size       = buf_size;
	rc->entry_size     = sizeof(debug_entry_t) + buf_size;
J
Jean Delvare 已提交
271
	strlcpy(rc->name, name, sizeof(rc->name));
L
Linus Torvalds 已提交
272
	memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
273 274
	memset(rc->debugfs_entries, 0 ,DEBUG_MAX_VIEWS *
		sizeof(struct dentry*));
L
Linus Torvalds 已提交
275 276 277 278 279
	atomic_set(&(rc->ref_count), 0);

	return rc;

fail_malloc_areas:
280 281 282 283
	kfree(rc->active_pages);
fail_malloc_active_pages:
	kfree(rc->active_entries);
fail_malloc_active_entries:
L
Linus Torvalds 已提交
284 285 286 287 288 289
	kfree(rc);
fail_malloc_rc:
	return NULL;
}

/*
290 291
 * debug_areas_free
 * - free all debug areas
L
Linus Torvalds 已提交
292 293
 */

294 295 296 297 298 299 300
static void
debug_areas_free(debug_info_t* db_info)
{
	int i,j;

	if(!db_info->areas)
		return;
L
Linus Torvalds 已提交
301
	for (i = 0; i < db_info->nr_areas; i++) {
302 303 304 305
		for(j = 0; j < db_info->pages_per_area; j++) {
			kfree(db_info->areas[i][j]);
		}
		kfree(db_info->areas[i]);
L
Linus Torvalds 已提交
306 307
	}
	kfree(db_info->areas);
308 309 310 311 312 313 314 315 316 317 318 319 320
	db_info->areas = NULL;
}

/*
 * debug_info_free
 * - free memory debug-info
 */

static void
debug_info_free(debug_info_t* db_info){
	debug_areas_free(db_info);
	kfree(db_info->active_entries);
	kfree(db_info->active_pages);
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328
	kfree(db_info);
}

/*
 * debug_info_create
 * - create new debug-info
 */

329 330
static debug_info_t*
debug_info_create(char *name, int pages_per_area, int nr_areas, int buf_size)
L
Linus Torvalds 已提交
331 332 333
{
	debug_info_t* rc;

334 335
        rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
				DEBUG_DEFAULT_LEVEL, ALL_AREAS);
L
Linus Torvalds 已提交
336 337 338
        if(!rc) 
		goto out;

339 340 341
	/* create root directory */
        rc->debugfs_root_entry = debugfs_create_dir(rc->name,
					debug_debugfs_root_entry);
L
Linus Torvalds 已提交
342 343

	/* append new element to linked list */
344
        if (!debug_area_first) {
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
                /* first element in list */
                debug_area_first = rc;
                rc->prev = NULL;
        } else {
                /* append element to end of list */
                debug_area_last->next = rc;
                rc->prev = debug_area_last;
        }
        debug_area_last = rc;
        rc->next = NULL;

	debug_info_get(rc);
out:
	return rc;
}

/*
 * debug_info_copy
 * - copy debug-info
 */

366 367
static debug_info_t*
debug_info_copy(debug_info_t* in, int mode)
L
Linus Torvalds 已提交
368
{
369
        int i,j;
L
Linus Torvalds 已提交
370
        debug_info_t* rc;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
        unsigned long flags;

	/* get a consistent copy of the debug areas */
	do {
		rc = debug_info_alloc(in->name, in->pages_per_area,
			in->nr_areas, in->buf_size, in->level, mode);
		spin_lock_irqsave(&in->lock, flags);
		if(!rc)
			goto out;
		/* has something changed in the meantime ? */
		if((rc->pages_per_area == in->pages_per_area) &&
		   (rc->nr_areas == in->nr_areas)) {
			break;
		}
		spin_unlock_irqrestore(&in->lock, flags);
		debug_info_free(rc);
	} while (1);
388 389

        if(!rc || (mode == NO_AREAS))
L
Linus Torvalds 已提交
390 391 392
                goto out;

        for(i = 0; i < in->nr_areas; i++){
393 394 395
		for(j = 0; j < in->pages_per_area; j++) {
			memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
		}
L
Linus Torvalds 已提交
396 397
        }
out:
398
        spin_unlock_irqrestore(&in->lock, flags);
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406
        return rc;
}

/*
 * debug_info_get
 * - increments reference count for debug-info
 */

407 408
static void
debug_info_get(debug_info_t * db_info)
L
Linus Torvalds 已提交
409 410 411 412 413 414 415 416 417 418
{
	if (db_info)
		atomic_inc(&db_info->ref_count);
}

/*
 * debug_info_put:
 * - decreases reference count for debug-info and frees it if necessary
 */

419 420
static void
debug_info_put(debug_info_t *db_info)
L
Linus Torvalds 已提交
421 422 423 424 425 426 427
{
	int i;

	if (!db_info)
		return;
	if (atomic_dec_and_test(&db_info->ref_count)) {
		for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
428
			if (!db_info->views[i])
L
Linus Torvalds 已提交
429
				continue;
430
			debugfs_remove(db_info->debugfs_entries[i]);
L
Linus Torvalds 已提交
431
		}
432
		debugfs_remove(db_info->debugfs_root_entry);
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
		if(db_info == debug_area_first)
			debug_area_first = db_info->next;
		if(db_info == debug_area_last)
			debug_area_last = db_info->prev;
		if(db_info->prev) db_info->prev->next = db_info->next;
		if(db_info->next) db_info->next->prev = db_info->prev;
		debug_info_free(db_info);
	}
}

/*
 * debug_format_entry:
 * - format one debug entry and return size of formated data
 */

448 449
static int
debug_format_entry(file_private_info_t *p_info)
L
Linus Torvalds 已提交
450 451 452 453 454 455 456 457
{
	debug_info_t *id_snap   = p_info->debug_info_snap;
	struct debug_view *view = p_info->view;
	debug_entry_t *act_entry;
	size_t len = 0;
	if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
		/* print prolog */
        	if (view->prolog_proc)
458
                	len += view->prolog_proc(id_snap,view,p_info->temp_buf);
L
Linus Torvalds 已提交
459 460
		goto out;
	}
461 462 463 464
	if (!id_snap->areas) /* this is true, if we have a prolog only view */
		goto out;    /* or if 'pages_per_area' is 0 */
	act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area]
				[p_info->act_page] + p_info->act_entry);
L
Linus Torvalds 已提交
465 466 467 468
                        
	if (act_entry->id.stck == 0LL)
			goto out;  /* empty entry */
	if (view->header_proc)
469
		len += view->header_proc(id_snap, view, p_info->act_area,
L
Linus Torvalds 已提交
470 471
					act_entry, p_info->temp_buf + len);
	if (view->format_proc)
472
		len += view->format_proc(id_snap, view, p_info->temp_buf + len,
L
Linus Torvalds 已提交
473
						DEBUG_DATA(act_entry));
474
out:
L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482
        return len;
}

/*
 * debug_next_entry:
 * - goto next entry in p_info
 */

483
static inline int
484
debug_next_entry(file_private_info_t *p_info)
L
Linus Torvalds 已提交
485
{
486 487 488
	debug_info_t *id;

	id = p_info->debug_info_snap;
L
Linus Torvalds 已提交
489 490
	if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
		p_info->act_entry = 0;
491
		p_info->act_page  = 0;
L
Linus Torvalds 已提交
492 493
		goto out;
	}
494 495 496 497 498 499
	if(!id->areas)
		return 1;
	p_info->act_entry += id->entry_size;
	/* switch to next page, if we reached the end of the page  */
	if (p_info->act_entry > (PAGE_SIZE - id->entry_size)){
		/* next page */
L
Linus Torvalds 已提交
500
		p_info->act_entry = 0;
501 502 503 504 505 506
		p_info->act_page += 1;
		if((p_info->act_page % id->pages_per_area) == 0) {
			/* next area */
        		p_info->act_area++;
			p_info->act_page=0;
		}
L
Linus Torvalds 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519
        	if(p_info->act_area >= id->nr_areas)
			return 1;
	}
out:
	return 0;	
}

/*
 * debug_output:
 * - called for user read()
 * - copies formated debug entries to the user buffer
 */

520 521 522 523 524
static ssize_t
debug_output(struct file *file,		/* file descriptor */
	    char __user *user_buf,	/* user buffer */
	    size_t  len,		/* length of buffer */
	    loff_t *offset)		/* offset in the file */
L
Linus Torvalds 已提交
525 526
{
	size_t count = 0;
527
	size_t entry_offset;
L
Linus Torvalds 已提交
528 529 530 531 532 533 534 535 536
	file_private_info_t *p_info;

	p_info = ((file_private_info_t *) file->private_data);
	if (*offset != p_info->offset) 
		return -EPIPE;
	if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
		return 0;
	entry_offset = p_info->act_entry_offset;
	while(count < len){
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
		int formatted_line_size;
		int formatted_line_residue;
		int user_buf_residue;
		size_t copy_size;

		formatted_line_size = debug_format_entry(p_info);
		formatted_line_residue = formatted_line_size - entry_offset;
		user_buf_residue = len-count;
		copy_size = min(user_buf_residue, formatted_line_residue);
		if(copy_size){
			if (copy_to_user(user_buf + count, p_info->temp_buf
					+ entry_offset, copy_size))
				return -EFAULT;
			count += copy_size;
			entry_offset += copy_size;
L
Linus Torvalds 已提交
552
		}
553 554 555
		if(copy_size == formatted_line_residue){
			entry_offset = 0;
			if(debug_next_entry(p_info))
L
Linus Torvalds 已提交
556
				goto out;
557
		}
L
Linus Torvalds 已提交
558 559 560
	}
out:
	p_info->offset           = *offset + count;
561
	p_info->act_entry_offset = entry_offset;
L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569 570 571
	*offset = p_info->offset;
	return count;
}

/*
 * debug_input:
 * - called for user write()
 * - calls input function of view
 */

572 573 574
static ssize_t
debug_input(struct file *file, const char __user *user_buf, size_t length,
		loff_t *offset)
L
Linus Torvalds 已提交
575 576 577 578
{
	int rc = 0;
	file_private_info_t *p_info;

579
	mutex_lock(&debug_mutex);
L
Linus Torvalds 已提交
580 581 582 583 584 585 586
	p_info = ((file_private_info_t *) file->private_data);
	if (p_info->view->input_proc)
		rc = p_info->view->input_proc(p_info->debug_info_org,
					      p_info->view, file, user_buf,
					      length, offset);
	else
		rc = -EPERM;
587
	mutex_unlock(&debug_mutex);
L
Linus Torvalds 已提交
588 589 590 591 592 593 594 595 596 597
	return rc;		/* number of input characters */
}

/*
 * debug_open:
 * - called for user open()
 * - copies formated output to private_data area of the file
 *   handle
 */

598 599
static int
debug_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
600 601 602 603 604
{
	int i = 0, rc = 0;
	file_private_info_t *p_info;
	debug_info_t *debug_info, *debug_info_snapshot;

605
	mutex_lock(&debug_mutex);
J
Josef Sipek 已提交
606
	debug_info = file->f_path.dentry->d_inode->i_private;
607 608 609 610 611
	/* find debug view */
	for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
		if (!debug_info->views[i])
			continue;
		else if (debug_info->debugfs_entries[i] ==
J
Josef Sipek 已提交
612
			 file->f_path.dentry) {
613
			goto found;	/* found view ! */
L
Linus Torvalds 已提交
614 615 616 617 618 619
		}
	}
	/* no entry found */
	rc = -EINVAL;
	goto out;

620
found:
L
Linus Torvalds 已提交
621

622 623 624
	/* Make snapshot of current debug areas to get it consistent.     */
	/* To copy all the areas is only needed, if we have a view which  */
	/* formats the debug areas. */
L
Linus Torvalds 已提交
625

626 627 628 629 630 631
	if(!debug_info->views[i]->format_proc &&
		!debug_info->views[i]->header_proc){
		debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
	} else {
		debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
	}
L
Linus Torvalds 已提交
632 633 634 635 636

	if(!debug_info_snapshot){
		rc = -ENOMEM;
		goto out;
	}
637
	p_info = kmalloc(sizeof(file_private_info_t),
638 639 640 641
						GFP_KERNEL);
	if(!p_info){
		if(debug_info_snapshot)
			debug_info_free(debug_info_snapshot);
L
Linus Torvalds 已提交
642 643 644 645 646 647 648 649
		rc = -ENOMEM;
		goto out;
	}
	p_info->offset = 0;
	p_info->debug_info_snap = debug_info_snapshot;
	p_info->debug_info_org  = debug_info;
	p_info->view = debug_info->views[i];
	p_info->act_area = 0;
650
	p_info->act_page = 0;
L
Linus Torvalds 已提交
651 652
	p_info->act_entry = DEBUG_PROLOG_ENTRY;
	p_info->act_entry_offset = 0;
653
	file->private_data = p_info;
L
Linus Torvalds 已提交
654
	debug_info_get(debug_info);
655
out:
656
	mutex_unlock(&debug_mutex);
L
Linus Torvalds 已提交
657 658 659 660 661 662 663 664 665
	return rc;
}

/*
 * debug_close:
 * - called for user close()
 * - deletes  private_data area of the file handle
 */

666 667
static int
debug_close(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
668 669 670
{
	file_private_info_t *p_info;
	p_info = (file_private_info_t *) file->private_data;
671 672
	if(p_info->debug_info_snap)
		debug_info_free(p_info->debug_info_snap);
L
Linus Torvalds 已提交
673 674 675 676 677 678 679 680 681 682 683
	debug_info_put(p_info->debug_info_org);
	kfree(file->private_data);
	return 0;		/* success */
}

/*
 * debug_register:
 * - creates and initializes debug area for the caller
 * - returns handle for debug area
 */

684 685
debug_info_t*
debug_register (char *name, int pages_per_area, int nr_areas, int buf_size)
L
Linus Torvalds 已提交
686 687 688 689 690
{
	debug_info_t *rc = NULL;

	if (!initialized)
		BUG();
691
	mutex_lock(&debug_mutex);
L
Linus Torvalds 已提交
692 693 694

        /* create new debug_info */

695
	rc = debug_info_create(name, pages_per_area, nr_areas, buf_size);
L
Linus Torvalds 已提交
696 697 698 699
	if(!rc) 
		goto out;
	debug_register_view(rc, &debug_level_view);
        debug_register_view(rc, &debug_flush_view);
700 701 702
	debug_register_view(rc, &debug_pages_view);
out:
        if (!rc){
L
Linus Torvalds 已提交
703 704
		printk(KERN_ERR "debug: debug_register failed for %s\n",name);
        }
705
	mutex_unlock(&debug_mutex);
L
Linus Torvalds 已提交
706 707 708 709 710 711 712 713
	return rc;
}

/*
 * debug_unregister:
 * - give back debug area
 */

714 715
void
debug_unregister(debug_info_t * id)
L
Linus Torvalds 已提交
716 717 718
{
	if (!id)
		goto out;
719
	mutex_lock(&debug_mutex);
L
Linus Torvalds 已提交
720
	debug_info_put(id);
721
	mutex_unlock(&debug_mutex);
L
Linus Torvalds 已提交
722

723
out:
L
Linus Torvalds 已提交
724 725 726
	return;
}

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
/*
 * debug_set_size:
 * - set area size (number of pages) and number of areas
 */
static int
debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
{
	unsigned long flags;
	debug_entry_t *** new_areas;
	int rc=0;

	if(!id || (nr_areas <= 0) || (pages_per_area < 0))
		return -EINVAL;
	if(pages_per_area > 0){
		new_areas = debug_areas_alloc(pages_per_area, nr_areas);
		if(!new_areas) {
			printk(KERN_WARNING "debug: could not allocate memory "\
					 "for pagenumber: %i\n",pages_per_area);
			rc = -ENOMEM;
			goto out;
		}
	} else {
		new_areas = NULL;
	}
	spin_lock_irqsave(&id->lock,flags);
	debug_areas_free(id);
	id->areas = new_areas;
	id->nr_areas = nr_areas;
	id->pages_per_area = pages_per_area;
	id->active_area = 0;
	memset(id->active_entries,0,sizeof(int)*id->nr_areas);
	memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
	spin_unlock_irqrestore(&id->lock,flags);
	printk(KERN_INFO "debug: %s: set new size (%i pages)\n"\
			 ,id->name, pages_per_area);
out:
	return rc;
}

L
Linus Torvalds 已提交
766 767 768 769 770
/*
 * debug_set_level:
 * - set actual debug level
 */

771 772
void
debug_set_level(debug_info_t* id, int new_level)
L
Linus Torvalds 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
{
	unsigned long flags;
	if(!id)
		return;	
	spin_lock_irqsave(&id->lock,flags);
        if(new_level == DEBUG_OFF_LEVEL){
                id->level = DEBUG_OFF_LEVEL;
                printk(KERN_INFO "debug: %s: switched off\n",id->name);
        } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
                printk(KERN_INFO
                        "debug: %s: level %i is out of range (%i - %i)\n",
                        id->name, new_level, 0, DEBUG_MAX_LEVEL);
        } else {
                id->level = new_level;
        }
	spin_unlock_irqrestore(&id->lock,flags);
}


/*
 * proceed_active_entry:
 * - set active entry to next in the ring buffer
 */

797
static inline void
798
proceed_active_entry(debug_info_t * id)
L
Linus Torvalds 已提交
799
{
800 801 802 803 804 805 806
	if ((id->active_entries[id->active_area] += id->entry_size)
	    > (PAGE_SIZE - id->entry_size)){
		id->active_entries[id->active_area] = 0;
		id->active_pages[id->active_area] =
			(id->active_pages[id->active_area] + 1) %
			id->pages_per_area;
	}
L
Linus Torvalds 已提交
807 808 809 810 811 812 813
}

/*
 * proceed_active_area:
 * - set active area to next in the ring buffer
 */

814
static inline void
815
proceed_active_area(debug_info_t * id)
L
Linus Torvalds 已提交
816 817 818 819 820 821 822 823 824
{
	id->active_area++;
	id->active_area = id->active_area % id->nr_areas;
}

/*
 * get_active_entry:
 */

825
static inline debug_entry_t*
826
get_active_entry(debug_info_t * id)
L
Linus Torvalds 已提交
827
{
828 829 830
	return (debug_entry_t *) (((char *) id->areas[id->active_area]
					[id->active_pages[id->active_area]]) +
					id->active_entries[id->active_area]);
L
Linus Torvalds 已提交
831 832 833 834 835 836 837
}

/*
 * debug_finish_entry:
 * - set timestamp, caller address, cpu number etc.
 */

838
static inline void
839 840
debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
			int exception)
L
Linus Torvalds 已提交
841
{
842
	active->id.stck = get_clock();
L
Linus Torvalds 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
	active->id.fields.cpuid = smp_processor_id();
	active->caller = __builtin_return_address(0);
	active->id.fields.exception = exception;
	active->id.fields.level     = level;
	proceed_active_entry(id);
	if(exception)
		proceed_active_area(id);
}

static int debug_stoppable=1;
static int debug_active=1;

#define CTL_S390DBF_STOPPABLE 5678
#define CTL_S390DBF_ACTIVE 5679

/*
 * proc handler for the running debug_active sysctl
 * always allow read, allow write only if debug_stoppable is set or
 * if debug_active is already off
 */
863 864
static int
s390dbf_procactive(ctl_table *table, int write, struct file *filp,
L
Linus Torvalds 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
                     void __user *buffer, size_t *lenp, loff_t *ppos)
{
	if (!write || debug_stoppable || !debug_active)
		return proc_dointvec(table, write, filp, buffer, lenp, ppos);
	else
		return 0;
}


static struct ctl_table s390dbf_table[] = {
	{
		.ctl_name       = CTL_S390DBF_STOPPABLE,
		.procname       = "debug_stoppable",
		.data		= &debug_stoppable,
		.maxlen		= sizeof(int),
		.mode           = S_IRUGO | S_IWUSR,
		.proc_handler   = &proc_dointvec,
		.strategy	= &sysctl_intvec,
	},
	 {
		.ctl_name       = CTL_S390DBF_ACTIVE,
		.procname       = "debug_active",
		.data		= &debug_active,
		.maxlen		= sizeof(int),
		.mode           = S_IRUGO | S_IWUSR,
		.proc_handler   = &s390dbf_procactive,
		.strategy	= &sysctl_intvec,
	},
	{ .ctl_name = 0 }
};

static struct ctl_table s390dbf_dir_table[] = {
	{
		.ctl_name       = CTL_S390DBF,
		.procname       = "s390dbf",
		.maxlen         = 0,
		.mode           = S_IRUGO | S_IXUGO,
		.child          = s390dbf_table,
	},
	{ .ctl_name = 0 }
};

907
static struct ctl_table_header *s390dbf_sysctl_header;
L
Linus Torvalds 已提交
908

909 910
void
debug_stop_all(void)
L
Linus Torvalds 已提交
911 912 913 914 915 916 917 918 919 920 921
{
	if (debug_stoppable)
		debug_active = 0;
}


/*
 * debug_event_common:
 * - write debug entry with given size
 */

922 923
debug_entry_t*
debug_event_common(debug_info_t * id, int level, const void *buf, int len)
L
Linus Torvalds 已提交
924 925 926 927
{
	unsigned long flags;
	debug_entry_t *active;

928
	if (!debug_active || !id->areas)
L
Linus Torvalds 已提交
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
		return NULL;
	spin_lock_irqsave(&id->lock, flags);
	active = get_active_entry(id);
	memset(DEBUG_DATA(active), 0, id->buf_size);
	memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
	debug_finish_entry(id, active, level, 0);
	spin_unlock_irqrestore(&id->lock, flags);

	return active;
}

/*
 * debug_exception_common:
 * - write debug entry with given size and switch to next debug area
 */

945 946
debug_entry_t
*debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
L
Linus Torvalds 已提交
947 948 949 950
{
	unsigned long flags;
	debug_entry_t *active;

951
	if (!debug_active || !id->areas)
L
Linus Torvalds 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
		return NULL;
	spin_lock_irqsave(&id->lock, flags);
	active = get_active_entry(id);
	memset(DEBUG_DATA(active), 0, id->buf_size);
	memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
	debug_finish_entry(id, active, level, 1);
	spin_unlock_irqrestore(&id->lock, flags);

	return active;
}

/*
 * counts arguments in format string for sprintf view
 */

967
static inline int
968
debug_count_numargs(char *string)
L
Linus Torvalds 已提交
969 970 971 972 973 974 975 976 977 978 979 980 981 982
{
	int numargs=0;

	while(*string) {
		if(*string++=='%')
			numargs++;
	}
	return(numargs);
}

/*
 * debug_sprintf_event:
 */

983 984
debug_entry_t*
debug_sprintf_event(debug_info_t* id, int level,char *string,...)
L
Linus Torvalds 已提交
985 986 987 988 989 990 991 992 993
{
	va_list   ap;
	int numargs,idx;
	unsigned long flags;
	debug_sprintf_entry_t *curr_event;
	debug_entry_t *active;

	if((!id) || (level > id->level))
		return NULL;
994
	if (!debug_active || !id->areas)
L
Linus Torvalds 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
		return NULL;
	numargs=debug_count_numargs(string);

	spin_lock_irqsave(&id->lock, flags);
	active = get_active_entry(id);
	curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
	va_start(ap,string);
	curr_event->string=string;
	for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
		curr_event->args[idx]=va_arg(ap,long);
	va_end(ap);
	debug_finish_entry(id, active, level, 0);
	spin_unlock_irqrestore(&id->lock, flags);

	return active;
}

/*
 * debug_sprintf_exception:
 */

1016 1017
debug_entry_t*
debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
L
Linus Torvalds 已提交
1018 1019 1020 1021 1022 1023 1024 1025 1026
{
	va_list   ap;
	int numargs,idx;
	unsigned long flags;
	debug_sprintf_entry_t *curr_event;
	debug_entry_t *active;

	if((!id) || (level > id->level))
		return NULL;
1027
	if (!debug_active || !id->areas)
L
Linus Torvalds 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
		return NULL;

	numargs=debug_count_numargs(string);

	spin_lock_irqsave(&id->lock, flags);
	active = get_active_entry(id);
	curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
	va_start(ap,string);
	curr_event->string=string;
	for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
		curr_event->args[idx]=va_arg(ap,long);
	va_end(ap);
	debug_finish_entry(id, active, level, 1);
	spin_unlock_irqrestore(&id->lock, flags);

	return active;
}

/*
 * debug_init:
 * - is called exactly once to initialize the debug feature
 */

1051 1052
static int
__init debug_init(void)
L
Linus Torvalds 已提交
1053 1054 1055
{
	int rc = 0;

1056
	s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
1057
	mutex_lock(&debug_mutex);
1058
	debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
L
Linus Torvalds 已提交
1059 1060
	printk(KERN_INFO "debug: Initialization complete\n");
	initialized = 1;
1061
	mutex_unlock(&debug_mutex);
L
Linus Torvalds 已提交
1062 1063 1064 1065 1066 1067 1068 1069

	return rc;
}

/*
 * debug_register_view:
 */

1070 1071
int
debug_register_view(debug_info_t * id, struct debug_view *view)
L
Linus Torvalds 已提交
1072 1073 1074 1075 1076
{
	int rc = 0;
	int i;
	unsigned long flags;
	mode_t mode = S_IFREG;
1077
	struct dentry *pde;
L
Linus Torvalds 已提交
1078 1079 1080 1081 1082 1083 1084

	if (!id)
		goto out;
	if (view->prolog_proc || view->format_proc || view->header_proc)
		mode |= S_IRUSR;
	if (view->input_proc)
		mode |= S_IWUSR;
1085
	pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
1086
				id , &debug_file_ops);
L
Linus Torvalds 已提交
1087
	if (!pde){
1088 1089
		printk(KERN_WARNING "debug: debugfs_create_file() failed!"\
			" Cannot register view %s/%s\n", id->name,view->name);
L
Linus Torvalds 已提交
1090 1091 1092 1093 1094
		rc = -1;
		goto out;
	}
	spin_lock_irqsave(&id->lock, flags);
	for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1095
		if (!id->views[i])
L
Linus Torvalds 已提交
1096 1097 1098 1099 1100 1101 1102
			break;
	}
	if (i == DEBUG_MAX_VIEWS) {
		printk(KERN_WARNING "debug: cannot register view %s/%s\n",
			id->name,view->name);
		printk(KERN_WARNING 
			"debug: maximum number of views reached (%i)!\n", i);
1103
		debugfs_remove(pde);
L
Linus Torvalds 已提交
1104
		rc = -1;
1105
	} else {
L
Linus Torvalds 已提交
1106
		id->views[i] = view;
1107
		id->debugfs_entries[i] = pde;
L
Linus Torvalds 已提交
1108 1109
	}
	spin_unlock_irqrestore(&id->lock, flags);
1110
out:
L
Linus Torvalds 已提交
1111 1112 1113 1114 1115 1116 1117
	return rc;
}

/*
 * debug_unregister_view:
 */

1118 1119
int
debug_unregister_view(debug_info_t * id, struct debug_view *view)
L
Linus Torvalds 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
{
	int rc = 0;
	int i;
	unsigned long flags;

	if (!id)
		goto out;
	spin_lock_irqsave(&id->lock, flags);
	for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
		if (id->views[i] == view)
			break;
	}
	if (i == DEBUG_MAX_VIEWS)
		rc = -1;
	else {
1135
		debugfs_remove(id->debugfs_entries[i]);
L
Linus Torvalds 已提交
1136 1137 1138 1139
		id->views[i] = NULL;
		rc = 0;
	}
	spin_unlock_irqrestore(&id->lock, flags);
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
out:
	return rc;
}

static inline char *
debug_get_user_string(const char __user *user_buf, size_t user_len)
{
	char* buffer;

	buffer = kmalloc(user_len + 1, GFP_KERNEL);
	if (!buffer)
		return ERR_PTR(-ENOMEM);
	if (copy_from_user(buffer, user_buf, user_len) != 0) {
		kfree(buffer);
		return ERR_PTR(-EFAULT);
	}
	/* got the string, now strip linefeed. */
	if (buffer[user_len - 1] == '\n')
		buffer[user_len - 1] = 0;
	else
		buffer[user_len] = 0;
        return buffer;
}

static inline int
debug_get_uint(char *buf)
{
	int rc;

	for(; isspace(*buf); buf++);
	rc = simple_strtoul(buf, &buf, 10);
	if(*buf){
		printk("debug: no integer specified!\n");
		rc = -EINVAL;
	}
L
Linus Torvalds 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	return rc;
}

/*
 * functions for debug-views
 ***********************************
*/

/*
 * prints out actual debug level
 */

1187 1188
static int
debug_prolog_pages_fn(debug_info_t * id,
L
Linus Torvalds 已提交
1189
				 struct debug_view *view, char *out_buf)
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
{
	return sprintf(out_buf, "%i\n", id->pages_per_area);
}

/*
 * reads new size (number of pages per debug area)
 */

static int
debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
			struct file *file, const char __user *user_buf,
			size_t user_len, loff_t * offset)
{
	char *str;
	int rc,new_pages;

	if (user_len > 0x10000)
                user_len = 0x10000;
	if (*offset != 0){
		rc = -EPIPE;
		goto out;
	}
	str = debug_get_user_string(user_buf,user_len);
	if(IS_ERR(str)){
		rc = PTR_ERR(str);
		goto out;
	}
	new_pages = debug_get_uint(str);
	if(new_pages < 0){
		rc = -EINVAL;
		goto free_str;
	}
	rc = debug_set_size(id,id->nr_areas, new_pages);
	if(rc != 0){
		rc = -EINVAL;
		goto free_str;
	}
	rc = user_len;
free_str:
	kfree(str);
out:
	*offset += user_len;
	return rc;		/* number of input characters */
}

/*
 * prints out actual debug level
 */

static int
debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
L
Linus Torvalds 已提交
1241 1242 1243
{
	int rc = 0;

1244 1245 1246 1247 1248 1249
	if(id->level == DEBUG_OFF_LEVEL) {
		rc = sprintf(out_buf,"-\n");
	}
	else {
		rc = sprintf(out_buf, "%i\n", id->level);
	}
L
Linus Torvalds 已提交
1250 1251 1252 1253 1254 1255 1256
	return rc;
}

/*
 * reads new debug level
 */

1257 1258 1259 1260
static int
debug_input_level_fn(debug_info_t * id, struct debug_view *view,
			struct file *file, const char __user *user_buf,
			size_t user_len, loff_t * offset)
L
Linus Torvalds 已提交
1261
{
1262 1263
	char *str;
	int rc,new_level;
L
Linus Torvalds 已提交
1264

1265 1266 1267 1268
	if (user_len > 0x10000)
                user_len = 0x10000;
	if (*offset != 0){
		rc = -EPIPE;
L
Linus Torvalds 已提交
1269
		goto out;
1270 1271 1272 1273
	}
	str = debug_get_user_string(user_buf,user_len);
	if(IS_ERR(str)){
		rc = PTR_ERR(str);
L
Linus Torvalds 已提交
1274 1275
		goto out;
	}
1276
	if(str[0] == '-'){
L
Linus Torvalds 已提交
1277
		debug_set_level(id, DEBUG_OFF_LEVEL);
1278 1279
		rc = user_len;
		goto free_str;
L
Linus Torvalds 已提交
1280
	} else {
1281
		new_level = debug_get_uint(str);
L
Linus Torvalds 已提交
1282
	}
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
	if(new_level < 0) {
		printk(KERN_INFO "debug: level `%s` is not valid\n", str);
		rc = -EINVAL;
	} else {
		debug_set_level(id, new_level);
		rc = user_len;
	}
free_str:
	kfree(str);
out:
	*offset += user_len;
L
Linus Torvalds 已提交
1294 1295 1296 1297 1298 1299 1300 1301
	return rc;		/* number of input characters */
}


/*
 * flushes debug areas
 */
 
1302
static void debug_flush(debug_info_t* id, int area)
L
Linus Torvalds 已提交
1303 1304
{
        unsigned long flags;
1305
        int i,j;
L
Linus Torvalds 已提交
1306

1307
        if(!id || !id->areas)
L
Linus Torvalds 已提交
1308 1309 1310 1311
                return;
        spin_lock_irqsave(&id->lock,flags);
        if(area == DEBUG_FLUSH_ALL){
                id->active_area = 0;
1312 1313 1314 1315 1316 1317 1318
                memset(id->active_entries, 0, id->nr_areas * sizeof(int));
                for (i = 0; i < id->nr_areas; i++) {
			id->active_pages[i] = 0;
			for(j = 0; j < id->pages_per_area; j++) {
                        	memset(id->areas[i][j], 0, PAGE_SIZE);
			}
		}
L
Linus Torvalds 已提交
1319 1320
                printk(KERN_INFO "debug: %s: all areas flushed\n",id->name);
        } else if(area >= 0 && area < id->nr_areas) {
1321 1322 1323 1324 1325 1326
                id->active_entries[area] = 0;
		id->active_pages[area] = 0;
		for(i = 0; i < id->pages_per_area; i++) {
                	memset(id->areas[area][i],0,PAGE_SIZE);
		}
                printk(KERN_INFO "debug: %s: area %i has been flushed\n",
L
Linus Torvalds 已提交
1327 1328 1329
                        id->name, area);
        } else {
                printk(KERN_INFO
1330
                      "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
L
Linus Torvalds 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339
                        id->name, area, 0, id->nr_areas-1);
        }
        spin_unlock_irqrestore(&id->lock,flags);
}

/*
 * view function: flushes debug areas 
 */

1340 1341 1342 1343
static int
debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
			struct file *file, const char __user *user_buf,
			size_t user_len, loff_t * offset)
L
Linus Torvalds 已提交
1344 1345
{
        char input_buf[1];
1346 1347 1348 1349 1350 1351
        int rc = user_len;

	if (user_len > 0x10000)
                user_len = 0x10000;
        if (*offset != 0){
		rc = -EPIPE;
L
Linus Torvalds 已提交
1352
                goto out;
1353
	}
L
Linus Torvalds 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
        if (copy_from_user(input_buf, user_buf, 1)){
                rc = -EFAULT;
                goto out;
        }
        if(input_buf[0] == '-') { 
                debug_flush(id, DEBUG_FLUSH_ALL);
                goto out;
        }
        if (isdigit(input_buf[0])) {
                int area = ((int) input_buf[0] - (int) '0');
                debug_flush(id, area);
                goto out;
        }

        printk(KERN_INFO "debug: area `%c` is not valid\n", input_buf[0]);

1370 1371
out:
        *offset += user_len;
L
Linus Torvalds 已提交
1372 1373 1374 1375 1376 1377 1378
        return rc;              /* number of input characters */
}

/*
 * prints debug header in raw format
 */

1379 1380 1381
static int
debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
			int area, debug_entry_t * entry, char *out_buf)
L
Linus Torvalds 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
{
        int rc;

	rc = sizeof(debug_entry_t);
	memcpy(out_buf,entry,sizeof(debug_entry_t));
        return rc;
}

/*
 * prints debug data in raw format
 */

1394 1395
static int
debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
L
Linus Torvalds 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
			       char *out_buf, const char *in_buf)
{
	int rc;

	rc = id->buf_size;
	memcpy(out_buf, in_buf, id->buf_size);
	return rc;
}

/*
 * prints debug data in hex/ascii format
 */

1409 1410 1411
static int
debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
	    		  char *out_buf, const char *in_buf)
L
Linus Torvalds 已提交
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
{
	int i, rc = 0;

	for (i = 0; i < id->buf_size; i++) {
                rc += sprintf(out_buf + rc, "%02x ",
                              ((unsigned char *) in_buf)[i]);
        }
	rc += sprintf(out_buf + rc, "| ");
	for (i = 0; i < id->buf_size; i++) {
		unsigned char c = in_buf[i];
		if (!isprint(c))
			rc += sprintf(out_buf + rc, ".");
		else
			rc += sprintf(out_buf + rc, "%c", c);
	}
	rc += sprintf(out_buf + rc, "\n");
	return rc;
}

/*
 * prints header for debug entry
 */

1435 1436
int
debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
L
Linus Torvalds 已提交
1437 1438
			 int area, debug_entry_t * entry, char *out_buf)
{
1439
	struct timespec time_spec;
L
Linus Torvalds 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
	unsigned long long time;
	char *except_str;
	unsigned long caller;
	int rc = 0;
	unsigned int level;

	level = entry->id.fields.level;
	time = entry->id.stck;
	/* adjust todclock to 1970 */
	time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
1450
	tod_to_timeval(time, &time_spec);
L
Linus Torvalds 已提交
1451 1452 1453 1454 1455 1456 1457

	if (entry->id.fields.exception)
		except_str = "*";
	else
		except_str = "-";
	caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
	rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p  ",
1458
		      area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
L
Linus Torvalds 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
		      except_str, entry->id.fields.cpuid, (void *) caller);
	return rc;
}

/*
 * prints debug data sprintf-formated:
 * debug_sprinf_event/exception calls must be used together with this view
 */

#define DEBUG_SPRINTF_MAX_ARGS 10

1470 1471 1472
static int
debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
                        char *out_buf, debug_sprintf_entry_t *curr_event)
L
Linus Torvalds 已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
{
	int num_longs, num_used_args = 0,i, rc = 0;
	int index[DEBUG_SPRINTF_MAX_ARGS];

	/* count of longs fit into one entry */
	num_longs = id->buf_size /  sizeof(long); 

	if(num_longs < 1)
		goto out; /* bufsize of entry too small */
	if(num_longs == 1) {
		/* no args, we use only the string */
		strcpy(out_buf, curr_event->string);
		rc = strlen(curr_event->string);
		goto out;
	}

	/* number of arguments used for sprintf (without the format string) */
	num_used_args   = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));

	memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));

	for(i = 0; i < num_used_args; i++)
		index[i] = i;

	rc =  sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
		curr_event->args[index[1]], curr_event->args[index[2]],
		curr_event->args[index[3]], curr_event->args[index[4]],
		curr_event->args[index[5]], curr_event->args[index[6]],
		curr_event->args[index[7]], curr_event->args[index[8]],
		curr_event->args[index[9]]);

out:

	return rc;
}

/*
 * clean up module
 */
1512
static void __exit debug_exit(void)
L
Linus Torvalds 已提交
1513
{
1514
	debugfs_remove(debug_debugfs_root_entry);
L
Linus Torvalds 已提交
1515 1516 1517 1518 1519 1520 1521
	unregister_sysctl_table(s390dbf_sysctl_header);
	return;
}

/*
 * module definitions
 */
1522
postcore_initcall(debug_init);
L
Linus Torvalds 已提交
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
module_exit(debug_exit);
MODULE_LICENSE("GPL");

EXPORT_SYMBOL(debug_register);
EXPORT_SYMBOL(debug_unregister); 
EXPORT_SYMBOL(debug_set_level);
EXPORT_SYMBOL(debug_stop_all);
EXPORT_SYMBOL(debug_register_view);
EXPORT_SYMBOL(debug_unregister_view);
EXPORT_SYMBOL(debug_event_common);
EXPORT_SYMBOL(debug_exception_common);
EXPORT_SYMBOL(debug_hex_ascii_view);
EXPORT_SYMBOL(debug_raw_view);
EXPORT_SYMBOL(debug_dflt_header_fn);
EXPORT_SYMBOL(debug_sprintf_view);
EXPORT_SYMBOL(debug_sprintf_exception);
EXPORT_SYMBOL(debug_sprintf_event);