blob: 960ba6029c3a00c0f77727519450b3591278a272 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/kernel/debug.c
3 * S/390 debug facility
4 *
5 * Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Michael Holzheu (holzheu@de.ibm.com),
8 * Holger Smolinski (Holger.Smolinski@de.ibm.com)
9 *
10 * Bugreports to: <Linux390@de.ibm.com>
11 */
12
13#include <linux/config.h>
14#include <linux/stddef.h>
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/ctype.h>
19#include <linux/sysctl.h>
20#include <asm/uaccess.h>
21#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/init.h>
Michael Holzheu66a464d2005-06-25 14:55:33 -070024#include <linux/fs.h>
25#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/debug.h>
28
29#define DEBUG_PROLOG_ENTRY -1
30
Michael Holzheu66a464d2005-06-25 14:55:33 -070031#define ALL_AREAS 0 /* copy all debug areas */
32#define NO_AREAS 1 /* copy no debug areas */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* typedefs */
35
36typedef struct file_private_info {
37 loff_t offset; /* offset of last read in file */
38 int act_area; /* number of last formated area */
Michael Holzheu66a464d2005-06-25 14:55:33 -070039 int act_page; /* act page in given area */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 int act_entry; /* last formated entry (offset */
41 /* relative to beginning of last */
Michael Holzheu66a464d2005-06-25 14:55:33 -070042 /* formated page) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 size_t act_entry_offset; /* up to this offset we copied */
44 /* in last read the last formated */
45 /* entry to userland */
46 char temp_buf[2048]; /* buffer for output */
47 debug_info_t *debug_info_org; /* original debug information */
48 debug_info_t *debug_info_snap; /* snapshot of debug information */
49 struct debug_view *view; /* used view of debug info */
50} file_private_info_t;
51
52typedef struct
53{
54 char *string;
55 /*
56 * This assumes that all args are converted into longs
57 * on L/390 this is the case for all types of parameter
58 * except of floats, and long long (32 bit)
Michael Holzheu66a464d2005-06-25 14:55:33 -070059 *
60 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 long args[0];
62} debug_sprintf_entry_t;
63
64
65extern void tod_to_timeval(uint64_t todval, struct timeval *xtime);
66
67/* internal function prototyes */
68
69static int debug_init(void);
70static ssize_t debug_output(struct file *file, char __user *user_buf,
Michael Holzheu66a464d2005-06-25 14:55:33 -070071 size_t user_len, loff_t * offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static ssize_t debug_input(struct file *file, const char __user *user_buf,
Michael Holzheu66a464d2005-06-25 14:55:33 -070073 size_t user_len, loff_t * offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int debug_open(struct inode *inode, struct file *file);
75static int debug_close(struct inode *inode, struct file *file);
Michael Holzheu66a464d2005-06-25 14:55:33 -070076static debug_info_t* debug_info_create(char *name, int pages_per_area,
77 int nr_areas, int buf_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static 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,
Michael Holzheu66a464d2005-06-25 14:55:33 -070081 struct debug_view *view, char *out_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,
Michael Holzheu66a464d2005-06-25 14:55:33 -070083 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
Michael Holzheu66a464d2005-06-25 14:55:33 -070091 struct file *file, const char __user *user_buf,
92 size_t user_buf_size, loff_t * offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
Michael Holzheu66a464d2005-06-25 14:55:33 -070094 char *out_buf, const char *in_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int debug_raw_format_fn(debug_info_t * id,
Michael Holzheu66a464d2005-06-25 14:55:33 -070096 struct debug_view *view, char *out_buf,
97 const char *in_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
Michael Holzheu66a464d2005-06-25 14:55:33 -070099 int area, debug_entry_t * entry, char *out_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101static int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
Michael Holzheu66a464d2005-06-25 14:55:33 -0700102 char *out_buf, debug_sprintf_entry_t *curr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
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
124struct debug_view debug_level_view = {
125 "level",
126 &debug_prolog_level_fn,
127 NULL,
128 NULL,
129 &debug_input_level_fn,
130 NULL
131};
132
Michael Holzheu66a464d2005-06-25 14:55:33 -0700133struct debug_view debug_pages_view = {
134 "pages",
135 &debug_prolog_pages_fn,
136 NULL,
137 NULL,
138 &debug_input_pages_fn,
139 NULL
140};
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142struct debug_view debug_flush_view = {
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
160
161unsigned int debug_feature_version = __DEBUG_FEATURE_VERSION;
162
163/* static globals */
164
165static debug_info_t *debug_area_first = NULL;
166static debug_info_t *debug_area_last = NULL;
167DECLARE_MUTEX(debug_lock);
168
169static int initialized;
170
171static struct file_operations debug_file_ops = {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700172 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .read = debug_output,
Michael Holzheu66a464d2005-06-25 14:55:33 -0700174 .write = debug_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .open = debug_open,
176 .release = debug_close,
177};
178
Michael Holzheu66a464d2005-06-25 14:55:33 -0700179static struct dentry *debug_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/* functions */
182
183/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700184 * 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
195 areas = (debug_entry_t ***) kmalloc(nr_areas *
196 sizeof(debug_entry_t**),
197 GFP_KERNEL);
198 if (!areas)
199 goto fail_malloc_areas;
200 for (i = 0; i < nr_areas; i++) {
201 areas[i] = (debug_entry_t**) kmalloc(pages_per_area *
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++) {
207 areas[i][j] = (debug_entry_t*)kmalloc(PAGE_SIZE,
208 GFP_KERNEL);
209 if(!areas[i][j]) {
210 for(j--; j >=0 ; j--) {
211 kfree(areas[i][j]);
212 }
213 kfree(areas[i]);
214 goto fail_malloc_areas2;
215 } else {
216 memset(areas[i][j],0,PAGE_SIZE);
217 }
218 }
219 }
220 return areas;
221
222fail_malloc_areas2:
223 for(i--; i >= 0; i--){
224 for(j=0; j < pages_per_area;j++){
225 kfree(areas[i][j]);
226 }
227 kfree(areas[i]);
228 }
229 kfree(areas);
230fail_malloc_areas:
231 return NULL;
232
233}
234
235
236/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * debug_info_alloc
238 * - alloc new debug-info
239 */
240
Michael Holzheu66a464d2005-06-25 14:55:33 -0700241static debug_info_t*
242debug_info_alloc(char *name, int pages_per_area, int nr_areas, int buf_size,
243 int level, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 debug_info_t* rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* alloc everything */
248
Michael Holzheu66a464d2005-06-25 14:55:33 -0700249 rc = (debug_info_t*) kmalloc(sizeof(debug_info_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if(!rc)
251 goto fail_malloc_rc;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700252 rc->active_entries = (int*)kmalloc(nr_areas * sizeof(int), GFP_KERNEL);
253 if(!rc->active_entries)
254 goto fail_malloc_active_entries;
255 memset(rc->active_entries, 0, nr_areas * sizeof(int));
256 rc->active_pages = (int*)kmalloc(nr_areas * sizeof(int), GFP_KERNEL);
257 if(!rc->active_pages)
258 goto fail_malloc_active_pages;
259 memset(rc->active_pages, 0, nr_areas * sizeof(int));
260 if((mode == ALL_AREAS) && (pages_per_area != 0)){
261 rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
262 if(!rc->areas)
263 goto fail_malloc_areas;
264 } else {
265 rc->areas = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
268 /* initialize members */
269
270 spin_lock_init(&rc->lock);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700271 rc->pages_per_area = pages_per_area;
272 rc->nr_areas = nr_areas;
273 rc->active_area = 0;
274 rc->level = level;
275 rc->buf_size = buf_size;
276 rc->entry_size = sizeof(debug_entry_t) + buf_size;
277 strlcpy(rc->name, name, sizeof(rc->name)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
Michael Holzheu66a464d2005-06-25 14:55:33 -0700279 memset(rc->debugfs_entries, 0 ,DEBUG_MAX_VIEWS *
280 sizeof(struct dentry*));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 atomic_set(&(rc->ref_count), 0);
282
283 return rc;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285fail_malloc_areas:
Michael Holzheu66a464d2005-06-25 14:55:33 -0700286 kfree(rc->active_pages);
287fail_malloc_active_pages:
288 kfree(rc->active_entries);
289fail_malloc_active_entries:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 kfree(rc);
291fail_malloc_rc:
292 return NULL;
293}
294
295/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700296 * debug_areas_free
297 * - free all debug areas
298 */
299
300static void
301debug_areas_free(debug_info_t* db_info)
302{
303 int i,j;
304
305 if(!db_info->areas)
306 return;
307 for (i = 0; i < db_info->nr_areas; i++) {
308 for(j = 0; j < db_info->pages_per_area; j++) {
309 kfree(db_info->areas[i][j]);
310 }
311 kfree(db_info->areas[i]);
312 }
313 kfree(db_info->areas);
314 db_info->areas = NULL;
315}
316
317/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * debug_info_free
319 * - free memory debug-info
320 */
321
Michael Holzheu66a464d2005-06-25 14:55:33 -0700322static void
323debug_info_free(debug_info_t* db_info){
324 debug_areas_free(db_info);
325 kfree(db_info->active_entries);
326 kfree(db_info->active_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 kfree(db_info);
328}
329
330/*
331 * debug_info_create
332 * - create new debug-info
333 */
334
Michael Holzheu66a464d2005-06-25 14:55:33 -0700335static debug_info_t*
336debug_info_create(char *name, int pages_per_area, int nr_areas, int buf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 debug_info_t* rc;
339
Michael Holzheu66a464d2005-06-25 14:55:33 -0700340 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
341 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if(!rc)
343 goto out;
344
Michael Holzheu66a464d2005-06-25 14:55:33 -0700345 /* create root directory */
346 rc->debugfs_root_entry = debugfs_create_dir(rc->name,
347 debug_debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* append new element to linked list */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700350 if (!debug_area_first) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* first element in list */
352 debug_area_first = rc;
353 rc->prev = NULL;
354 } else {
355 /* append element to end of list */
356 debug_area_last->next = rc;
357 rc->prev = debug_area_last;
358 }
359 debug_area_last = rc;
360 rc->next = NULL;
361
362 debug_info_get(rc);
363out:
364 return rc;
365}
366
367/*
368 * debug_info_copy
369 * - copy debug-info
370 */
371
Michael Holzheu66a464d2005-06-25 14:55:33 -0700372static debug_info_t*
373debug_info_copy(debug_info_t* in, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700375 int i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 debug_info_t* rc;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700377
378 rc = debug_info_alloc(in->name, in->pages_per_area, in->nr_areas,
379 in->buf_size, in->level, mode);
380 if(!rc || (mode == NO_AREAS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto out;
382
383 for(i = 0; i < in->nr_areas; i++){
Michael Holzheu66a464d2005-06-25 14:55:33 -0700384 for(j = 0; j < in->pages_per_area; j++) {
385 memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388out:
389 return rc;
390}
391
392/*
393 * debug_info_get
394 * - increments reference count for debug-info
395 */
396
Michael Holzheu66a464d2005-06-25 14:55:33 -0700397static void
398debug_info_get(debug_info_t * db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 if (db_info)
401 atomic_inc(&db_info->ref_count);
402}
403
404/*
405 * debug_info_put:
406 * - decreases reference count for debug-info and frees it if necessary
407 */
408
Michael Holzheu66a464d2005-06-25 14:55:33 -0700409static void
410debug_info_put(debug_info_t *db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 int i;
413
414 if (!db_info)
415 return;
416 if (atomic_dec_and_test(&db_info->ref_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700418 if (!db_info->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 continue;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700420 debugfs_remove(db_info->debugfs_entries[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700422 debugfs_remove(db_info->debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if(db_info == debug_area_first)
424 debug_area_first = db_info->next;
425 if(db_info == debug_area_last)
426 debug_area_last = db_info->prev;
427 if(db_info->prev) db_info->prev->next = db_info->next;
428 if(db_info->next) db_info->next->prev = db_info->prev;
429 debug_info_free(db_info);
430 }
431}
432
433/*
434 * debug_format_entry:
435 * - format one debug entry and return size of formated data
436 */
437
Michael Holzheu66a464d2005-06-25 14:55:33 -0700438static int
439debug_format_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 debug_info_t *id_snap = p_info->debug_info_snap;
442 struct debug_view *view = p_info->view;
443 debug_entry_t *act_entry;
444 size_t len = 0;
445 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
446 /* print prolog */
447 if (view->prolog_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700448 len += view->prolog_proc(id_snap,view,p_info->temp_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto out;
450 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700451 if (!id_snap->areas) /* this is true, if we have a prolog only view */
452 goto out; /* or if 'pages_per_area' is 0 */
453 act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area]
454 [p_info->act_page] + p_info->act_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (act_entry->id.stck == 0LL)
457 goto out; /* empty entry */
458 if (view->header_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700459 len += view->header_proc(id_snap, view, p_info->act_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 act_entry, p_info->temp_buf + len);
461 if (view->format_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700462 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 DEBUG_DATA(act_entry));
Michael Holzheu66a464d2005-06-25 14:55:33 -0700464out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return len;
466}
467
468/*
469 * debug_next_entry:
470 * - goto next entry in p_info
471 */
472
Michael Holzheu66a464d2005-06-25 14:55:33 -0700473extern inline int
474debug_next_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700476 debug_info_t *id;
477
478 id = p_info->debug_info_snap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
480 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700481 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto out;
483 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700484 if(!id->areas)
485 return 1;
486 p_info->act_entry += id->entry_size;
487 /* switch to next page, if we reached the end of the page */
488 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)){
489 /* next page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700491 p_info->act_page += 1;
492 if((p_info->act_page % id->pages_per_area) == 0) {
493 /* next area */
494 p_info->act_area++;
495 p_info->act_page=0;
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if(p_info->act_area >= id->nr_areas)
498 return 1;
499 }
500out:
501 return 0;
502}
503
504/*
505 * debug_output:
506 * - called for user read()
507 * - copies formated debug entries to the user buffer
508 */
509
Michael Holzheu66a464d2005-06-25 14:55:33 -0700510static ssize_t
511debug_output(struct file *file, /* file descriptor */
512 char __user *user_buf, /* user buffer */
513 size_t len, /* length of buffer */
514 loff_t *offset) /* offset in the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 size_t count = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700517 size_t entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 file_private_info_t *p_info;
519
520 p_info = ((file_private_info_t *) file->private_data);
521 if (*offset != p_info->offset)
522 return -EPIPE;
523 if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 entry_offset = p_info->act_entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 while(count < len){
Michael Holzheu66a464d2005-06-25 14:55:33 -0700527 int formatted_line_size;
528 int formatted_line_residue;
529 int user_buf_residue;
530 size_t copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Michael Holzheu66a464d2005-06-25 14:55:33 -0700532 formatted_line_size = debug_format_entry(p_info);
533 formatted_line_residue = formatted_line_size - entry_offset;
534 user_buf_residue = len-count;
535 copy_size = min(user_buf_residue, formatted_line_residue);
536 if(copy_size){
537 if (copy_to_user(user_buf + count, p_info->temp_buf
538 + entry_offset, copy_size))
539 return -EFAULT;
540 count += copy_size;
541 entry_offset += copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700543 if(copy_size == formatted_line_residue){
544 entry_offset = 0;
545 if(debug_next_entry(p_info))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549out:
550 p_info->offset = *offset + count;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700551 p_info->act_entry_offset = entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *offset = p_info->offset;
553 return count;
554}
555
556/*
557 * debug_input:
558 * - called for user write()
559 * - calls input function of view
560 */
561
Michael Holzheu66a464d2005-06-25 14:55:33 -0700562static ssize_t
563debug_input(struct file *file, const char __user *user_buf, size_t length,
564 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 int rc = 0;
567 file_private_info_t *p_info;
568
569 down(&debug_lock);
570 p_info = ((file_private_info_t *) file->private_data);
571 if (p_info->view->input_proc)
572 rc = p_info->view->input_proc(p_info->debug_info_org,
573 p_info->view, file, user_buf,
574 length, offset);
575 else
576 rc = -EPERM;
577 up(&debug_lock);
578 return rc; /* number of input characters */
579}
580
581/*
582 * debug_open:
583 * - called for user open()
584 * - copies formated output to private_data area of the file
585 * handle
586 */
587
Michael Holzheu66a464d2005-06-25 14:55:33 -0700588static int
589debug_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 int i = 0, rc = 0;
592 file_private_info_t *p_info;
593 debug_info_t *debug_info, *debug_info_snapshot;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 down(&debug_lock);
596
597 /* find debug log and view */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 debug_info = debug_area_first;
599 while(debug_info != NULL){
600 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700601 if (!debug_info->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 continue;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700603 else if (debug_info->debugfs_entries[i] ==
604 file->f_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto found; /* found view ! */
606 }
607 }
608 debug_info = debug_info->next;
609 }
610 /* no entry found */
611 rc = -EINVAL;
612 goto out;
613
Michael Holzheu66a464d2005-06-25 14:55:33 -0700614found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Michael Holzheu66a464d2005-06-25 14:55:33 -0700616 /* Make snapshot of current debug areas to get it consistent. */
617 /* To copy all the areas is only needed, if we have a view which */
618 /* formats the debug areas. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Michael Holzheu66a464d2005-06-25 14:55:33 -0700620 if(!debug_info->views[i]->format_proc &&
621 !debug_info->views[i]->header_proc){
622 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
623 } else {
624 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if(!debug_info_snapshot){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 rc = -ENOMEM;
629 goto out;
630 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700631 p_info = (file_private_info_t *) kmalloc(sizeof(file_private_info_t),
632 GFP_KERNEL);
633 if(!p_info){
634 if(debug_info_snapshot)
635 debug_info_free(debug_info_snapshot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 rc = -ENOMEM;
637 goto out;
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 p_info->offset = 0;
640 p_info->debug_info_snap = debug_info_snapshot;
641 p_info->debug_info_org = debug_info;
642 p_info->view = debug_info->views[i];
643 p_info->act_area = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700644 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 p_info->act_entry = DEBUG_PROLOG_ENTRY;
646 p_info->act_entry_offset = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700647 file->private_data = p_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 debug_info_get(debug_info);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700649out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 up(&debug_lock);
651 return rc;
652}
653
654/*
655 * debug_close:
656 * - called for user close()
657 * - deletes private_data area of the file handle
658 */
659
Michael Holzheu66a464d2005-06-25 14:55:33 -0700660static int
661debug_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 file_private_info_t *p_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 p_info = (file_private_info_t *) file->private_data;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700665 if(p_info->debug_info_snap)
666 debug_info_free(p_info->debug_info_snap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 debug_info_put(p_info->debug_info_org);
668 kfree(file->private_data);
669 return 0; /* success */
670}
671
672/*
673 * debug_register:
674 * - creates and initializes debug area for the caller
675 * - returns handle for debug area
676 */
677
Michael Holzheu66a464d2005-06-25 14:55:33 -0700678debug_info_t*
679debug_register (char *name, int pages_per_area, int nr_areas, int buf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 debug_info_t *rc = NULL;
682
683 if (!initialized)
684 BUG();
685 down(&debug_lock);
686
687 /* create new debug_info */
688
Michael Holzheu66a464d2005-06-25 14:55:33 -0700689 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if(!rc)
691 goto out;
692 debug_register_view(rc, &debug_level_view);
693 debug_register_view(rc, &debug_flush_view);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700694 debug_register_view(rc, &debug_pages_view);
695out:
696 if (!rc){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 printk(KERN_ERR "debug: debug_register failed for %s\n",name);
698 }
699 up(&debug_lock);
700 return rc;
701}
702
703/*
704 * debug_unregister:
705 * - give back debug area
706 */
707
Michael Holzheu66a464d2005-06-25 14:55:33 -0700708void
709debug_unregister(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 if (!id)
712 goto out;
713 down(&debug_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 debug_info_put(id);
715 up(&debug_lock);
716
Michael Holzheu66a464d2005-06-25 14:55:33 -0700717out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return;
719}
720
721/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700722 * debug_set_size:
723 * - set area size (number of pages) and number of areas
724 */
725static int
726debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
727{
728 unsigned long flags;
729 debug_entry_t *** new_areas;
730 int rc=0;
731
732 if(!id || (nr_areas <= 0) || (pages_per_area < 0))
733 return -EINVAL;
734 if(pages_per_area > 0){
735 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
736 if(!new_areas) {
737 printk(KERN_WARNING "debug: could not allocate memory "\
738 "for pagenumber: %i\n",pages_per_area);
739 rc = -ENOMEM;
740 goto out;
741 }
742 } else {
743 new_areas = NULL;
744 }
745 spin_lock_irqsave(&id->lock,flags);
746 debug_areas_free(id);
747 id->areas = new_areas;
748 id->nr_areas = nr_areas;
749 id->pages_per_area = pages_per_area;
750 id->active_area = 0;
751 memset(id->active_entries,0,sizeof(int)*id->nr_areas);
752 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
753 spin_unlock_irqrestore(&id->lock,flags);
754 printk(KERN_INFO "debug: %s: set new size (%i pages)\n"\
755 ,id->name, pages_per_area);
756out:
757 return rc;
758}
759
760/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * debug_set_level:
762 * - set actual debug level
763 */
764
Michael Holzheu66a464d2005-06-25 14:55:33 -0700765void
766debug_set_level(debug_info_t* id, int new_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 unsigned long flags;
769 if(!id)
770 return;
771 spin_lock_irqsave(&id->lock,flags);
772 if(new_level == DEBUG_OFF_LEVEL){
773 id->level = DEBUG_OFF_LEVEL;
774 printk(KERN_INFO "debug: %s: switched off\n",id->name);
775 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
776 printk(KERN_INFO
777 "debug: %s: level %i is out of range (%i - %i)\n",
778 id->name, new_level, 0, DEBUG_MAX_LEVEL);
779 } else {
780 id->level = new_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782 spin_unlock_irqrestore(&id->lock,flags);
783}
784
785
786/*
787 * proceed_active_entry:
788 * - set active entry to next in the ring buffer
789 */
790
Michael Holzheu66a464d2005-06-25 14:55:33 -0700791extern inline void
792proceed_active_entry(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700794 if ((id->active_entries[id->active_area] += id->entry_size)
795 > (PAGE_SIZE - id->entry_size)){
796 id->active_entries[id->active_area] = 0;
797 id->active_pages[id->active_area] =
798 (id->active_pages[id->active_area] + 1) %
799 id->pages_per_area;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803/*
804 * proceed_active_area:
805 * - set active area to next in the ring buffer
806 */
807
Michael Holzheu66a464d2005-06-25 14:55:33 -0700808extern inline void
809proceed_active_area(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 id->active_area++;
812 id->active_area = id->active_area % id->nr_areas;
813}
814
815/*
816 * get_active_entry:
817 */
818
Michael Holzheu66a464d2005-06-25 14:55:33 -0700819extern inline debug_entry_t*
820get_active_entry(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700822 return (debug_entry_t *) (((char *) id->areas[id->active_area]
823 [id->active_pages[id->active_area]]) +
824 id->active_entries[id->active_area]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827/*
828 * debug_finish_entry:
829 * - set timestamp, caller address, cpu number etc.
830 */
831
Michael Holzheu66a464d2005-06-25 14:55:33 -0700832extern inline void
833debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
834 int exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 STCK(active->id.stck);
837 active->id.fields.cpuid = smp_processor_id();
838 active->caller = __builtin_return_address(0);
839 active->id.fields.exception = exception;
840 active->id.fields.level = level;
841 proceed_active_entry(id);
842 if(exception)
843 proceed_active_area(id);
844}
845
846static int debug_stoppable=1;
847static int debug_active=1;
848
849#define CTL_S390DBF 5677
850#define CTL_S390DBF_STOPPABLE 5678
851#define CTL_S390DBF_ACTIVE 5679
852
853/*
854 * proc handler for the running debug_active sysctl
855 * always allow read, allow write only if debug_stoppable is set or
856 * if debug_active is already off
857 */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700858static int
859s390dbf_procactive(ctl_table *table, int write, struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 void __user *buffer, size_t *lenp, loff_t *ppos)
861{
862 if (!write || debug_stoppable || !debug_active)
863 return proc_dointvec(table, write, filp, buffer, lenp, ppos);
864 else
865 return 0;
866}
867
868
869static struct ctl_table s390dbf_table[] = {
870 {
871 .ctl_name = CTL_S390DBF_STOPPABLE,
872 .procname = "debug_stoppable",
873 .data = &debug_stoppable,
874 .maxlen = sizeof(int),
875 .mode = S_IRUGO | S_IWUSR,
876 .proc_handler = &proc_dointvec,
877 .strategy = &sysctl_intvec,
878 },
879 {
880 .ctl_name = CTL_S390DBF_ACTIVE,
881 .procname = "debug_active",
882 .data = &debug_active,
883 .maxlen = sizeof(int),
884 .mode = S_IRUGO | S_IWUSR,
885 .proc_handler = &s390dbf_procactive,
886 .strategy = &sysctl_intvec,
887 },
888 { .ctl_name = 0 }
889};
890
891static struct ctl_table s390dbf_dir_table[] = {
892 {
893 .ctl_name = CTL_S390DBF,
894 .procname = "s390dbf",
895 .maxlen = 0,
896 .mode = S_IRUGO | S_IXUGO,
897 .child = s390dbf_table,
898 },
899 { .ctl_name = 0 }
900};
901
902struct ctl_table_header *s390dbf_sysctl_header;
903
Michael Holzheu66a464d2005-06-25 14:55:33 -0700904void
905debug_stop_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 if (debug_stoppable)
908 debug_active = 0;
909}
910
911
912/*
913 * debug_event_common:
914 * - write debug entry with given size
915 */
916
Michael Holzheu66a464d2005-06-25 14:55:33 -0700917debug_entry_t*
918debug_event_common(debug_info_t * id, int level, const void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 unsigned long flags;
921 debug_entry_t *active;
922
Michael Holzheu66a464d2005-06-25 14:55:33 -0700923 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return NULL;
925 spin_lock_irqsave(&id->lock, flags);
926 active = get_active_entry(id);
927 memset(DEBUG_DATA(active), 0, id->buf_size);
928 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
929 debug_finish_entry(id, active, level, 0);
930 spin_unlock_irqrestore(&id->lock, flags);
931
932 return active;
933}
934
935/*
936 * debug_exception_common:
937 * - write debug entry with given size and switch to next debug area
938 */
939
Michael Holzheu66a464d2005-06-25 14:55:33 -0700940debug_entry_t
941*debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 unsigned long flags;
944 debug_entry_t *active;
945
Michael Holzheu66a464d2005-06-25 14:55:33 -0700946 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return NULL;
948 spin_lock_irqsave(&id->lock, flags);
949 active = get_active_entry(id);
950 memset(DEBUG_DATA(active), 0, id->buf_size);
951 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
952 debug_finish_entry(id, active, level, 1);
953 spin_unlock_irqrestore(&id->lock, flags);
954
955 return active;
956}
957
958/*
959 * counts arguments in format string for sprintf view
960 */
961
Michael Holzheu66a464d2005-06-25 14:55:33 -0700962extern inline int
963debug_count_numargs(char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 int numargs=0;
966
967 while(*string) {
968 if(*string++=='%')
969 numargs++;
970 }
971 return(numargs);
972}
973
974/*
975 * debug_sprintf_event:
976 */
977
Michael Holzheu66a464d2005-06-25 14:55:33 -0700978debug_entry_t*
979debug_sprintf_event(debug_info_t* id, int level,char *string,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 va_list ap;
982 int numargs,idx;
983 unsigned long flags;
984 debug_sprintf_entry_t *curr_event;
985 debug_entry_t *active;
986
987 if((!id) || (level > id->level))
988 return NULL;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700989 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return NULL;
991 numargs=debug_count_numargs(string);
992
993 spin_lock_irqsave(&id->lock, flags);
994 active = get_active_entry(id);
995 curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
996 va_start(ap,string);
997 curr_event->string=string;
998 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
999 curr_event->args[idx]=va_arg(ap,long);
1000 va_end(ap);
1001 debug_finish_entry(id, active, level, 0);
1002 spin_unlock_irqrestore(&id->lock, flags);
1003
1004 return active;
1005}
1006
1007/*
1008 * debug_sprintf_exception:
1009 */
1010
Michael Holzheu66a464d2005-06-25 14:55:33 -07001011debug_entry_t*
1012debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 va_list ap;
1015 int numargs,idx;
1016 unsigned long flags;
1017 debug_sprintf_entry_t *curr_event;
1018 debug_entry_t *active;
1019
1020 if((!id) || (level > id->level))
1021 return NULL;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001022 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return NULL;
1024
1025 numargs=debug_count_numargs(string);
1026
1027 spin_lock_irqsave(&id->lock, flags);
1028 active = get_active_entry(id);
1029 curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
1030 va_start(ap,string);
1031 curr_event->string=string;
1032 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1033 curr_event->args[idx]=va_arg(ap,long);
1034 va_end(ap);
1035 debug_finish_entry(id, active, level, 1);
1036 spin_unlock_irqrestore(&id->lock, flags);
1037
1038 return active;
1039}
1040
1041/*
1042 * debug_init:
1043 * - is called exactly once to initialize the debug feature
1044 */
1045
Michael Holzheu66a464d2005-06-25 14:55:33 -07001046static int
1047__init debug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 int rc = 0;
1050
1051 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table, 1);
1052 down(&debug_lock);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001053 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 printk(KERN_INFO "debug: Initialization complete\n");
1055 initialized = 1;
1056 up(&debug_lock);
1057
1058 return rc;
1059}
1060
1061/*
1062 * debug_register_view:
1063 */
1064
Michael Holzheu66a464d2005-06-25 14:55:33 -07001065int
1066debug_register_view(debug_info_t * id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 int rc = 0;
1069 int i;
1070 unsigned long flags;
1071 mode_t mode = S_IFREG;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001072 struct dentry *pde;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (!id)
1075 goto out;
1076 if (view->prolog_proc || view->format_proc || view->header_proc)
1077 mode |= S_IRUSR;
1078 if (view->input_proc)
1079 mode |= S_IWUSR;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001080 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
1081 NULL, &debug_file_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!pde){
Michael Holzheu66a464d2005-06-25 14:55:33 -07001083 printk(KERN_WARNING "debug: debugfs_create_file() failed!"\
1084 " Cannot register view %s/%s\n", id->name,view->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 rc = -1;
1086 goto out;
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 spin_lock_irqsave(&id->lock, flags);
1089 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001090 if (!id->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 break;
1092 }
1093 if (i == DEBUG_MAX_VIEWS) {
1094 printk(KERN_WARNING "debug: cannot register view %s/%s\n",
1095 id->name,view->name);
1096 printk(KERN_WARNING
1097 "debug: maximum number of views reached (%i)!\n", i);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001098 debugfs_remove(pde);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 rc = -1;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001100 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 id->views[i] = view;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001102 id->debugfs_entries[i] = pde;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001105out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return rc;
1107}
1108
1109/*
1110 * debug_unregister_view:
1111 */
1112
Michael Holzheu66a464d2005-06-25 14:55:33 -07001113int
1114debug_unregister_view(debug_info_t * id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 int rc = 0;
1117 int i;
1118 unsigned long flags;
1119
1120 if (!id)
1121 goto out;
1122 spin_lock_irqsave(&id->lock, flags);
1123 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1124 if (id->views[i] == view)
1125 break;
1126 }
1127 if (i == DEBUG_MAX_VIEWS)
1128 rc = -1;
1129 else {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001130 debugfs_remove(id->debugfs_entries[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 id->views[i] = NULL;
1132 rc = 0;
1133 }
1134 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001135out:
1136 return rc;
1137}
1138
1139static inline char *
1140debug_get_user_string(const char __user *user_buf, size_t user_len)
1141{
1142 char* buffer;
1143
1144 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1145 if (!buffer)
1146 return ERR_PTR(-ENOMEM);
1147 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1148 kfree(buffer);
1149 return ERR_PTR(-EFAULT);
1150 }
1151 /* got the string, now strip linefeed. */
1152 if (buffer[user_len - 1] == '\n')
1153 buffer[user_len - 1] = 0;
1154 else
1155 buffer[user_len] = 0;
1156 return buffer;
1157}
1158
1159static inline int
1160debug_get_uint(char *buf)
1161{
1162 int rc;
1163
1164 for(; isspace(*buf); buf++);
1165 rc = simple_strtoul(buf, &buf, 10);
1166 if(*buf){
1167 printk("debug: no integer specified!\n");
1168 rc = -EINVAL;
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return rc;
1171}
1172
1173/*
1174 * functions for debug-views
1175 ***********************************
1176*/
1177
1178/*
1179 * prints out actual debug level
1180 */
1181
Michael Holzheu66a464d2005-06-25 14:55:33 -07001182static int
1183debug_prolog_pages_fn(debug_info_t * id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct debug_view *view, char *out_buf)
1185{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001186 return sprintf(out_buf, "%i\n", id->pages_per_area);
1187}
1188
1189/*
1190 * reads new size (number of pages per debug area)
1191 */
1192
1193static int
1194debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
1195 struct file *file, const char __user *user_buf,
1196 size_t user_len, loff_t * offset)
1197{
1198 char *str;
1199 int rc,new_pages;
1200
1201 if (user_len > 0x10000)
1202 user_len = 0x10000;
1203 if (*offset != 0){
1204 rc = -EPIPE;
1205 goto out;
1206 }
1207 str = debug_get_user_string(user_buf,user_len);
1208 if(IS_ERR(str)){
1209 rc = PTR_ERR(str);
1210 goto out;
1211 }
1212 new_pages = debug_get_uint(str);
1213 if(new_pages < 0){
1214 rc = -EINVAL;
1215 goto free_str;
1216 }
1217 rc = debug_set_size(id,id->nr_areas, new_pages);
1218 if(rc != 0){
1219 rc = -EINVAL;
1220 goto free_str;
1221 }
1222 rc = user_len;
1223free_str:
1224 kfree(str);
1225out:
1226 *offset += user_len;
1227 return rc; /* number of input characters */
1228}
1229
1230/*
1231 * prints out actual debug level
1232 */
1233
1234static int
1235debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
1236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int rc = 0;
1238
Michael Holzheu66a464d2005-06-25 14:55:33 -07001239 if(id->level == DEBUG_OFF_LEVEL) {
1240 rc = sprintf(out_buf,"-\n");
1241 }
1242 else {
1243 rc = sprintf(out_buf, "%i\n", id->level);
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return rc;
1246}
1247
1248/*
1249 * reads new debug level
1250 */
1251
Michael Holzheu66a464d2005-06-25 14:55:33 -07001252static int
1253debug_input_level_fn(debug_info_t * id, struct debug_view *view,
1254 struct file *file, const char __user *user_buf,
1255 size_t user_len, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001257 char *str;
1258 int rc,new_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Michael Holzheu66a464d2005-06-25 14:55:33 -07001260 if (user_len > 0x10000)
1261 user_len = 0x10000;
1262 if (*offset != 0){
1263 rc = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto out;
1265 }
Michael Holzheu66a464d2005-06-25 14:55:33 -07001266 str = debug_get_user_string(user_buf,user_len);
1267 if(IS_ERR(str)){
1268 rc = PTR_ERR(str);
1269 goto out;
1270 }
1271 if(str[0] == '-'){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 debug_set_level(id, DEBUG_OFF_LEVEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001273 rc = user_len;
1274 goto free_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 } else {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001276 new_level = debug_get_uint(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
Michael Holzheu66a464d2005-06-25 14:55:33 -07001278 if(new_level < 0) {
1279 printk(KERN_INFO "debug: level `%s` is not valid\n", str);
1280 rc = -EINVAL;
1281 } else {
1282 debug_set_level(id, new_level);
1283 rc = user_len;
1284 }
1285free_str:
1286 kfree(str);
1287out:
1288 *offset += user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return rc; /* number of input characters */
1290}
1291
1292
1293/*
1294 * flushes debug areas
1295 */
1296
Michael Holzheu66a464d2005-06-25 14:55:33 -07001297void
1298debug_flush(debug_info_t* id, int area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 unsigned long flags;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001301 int i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Michael Holzheu66a464d2005-06-25 14:55:33 -07001303 if(!id || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return;
1305 spin_lock_irqsave(&id->lock,flags);
1306 if(area == DEBUG_FLUSH_ALL){
1307 id->active_area = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001308 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1309 for (i = 0; i < id->nr_areas; i++) {
1310 id->active_pages[i] = 0;
1311 for(j = 0; j < id->pages_per_area; j++) {
1312 memset(id->areas[i][j], 0, PAGE_SIZE);
1313 }
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 printk(KERN_INFO "debug: %s: all areas flushed\n",id->name);
1316 } else if(area >= 0 && area < id->nr_areas) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001317 id->active_entries[area] = 0;
1318 id->active_pages[area] = 0;
1319 for(i = 0; i < id->pages_per_area; i++) {
1320 memset(id->areas[area][i],0,PAGE_SIZE);
1321 }
1322 printk(KERN_INFO "debug: %s: area %i has been flushed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 id->name, area);
1324 } else {
1325 printk(KERN_INFO
Michael Holzheu66a464d2005-06-25 14:55:33 -07001326 "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 id->name, area, 0, id->nr_areas-1);
1328 }
1329 spin_unlock_irqrestore(&id->lock,flags);
1330}
1331
1332/*
1333 * view function: flushes debug areas
1334 */
1335
Michael Holzheu66a464d2005-06-25 14:55:33 -07001336static int
1337debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
1338 struct file *file, const char __user *user_buf,
1339 size_t user_len, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 char input_buf[1];
Michael Holzheu66a464d2005-06-25 14:55:33 -07001342 int rc = user_len;
1343
1344 if (user_len > 0x10000)
1345 user_len = 0x10000;
1346 if (*offset != 0){
1347 rc = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (copy_from_user(input_buf, user_buf, 1)){
1351 rc = -EFAULT;
1352 goto out;
1353 }
1354 if(input_buf[0] == '-') {
1355 debug_flush(id, DEBUG_FLUSH_ALL);
1356 goto out;
1357 }
1358 if (isdigit(input_buf[0])) {
1359 int area = ((int) input_buf[0] - (int) '0');
1360 debug_flush(id, area);
1361 goto out;
1362 }
1363
1364 printk(KERN_INFO "debug: area `%c` is not valid\n", input_buf[0]);
1365
Michael Holzheu66a464d2005-06-25 14:55:33 -07001366out:
1367 *offset += user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return rc; /* number of input characters */
1369}
1370
1371/*
1372 * prints debug header in raw format
1373 */
1374
Michael Holzheu66a464d2005-06-25 14:55:33 -07001375static int
1376debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
1377 int area, debug_entry_t * entry, char *out_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 int rc;
1380
1381 rc = sizeof(debug_entry_t);
1382 memcpy(out_buf,entry,sizeof(debug_entry_t));
1383 return rc;
1384}
1385
1386/*
1387 * prints debug data in raw format
1388 */
1389
Michael Holzheu66a464d2005-06-25 14:55:33 -07001390static int
1391debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 char *out_buf, const char *in_buf)
1393{
1394 int rc;
1395
1396 rc = id->buf_size;
1397 memcpy(out_buf, in_buf, id->buf_size);
1398 return rc;
1399}
1400
1401/*
1402 * prints debug data in hex/ascii format
1403 */
1404
Michael Holzheu66a464d2005-06-25 14:55:33 -07001405static int
1406debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
1407 char *out_buf, const char *in_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 int i, rc = 0;
1410
1411 for (i = 0; i < id->buf_size; i++) {
1412 rc += sprintf(out_buf + rc, "%02x ",
1413 ((unsigned char *) in_buf)[i]);
1414 }
1415 rc += sprintf(out_buf + rc, "| ");
1416 for (i = 0; i < id->buf_size; i++) {
1417 unsigned char c = in_buf[i];
1418 if (!isprint(c))
1419 rc += sprintf(out_buf + rc, ".");
1420 else
1421 rc += sprintf(out_buf + rc, "%c", c);
1422 }
1423 rc += sprintf(out_buf + rc, "\n");
1424 return rc;
1425}
1426
1427/*
1428 * prints header for debug entry
1429 */
1430
Michael Holzheu66a464d2005-06-25 14:55:33 -07001431int
1432debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int area, debug_entry_t * entry, char *out_buf)
1434{
1435 struct timeval time_val;
1436 unsigned long long time;
1437 char *except_str;
1438 unsigned long caller;
1439 int rc = 0;
1440 unsigned int level;
1441
1442 level = entry->id.fields.level;
1443 time = entry->id.stck;
1444 /* adjust todclock to 1970 */
1445 time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
1446 tod_to_timeval(time, &time_val);
1447
1448 if (entry->id.fields.exception)
1449 except_str = "*";
1450 else
1451 except_str = "-";
1452 caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
1453 rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
1454 area, time_val.tv_sec, time_val.tv_usec, level,
1455 except_str, entry->id.fields.cpuid, (void *) caller);
1456 return rc;
1457}
1458
1459/*
1460 * prints debug data sprintf-formated:
1461 * debug_sprinf_event/exception calls must be used together with this view
1462 */
1463
1464#define DEBUG_SPRINTF_MAX_ARGS 10
1465
Michael Holzheu66a464d2005-06-25 14:55:33 -07001466static int
1467debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
1468 char *out_buf, debug_sprintf_entry_t *curr_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 int num_longs, num_used_args = 0,i, rc = 0;
1471 int index[DEBUG_SPRINTF_MAX_ARGS];
1472
1473 /* count of longs fit into one entry */
1474 num_longs = id->buf_size / sizeof(long);
1475
1476 if(num_longs < 1)
1477 goto out; /* bufsize of entry too small */
1478 if(num_longs == 1) {
1479 /* no args, we use only the string */
1480 strcpy(out_buf, curr_event->string);
1481 rc = strlen(curr_event->string);
1482 goto out;
1483 }
1484
1485 /* number of arguments used for sprintf (without the format string) */
1486 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1487
1488 memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1489
1490 for(i = 0; i < num_used_args; i++)
1491 index[i] = i;
1492
1493 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1494 curr_event->args[index[1]], curr_event->args[index[2]],
1495 curr_event->args[index[3]], curr_event->args[index[4]],
1496 curr_event->args[index[5]], curr_event->args[index[6]],
1497 curr_event->args[index[7]], curr_event->args[index[8]],
1498 curr_event->args[index[9]]);
1499
1500out:
1501
1502 return rc;
1503}
1504
1505/*
1506 * clean up module
1507 */
Michael Holzheu66a464d2005-06-25 14:55:33 -07001508void
1509__exit debug_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001511 debugfs_remove(debug_debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 unregister_sysctl_table(s390dbf_sysctl_header);
1513 return;
1514}
1515
1516/*
1517 * module definitions
1518 */
Michael Holzheu66a464d2005-06-25 14:55:33 -07001519postcore_initcall(debug_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520module_exit(debug_exit);
1521MODULE_LICENSE("GPL");
1522
1523EXPORT_SYMBOL(debug_register);
1524EXPORT_SYMBOL(debug_unregister);
1525EXPORT_SYMBOL(debug_set_level);
1526EXPORT_SYMBOL(debug_stop_all);
1527EXPORT_SYMBOL(debug_register_view);
1528EXPORT_SYMBOL(debug_unregister_view);
1529EXPORT_SYMBOL(debug_event_common);
1530EXPORT_SYMBOL(debug_exception_common);
1531EXPORT_SYMBOL(debug_hex_ascii_view);
1532EXPORT_SYMBOL(debug_raw_view);
1533EXPORT_SYMBOL(debug_dflt_header_fn);
1534EXPORT_SYMBOL(debug_sprintf_view);
1535EXPORT_SYMBOL(debug_sprintf_exception);
1536EXPORT_SYMBOL(debug_sprintf_event);