blob: 06a3fbc12536d8a3dc49386e579db46b1ae1c592 [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++) {
Eric Sesterhennfb630512006-03-24 03:15:31 -0800207 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700208 if(!areas[i][j]) {
209 for(j--; j >=0 ; j--) {
210 kfree(areas[i][j]);
211 }
212 kfree(areas[i]);
213 goto fail_malloc_areas2;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700214 }
215 }
216 }
217 return areas;
218
219fail_malloc_areas2:
220 for(i--; i >= 0; i--){
221 for(j=0; j < pages_per_area;j++){
222 kfree(areas[i][j]);
223 }
224 kfree(areas[i]);
225 }
226 kfree(areas);
227fail_malloc_areas:
228 return NULL;
229
230}
231
232
233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * debug_info_alloc
235 * - alloc new debug-info
236 */
237
Michael Holzheu66a464d2005-06-25 14:55:33 -0700238static debug_info_t*
239debug_info_alloc(char *name, int pages_per_area, int nr_areas, int buf_size,
240 int level, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 debug_info_t* rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* alloc everything */
245
Michael Holzheu66a464d2005-06-25 14:55:33 -0700246 rc = (debug_info_t*) kmalloc(sizeof(debug_info_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if(!rc)
248 goto fail_malloc_rc;
Eric Sesterhennfb630512006-03-24 03:15:31 -0800249 rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700250 if(!rc->active_entries)
251 goto fail_malloc_active_entries;
Eric Sesterhennfb630512006-03-24 03:15:31 -0800252 rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700253 if(!rc->active_pages)
254 goto fail_malloc_active_pages;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700255 if((mode == ALL_AREAS) && (pages_per_area != 0)){
256 rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
257 if(!rc->areas)
258 goto fail_malloc_areas;
259 } else {
260 rc->areas = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262
263 /* initialize members */
264
265 spin_lock_init(&rc->lock);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700266 rc->pages_per_area = pages_per_area;
267 rc->nr_areas = nr_areas;
268 rc->active_area = 0;
269 rc->level = level;
270 rc->buf_size = buf_size;
271 rc->entry_size = sizeof(debug_entry_t) + buf_size;
272 strlcpy(rc->name, name, sizeof(rc->name)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
Michael Holzheu66a464d2005-06-25 14:55:33 -0700274 memset(rc->debugfs_entries, 0 ,DEBUG_MAX_VIEWS *
275 sizeof(struct dentry*));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 atomic_set(&(rc->ref_count), 0);
277
278 return rc;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280fail_malloc_areas:
Michael Holzheu66a464d2005-06-25 14:55:33 -0700281 kfree(rc->active_pages);
282fail_malloc_active_pages:
283 kfree(rc->active_entries);
284fail_malloc_active_entries:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 kfree(rc);
286fail_malloc_rc:
287 return NULL;
288}
289
290/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700291 * debug_areas_free
292 * - free all debug areas
293 */
294
295static void
296debug_areas_free(debug_info_t* db_info)
297{
298 int i,j;
299
300 if(!db_info->areas)
301 return;
302 for (i = 0; i < db_info->nr_areas; i++) {
303 for(j = 0; j < db_info->pages_per_area; j++) {
304 kfree(db_info->areas[i][j]);
305 }
306 kfree(db_info->areas[i]);
307 }
308 kfree(db_info->areas);
309 db_info->areas = NULL;
310}
311
312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * debug_info_free
314 * - free memory debug-info
315 */
316
Michael Holzheu66a464d2005-06-25 14:55:33 -0700317static void
318debug_info_free(debug_info_t* db_info){
319 debug_areas_free(db_info);
320 kfree(db_info->active_entries);
321 kfree(db_info->active_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 kfree(db_info);
323}
324
325/*
326 * debug_info_create
327 * - create new debug-info
328 */
329
Michael Holzheu66a464d2005-06-25 14:55:33 -0700330static debug_info_t*
331debug_info_create(char *name, int pages_per_area, int nr_areas, int buf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 debug_info_t* rc;
334
Michael Holzheu66a464d2005-06-25 14:55:33 -0700335 rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
336 DEBUG_DEFAULT_LEVEL, ALL_AREAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if(!rc)
338 goto out;
339
Michael Holzheu66a464d2005-06-25 14:55:33 -0700340 /* create root directory */
341 rc->debugfs_root_entry = debugfs_create_dir(rc->name,
342 debug_debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* append new element to linked list */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700345 if (!debug_area_first) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* first element in list */
347 debug_area_first = rc;
348 rc->prev = NULL;
349 } else {
350 /* append element to end of list */
351 debug_area_last->next = rc;
352 rc->prev = debug_area_last;
353 }
354 debug_area_last = rc;
355 rc->next = NULL;
356
357 debug_info_get(rc);
358out:
359 return rc;
360}
361
362/*
363 * debug_info_copy
364 * - copy debug-info
365 */
366
Michael Holzheu66a464d2005-06-25 14:55:33 -0700367static debug_info_t*
368debug_info_copy(debug_info_t* in, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700370 int i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 debug_info_t* rc;
Michael Holzheu942eaab2005-09-03 15:57:58 -0700372 unsigned long flags;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700373
Michael Holzheu942eaab2005-09-03 15:57:58 -0700374 /* get a consistent copy of the debug areas */
375 do {
376 rc = debug_info_alloc(in->name, in->pages_per_area,
377 in->nr_areas, in->buf_size, in->level, mode);
378 spin_lock_irqsave(&in->lock, flags);
379 if(!rc)
380 goto out;
381 /* has something changed in the meantime ? */
382 if((rc->pages_per_area == in->pages_per_area) &&
383 (rc->nr_areas == in->nr_areas)) {
384 break;
385 }
386 spin_unlock_irqrestore(&in->lock, flags);
387 debug_info_free(rc);
388 } while (1);
389
Michael Holzheu66a464d2005-06-25 14:55:33 -0700390 if(!rc || (mode == NO_AREAS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 goto out;
392
393 for(i = 0; i < in->nr_areas; i++){
Michael Holzheu66a464d2005-06-25 14:55:33 -0700394 for(j = 0; j < in->pages_per_area; j++) {
395 memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398out:
Michael Holzheu942eaab2005-09-03 15:57:58 -0700399 spin_unlock_irqrestore(&in->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return rc;
401}
402
403/*
404 * debug_info_get
405 * - increments reference count for debug-info
406 */
407
Michael Holzheu66a464d2005-06-25 14:55:33 -0700408static void
409debug_info_get(debug_info_t * db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 if (db_info)
412 atomic_inc(&db_info->ref_count);
413}
414
415/*
416 * debug_info_put:
417 * - decreases reference count for debug-info and frees it if necessary
418 */
419
Michael Holzheu66a464d2005-06-25 14:55:33 -0700420static void
421debug_info_put(debug_info_t *db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 int i;
424
425 if (!db_info)
426 return;
427 if (atomic_dec_and_test(&db_info->ref_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700429 if (!db_info->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 continue;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700431 debugfs_remove(db_info->debugfs_entries[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700433 debugfs_remove(db_info->debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if(db_info == debug_area_first)
435 debug_area_first = db_info->next;
436 if(db_info == debug_area_last)
437 debug_area_last = db_info->prev;
438 if(db_info->prev) db_info->prev->next = db_info->next;
439 if(db_info->next) db_info->next->prev = db_info->prev;
440 debug_info_free(db_info);
441 }
442}
443
444/*
445 * debug_format_entry:
446 * - format one debug entry and return size of formated data
447 */
448
Michael Holzheu66a464d2005-06-25 14:55:33 -0700449static int
450debug_format_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 debug_info_t *id_snap = p_info->debug_info_snap;
453 struct debug_view *view = p_info->view;
454 debug_entry_t *act_entry;
455 size_t len = 0;
456 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
457 /* print prolog */
458 if (view->prolog_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700459 len += view->prolog_proc(id_snap,view,p_info->temp_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 goto out;
461 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700462 if (!id_snap->areas) /* this is true, if we have a prolog only view */
463 goto out; /* or if 'pages_per_area' is 0 */
464 act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area]
465 [p_info->act_page] + p_info->act_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 if (act_entry->id.stck == 0LL)
468 goto out; /* empty entry */
469 if (view->header_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700470 len += view->header_proc(id_snap, view, p_info->act_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 act_entry, p_info->temp_buf + len);
472 if (view->format_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700473 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 DEBUG_DATA(act_entry));
Michael Holzheu66a464d2005-06-25 14:55:33 -0700475out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return len;
477}
478
479/*
480 * debug_next_entry:
481 * - goto next entry in p_info
482 */
483
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800484static inline int
Michael Holzheu66a464d2005-06-25 14:55:33 -0700485debug_next_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700487 debug_info_t *id;
488
489 id = p_info->debug_info_snap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
491 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700492 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto out;
494 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700495 if(!id->areas)
496 return 1;
497 p_info->act_entry += id->entry_size;
498 /* switch to next page, if we reached the end of the page */
499 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)){
500 /* next page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700502 p_info->act_page += 1;
503 if((p_info->act_page % id->pages_per_area) == 0) {
504 /* next area */
505 p_info->act_area++;
506 p_info->act_page=0;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if(p_info->act_area >= id->nr_areas)
509 return 1;
510 }
511out:
512 return 0;
513}
514
515/*
516 * debug_output:
517 * - called for user read()
518 * - copies formated debug entries to the user buffer
519 */
520
Michael Holzheu66a464d2005-06-25 14:55:33 -0700521static ssize_t
522debug_output(struct file *file, /* file descriptor */
523 char __user *user_buf, /* user buffer */
524 size_t len, /* length of buffer */
525 loff_t *offset) /* offset in the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 size_t count = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700528 size_t entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 file_private_info_t *p_info;
530
531 p_info = ((file_private_info_t *) file->private_data);
532 if (*offset != p_info->offset)
533 return -EPIPE;
534 if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 entry_offset = p_info->act_entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 while(count < len){
Michael Holzheu66a464d2005-06-25 14:55:33 -0700538 int formatted_line_size;
539 int formatted_line_residue;
540 int user_buf_residue;
541 size_t copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Michael Holzheu66a464d2005-06-25 14:55:33 -0700543 formatted_line_size = debug_format_entry(p_info);
544 formatted_line_residue = formatted_line_size - entry_offset;
545 user_buf_residue = len-count;
546 copy_size = min(user_buf_residue, formatted_line_residue);
547 if(copy_size){
548 if (copy_to_user(user_buf + count, p_info->temp_buf
549 + entry_offset, copy_size))
550 return -EFAULT;
551 count += copy_size;
552 entry_offset += copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700554 if(copy_size == formatted_line_residue){
555 entry_offset = 0;
556 if(debug_next_entry(p_info))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560out:
561 p_info->offset = *offset + count;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700562 p_info->act_entry_offset = entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 *offset = p_info->offset;
564 return count;
565}
566
567/*
568 * debug_input:
569 * - called for user write()
570 * - calls input function of view
571 */
572
Michael Holzheu66a464d2005-06-25 14:55:33 -0700573static ssize_t
574debug_input(struct file *file, const char __user *user_buf, size_t length,
575 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 int rc = 0;
578 file_private_info_t *p_info;
579
580 down(&debug_lock);
581 p_info = ((file_private_info_t *) file->private_data);
582 if (p_info->view->input_proc)
583 rc = p_info->view->input_proc(p_info->debug_info_org,
584 p_info->view, file, user_buf,
585 length, offset);
586 else
587 rc = -EPERM;
588 up(&debug_lock);
589 return rc; /* number of input characters */
590}
591
592/*
593 * debug_open:
594 * - called for user open()
595 * - copies formated output to private_data area of the file
596 * handle
597 */
598
Michael Holzheu66a464d2005-06-25 14:55:33 -0700599static int
600debug_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 int i = 0, rc = 0;
603 file_private_info_t *p_info;
604 debug_info_t *debug_info, *debug_info_snapshot;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 down(&debug_lock);
Michael Holzheu942eaab2005-09-03 15:57:58 -0700607 debug_info = (struct debug_info*)file->f_dentry->d_inode->u.generic_ip;
608 /* find debug view */
609 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
610 if (!debug_info->views[i])
611 continue;
612 else if (debug_info->debugfs_entries[i] ==
613 file->f_dentry) {
614 goto found; /* found view ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 /* no entry found */
618 rc = -EINVAL;
619 goto out;
620
Michael Holzheu66a464d2005-06-25 14:55:33 -0700621found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Michael Holzheu66a464d2005-06-25 14:55:33 -0700623 /* Make snapshot of current debug areas to get it consistent. */
624 /* To copy all the areas is only needed, if we have a view which */
625 /* formats the debug areas. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Michael Holzheu66a464d2005-06-25 14:55:33 -0700627 if(!debug_info->views[i]->format_proc &&
628 !debug_info->views[i]->header_proc){
629 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
630 } else {
631 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if(!debug_info_snapshot){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 rc = -ENOMEM;
636 goto out;
637 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700638 p_info = (file_private_info_t *) kmalloc(sizeof(file_private_info_t),
639 GFP_KERNEL);
640 if(!p_info){
641 if(debug_info_snapshot)
642 debug_info_free(debug_info_snapshot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 rc = -ENOMEM;
644 goto out;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 p_info->offset = 0;
647 p_info->debug_info_snap = debug_info_snapshot;
648 p_info->debug_info_org = debug_info;
649 p_info->view = debug_info->views[i];
650 p_info->act_area = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700651 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 p_info->act_entry = DEBUG_PROLOG_ENTRY;
653 p_info->act_entry_offset = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700654 file->private_data = p_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 debug_info_get(debug_info);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700656out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 up(&debug_lock);
658 return rc;
659}
660
661/*
662 * debug_close:
663 * - called for user close()
664 * - deletes private_data area of the file handle
665 */
666
Michael Holzheu66a464d2005-06-25 14:55:33 -0700667static int
668debug_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 file_private_info_t *p_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 p_info = (file_private_info_t *) file->private_data;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700672 if(p_info->debug_info_snap)
673 debug_info_free(p_info->debug_info_snap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 debug_info_put(p_info->debug_info_org);
675 kfree(file->private_data);
676 return 0; /* success */
677}
678
679/*
680 * debug_register:
681 * - creates and initializes debug area for the caller
682 * - returns handle for debug area
683 */
684
Michael Holzheu66a464d2005-06-25 14:55:33 -0700685debug_info_t*
686debug_register (char *name, int pages_per_area, int nr_areas, int buf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 debug_info_t *rc = NULL;
689
690 if (!initialized)
691 BUG();
692 down(&debug_lock);
693
694 /* create new debug_info */
695
Michael Holzheu66a464d2005-06-25 14:55:33 -0700696 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if(!rc)
698 goto out;
699 debug_register_view(rc, &debug_level_view);
700 debug_register_view(rc, &debug_flush_view);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700701 debug_register_view(rc, &debug_pages_view);
702out:
703 if (!rc){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 printk(KERN_ERR "debug: debug_register failed for %s\n",name);
705 }
706 up(&debug_lock);
707 return rc;
708}
709
710/*
711 * debug_unregister:
712 * - give back debug area
713 */
714
Michael Holzheu66a464d2005-06-25 14:55:33 -0700715void
716debug_unregister(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 if (!id)
719 goto out;
720 down(&debug_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 debug_info_put(id);
722 up(&debug_lock);
723
Michael Holzheu66a464d2005-06-25 14:55:33 -0700724out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return;
726}
727
728/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700729 * debug_set_size:
730 * - set area size (number of pages) and number of areas
731 */
732static int
733debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
734{
735 unsigned long flags;
736 debug_entry_t *** new_areas;
737 int rc=0;
738
739 if(!id || (nr_areas <= 0) || (pages_per_area < 0))
740 return -EINVAL;
741 if(pages_per_area > 0){
742 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
743 if(!new_areas) {
744 printk(KERN_WARNING "debug: could not allocate memory "\
745 "for pagenumber: %i\n",pages_per_area);
746 rc = -ENOMEM;
747 goto out;
748 }
749 } else {
750 new_areas = NULL;
751 }
752 spin_lock_irqsave(&id->lock,flags);
753 debug_areas_free(id);
754 id->areas = new_areas;
755 id->nr_areas = nr_areas;
756 id->pages_per_area = pages_per_area;
757 id->active_area = 0;
758 memset(id->active_entries,0,sizeof(int)*id->nr_areas);
759 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
760 spin_unlock_irqrestore(&id->lock,flags);
761 printk(KERN_INFO "debug: %s: set new size (%i pages)\n"\
762 ,id->name, pages_per_area);
763out:
764 return rc;
765}
766
767/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * debug_set_level:
769 * - set actual debug level
770 */
771
Michael Holzheu66a464d2005-06-25 14:55:33 -0700772void
773debug_set_level(debug_info_t* id, int new_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 unsigned long flags;
776 if(!id)
777 return;
778 spin_lock_irqsave(&id->lock,flags);
779 if(new_level == DEBUG_OFF_LEVEL){
780 id->level = DEBUG_OFF_LEVEL;
781 printk(KERN_INFO "debug: %s: switched off\n",id->name);
782 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
783 printk(KERN_INFO
784 "debug: %s: level %i is out of range (%i - %i)\n",
785 id->name, new_level, 0, DEBUG_MAX_LEVEL);
786 } else {
787 id->level = new_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789 spin_unlock_irqrestore(&id->lock,flags);
790}
791
792
793/*
794 * proceed_active_entry:
795 * - set active entry to next in the ring buffer
796 */
797
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800798static inline void
Michael Holzheu66a464d2005-06-25 14:55:33 -0700799proceed_active_entry(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700801 if ((id->active_entries[id->active_area] += id->entry_size)
802 > (PAGE_SIZE - id->entry_size)){
803 id->active_entries[id->active_area] = 0;
804 id->active_pages[id->active_area] =
805 (id->active_pages[id->active_area] + 1) %
806 id->pages_per_area;
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810/*
811 * proceed_active_area:
812 * - set active area to next in the ring buffer
813 */
814
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800815static inline void
Michael Holzheu66a464d2005-06-25 14:55:33 -0700816proceed_active_area(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 id->active_area++;
819 id->active_area = id->active_area % id->nr_areas;
820}
821
822/*
823 * get_active_entry:
824 */
825
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800826static inline debug_entry_t*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700827get_active_entry(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700829 return (debug_entry_t *) (((char *) id->areas[id->active_area]
830 [id->active_pages[id->active_area]]) +
831 id->active_entries[id->active_area]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/*
835 * debug_finish_entry:
836 * - set timestamp, caller address, cpu number etc.
837 */
838
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800839static inline void
Michael Holzheu66a464d2005-06-25 14:55:33 -0700840debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
841 int exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Michael Holzheu942eaab2005-09-03 15:57:58 -0700843 active->id.stck = get_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 active->id.fields.cpuid = smp_processor_id();
845 active->caller = __builtin_return_address(0);
846 active->id.fields.exception = exception;
847 active->id.fields.level = level;
848 proceed_active_entry(id);
849 if(exception)
850 proceed_active_area(id);
851}
852
853static int debug_stoppable=1;
854static int debug_active=1;
855
856#define CTL_S390DBF 5677
857#define CTL_S390DBF_STOPPABLE 5678
858#define CTL_S390DBF_ACTIVE 5679
859
860/*
861 * proc handler for the running debug_active sysctl
862 * always allow read, allow write only if debug_stoppable is set or
863 * if debug_active is already off
864 */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700865static int
866s390dbf_procactive(ctl_table *table, int write, struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 void __user *buffer, size_t *lenp, loff_t *ppos)
868{
869 if (!write || debug_stoppable || !debug_active)
870 return proc_dointvec(table, write, filp, buffer, lenp, ppos);
871 else
872 return 0;
873}
874
875
876static struct ctl_table s390dbf_table[] = {
877 {
878 .ctl_name = CTL_S390DBF_STOPPABLE,
879 .procname = "debug_stoppable",
880 .data = &debug_stoppable,
881 .maxlen = sizeof(int),
882 .mode = S_IRUGO | S_IWUSR,
883 .proc_handler = &proc_dointvec,
884 .strategy = &sysctl_intvec,
885 },
886 {
887 .ctl_name = CTL_S390DBF_ACTIVE,
888 .procname = "debug_active",
889 .data = &debug_active,
890 .maxlen = sizeof(int),
891 .mode = S_IRUGO | S_IWUSR,
892 .proc_handler = &s390dbf_procactive,
893 .strategy = &sysctl_intvec,
894 },
895 { .ctl_name = 0 }
896};
897
898static struct ctl_table s390dbf_dir_table[] = {
899 {
900 .ctl_name = CTL_S390DBF,
901 .procname = "s390dbf",
902 .maxlen = 0,
903 .mode = S_IRUGO | S_IXUGO,
904 .child = s390dbf_table,
905 },
906 { .ctl_name = 0 }
907};
908
909struct ctl_table_header *s390dbf_sysctl_header;
910
Michael Holzheu66a464d2005-06-25 14:55:33 -0700911void
912debug_stop_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 if (debug_stoppable)
915 debug_active = 0;
916}
917
918
919/*
920 * debug_event_common:
921 * - write debug entry with given size
922 */
923
Michael Holzheu66a464d2005-06-25 14:55:33 -0700924debug_entry_t*
925debug_event_common(debug_info_t * id, int level, const void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 unsigned long flags;
928 debug_entry_t *active;
929
Michael Holzheu66a464d2005-06-25 14:55:33 -0700930 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return NULL;
932 spin_lock_irqsave(&id->lock, flags);
933 active = get_active_entry(id);
934 memset(DEBUG_DATA(active), 0, id->buf_size);
935 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
936 debug_finish_entry(id, active, level, 0);
937 spin_unlock_irqrestore(&id->lock, flags);
938
939 return active;
940}
941
942/*
943 * debug_exception_common:
944 * - write debug entry with given size and switch to next debug area
945 */
946
Michael Holzheu66a464d2005-06-25 14:55:33 -0700947debug_entry_t
948*debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 unsigned long flags;
951 debug_entry_t *active;
952
Michael Holzheu66a464d2005-06-25 14:55:33 -0700953 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return NULL;
955 spin_lock_irqsave(&id->lock, flags);
956 active = get_active_entry(id);
957 memset(DEBUG_DATA(active), 0, id->buf_size);
958 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
959 debug_finish_entry(id, active, level, 1);
960 spin_unlock_irqrestore(&id->lock, flags);
961
962 return active;
963}
964
965/*
966 * counts arguments in format string for sprintf view
967 */
968
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800969static inline int
Michael Holzheu66a464d2005-06-25 14:55:33 -0700970debug_count_numargs(char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 int numargs=0;
973
974 while(*string) {
975 if(*string++=='%')
976 numargs++;
977 }
978 return(numargs);
979}
980
981/*
982 * debug_sprintf_event:
983 */
984
Michael Holzheu66a464d2005-06-25 14:55:33 -0700985debug_entry_t*
986debug_sprintf_event(debug_info_t* id, int level,char *string,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 va_list ap;
989 int numargs,idx;
990 unsigned long flags;
991 debug_sprintf_entry_t *curr_event;
992 debug_entry_t *active;
993
994 if((!id) || (level > id->level))
995 return NULL;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700996 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return NULL;
998 numargs=debug_count_numargs(string);
999
1000 spin_lock_irqsave(&id->lock, flags);
1001 active = get_active_entry(id);
1002 curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
1003 va_start(ap,string);
1004 curr_event->string=string;
1005 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1006 curr_event->args[idx]=va_arg(ap,long);
1007 va_end(ap);
1008 debug_finish_entry(id, active, level, 0);
1009 spin_unlock_irqrestore(&id->lock, flags);
1010
1011 return active;
1012}
1013
1014/*
1015 * debug_sprintf_exception:
1016 */
1017
Michael Holzheu66a464d2005-06-25 14:55:33 -07001018debug_entry_t*
1019debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 va_list ap;
1022 int numargs,idx;
1023 unsigned long flags;
1024 debug_sprintf_entry_t *curr_event;
1025 debug_entry_t *active;
1026
1027 if((!id) || (level > id->level))
1028 return NULL;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001029 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return NULL;
1031
1032 numargs=debug_count_numargs(string);
1033
1034 spin_lock_irqsave(&id->lock, flags);
1035 active = get_active_entry(id);
1036 curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
1037 va_start(ap,string);
1038 curr_event->string=string;
1039 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1040 curr_event->args[idx]=va_arg(ap,long);
1041 va_end(ap);
1042 debug_finish_entry(id, active, level, 1);
1043 spin_unlock_irqrestore(&id->lock, flags);
1044
1045 return active;
1046}
1047
1048/*
1049 * debug_init:
1050 * - is called exactly once to initialize the debug feature
1051 */
1052
Michael Holzheu66a464d2005-06-25 14:55:33 -07001053static int
1054__init debug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 int rc = 0;
1057
1058 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table, 1);
1059 down(&debug_lock);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001060 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 printk(KERN_INFO "debug: Initialization complete\n");
1062 initialized = 1;
1063 up(&debug_lock);
1064
1065 return rc;
1066}
1067
1068/*
1069 * debug_register_view:
1070 */
1071
Michael Holzheu66a464d2005-06-25 14:55:33 -07001072int
1073debug_register_view(debug_info_t * id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 int rc = 0;
1076 int i;
1077 unsigned long flags;
1078 mode_t mode = S_IFREG;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001079 struct dentry *pde;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (!id)
1082 goto out;
1083 if (view->prolog_proc || view->format_proc || view->header_proc)
1084 mode |= S_IRUSR;
1085 if (view->input_proc)
1086 mode |= S_IWUSR;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001087 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
Michael Holzheu942eaab2005-09-03 15:57:58 -07001088 id , &debug_file_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (!pde){
Michael Holzheu66a464d2005-06-25 14:55:33 -07001090 printk(KERN_WARNING "debug: debugfs_create_file() failed!"\
1091 " Cannot register view %s/%s\n", id->name,view->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 rc = -1;
1093 goto out;
1094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 spin_lock_irqsave(&id->lock, flags);
1096 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001097 if (!id->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 break;
1099 }
1100 if (i == DEBUG_MAX_VIEWS) {
1101 printk(KERN_WARNING "debug: cannot register view %s/%s\n",
1102 id->name,view->name);
1103 printk(KERN_WARNING
1104 "debug: maximum number of views reached (%i)!\n", i);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001105 debugfs_remove(pde);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 rc = -1;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001107 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 id->views[i] = view;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001109 id->debugfs_entries[i] = pde;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001112out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return rc;
1114}
1115
1116/*
1117 * debug_unregister_view:
1118 */
1119
Michael Holzheu66a464d2005-06-25 14:55:33 -07001120int
1121debug_unregister_view(debug_info_t * id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 int rc = 0;
1124 int i;
1125 unsigned long flags;
1126
1127 if (!id)
1128 goto out;
1129 spin_lock_irqsave(&id->lock, flags);
1130 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1131 if (id->views[i] == view)
1132 break;
1133 }
1134 if (i == DEBUG_MAX_VIEWS)
1135 rc = -1;
1136 else {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001137 debugfs_remove(id->debugfs_entries[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 id->views[i] = NULL;
1139 rc = 0;
1140 }
1141 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001142out:
1143 return rc;
1144}
1145
1146static inline char *
1147debug_get_user_string(const char __user *user_buf, size_t user_len)
1148{
1149 char* buffer;
1150
1151 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1152 if (!buffer)
1153 return ERR_PTR(-ENOMEM);
1154 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1155 kfree(buffer);
1156 return ERR_PTR(-EFAULT);
1157 }
1158 /* got the string, now strip linefeed. */
1159 if (buffer[user_len - 1] == '\n')
1160 buffer[user_len - 1] = 0;
1161 else
1162 buffer[user_len] = 0;
1163 return buffer;
1164}
1165
1166static inline int
1167debug_get_uint(char *buf)
1168{
1169 int rc;
1170
1171 for(; isspace(*buf); buf++);
1172 rc = simple_strtoul(buf, &buf, 10);
1173 if(*buf){
1174 printk("debug: no integer specified!\n");
1175 rc = -EINVAL;
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return rc;
1178}
1179
1180/*
1181 * functions for debug-views
1182 ***********************************
1183*/
1184
1185/*
1186 * prints out actual debug level
1187 */
1188
Michael Holzheu66a464d2005-06-25 14:55:33 -07001189static int
1190debug_prolog_pages_fn(debug_info_t * id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 struct debug_view *view, char *out_buf)
1192{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001193 return sprintf(out_buf, "%i\n", id->pages_per_area);
1194}
1195
1196/*
1197 * reads new size (number of pages per debug area)
1198 */
1199
1200static int
1201debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
1202 struct file *file, const char __user *user_buf,
1203 size_t user_len, loff_t * offset)
1204{
1205 char *str;
1206 int rc,new_pages;
1207
1208 if (user_len > 0x10000)
1209 user_len = 0x10000;
1210 if (*offset != 0){
1211 rc = -EPIPE;
1212 goto out;
1213 }
1214 str = debug_get_user_string(user_buf,user_len);
1215 if(IS_ERR(str)){
1216 rc = PTR_ERR(str);
1217 goto out;
1218 }
1219 new_pages = debug_get_uint(str);
1220 if(new_pages < 0){
1221 rc = -EINVAL;
1222 goto free_str;
1223 }
1224 rc = debug_set_size(id,id->nr_areas, new_pages);
1225 if(rc != 0){
1226 rc = -EINVAL;
1227 goto free_str;
1228 }
1229 rc = user_len;
1230free_str:
1231 kfree(str);
1232out:
1233 *offset += user_len;
1234 return rc; /* number of input characters */
1235}
1236
1237/*
1238 * prints out actual debug level
1239 */
1240
1241static int
1242debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
1243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int rc = 0;
1245
Michael Holzheu66a464d2005-06-25 14:55:33 -07001246 if(id->level == DEBUG_OFF_LEVEL) {
1247 rc = sprintf(out_buf,"-\n");
1248 }
1249 else {
1250 rc = sprintf(out_buf, "%i\n", id->level);
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return rc;
1253}
1254
1255/*
1256 * reads new debug level
1257 */
1258
Michael Holzheu66a464d2005-06-25 14:55:33 -07001259static int
1260debug_input_level_fn(debug_info_t * id, struct debug_view *view,
1261 struct file *file, const char __user *user_buf,
1262 size_t user_len, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001264 char *str;
1265 int rc,new_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Michael Holzheu66a464d2005-06-25 14:55:33 -07001267 if (user_len > 0x10000)
1268 user_len = 0x10000;
1269 if (*offset != 0){
1270 rc = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 goto out;
1272 }
Michael Holzheu66a464d2005-06-25 14:55:33 -07001273 str = debug_get_user_string(user_buf,user_len);
1274 if(IS_ERR(str)){
1275 rc = PTR_ERR(str);
1276 goto out;
1277 }
1278 if(str[0] == '-'){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 debug_set_level(id, DEBUG_OFF_LEVEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001280 rc = user_len;
1281 goto free_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 } else {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001283 new_level = debug_get_uint(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
Michael Holzheu66a464d2005-06-25 14:55:33 -07001285 if(new_level < 0) {
1286 printk(KERN_INFO "debug: level `%s` is not valid\n", str);
1287 rc = -EINVAL;
1288 } else {
1289 debug_set_level(id, new_level);
1290 rc = user_len;
1291 }
1292free_str:
1293 kfree(str);
1294out:
1295 *offset += user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return rc; /* number of input characters */
1297}
1298
1299
1300/*
1301 * flushes debug areas
1302 */
1303
Michael Holzheu66a464d2005-06-25 14:55:33 -07001304void
1305debug_flush(debug_info_t* id, int area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 unsigned long flags;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001308 int i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Michael Holzheu66a464d2005-06-25 14:55:33 -07001310 if(!id || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return;
1312 spin_lock_irqsave(&id->lock,flags);
1313 if(area == DEBUG_FLUSH_ALL){
1314 id->active_area = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001315 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1316 for (i = 0; i < id->nr_areas; i++) {
1317 id->active_pages[i] = 0;
1318 for(j = 0; j < id->pages_per_area; j++) {
1319 memset(id->areas[i][j], 0, PAGE_SIZE);
1320 }
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 printk(KERN_INFO "debug: %s: all areas flushed\n",id->name);
1323 } else if(area >= 0 && area < id->nr_areas) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001324 id->active_entries[area] = 0;
1325 id->active_pages[area] = 0;
1326 for(i = 0; i < id->pages_per_area; i++) {
1327 memset(id->areas[area][i],0,PAGE_SIZE);
1328 }
1329 printk(KERN_INFO "debug: %s: area %i has been flushed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 id->name, area);
1331 } else {
1332 printk(KERN_INFO
Michael Holzheu66a464d2005-06-25 14:55:33 -07001333 "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 id->name, area, 0, id->nr_areas-1);
1335 }
1336 spin_unlock_irqrestore(&id->lock,flags);
1337}
1338
1339/*
1340 * view function: flushes debug areas
1341 */
1342
Michael Holzheu66a464d2005-06-25 14:55:33 -07001343static int
1344debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
1345 struct file *file, const char __user *user_buf,
1346 size_t user_len, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 char input_buf[1];
Michael Holzheu66a464d2005-06-25 14:55:33 -07001349 int rc = user_len;
1350
1351 if (user_len > 0x10000)
1352 user_len = 0x10000;
1353 if (*offset != 0){
1354 rc = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (copy_from_user(input_buf, user_buf, 1)){
1358 rc = -EFAULT;
1359 goto out;
1360 }
1361 if(input_buf[0] == '-') {
1362 debug_flush(id, DEBUG_FLUSH_ALL);
1363 goto out;
1364 }
1365 if (isdigit(input_buf[0])) {
1366 int area = ((int) input_buf[0] - (int) '0');
1367 debug_flush(id, area);
1368 goto out;
1369 }
1370
1371 printk(KERN_INFO "debug: area `%c` is not valid\n", input_buf[0]);
1372
Michael Holzheu66a464d2005-06-25 14:55:33 -07001373out:
1374 *offset += user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return rc; /* number of input characters */
1376}
1377
1378/*
1379 * prints debug header in raw format
1380 */
1381
Michael Holzheu66a464d2005-06-25 14:55:33 -07001382static int
1383debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
1384 int area, debug_entry_t * entry, char *out_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 int rc;
1387
1388 rc = sizeof(debug_entry_t);
1389 memcpy(out_buf,entry,sizeof(debug_entry_t));
1390 return rc;
1391}
1392
1393/*
1394 * prints debug data in raw format
1395 */
1396
Michael Holzheu66a464d2005-06-25 14:55:33 -07001397static int
1398debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 char *out_buf, const char *in_buf)
1400{
1401 int rc;
1402
1403 rc = id->buf_size;
1404 memcpy(out_buf, in_buf, id->buf_size);
1405 return rc;
1406}
1407
1408/*
1409 * prints debug data in hex/ascii format
1410 */
1411
Michael Holzheu66a464d2005-06-25 14:55:33 -07001412static int
1413debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
1414 char *out_buf, const char *in_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 int i, rc = 0;
1417
1418 for (i = 0; i < id->buf_size; i++) {
1419 rc += sprintf(out_buf + rc, "%02x ",
1420 ((unsigned char *) in_buf)[i]);
1421 }
1422 rc += sprintf(out_buf + rc, "| ");
1423 for (i = 0; i < id->buf_size; i++) {
1424 unsigned char c = in_buf[i];
1425 if (!isprint(c))
1426 rc += sprintf(out_buf + rc, ".");
1427 else
1428 rc += sprintf(out_buf + rc, "%c", c);
1429 }
1430 rc += sprintf(out_buf + rc, "\n");
1431 return rc;
1432}
1433
1434/*
1435 * prints header for debug entry
1436 */
1437
Michael Holzheu66a464d2005-06-25 14:55:33 -07001438int
1439debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 int area, debug_entry_t * entry, char *out_buf)
1441{
Michael Holzheu942eaab2005-09-03 15:57:58 -07001442 struct timespec time_spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 unsigned long long time;
1444 char *except_str;
1445 unsigned long caller;
1446 int rc = 0;
1447 unsigned int level;
1448
1449 level = entry->id.fields.level;
1450 time = entry->id.stck;
1451 /* adjust todclock to 1970 */
1452 time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
Michael Holzheu942eaab2005-09-03 15:57:58 -07001453 tod_to_timeval(time, &time_spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (entry->id.fields.exception)
1456 except_str = "*";
1457 else
1458 except_str = "-";
1459 caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
1460 rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
Michael Holzheu942eaab2005-09-03 15:57:58 -07001461 area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 except_str, entry->id.fields.cpuid, (void *) caller);
1463 return rc;
1464}
1465
1466/*
1467 * prints debug data sprintf-formated:
1468 * debug_sprinf_event/exception calls must be used together with this view
1469 */
1470
1471#define DEBUG_SPRINTF_MAX_ARGS 10
1472
Michael Holzheu66a464d2005-06-25 14:55:33 -07001473static int
1474debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
1475 char *out_buf, debug_sprintf_entry_t *curr_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 int num_longs, num_used_args = 0,i, rc = 0;
1478 int index[DEBUG_SPRINTF_MAX_ARGS];
1479
1480 /* count of longs fit into one entry */
1481 num_longs = id->buf_size / sizeof(long);
1482
1483 if(num_longs < 1)
1484 goto out; /* bufsize of entry too small */
1485 if(num_longs == 1) {
1486 /* no args, we use only the string */
1487 strcpy(out_buf, curr_event->string);
1488 rc = strlen(curr_event->string);
1489 goto out;
1490 }
1491
1492 /* number of arguments used for sprintf (without the format string) */
1493 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1494
1495 memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1496
1497 for(i = 0; i < num_used_args; i++)
1498 index[i] = i;
1499
1500 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1501 curr_event->args[index[1]], curr_event->args[index[2]],
1502 curr_event->args[index[3]], curr_event->args[index[4]],
1503 curr_event->args[index[5]], curr_event->args[index[6]],
1504 curr_event->args[index[7]], curr_event->args[index[8]],
1505 curr_event->args[index[9]]);
1506
1507out:
1508
1509 return rc;
1510}
1511
1512/*
1513 * clean up module
1514 */
Michael Holzheu66a464d2005-06-25 14:55:33 -07001515void
1516__exit debug_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001518 debugfs_remove(debug_debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 unregister_sysctl_table(s390dbf_sysctl_header);
1520 return;
1521}
1522
1523/*
1524 * module definitions
1525 */
Michael Holzheu66a464d2005-06-25 14:55:33 -07001526postcore_initcall(debug_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527module_exit(debug_exit);
1528MODULE_LICENSE("GPL");
1529
1530EXPORT_SYMBOL(debug_register);
1531EXPORT_SYMBOL(debug_unregister);
1532EXPORT_SYMBOL(debug_set_level);
1533EXPORT_SYMBOL(debug_stop_all);
1534EXPORT_SYMBOL(debug_register_view);
1535EXPORT_SYMBOL(debug_unregister_view);
1536EXPORT_SYMBOL(debug_event_common);
1537EXPORT_SYMBOL(debug_exception_common);
1538EXPORT_SYMBOL(debug_hex_ascii_view);
1539EXPORT_SYMBOL(debug_raw_view);
1540EXPORT_SYMBOL(debug_dflt_header_fn);
1541EXPORT_SYMBOL(debug_sprintf_view);
1542EXPORT_SYMBOL(debug_sprintf_exception);
1543EXPORT_SYMBOL(debug_sprintf_event);