blob: bc59282da7620408fd98aec3b8e3b643759e47e0 [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
Michael Holzheu942eaab2005-09-03 15:57:58 -070065extern void tod_to_timeval(uint64_t todval, struct timespec *xtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
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 Holzheu942eaab2005-09-03 15:57:58 -0700377 unsigned long flags;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700378
Michael Holzheu942eaab2005-09-03 15:57:58 -0700379 /* get a consistent copy of the debug areas */
380 do {
381 rc = debug_info_alloc(in->name, in->pages_per_area,
382 in->nr_areas, in->buf_size, in->level, mode);
383 spin_lock_irqsave(&in->lock, flags);
384 if(!rc)
385 goto out;
386 /* has something changed in the meantime ? */
387 if((rc->pages_per_area == in->pages_per_area) &&
388 (rc->nr_areas == in->nr_areas)) {
389 break;
390 }
391 spin_unlock_irqrestore(&in->lock, flags);
392 debug_info_free(rc);
393 } while (1);
394
Michael Holzheu66a464d2005-06-25 14:55:33 -0700395 if(!rc || (mode == NO_AREAS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 goto out;
397
398 for(i = 0; i < in->nr_areas; i++){
Michael Holzheu66a464d2005-06-25 14:55:33 -0700399 for(j = 0; j < in->pages_per_area; j++) {
400 memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403out:
Michael Holzheu942eaab2005-09-03 15:57:58 -0700404 spin_unlock_irqrestore(&in->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return rc;
406}
407
408/*
409 * debug_info_get
410 * - increments reference count for debug-info
411 */
412
Michael Holzheu66a464d2005-06-25 14:55:33 -0700413static void
414debug_info_get(debug_info_t * db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 if (db_info)
417 atomic_inc(&db_info->ref_count);
418}
419
420/*
421 * debug_info_put:
422 * - decreases reference count for debug-info and frees it if necessary
423 */
424
Michael Holzheu66a464d2005-06-25 14:55:33 -0700425static void
426debug_info_put(debug_info_t *db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 int i;
429
430 if (!db_info)
431 return;
432 if (atomic_dec_and_test(&db_info->ref_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700434 if (!db_info->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 continue;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700436 debugfs_remove(db_info->debugfs_entries[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700438 debugfs_remove(db_info->debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if(db_info == debug_area_first)
440 debug_area_first = db_info->next;
441 if(db_info == debug_area_last)
442 debug_area_last = db_info->prev;
443 if(db_info->prev) db_info->prev->next = db_info->next;
444 if(db_info->next) db_info->next->prev = db_info->prev;
445 debug_info_free(db_info);
446 }
447}
448
449/*
450 * debug_format_entry:
451 * - format one debug entry and return size of formated data
452 */
453
Michael Holzheu66a464d2005-06-25 14:55:33 -0700454static int
455debug_format_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 debug_info_t *id_snap = p_info->debug_info_snap;
458 struct debug_view *view = p_info->view;
459 debug_entry_t *act_entry;
460 size_t len = 0;
461 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
462 /* print prolog */
463 if (view->prolog_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700464 len += view->prolog_proc(id_snap,view,p_info->temp_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto out;
466 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700467 if (!id_snap->areas) /* this is true, if we have a prolog only view */
468 goto out; /* or if 'pages_per_area' is 0 */
469 act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area]
470 [p_info->act_page] + p_info->act_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (act_entry->id.stck == 0LL)
473 goto out; /* empty entry */
474 if (view->header_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700475 len += view->header_proc(id_snap, view, p_info->act_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 act_entry, p_info->temp_buf + len);
477 if (view->format_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700478 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 DEBUG_DATA(act_entry));
Michael Holzheu66a464d2005-06-25 14:55:33 -0700480out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return len;
482}
483
484/*
485 * debug_next_entry:
486 * - goto next entry in p_info
487 */
488
Michael Holzheu66a464d2005-06-25 14:55:33 -0700489extern inline int
490debug_next_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700492 debug_info_t *id;
493
494 id = p_info->debug_info_snap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
496 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700497 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto out;
499 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700500 if(!id->areas)
501 return 1;
502 p_info->act_entry += id->entry_size;
503 /* switch to next page, if we reached the end of the page */
504 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)){
505 /* next page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700507 p_info->act_page += 1;
508 if((p_info->act_page % id->pages_per_area) == 0) {
509 /* next area */
510 p_info->act_area++;
511 p_info->act_page=0;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if(p_info->act_area >= id->nr_areas)
514 return 1;
515 }
516out:
517 return 0;
518}
519
520/*
521 * debug_output:
522 * - called for user read()
523 * - copies formated debug entries to the user buffer
524 */
525
Michael Holzheu66a464d2005-06-25 14:55:33 -0700526static ssize_t
527debug_output(struct file *file, /* file descriptor */
528 char __user *user_buf, /* user buffer */
529 size_t len, /* length of buffer */
530 loff_t *offset) /* offset in the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 size_t count = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700533 size_t entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 file_private_info_t *p_info;
535
536 p_info = ((file_private_info_t *) file->private_data);
537 if (*offset != p_info->offset)
538 return -EPIPE;
539 if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 entry_offset = p_info->act_entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 while(count < len){
Michael Holzheu66a464d2005-06-25 14:55:33 -0700543 int formatted_line_size;
544 int formatted_line_residue;
545 int user_buf_residue;
546 size_t copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Michael Holzheu66a464d2005-06-25 14:55:33 -0700548 formatted_line_size = debug_format_entry(p_info);
549 formatted_line_residue = formatted_line_size - entry_offset;
550 user_buf_residue = len-count;
551 copy_size = min(user_buf_residue, formatted_line_residue);
552 if(copy_size){
553 if (copy_to_user(user_buf + count, p_info->temp_buf
554 + entry_offset, copy_size))
555 return -EFAULT;
556 count += copy_size;
557 entry_offset += copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700559 if(copy_size == formatted_line_residue){
560 entry_offset = 0;
561 if(debug_next_entry(p_info))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565out:
566 p_info->offset = *offset + count;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700567 p_info->act_entry_offset = entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 *offset = p_info->offset;
569 return count;
570}
571
572/*
573 * debug_input:
574 * - called for user write()
575 * - calls input function of view
576 */
577
Michael Holzheu66a464d2005-06-25 14:55:33 -0700578static ssize_t
579debug_input(struct file *file, const char __user *user_buf, size_t length,
580 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int rc = 0;
583 file_private_info_t *p_info;
584
585 down(&debug_lock);
586 p_info = ((file_private_info_t *) file->private_data);
587 if (p_info->view->input_proc)
588 rc = p_info->view->input_proc(p_info->debug_info_org,
589 p_info->view, file, user_buf,
590 length, offset);
591 else
592 rc = -EPERM;
593 up(&debug_lock);
594 return rc; /* number of input characters */
595}
596
597/*
598 * debug_open:
599 * - called for user open()
600 * - copies formated output to private_data area of the file
601 * handle
602 */
603
Michael Holzheu66a464d2005-06-25 14:55:33 -0700604static int
605debug_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 int i = 0, rc = 0;
608 file_private_info_t *p_info;
609 debug_info_t *debug_info, *debug_info_snapshot;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 down(&debug_lock);
Michael Holzheu942eaab2005-09-03 15:57:58 -0700612 debug_info = (struct debug_info*)file->f_dentry->d_inode->u.generic_ip;
613 /* find debug view */
614 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
615 if (!debug_info->views[i])
616 continue;
617 else if (debug_info->debugfs_entries[i] ==
618 file->f_dentry) {
619 goto found; /* found view ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622 /* no entry found */
623 rc = -EINVAL;
624 goto out;
625
Michael Holzheu66a464d2005-06-25 14:55:33 -0700626found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Michael Holzheu66a464d2005-06-25 14:55:33 -0700628 /* Make snapshot of current debug areas to get it consistent. */
629 /* To copy all the areas is only needed, if we have a view which */
630 /* formats the debug areas. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Michael Holzheu66a464d2005-06-25 14:55:33 -0700632 if(!debug_info->views[i]->format_proc &&
633 !debug_info->views[i]->header_proc){
634 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
635 } else {
636 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if(!debug_info_snapshot){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 rc = -ENOMEM;
641 goto out;
642 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700643 p_info = (file_private_info_t *) kmalloc(sizeof(file_private_info_t),
644 GFP_KERNEL);
645 if(!p_info){
646 if(debug_info_snapshot)
647 debug_info_free(debug_info_snapshot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 rc = -ENOMEM;
649 goto out;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 p_info->offset = 0;
652 p_info->debug_info_snap = debug_info_snapshot;
653 p_info->debug_info_org = debug_info;
654 p_info->view = debug_info->views[i];
655 p_info->act_area = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700656 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 p_info->act_entry = DEBUG_PROLOG_ENTRY;
658 p_info->act_entry_offset = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700659 file->private_data = p_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 debug_info_get(debug_info);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700661out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 up(&debug_lock);
663 return rc;
664}
665
666/*
667 * debug_close:
668 * - called for user close()
669 * - deletes private_data area of the file handle
670 */
671
Michael Holzheu66a464d2005-06-25 14:55:33 -0700672static int
673debug_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 file_private_info_t *p_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 p_info = (file_private_info_t *) file->private_data;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700677 if(p_info->debug_info_snap)
678 debug_info_free(p_info->debug_info_snap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 debug_info_put(p_info->debug_info_org);
680 kfree(file->private_data);
681 return 0; /* success */
682}
683
684/*
685 * debug_register:
686 * - creates and initializes debug area for the caller
687 * - returns handle for debug area
688 */
689
Michael Holzheu66a464d2005-06-25 14:55:33 -0700690debug_info_t*
691debug_register (char *name, int pages_per_area, int nr_areas, int buf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 debug_info_t *rc = NULL;
694
695 if (!initialized)
696 BUG();
697 down(&debug_lock);
698
699 /* create new debug_info */
700
Michael Holzheu66a464d2005-06-25 14:55:33 -0700701 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if(!rc)
703 goto out;
704 debug_register_view(rc, &debug_level_view);
705 debug_register_view(rc, &debug_flush_view);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700706 debug_register_view(rc, &debug_pages_view);
707out:
708 if (!rc){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 printk(KERN_ERR "debug: debug_register failed for %s\n",name);
710 }
711 up(&debug_lock);
712 return rc;
713}
714
715/*
716 * debug_unregister:
717 * - give back debug area
718 */
719
Michael Holzheu66a464d2005-06-25 14:55:33 -0700720void
721debug_unregister(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 if (!id)
724 goto out;
725 down(&debug_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 debug_info_put(id);
727 up(&debug_lock);
728
Michael Holzheu66a464d2005-06-25 14:55:33 -0700729out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return;
731}
732
733/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700734 * debug_set_size:
735 * - set area size (number of pages) and number of areas
736 */
737static int
738debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
739{
740 unsigned long flags;
741 debug_entry_t *** new_areas;
742 int rc=0;
743
744 if(!id || (nr_areas <= 0) || (pages_per_area < 0))
745 return -EINVAL;
746 if(pages_per_area > 0){
747 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
748 if(!new_areas) {
749 printk(KERN_WARNING "debug: could not allocate memory "\
750 "for pagenumber: %i\n",pages_per_area);
751 rc = -ENOMEM;
752 goto out;
753 }
754 } else {
755 new_areas = NULL;
756 }
757 spin_lock_irqsave(&id->lock,flags);
758 debug_areas_free(id);
759 id->areas = new_areas;
760 id->nr_areas = nr_areas;
761 id->pages_per_area = pages_per_area;
762 id->active_area = 0;
763 memset(id->active_entries,0,sizeof(int)*id->nr_areas);
764 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
765 spin_unlock_irqrestore(&id->lock,flags);
766 printk(KERN_INFO "debug: %s: set new size (%i pages)\n"\
767 ,id->name, pages_per_area);
768out:
769 return rc;
770}
771
772/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * debug_set_level:
774 * - set actual debug level
775 */
776
Michael Holzheu66a464d2005-06-25 14:55:33 -0700777void
778debug_set_level(debug_info_t* id, int new_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 unsigned long flags;
781 if(!id)
782 return;
783 spin_lock_irqsave(&id->lock,flags);
784 if(new_level == DEBUG_OFF_LEVEL){
785 id->level = DEBUG_OFF_LEVEL;
786 printk(KERN_INFO "debug: %s: switched off\n",id->name);
787 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
788 printk(KERN_INFO
789 "debug: %s: level %i is out of range (%i - %i)\n",
790 id->name, new_level, 0, DEBUG_MAX_LEVEL);
791 } else {
792 id->level = new_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 spin_unlock_irqrestore(&id->lock,flags);
795}
796
797
798/*
799 * proceed_active_entry:
800 * - set active entry to next in the ring buffer
801 */
802
Michael Holzheu66a464d2005-06-25 14:55:33 -0700803extern inline void
804proceed_active_entry(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700806 if ((id->active_entries[id->active_area] += id->entry_size)
807 > (PAGE_SIZE - id->entry_size)){
808 id->active_entries[id->active_area] = 0;
809 id->active_pages[id->active_area] =
810 (id->active_pages[id->active_area] + 1) %
811 id->pages_per_area;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815/*
816 * proceed_active_area:
817 * - set active area to next in the ring buffer
818 */
819
Michael Holzheu66a464d2005-06-25 14:55:33 -0700820extern inline void
821proceed_active_area(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 id->active_area++;
824 id->active_area = id->active_area % id->nr_areas;
825}
826
827/*
828 * get_active_entry:
829 */
830
Michael Holzheu66a464d2005-06-25 14:55:33 -0700831extern inline debug_entry_t*
832get_active_entry(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700834 return (debug_entry_t *) (((char *) id->areas[id->active_area]
835 [id->active_pages[id->active_area]]) +
836 id->active_entries[id->active_area]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839/*
840 * debug_finish_entry:
841 * - set timestamp, caller address, cpu number etc.
842 */
843
Michael Holzheu66a464d2005-06-25 14:55:33 -0700844extern inline void
845debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
846 int exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Michael Holzheu942eaab2005-09-03 15:57:58 -0700848 active->id.stck = get_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 active->id.fields.cpuid = smp_processor_id();
850 active->caller = __builtin_return_address(0);
851 active->id.fields.exception = exception;
852 active->id.fields.level = level;
853 proceed_active_entry(id);
854 if(exception)
855 proceed_active_area(id);
856}
857
858static int debug_stoppable=1;
859static int debug_active=1;
860
861#define CTL_S390DBF 5677
862#define CTL_S390DBF_STOPPABLE 5678
863#define CTL_S390DBF_ACTIVE 5679
864
865/*
866 * proc handler for the running debug_active sysctl
867 * always allow read, allow write only if debug_stoppable is set or
868 * if debug_active is already off
869 */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700870static int
871s390dbf_procactive(ctl_table *table, int write, struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 void __user *buffer, size_t *lenp, loff_t *ppos)
873{
874 if (!write || debug_stoppable || !debug_active)
875 return proc_dointvec(table, write, filp, buffer, lenp, ppos);
876 else
877 return 0;
878}
879
880
881static struct ctl_table s390dbf_table[] = {
882 {
883 .ctl_name = CTL_S390DBF_STOPPABLE,
884 .procname = "debug_stoppable",
885 .data = &debug_stoppable,
886 .maxlen = sizeof(int),
887 .mode = S_IRUGO | S_IWUSR,
888 .proc_handler = &proc_dointvec,
889 .strategy = &sysctl_intvec,
890 },
891 {
892 .ctl_name = CTL_S390DBF_ACTIVE,
893 .procname = "debug_active",
894 .data = &debug_active,
895 .maxlen = sizeof(int),
896 .mode = S_IRUGO | S_IWUSR,
897 .proc_handler = &s390dbf_procactive,
898 .strategy = &sysctl_intvec,
899 },
900 { .ctl_name = 0 }
901};
902
903static struct ctl_table s390dbf_dir_table[] = {
904 {
905 .ctl_name = CTL_S390DBF,
906 .procname = "s390dbf",
907 .maxlen = 0,
908 .mode = S_IRUGO | S_IXUGO,
909 .child = s390dbf_table,
910 },
911 { .ctl_name = 0 }
912};
913
914struct ctl_table_header *s390dbf_sysctl_header;
915
Michael Holzheu66a464d2005-06-25 14:55:33 -0700916void
917debug_stop_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 if (debug_stoppable)
920 debug_active = 0;
921}
922
923
924/*
925 * debug_event_common:
926 * - write debug entry with given size
927 */
928
Michael Holzheu66a464d2005-06-25 14:55:33 -0700929debug_entry_t*
930debug_event_common(debug_info_t * id, int level, const void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 unsigned long flags;
933 debug_entry_t *active;
934
Michael Holzheu66a464d2005-06-25 14:55:33 -0700935 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return NULL;
937 spin_lock_irqsave(&id->lock, flags);
938 active = get_active_entry(id);
939 memset(DEBUG_DATA(active), 0, id->buf_size);
940 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
941 debug_finish_entry(id, active, level, 0);
942 spin_unlock_irqrestore(&id->lock, flags);
943
944 return active;
945}
946
947/*
948 * debug_exception_common:
949 * - write debug entry with given size and switch to next debug area
950 */
951
Michael Holzheu66a464d2005-06-25 14:55:33 -0700952debug_entry_t
953*debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 unsigned long flags;
956 debug_entry_t *active;
957
Michael Holzheu66a464d2005-06-25 14:55:33 -0700958 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return NULL;
960 spin_lock_irqsave(&id->lock, flags);
961 active = get_active_entry(id);
962 memset(DEBUG_DATA(active), 0, id->buf_size);
963 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
964 debug_finish_entry(id, active, level, 1);
965 spin_unlock_irqrestore(&id->lock, flags);
966
967 return active;
968}
969
970/*
971 * counts arguments in format string for sprintf view
972 */
973
Michael Holzheu66a464d2005-06-25 14:55:33 -0700974extern inline int
975debug_count_numargs(char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 int numargs=0;
978
979 while(*string) {
980 if(*string++=='%')
981 numargs++;
982 }
983 return(numargs);
984}
985
986/*
987 * debug_sprintf_event:
988 */
989
Michael Holzheu66a464d2005-06-25 14:55:33 -0700990debug_entry_t*
991debug_sprintf_event(debug_info_t* id, int level,char *string,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 va_list ap;
994 int numargs,idx;
995 unsigned long flags;
996 debug_sprintf_entry_t *curr_event;
997 debug_entry_t *active;
998
999 if((!id) || (level > id->level))
1000 return NULL;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001001 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return NULL;
1003 numargs=debug_count_numargs(string);
1004
1005 spin_lock_irqsave(&id->lock, flags);
1006 active = get_active_entry(id);
1007 curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
1008 va_start(ap,string);
1009 curr_event->string=string;
1010 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1011 curr_event->args[idx]=va_arg(ap,long);
1012 va_end(ap);
1013 debug_finish_entry(id, active, level, 0);
1014 spin_unlock_irqrestore(&id->lock, flags);
1015
1016 return active;
1017}
1018
1019/*
1020 * debug_sprintf_exception:
1021 */
1022
Michael Holzheu66a464d2005-06-25 14:55:33 -07001023debug_entry_t*
1024debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 va_list ap;
1027 int numargs,idx;
1028 unsigned long flags;
1029 debug_sprintf_entry_t *curr_event;
1030 debug_entry_t *active;
1031
1032 if((!id) || (level > id->level))
1033 return NULL;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001034 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return NULL;
1036
1037 numargs=debug_count_numargs(string);
1038
1039 spin_lock_irqsave(&id->lock, flags);
1040 active = get_active_entry(id);
1041 curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
1042 va_start(ap,string);
1043 curr_event->string=string;
1044 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1045 curr_event->args[idx]=va_arg(ap,long);
1046 va_end(ap);
1047 debug_finish_entry(id, active, level, 1);
1048 spin_unlock_irqrestore(&id->lock, flags);
1049
1050 return active;
1051}
1052
1053/*
1054 * debug_init:
1055 * - is called exactly once to initialize the debug feature
1056 */
1057
Michael Holzheu66a464d2005-06-25 14:55:33 -07001058static int
1059__init debug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 int rc = 0;
1062
1063 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table, 1);
1064 down(&debug_lock);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001065 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 printk(KERN_INFO "debug: Initialization complete\n");
1067 initialized = 1;
1068 up(&debug_lock);
1069
1070 return rc;
1071}
1072
1073/*
1074 * debug_register_view:
1075 */
1076
Michael Holzheu66a464d2005-06-25 14:55:33 -07001077int
1078debug_register_view(debug_info_t * id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 int rc = 0;
1081 int i;
1082 unsigned long flags;
1083 mode_t mode = S_IFREG;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001084 struct dentry *pde;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if (!id)
1087 goto out;
1088 if (view->prolog_proc || view->format_proc || view->header_proc)
1089 mode |= S_IRUSR;
1090 if (view->input_proc)
1091 mode |= S_IWUSR;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001092 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
Michael Holzheu942eaab2005-09-03 15:57:58 -07001093 id , &debug_file_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!pde){
Michael Holzheu66a464d2005-06-25 14:55:33 -07001095 printk(KERN_WARNING "debug: debugfs_create_file() failed!"\
1096 " Cannot register view %s/%s\n", id->name,view->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 rc = -1;
1098 goto out;
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 spin_lock_irqsave(&id->lock, flags);
1101 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001102 if (!id->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 break;
1104 }
1105 if (i == DEBUG_MAX_VIEWS) {
1106 printk(KERN_WARNING "debug: cannot register view %s/%s\n",
1107 id->name,view->name);
1108 printk(KERN_WARNING
1109 "debug: maximum number of views reached (%i)!\n", i);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001110 debugfs_remove(pde);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 rc = -1;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001112 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 id->views[i] = view;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001114 id->debugfs_entries[i] = pde;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001117out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return rc;
1119}
1120
1121/*
1122 * debug_unregister_view:
1123 */
1124
Michael Holzheu66a464d2005-06-25 14:55:33 -07001125int
1126debug_unregister_view(debug_info_t * id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 int rc = 0;
1129 int i;
1130 unsigned long flags;
1131
1132 if (!id)
1133 goto out;
1134 spin_lock_irqsave(&id->lock, flags);
1135 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1136 if (id->views[i] == view)
1137 break;
1138 }
1139 if (i == DEBUG_MAX_VIEWS)
1140 rc = -1;
1141 else {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001142 debugfs_remove(id->debugfs_entries[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 id->views[i] = NULL;
1144 rc = 0;
1145 }
1146 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001147out:
1148 return rc;
1149}
1150
1151static inline char *
1152debug_get_user_string(const char __user *user_buf, size_t user_len)
1153{
1154 char* buffer;
1155
1156 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1157 if (!buffer)
1158 return ERR_PTR(-ENOMEM);
1159 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1160 kfree(buffer);
1161 return ERR_PTR(-EFAULT);
1162 }
1163 /* got the string, now strip linefeed. */
1164 if (buffer[user_len - 1] == '\n')
1165 buffer[user_len - 1] = 0;
1166 else
1167 buffer[user_len] = 0;
1168 return buffer;
1169}
1170
1171static inline int
1172debug_get_uint(char *buf)
1173{
1174 int rc;
1175
1176 for(; isspace(*buf); buf++);
1177 rc = simple_strtoul(buf, &buf, 10);
1178 if(*buf){
1179 printk("debug: no integer specified!\n");
1180 rc = -EINVAL;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return rc;
1183}
1184
1185/*
1186 * functions for debug-views
1187 ***********************************
1188*/
1189
1190/*
1191 * prints out actual debug level
1192 */
1193
Michael Holzheu66a464d2005-06-25 14:55:33 -07001194static int
1195debug_prolog_pages_fn(debug_info_t * id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 struct debug_view *view, char *out_buf)
1197{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001198 return sprintf(out_buf, "%i\n", id->pages_per_area);
1199}
1200
1201/*
1202 * reads new size (number of pages per debug area)
1203 */
1204
1205static int
1206debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
1207 struct file *file, const char __user *user_buf,
1208 size_t user_len, loff_t * offset)
1209{
1210 char *str;
1211 int rc,new_pages;
1212
1213 if (user_len > 0x10000)
1214 user_len = 0x10000;
1215 if (*offset != 0){
1216 rc = -EPIPE;
1217 goto out;
1218 }
1219 str = debug_get_user_string(user_buf,user_len);
1220 if(IS_ERR(str)){
1221 rc = PTR_ERR(str);
1222 goto out;
1223 }
1224 new_pages = debug_get_uint(str);
1225 if(new_pages < 0){
1226 rc = -EINVAL;
1227 goto free_str;
1228 }
1229 rc = debug_set_size(id,id->nr_areas, new_pages);
1230 if(rc != 0){
1231 rc = -EINVAL;
1232 goto free_str;
1233 }
1234 rc = user_len;
1235free_str:
1236 kfree(str);
1237out:
1238 *offset += user_len;
1239 return rc; /* number of input characters */
1240}
1241
1242/*
1243 * prints out actual debug level
1244 */
1245
1246static int
1247debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
1248{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int rc = 0;
1250
Michael Holzheu66a464d2005-06-25 14:55:33 -07001251 if(id->level == DEBUG_OFF_LEVEL) {
1252 rc = sprintf(out_buf,"-\n");
1253 }
1254 else {
1255 rc = sprintf(out_buf, "%i\n", id->level);
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return rc;
1258}
1259
1260/*
1261 * reads new debug level
1262 */
1263
Michael Holzheu66a464d2005-06-25 14:55:33 -07001264static int
1265debug_input_level_fn(debug_info_t * id, struct debug_view *view,
1266 struct file *file, const char __user *user_buf,
1267 size_t user_len, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001269 char *str;
1270 int rc,new_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Michael Holzheu66a464d2005-06-25 14:55:33 -07001272 if (user_len > 0x10000)
1273 user_len = 0x10000;
1274 if (*offset != 0){
1275 rc = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 goto out;
1277 }
Michael Holzheu66a464d2005-06-25 14:55:33 -07001278 str = debug_get_user_string(user_buf,user_len);
1279 if(IS_ERR(str)){
1280 rc = PTR_ERR(str);
1281 goto out;
1282 }
1283 if(str[0] == '-'){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 debug_set_level(id, DEBUG_OFF_LEVEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001285 rc = user_len;
1286 goto free_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 } else {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001288 new_level = debug_get_uint(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
Michael Holzheu66a464d2005-06-25 14:55:33 -07001290 if(new_level < 0) {
1291 printk(KERN_INFO "debug: level `%s` is not valid\n", str);
1292 rc = -EINVAL;
1293 } else {
1294 debug_set_level(id, new_level);
1295 rc = user_len;
1296 }
1297free_str:
1298 kfree(str);
1299out:
1300 *offset += user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return rc; /* number of input characters */
1302}
1303
1304
1305/*
1306 * flushes debug areas
1307 */
1308
Michael Holzheu66a464d2005-06-25 14:55:33 -07001309void
1310debug_flush(debug_info_t* id, int area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 unsigned long flags;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001313 int i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Michael Holzheu66a464d2005-06-25 14:55:33 -07001315 if(!id || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return;
1317 spin_lock_irqsave(&id->lock,flags);
1318 if(area == DEBUG_FLUSH_ALL){
1319 id->active_area = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001320 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1321 for (i = 0; i < id->nr_areas; i++) {
1322 id->active_pages[i] = 0;
1323 for(j = 0; j < id->pages_per_area; j++) {
1324 memset(id->areas[i][j], 0, PAGE_SIZE);
1325 }
1326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 printk(KERN_INFO "debug: %s: all areas flushed\n",id->name);
1328 } else if(area >= 0 && area < id->nr_areas) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001329 id->active_entries[area] = 0;
1330 id->active_pages[area] = 0;
1331 for(i = 0; i < id->pages_per_area; i++) {
1332 memset(id->areas[area][i],0,PAGE_SIZE);
1333 }
1334 printk(KERN_INFO "debug: %s: area %i has been flushed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 id->name, area);
1336 } else {
1337 printk(KERN_INFO
Michael Holzheu66a464d2005-06-25 14:55:33 -07001338 "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 id->name, area, 0, id->nr_areas-1);
1340 }
1341 spin_unlock_irqrestore(&id->lock,flags);
1342}
1343
1344/*
1345 * view function: flushes debug areas
1346 */
1347
Michael Holzheu66a464d2005-06-25 14:55:33 -07001348static int
1349debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
1350 struct file *file, const char __user *user_buf,
1351 size_t user_len, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 char input_buf[1];
Michael Holzheu66a464d2005-06-25 14:55:33 -07001354 int rc = user_len;
1355
1356 if (user_len > 0x10000)
1357 user_len = 0x10000;
1358 if (*offset != 0){
1359 rc = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (copy_from_user(input_buf, user_buf, 1)){
1363 rc = -EFAULT;
1364 goto out;
1365 }
1366 if(input_buf[0] == '-') {
1367 debug_flush(id, DEBUG_FLUSH_ALL);
1368 goto out;
1369 }
1370 if (isdigit(input_buf[0])) {
1371 int area = ((int) input_buf[0] - (int) '0');
1372 debug_flush(id, area);
1373 goto out;
1374 }
1375
1376 printk(KERN_INFO "debug: area `%c` is not valid\n", input_buf[0]);
1377
Michael Holzheu66a464d2005-06-25 14:55:33 -07001378out:
1379 *offset += user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return rc; /* number of input characters */
1381}
1382
1383/*
1384 * prints debug header in raw format
1385 */
1386
Michael Holzheu66a464d2005-06-25 14:55:33 -07001387static int
1388debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
1389 int area, debug_entry_t * entry, char *out_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 int rc;
1392
1393 rc = sizeof(debug_entry_t);
1394 memcpy(out_buf,entry,sizeof(debug_entry_t));
1395 return rc;
1396}
1397
1398/*
1399 * prints debug data in raw format
1400 */
1401
Michael Holzheu66a464d2005-06-25 14:55:33 -07001402static int
1403debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 char *out_buf, const char *in_buf)
1405{
1406 int rc;
1407
1408 rc = id->buf_size;
1409 memcpy(out_buf, in_buf, id->buf_size);
1410 return rc;
1411}
1412
1413/*
1414 * prints debug data in hex/ascii format
1415 */
1416
Michael Holzheu66a464d2005-06-25 14:55:33 -07001417static int
1418debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
1419 char *out_buf, const char *in_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 int i, rc = 0;
1422
1423 for (i = 0; i < id->buf_size; i++) {
1424 rc += sprintf(out_buf + rc, "%02x ",
1425 ((unsigned char *) in_buf)[i]);
1426 }
1427 rc += sprintf(out_buf + rc, "| ");
1428 for (i = 0; i < id->buf_size; i++) {
1429 unsigned char c = in_buf[i];
1430 if (!isprint(c))
1431 rc += sprintf(out_buf + rc, ".");
1432 else
1433 rc += sprintf(out_buf + rc, "%c", c);
1434 }
1435 rc += sprintf(out_buf + rc, "\n");
1436 return rc;
1437}
1438
1439/*
1440 * prints header for debug entry
1441 */
1442
Michael Holzheu66a464d2005-06-25 14:55:33 -07001443int
1444debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int area, debug_entry_t * entry, char *out_buf)
1446{
Michael Holzheu942eaab2005-09-03 15:57:58 -07001447 struct timespec time_spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 unsigned long long time;
1449 char *except_str;
1450 unsigned long caller;
1451 int rc = 0;
1452 unsigned int level;
1453
1454 level = entry->id.fields.level;
1455 time = entry->id.stck;
1456 /* adjust todclock to 1970 */
1457 time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
Michael Holzheu942eaab2005-09-03 15:57:58 -07001458 tod_to_timeval(time, &time_spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 if (entry->id.fields.exception)
1461 except_str = "*";
1462 else
1463 except_str = "-";
1464 caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
1465 rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
Michael Holzheu942eaab2005-09-03 15:57:58 -07001466 area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 except_str, entry->id.fields.cpuid, (void *) caller);
1468 return rc;
1469}
1470
1471/*
1472 * prints debug data sprintf-formated:
1473 * debug_sprinf_event/exception calls must be used together with this view
1474 */
1475
1476#define DEBUG_SPRINTF_MAX_ARGS 10
1477
Michael Holzheu66a464d2005-06-25 14:55:33 -07001478static int
1479debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
1480 char *out_buf, debug_sprintf_entry_t *curr_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 int num_longs, num_used_args = 0,i, rc = 0;
1483 int index[DEBUG_SPRINTF_MAX_ARGS];
1484
1485 /* count of longs fit into one entry */
1486 num_longs = id->buf_size / sizeof(long);
1487
1488 if(num_longs < 1)
1489 goto out; /* bufsize of entry too small */
1490 if(num_longs == 1) {
1491 /* no args, we use only the string */
1492 strcpy(out_buf, curr_event->string);
1493 rc = strlen(curr_event->string);
1494 goto out;
1495 }
1496
1497 /* number of arguments used for sprintf (without the format string) */
1498 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1499
1500 memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1501
1502 for(i = 0; i < num_used_args; i++)
1503 index[i] = i;
1504
1505 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1506 curr_event->args[index[1]], curr_event->args[index[2]],
1507 curr_event->args[index[3]], curr_event->args[index[4]],
1508 curr_event->args[index[5]], curr_event->args[index[6]],
1509 curr_event->args[index[7]], curr_event->args[index[8]],
1510 curr_event->args[index[9]]);
1511
1512out:
1513
1514 return rc;
1515}
1516
1517/*
1518 * clean up module
1519 */
Michael Holzheu66a464d2005-06-25 14:55:33 -07001520void
1521__exit debug_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001523 debugfs_remove(debug_debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 unregister_sysctl_table(s390dbf_sysctl_header);
1525 return;
1526}
1527
1528/*
1529 * module definitions
1530 */
Michael Holzheu66a464d2005-06-25 14:55:33 -07001531postcore_initcall(debug_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532module_exit(debug_exit);
1533MODULE_LICENSE("GPL");
1534
1535EXPORT_SYMBOL(debug_register);
1536EXPORT_SYMBOL(debug_unregister);
1537EXPORT_SYMBOL(debug_set_level);
1538EXPORT_SYMBOL(debug_stop_all);
1539EXPORT_SYMBOL(debug_register_view);
1540EXPORT_SYMBOL(debug_unregister_view);
1541EXPORT_SYMBOL(debug_event_common);
1542EXPORT_SYMBOL(debug_exception_common);
1543EXPORT_SYMBOL(debug_hex_ascii_view);
1544EXPORT_SYMBOL(debug_raw_view);
1545EXPORT_SYMBOL(debug_dflt_header_fn);
1546EXPORT_SYMBOL(debug_sprintf_view);
1547EXPORT_SYMBOL(debug_sprintf_exception);
1548EXPORT_SYMBOL(debug_sprintf_event);