blob: 1e7d4ac7068bbfbd9b89d7823ac5ee085e2bf80d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/stddef.h>
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/slab.h>
17#include <linux/ctype.h>
18#include <linux/sysctl.h>
19#include <asm/uaccess.h>
20#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/init.h>
Michael Holzheu66a464d2005-06-25 14:55:33 -070023#include <linux/fs.h>
24#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/debug.h>
27
28#define DEBUG_PROLOG_ENTRY -1
29
Michael Holzheu66a464d2005-06-25 14:55:33 -070030#define ALL_AREAS 0 /* copy all debug areas */
31#define NO_AREAS 1 /* copy no debug areas */
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* typedefs */
34
35typedef struct file_private_info {
36 loff_t offset; /* offset of last read in file */
37 int act_area; /* number of last formated area */
Michael Holzheu66a464d2005-06-25 14:55:33 -070038 int act_page; /* act page in given area */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 int act_entry; /* last formated entry (offset */
40 /* relative to beginning of last */
Michael Holzheu66a464d2005-06-25 14:55:33 -070041 /* formated page) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 size_t act_entry_offset; /* up to this offset we copied */
43 /* in last read the last formated */
44 /* entry to userland */
45 char temp_buf[2048]; /* buffer for output */
46 debug_info_t *debug_info_org; /* original debug information */
47 debug_info_t *debug_info_snap; /* snapshot of debug information */
48 struct debug_view *view; /* used view of debug info */
49} file_private_info_t;
50
51typedef struct
52{
53 char *string;
54 /*
55 * This assumes that all args are converted into longs
56 * on L/390 this is the case for all types of parameter
57 * except of floats, and long long (32 bit)
Michael Holzheu66a464d2005-06-25 14:55:33 -070058 *
59 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 long args[0];
61} debug_sprintf_entry_t;
62
63
Michael Holzheu942eaab2005-09-03 15:57:58 -070064extern void tod_to_timeval(uint64_t todval, struct timespec *xtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* internal function prototyes */
67
68static int debug_init(void);
69static ssize_t debug_output(struct file *file, char __user *user_buf,
Michael Holzheu66a464d2005-06-25 14:55:33 -070070 size_t user_len, loff_t * offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static ssize_t debug_input(struct file *file, const char __user *user_buf,
Michael Holzheu66a464d2005-06-25 14:55:33 -070072 size_t user_len, loff_t * offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int debug_open(struct inode *inode, struct file *file);
74static int debug_close(struct inode *inode, struct file *file);
Michael Holzheu66a464d2005-06-25 14:55:33 -070075static debug_info_t* debug_info_create(char *name, int pages_per_area,
Michael Holzheu9637c3f2008-04-17 07:46:18 +020076 int nr_areas, int buf_size, mode_t mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static void debug_info_get(debug_info_t *);
78static void debug_info_put(debug_info_t *);
79static int debug_prolog_level_fn(debug_info_t * id,
Michael Holzheu66a464d2005-06-25 14:55:33 -070080 struct debug_view *view, char *out_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,
Michael Holzheu66a464d2005-06-25 14:55:33 -070082 struct file *file, const char __user *user_buf,
83 size_t user_buf_size, loff_t * offset);
84static int debug_prolog_pages_fn(debug_info_t * id,
85 struct debug_view *view, char *out_buf);
86static int debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
87 struct file *file, const char __user *user_buf,
88 size_t user_buf_size, loff_t * offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
Michael Holzheu66a464d2005-06-25 14:55:33 -070090 struct file *file, const char __user *user_buf,
91 size_t user_buf_size, loff_t * offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
Michael Holzheu66a464d2005-06-25 14:55:33 -070093 char *out_buf, const char *in_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int debug_raw_format_fn(debug_info_t * id,
Michael Holzheu66a464d2005-06-25 14:55:33 -070095 struct debug_view *view, char *out_buf,
96 const char *in_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
Michael Holzheu66a464d2005-06-25 14:55:33 -070098 int area, debug_entry_t * entry, char *out_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100static int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
Michael Holzheu66a464d2005-06-25 14:55:33 -0700101 char *out_buf, debug_sprintf_entry_t *curr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* globals */
104
105struct debug_view debug_raw_view = {
106 "raw",
107 NULL,
108 &debug_raw_header_fn,
109 &debug_raw_format_fn,
110 NULL,
111 NULL
112};
113
114struct debug_view debug_hex_ascii_view = {
115 "hex_ascii",
116 NULL,
117 &debug_dflt_header_fn,
118 &debug_hex_ascii_format_fn,
119 NULL,
120 NULL
121};
122
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100123static struct debug_view debug_level_view = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 "level",
125 &debug_prolog_level_fn,
126 NULL,
127 NULL,
128 &debug_input_level_fn,
129 NULL
130};
131
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100132static struct debug_view debug_pages_view = {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700133 "pages",
134 &debug_prolog_pages_fn,
135 NULL,
136 NULL,
137 &debug_input_pages_fn,
138 NULL
139};
140
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100141static struct debug_view debug_flush_view = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 "flush",
143 NULL,
144 NULL,
145 NULL,
146 &debug_input_flush_fn,
147 NULL
148};
149
150struct debug_view debug_sprintf_view = {
151 "sprintf",
152 NULL,
153 &debug_dflt_header_fn,
154 (debug_format_proc_t*)&debug_sprintf_format_fn,
155 NULL,
156 NULL
157};
158
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100159/* used by dump analysis tools to determine version of debug feature */
Heiko Carstensa8061702008-04-17 07:46:26 +0200160static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/* static globals */
163
164static debug_info_t *debug_area_first = NULL;
165static debug_info_t *debug_area_last = NULL;
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200166static DEFINE_MUTEX(debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168static int initialized;
169
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800170static const struct file_operations debug_file_ops = {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700171 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .read = debug_output,
Michael Holzheu66a464d2005-06-25 14:55:33 -0700173 .write = debug_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .open = debug_open,
175 .release = debug_close,
176};
177
Michael Holzheu66a464d2005-06-25 14:55:33 -0700178static struct dentry *debug_debugfs_root_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180/* functions */
181
182/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700183 * debug_areas_alloc
184 * - Debug areas are implemented as a threedimensonal array:
185 * areas[areanumber][pagenumber][pageoffset]
186 */
187
188static debug_entry_t***
189debug_areas_alloc(int pages_per_area, int nr_areas)
190{
191 debug_entry_t*** areas;
192 int i,j;
193
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800194 areas = kmalloc(nr_areas *
Michael Holzheu66a464d2005-06-25 14:55:33 -0700195 sizeof(debug_entry_t**),
196 GFP_KERNEL);
197 if (!areas)
198 goto fail_malloc_areas;
199 for (i = 0; i < nr_areas; i++) {
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800200 areas[i] = kmalloc(pages_per_area *
Michael Holzheu66a464d2005-06-25 14:55:33 -0700201 sizeof(debug_entry_t*),GFP_KERNEL);
202 if (!areas[i]) {
203 goto fail_malloc_areas2;
204 }
205 for(j = 0; j < pages_per_area; j++) {
Eric Sesterhennfb630512006-03-24 03:15:31 -0800206 areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700207 if(!areas[i][j]) {
208 for(j--; j >=0 ; j--) {
209 kfree(areas[i][j]);
210 }
211 kfree(areas[i]);
212 goto fail_malloc_areas2;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700213 }
214 }
215 }
216 return areas;
217
218fail_malloc_areas2:
219 for(i--; i >= 0; i--){
220 for(j=0; j < pages_per_area;j++){
221 kfree(areas[i][j]);
222 }
223 kfree(areas[i]);
224 }
225 kfree(areas);
226fail_malloc_areas:
227 return NULL;
228
229}
230
231
232/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * debug_info_alloc
234 * - alloc new debug-info
235 */
236
Michael Holzheu66a464d2005-06-25 14:55:33 -0700237static debug_info_t*
238debug_info_alloc(char *name, int pages_per_area, int nr_areas, int buf_size,
239 int level, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 debug_info_t* rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* alloc everything */
244
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800245 rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if(!rc)
247 goto fail_malloc_rc;
Eric Sesterhennfb630512006-03-24 03:15:31 -0800248 rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700249 if(!rc->active_entries)
250 goto fail_malloc_active_entries;
Eric Sesterhennfb630512006-03-24 03:15:31 -0800251 rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700252 if(!rc->active_pages)
253 goto fail_malloc_active_pages;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700254 if((mode == ALL_AREAS) && (pages_per_area != 0)){
255 rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
256 if(!rc->areas)
257 goto fail_malloc_areas;
258 } else {
259 rc->areas = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
262 /* initialize members */
263
264 spin_lock_init(&rc->lock);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700265 rc->pages_per_area = pages_per_area;
266 rc->nr_areas = nr_areas;
267 rc->active_area = 0;
268 rc->level = level;
269 rc->buf_size = buf_size;
270 rc->entry_size = sizeof(debug_entry_t) + buf_size;
Jean Delvare20cb9e72007-03-19 13:18:53 +0100271 strlcpy(rc->name, name, sizeof(rc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
Michael Holzheu66a464d2005-06-25 14:55:33 -0700273 memset(rc->debugfs_entries, 0 ,DEBUG_MAX_VIEWS *
274 sizeof(struct dentry*));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 atomic_set(&(rc->ref_count), 0);
276
277 return rc;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279fail_malloc_areas:
Michael Holzheu66a464d2005-06-25 14:55:33 -0700280 kfree(rc->active_pages);
281fail_malloc_active_pages:
282 kfree(rc->active_entries);
283fail_malloc_active_entries:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 kfree(rc);
285fail_malloc_rc:
286 return NULL;
287}
288
289/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700290 * debug_areas_free
291 * - free all debug areas
292 */
293
294static void
295debug_areas_free(debug_info_t* db_info)
296{
297 int i,j;
298
299 if(!db_info->areas)
300 return;
301 for (i = 0; i < db_info->nr_areas; i++) {
302 for(j = 0; j < db_info->pages_per_area; j++) {
303 kfree(db_info->areas[i][j]);
304 }
305 kfree(db_info->areas[i]);
306 }
307 kfree(db_info->areas);
308 db_info->areas = NULL;
309}
310
311/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * debug_info_free
313 * - free memory debug-info
314 */
315
Michael Holzheu66a464d2005-06-25 14:55:33 -0700316static void
317debug_info_free(debug_info_t* db_info){
318 debug_areas_free(db_info);
319 kfree(db_info->active_entries);
320 kfree(db_info->active_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 kfree(db_info);
322}
323
324/*
325 * debug_info_create
326 * - create new debug-info
327 */
328
Michael Holzheu66a464d2005-06-25 14:55:33 -0700329static debug_info_t*
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200330debug_info_create(char *name, int pages_per_area, int nr_areas, int buf_size,
331 mode_t mode)
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 Holzheu9637c3f2008-04-17 07:46:18 +0200340 rc->mode = mode & ~S_IFMT;
341
Michael Holzheu66a464d2005-06-25 14:55:33 -0700342 /* create root directory */
343 rc->debugfs_root_entry = debugfs_create_dir(rc->name,
344 debug_debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* append new element to linked list */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700347 if (!debug_area_first) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* first element in list */
349 debug_area_first = rc;
350 rc->prev = NULL;
351 } else {
352 /* append element to end of list */
353 debug_area_last->next = rc;
354 rc->prev = debug_area_last;
355 }
356 debug_area_last = rc;
357 rc->next = NULL;
358
359 debug_info_get(rc);
360out:
361 return rc;
362}
363
364/*
365 * debug_info_copy
366 * - copy debug-info
367 */
368
Michael Holzheu66a464d2005-06-25 14:55:33 -0700369static debug_info_t*
370debug_info_copy(debug_info_t* in, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700372 int i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 debug_info_t* rc;
Michael Holzheu942eaab2005-09-03 15:57:58 -0700374 unsigned long flags;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700375
Michael Holzheu942eaab2005-09-03 15:57:58 -0700376 /* get a consistent copy of the debug areas */
377 do {
378 rc = debug_info_alloc(in->name, in->pages_per_area,
379 in->nr_areas, in->buf_size, in->level, mode);
380 spin_lock_irqsave(&in->lock, flags);
381 if(!rc)
382 goto out;
383 /* has something changed in the meantime ? */
384 if((rc->pages_per_area == in->pages_per_area) &&
385 (rc->nr_areas == in->nr_areas)) {
386 break;
387 }
388 spin_unlock_irqrestore(&in->lock, flags);
389 debug_info_free(rc);
390 } while (1);
391
Michael Holzheu66a464d2005-06-25 14:55:33 -0700392 if(!rc || (mode == NO_AREAS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto out;
394
395 for(i = 0; i < in->nr_areas; i++){
Michael Holzheu66a464d2005-06-25 14:55:33 -0700396 for(j = 0; j < in->pages_per_area; j++) {
397 memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400out:
Michael Holzheu942eaab2005-09-03 15:57:58 -0700401 spin_unlock_irqrestore(&in->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return rc;
403}
404
405/*
406 * debug_info_get
407 * - increments reference count for debug-info
408 */
409
Michael Holzheu66a464d2005-06-25 14:55:33 -0700410static void
411debug_info_get(debug_info_t * db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 if (db_info)
414 atomic_inc(&db_info->ref_count);
415}
416
417/*
418 * debug_info_put:
419 * - decreases reference count for debug-info and frees it if necessary
420 */
421
Michael Holzheu66a464d2005-06-25 14:55:33 -0700422static void
423debug_info_put(debug_info_t *db_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 int i;
426
427 if (!db_info)
428 return;
429 if (atomic_dec_and_test(&db_info->ref_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -0700431 if (!db_info->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 continue;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700433 debugfs_remove(db_info->debugfs_entries[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700435 debugfs_remove(db_info->debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if(db_info == debug_area_first)
437 debug_area_first = db_info->next;
438 if(db_info == debug_area_last)
439 debug_area_last = db_info->prev;
440 if(db_info->prev) db_info->prev->next = db_info->next;
441 if(db_info->next) db_info->next->prev = db_info->prev;
442 debug_info_free(db_info);
443 }
444}
445
446/*
447 * debug_format_entry:
448 * - format one debug entry and return size of formated data
449 */
450
Michael Holzheu66a464d2005-06-25 14:55:33 -0700451static int
452debug_format_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 debug_info_t *id_snap = p_info->debug_info_snap;
455 struct debug_view *view = p_info->view;
456 debug_entry_t *act_entry;
457 size_t len = 0;
458 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
459 /* print prolog */
460 if (view->prolog_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700461 len += view->prolog_proc(id_snap,view,p_info->temp_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto out;
463 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700464 if (!id_snap->areas) /* this is true, if we have a prolog only view */
465 goto out; /* or if 'pages_per_area' is 0 */
466 act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area]
467 [p_info->act_page] + p_info->act_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if (act_entry->id.stck == 0LL)
470 goto out; /* empty entry */
471 if (view->header_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700472 len += view->header_proc(id_snap, view, p_info->act_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 act_entry, p_info->temp_buf + len);
474 if (view->format_proc)
Michael Holzheu66a464d2005-06-25 14:55:33 -0700475 len += view->format_proc(id_snap, view, p_info->temp_buf + len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 DEBUG_DATA(act_entry));
Michael Holzheu66a464d2005-06-25 14:55:33 -0700477out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return len;
479}
480
481/*
482 * debug_next_entry:
483 * - goto next entry in p_info
484 */
485
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800486static inline int
Michael Holzheu66a464d2005-06-25 14:55:33 -0700487debug_next_entry(file_private_info_t *p_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700489 debug_info_t *id;
490
491 id = p_info->debug_info_snap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
493 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700494 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto out;
496 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700497 if(!id->areas)
498 return 1;
499 p_info->act_entry += id->entry_size;
500 /* switch to next page, if we reached the end of the page */
501 if (p_info->act_entry > (PAGE_SIZE - id->entry_size)){
502 /* next page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 p_info->act_entry = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700504 p_info->act_page += 1;
505 if((p_info->act_page % id->pages_per_area) == 0) {
506 /* next area */
507 p_info->act_area++;
508 p_info->act_page=0;
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if(p_info->act_area >= id->nr_areas)
511 return 1;
512 }
513out:
514 return 0;
515}
516
517/*
518 * debug_output:
519 * - called for user read()
520 * - copies formated debug entries to the user buffer
521 */
522
Michael Holzheu66a464d2005-06-25 14:55:33 -0700523static ssize_t
524debug_output(struct file *file, /* file descriptor */
525 char __user *user_buf, /* user buffer */
526 size_t len, /* length of buffer */
527 loff_t *offset) /* offset in the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 size_t count = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700530 size_t entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 file_private_info_t *p_info;
532
533 p_info = ((file_private_info_t *) file->private_data);
534 if (*offset != p_info->offset)
535 return -EPIPE;
536 if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 entry_offset = p_info->act_entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 while(count < len){
Michael Holzheu66a464d2005-06-25 14:55:33 -0700540 int formatted_line_size;
541 int formatted_line_residue;
542 int user_buf_residue;
543 size_t copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Michael Holzheu66a464d2005-06-25 14:55:33 -0700545 formatted_line_size = debug_format_entry(p_info);
546 formatted_line_residue = formatted_line_size - entry_offset;
547 user_buf_residue = len-count;
548 copy_size = min(user_buf_residue, formatted_line_residue);
549 if(copy_size){
550 if (copy_to_user(user_buf + count, p_info->temp_buf
551 + entry_offset, copy_size))
552 return -EFAULT;
553 count += copy_size;
554 entry_offset += copy_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700556 if(copy_size == formatted_line_residue){
557 entry_offset = 0;
558 if(debug_next_entry(p_info))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562out:
563 p_info->offset = *offset + count;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700564 p_info->act_entry_offset = entry_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 *offset = p_info->offset;
566 return count;
567}
568
569/*
570 * debug_input:
571 * - called for user write()
572 * - calls input function of view
573 */
574
Michael Holzheu66a464d2005-06-25 14:55:33 -0700575static ssize_t
576debug_input(struct file *file, const char __user *user_buf, size_t length,
577 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 int rc = 0;
580 file_private_info_t *p_info;
581
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200582 mutex_lock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 p_info = ((file_private_info_t *) file->private_data);
584 if (p_info->view->input_proc)
585 rc = p_info->view->input_proc(p_info->debug_info_org,
586 p_info->view, file, user_buf,
587 length, offset);
588 else
589 rc = -EPERM;
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200590 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return rc; /* number of input characters */
592}
593
594/*
595 * debug_open:
596 * - called for user open()
597 * - copies formated output to private_data area of the file
598 * handle
599 */
600
Michael Holzheu66a464d2005-06-25 14:55:33 -0700601static int
602debug_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 int i = 0, rc = 0;
605 file_private_info_t *p_info;
606 debug_info_t *debug_info, *debug_info_snapshot;
607
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200608 mutex_lock(&debug_mutex);
Josef Sipekd20343e2006-12-08 02:37:35 -0800609 debug_info = file->f_path.dentry->d_inode->i_private;
Michael Holzheu942eaab2005-09-03 15:57:58 -0700610 /* find debug view */
611 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
612 if (!debug_info->views[i])
613 continue;
614 else if (debug_info->debugfs_entries[i] ==
Josef Sipekd20343e2006-12-08 02:37:35 -0800615 file->f_path.dentry) {
Michael Holzheu942eaab2005-09-03 15:57:58 -0700616 goto found; /* found view ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619 /* no entry found */
620 rc = -EINVAL;
621 goto out;
622
Michael Holzheu66a464d2005-06-25 14:55:33 -0700623found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Michael Holzheu66a464d2005-06-25 14:55:33 -0700625 /* Make snapshot of current debug areas to get it consistent. */
626 /* To copy all the areas is only needed, if we have a view which */
627 /* formats the debug areas. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Michael Holzheu66a464d2005-06-25 14:55:33 -0700629 if(!debug_info->views[i]->format_proc &&
630 !debug_info->views[i]->header_proc){
631 debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
632 } else {
633 debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if(!debug_info_snapshot){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 rc = -ENOMEM;
638 goto out;
639 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800640 p_info = kmalloc(sizeof(file_private_info_t),
Michael Holzheu66a464d2005-06-25 14:55:33 -0700641 GFP_KERNEL);
642 if(!p_info){
643 if(debug_info_snapshot)
644 debug_info_free(debug_info_snapshot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 rc = -ENOMEM;
646 goto out;
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 p_info->offset = 0;
649 p_info->debug_info_snap = debug_info_snapshot;
650 p_info->debug_info_org = debug_info;
651 p_info->view = debug_info->views[i];
652 p_info->act_area = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700653 p_info->act_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 p_info->act_entry = DEBUG_PROLOG_ENTRY;
655 p_info->act_entry_offset = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700656 file->private_data = p_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 debug_info_get(debug_info);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700658out:
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200659 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return rc;
661}
662
663/*
664 * debug_close:
665 * - called for user close()
666 * - deletes private_data area of the file handle
667 */
668
Michael Holzheu66a464d2005-06-25 14:55:33 -0700669static int
670debug_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 file_private_info_t *p_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 p_info = (file_private_info_t *) file->private_data;
Michael Holzheu66a464d2005-06-25 14:55:33 -0700674 if(p_info->debug_info_snap)
675 debug_info_free(p_info->debug_info_snap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 debug_info_put(p_info->debug_info_org);
677 kfree(file->private_data);
678 return 0; /* success */
679}
680
681/*
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200682 * debug_register_mode:
683 * - Creates and initializes debug area for the caller
684 * The mode parameter allows to specify access rights for the s390dbf files
685 * - Returns handle for debug area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
687
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200688debug_info_t *debug_register_mode(char *name, int pages_per_area, int nr_areas,
689 int buf_size, mode_t mode, uid_t uid,
690 gid_t gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 debug_info_t *rc = NULL;
693
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200694 /* Since debugfs currently does not support uid/gid other than root, */
695 /* we do not allow gid/uid != 0 until we get support for that. */
696 if ((uid != 0) || (gid != 0))
697 printk(KERN_WARNING "debug: Warning - Currently only uid/gid "
698 "= 0 are supported. Using root as owner now!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!initialized)
700 BUG();
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200701 mutex_lock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /* create new debug_info */
704
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200705 rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if(!rc)
707 goto out;
708 debug_register_view(rc, &debug_level_view);
709 debug_register_view(rc, &debug_flush_view);
Michael Holzheu66a464d2005-06-25 14:55:33 -0700710 debug_register_view(rc, &debug_pages_view);
711out:
712 if (!rc){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 printk(KERN_ERR "debug: debug_register failed for %s\n",name);
714 }
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200715 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return rc;
717}
Michael Holzheu9637c3f2008-04-17 07:46:18 +0200718EXPORT_SYMBOL(debug_register_mode);
719
720/*
721 * debug_register:
722 * - creates and initializes debug area for the caller
723 * - returns handle for debug area
724 */
725
726debug_info_t *debug_register(char *name, int pages_per_area, int nr_areas,
727 int buf_size)
728{
729 return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
730 S_IRUSR | S_IWUSR, 0, 0);
731}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733/*
734 * debug_unregister:
735 * - give back debug area
736 */
737
Michael Holzheu66a464d2005-06-25 14:55:33 -0700738void
739debug_unregister(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 if (!id)
742 goto out;
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200743 mutex_lock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 debug_info_put(id);
Christoph Hellwige11f0d02007-05-31 17:38:03 +0200745 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Michael Holzheu66a464d2005-06-25 14:55:33 -0700747out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return;
749}
750
751/*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700752 * debug_set_size:
753 * - set area size (number of pages) and number of areas
754 */
755static int
756debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
757{
758 unsigned long flags;
759 debug_entry_t *** new_areas;
760 int rc=0;
761
762 if(!id || (nr_areas <= 0) || (pages_per_area < 0))
763 return -EINVAL;
764 if(pages_per_area > 0){
765 new_areas = debug_areas_alloc(pages_per_area, nr_areas);
766 if(!new_areas) {
767 printk(KERN_WARNING "debug: could not allocate memory "\
768 "for pagenumber: %i\n",pages_per_area);
769 rc = -ENOMEM;
770 goto out;
771 }
772 } else {
773 new_areas = NULL;
774 }
775 spin_lock_irqsave(&id->lock,flags);
776 debug_areas_free(id);
777 id->areas = new_areas;
778 id->nr_areas = nr_areas;
779 id->pages_per_area = pages_per_area;
780 id->active_area = 0;
781 memset(id->active_entries,0,sizeof(int)*id->nr_areas);
782 memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
783 spin_unlock_irqrestore(&id->lock,flags);
784 printk(KERN_INFO "debug: %s: set new size (%i pages)\n"\
785 ,id->name, pages_per_area);
786out:
787 return rc;
788}
789
790/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * debug_set_level:
792 * - set actual debug level
793 */
794
Michael Holzheu66a464d2005-06-25 14:55:33 -0700795void
796debug_set_level(debug_info_t* id, int new_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 unsigned long flags;
799 if(!id)
800 return;
801 spin_lock_irqsave(&id->lock,flags);
802 if(new_level == DEBUG_OFF_LEVEL){
803 id->level = DEBUG_OFF_LEVEL;
804 printk(KERN_INFO "debug: %s: switched off\n",id->name);
805 } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
806 printk(KERN_INFO
807 "debug: %s: level %i is out of range (%i - %i)\n",
808 id->name, new_level, 0, DEBUG_MAX_LEVEL);
809 } else {
810 id->level = new_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 spin_unlock_irqrestore(&id->lock,flags);
813}
814
815
816/*
817 * proceed_active_entry:
818 * - set active entry to next in the ring buffer
819 */
820
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800821static inline void
Michael Holzheu66a464d2005-06-25 14:55:33 -0700822proceed_active_entry(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700824 if ((id->active_entries[id->active_area] += id->entry_size)
825 > (PAGE_SIZE - id->entry_size)){
826 id->active_entries[id->active_area] = 0;
827 id->active_pages[id->active_area] =
828 (id->active_pages[id->active_area] + 1) %
829 id->pages_per_area;
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833/*
834 * proceed_active_area:
835 * - set active area to next in the ring buffer
836 */
837
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800838static inline void
Michael Holzheu66a464d2005-06-25 14:55:33 -0700839proceed_active_area(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 id->active_area++;
842 id->active_area = id->active_area % id->nr_areas;
843}
844
845/*
846 * get_active_entry:
847 */
848
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800849static inline debug_entry_t*
Michael Holzheu66a464d2005-06-25 14:55:33 -0700850get_active_entry(debug_info_t * id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Michael Holzheu66a464d2005-06-25 14:55:33 -0700852 return (debug_entry_t *) (((char *) id->areas[id->active_area]
853 [id->active_pages[id->active_area]]) +
854 id->active_entries[id->active_area]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857/*
858 * debug_finish_entry:
859 * - set timestamp, caller address, cpu number etc.
860 */
861
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800862static inline void
Michael Holzheu66a464d2005-06-25 14:55:33 -0700863debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
864 int exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Michael Holzheu942eaab2005-09-03 15:57:58 -0700866 active->id.stck = get_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 active->id.fields.cpuid = smp_processor_id();
868 active->caller = __builtin_return_address(0);
869 active->id.fields.exception = exception;
870 active->id.fields.level = level;
871 proceed_active_entry(id);
872 if(exception)
873 proceed_active_area(id);
874}
875
876static int debug_stoppable=1;
877static int debug_active=1;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879#define CTL_S390DBF_STOPPABLE 5678
880#define CTL_S390DBF_ACTIVE 5679
881
882/*
883 * proc handler for the running debug_active sysctl
884 * always allow read, allow write only if debug_stoppable is set or
885 * if debug_active is already off
886 */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700887static int
888s390dbf_procactive(ctl_table *table, int write, struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 void __user *buffer, size_t *lenp, loff_t *ppos)
890{
891 if (!write || debug_stoppable || !debug_active)
892 return proc_dointvec(table, write, filp, buffer, lenp, ppos);
893 else
894 return 0;
895}
896
897
898static struct ctl_table s390dbf_table[] = {
899 {
900 .ctl_name = CTL_S390DBF_STOPPABLE,
901 .procname = "debug_stoppable",
902 .data = &debug_stoppable,
903 .maxlen = sizeof(int),
904 .mode = S_IRUGO | S_IWUSR,
905 .proc_handler = &proc_dointvec,
906 .strategy = &sysctl_intvec,
907 },
908 {
909 .ctl_name = CTL_S390DBF_ACTIVE,
910 .procname = "debug_active",
911 .data = &debug_active,
912 .maxlen = sizeof(int),
913 .mode = S_IRUGO | S_IWUSR,
914 .proc_handler = &s390dbf_procactive,
915 .strategy = &sysctl_intvec,
916 },
917 { .ctl_name = 0 }
918};
919
920static struct ctl_table s390dbf_dir_table[] = {
921 {
922 .ctl_name = CTL_S390DBF,
923 .procname = "s390dbf",
924 .maxlen = 0,
925 .mode = S_IRUGO | S_IXUGO,
926 .child = s390dbf_table,
927 },
928 { .ctl_name = 0 }
929};
930
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100931static struct ctl_table_header *s390dbf_sysctl_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Michael Holzheu66a464d2005-06-25 14:55:33 -0700933void
934debug_stop_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 if (debug_stoppable)
937 debug_active = 0;
938}
939
940
941/*
942 * debug_event_common:
943 * - write debug entry with given size
944 */
945
Michael Holzheu66a464d2005-06-25 14:55:33 -0700946debug_entry_t*
947debug_event_common(debug_info_t * id, int level, const void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 unsigned long flags;
950 debug_entry_t *active;
951
Michael Holzheu66a464d2005-06-25 14:55:33 -0700952 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return NULL;
954 spin_lock_irqsave(&id->lock, flags);
955 active = get_active_entry(id);
956 memset(DEBUG_DATA(active), 0, id->buf_size);
957 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
958 debug_finish_entry(id, active, level, 0);
959 spin_unlock_irqrestore(&id->lock, flags);
960
961 return active;
962}
963
964/*
965 * debug_exception_common:
966 * - write debug entry with given size and switch to next debug area
967 */
968
Michael Holzheu66a464d2005-06-25 14:55:33 -0700969debug_entry_t
970*debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 unsigned long flags;
973 debug_entry_t *active;
974
Michael Holzheu66a464d2005-06-25 14:55:33 -0700975 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return NULL;
977 spin_lock_irqsave(&id->lock, flags);
978 active = get_active_entry(id);
979 memset(DEBUG_DATA(active), 0, id->buf_size);
980 memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
981 debug_finish_entry(id, active, level, 1);
982 spin_unlock_irqrestore(&id->lock, flags);
983
984 return active;
985}
986
987/*
988 * counts arguments in format string for sprintf view
989 */
990
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800991static inline int
Michael Holzheu66a464d2005-06-25 14:55:33 -0700992debug_count_numargs(char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 int numargs=0;
995
996 while(*string) {
997 if(*string++=='%')
998 numargs++;
999 }
1000 return(numargs);
1001}
1002
1003/*
1004 * debug_sprintf_event:
1005 */
1006
Michael Holzheu66a464d2005-06-25 14:55:33 -07001007debug_entry_t*
1008debug_sprintf_event(debug_info_t* id, int level,char *string,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 va_list ap;
1011 int numargs,idx;
1012 unsigned long flags;
1013 debug_sprintf_entry_t *curr_event;
1014 debug_entry_t *active;
1015
1016 if((!id) || (level > id->level))
1017 return NULL;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001018 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return NULL;
1020 numargs=debug_count_numargs(string);
1021
1022 spin_lock_irqsave(&id->lock, flags);
1023 active = get_active_entry(id);
1024 curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
1025 va_start(ap,string);
1026 curr_event->string=string;
1027 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1028 curr_event->args[idx]=va_arg(ap,long);
1029 va_end(ap);
1030 debug_finish_entry(id, active, level, 0);
1031 spin_unlock_irqrestore(&id->lock, flags);
1032
1033 return active;
1034}
1035
1036/*
1037 * debug_sprintf_exception:
1038 */
1039
Michael Holzheu66a464d2005-06-25 14:55:33 -07001040debug_entry_t*
1041debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 va_list ap;
1044 int numargs,idx;
1045 unsigned long flags;
1046 debug_sprintf_entry_t *curr_event;
1047 debug_entry_t *active;
1048
1049 if((!id) || (level > id->level))
1050 return NULL;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001051 if (!debug_active || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return NULL;
1053
1054 numargs=debug_count_numargs(string);
1055
1056 spin_lock_irqsave(&id->lock, flags);
1057 active = get_active_entry(id);
1058 curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
1059 va_start(ap,string);
1060 curr_event->string=string;
1061 for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
1062 curr_event->args[idx]=va_arg(ap,long);
1063 va_end(ap);
1064 debug_finish_entry(id, active, level, 1);
1065 spin_unlock_irqrestore(&id->lock, flags);
1066
1067 return active;
1068}
1069
1070/*
1071 * debug_init:
1072 * - is called exactly once to initialize the debug feature
1073 */
1074
Michael Holzheu66a464d2005-06-25 14:55:33 -07001075static int
1076__init debug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 int rc = 0;
1079
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08001080 s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
Christoph Hellwige11f0d02007-05-31 17:38:03 +02001081 mutex_lock(&debug_mutex);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001082 debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 printk(KERN_INFO "debug: Initialization complete\n");
1084 initialized = 1;
Christoph Hellwige11f0d02007-05-31 17:38:03 +02001085 mutex_unlock(&debug_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 return rc;
1088}
1089
1090/*
1091 * debug_register_view:
1092 */
1093
Michael Holzheu66a464d2005-06-25 14:55:33 -07001094int
1095debug_register_view(debug_info_t * id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 int rc = 0;
1098 int i;
1099 unsigned long flags;
Michael Holzheu9637c3f2008-04-17 07:46:18 +02001100 mode_t mode;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001101 struct dentry *pde;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (!id)
1104 goto out;
Michael Holzheu9637c3f2008-04-17 07:46:18 +02001105 mode = (id->mode | S_IFREG) & ~S_IXUGO;
1106 if (!(view->prolog_proc || view->format_proc || view->header_proc))
1107 mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
1108 if (!view->input_proc)
1109 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001110 pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
Michael Holzheu942eaab2005-09-03 15:57:58 -07001111 id , &debug_file_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!pde){
Michael Holzheu66a464d2005-06-25 14:55:33 -07001113 printk(KERN_WARNING "debug: debugfs_create_file() failed!"\
1114 " Cannot register view %s/%s\n", id->name,view->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 rc = -1;
1116 goto out;
1117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 spin_lock_irqsave(&id->lock, flags);
1119 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001120 if (!id->views[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 break;
1122 }
1123 if (i == DEBUG_MAX_VIEWS) {
1124 printk(KERN_WARNING "debug: cannot register view %s/%s\n",
1125 id->name,view->name);
1126 printk(KERN_WARNING
1127 "debug: maximum number of views reached (%i)!\n", i);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001128 debugfs_remove(pde);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 rc = -1;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001130 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 id->views[i] = view;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001132 id->debugfs_entries[i] = pde;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001135out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return rc;
1137}
1138
1139/*
1140 * debug_unregister_view:
1141 */
1142
Michael Holzheu66a464d2005-06-25 14:55:33 -07001143int
1144debug_unregister_view(debug_info_t * id, struct debug_view *view)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 int rc = 0;
1147 int i;
1148 unsigned long flags;
1149
1150 if (!id)
1151 goto out;
1152 spin_lock_irqsave(&id->lock, flags);
1153 for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
1154 if (id->views[i] == view)
1155 break;
1156 }
1157 if (i == DEBUG_MAX_VIEWS)
1158 rc = -1;
1159 else {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001160 debugfs_remove(id->debugfs_entries[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 id->views[i] = NULL;
1162 rc = 0;
1163 }
1164 spin_unlock_irqrestore(&id->lock, flags);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001165out:
1166 return rc;
1167}
1168
1169static inline char *
1170debug_get_user_string(const char __user *user_buf, size_t user_len)
1171{
1172 char* buffer;
1173
1174 buffer = kmalloc(user_len + 1, GFP_KERNEL);
1175 if (!buffer)
1176 return ERR_PTR(-ENOMEM);
1177 if (copy_from_user(buffer, user_buf, user_len) != 0) {
1178 kfree(buffer);
1179 return ERR_PTR(-EFAULT);
1180 }
1181 /* got the string, now strip linefeed. */
1182 if (buffer[user_len - 1] == '\n')
1183 buffer[user_len - 1] = 0;
1184 else
1185 buffer[user_len] = 0;
1186 return buffer;
1187}
1188
1189static inline int
1190debug_get_uint(char *buf)
1191{
1192 int rc;
1193
1194 for(; isspace(*buf); buf++);
1195 rc = simple_strtoul(buf, &buf, 10);
1196 if(*buf){
1197 printk("debug: no integer specified!\n");
1198 rc = -EINVAL;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return rc;
1201}
1202
1203/*
1204 * functions for debug-views
1205 ***********************************
1206*/
1207
1208/*
1209 * prints out actual debug level
1210 */
1211
Michael Holzheu66a464d2005-06-25 14:55:33 -07001212static int
1213debug_prolog_pages_fn(debug_info_t * id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 struct debug_view *view, char *out_buf)
1215{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001216 return sprintf(out_buf, "%i\n", id->pages_per_area);
1217}
1218
1219/*
1220 * reads new size (number of pages per debug area)
1221 */
1222
1223static int
1224debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
1225 struct file *file, const char __user *user_buf,
1226 size_t user_len, loff_t * offset)
1227{
1228 char *str;
1229 int rc,new_pages;
1230
1231 if (user_len > 0x10000)
1232 user_len = 0x10000;
1233 if (*offset != 0){
1234 rc = -EPIPE;
1235 goto out;
1236 }
1237 str = debug_get_user_string(user_buf,user_len);
1238 if(IS_ERR(str)){
1239 rc = PTR_ERR(str);
1240 goto out;
1241 }
1242 new_pages = debug_get_uint(str);
1243 if(new_pages < 0){
1244 rc = -EINVAL;
1245 goto free_str;
1246 }
1247 rc = debug_set_size(id,id->nr_areas, new_pages);
1248 if(rc != 0){
1249 rc = -EINVAL;
1250 goto free_str;
1251 }
1252 rc = user_len;
1253free_str:
1254 kfree(str);
1255out:
1256 *offset += user_len;
1257 return rc; /* number of input characters */
1258}
1259
1260/*
1261 * prints out actual debug level
1262 */
1263
1264static int
1265debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
1266{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int rc = 0;
1268
Michael Holzheu66a464d2005-06-25 14:55:33 -07001269 if(id->level == DEBUG_OFF_LEVEL) {
1270 rc = sprintf(out_buf,"-\n");
1271 }
1272 else {
1273 rc = sprintf(out_buf, "%i\n", id->level);
1274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return rc;
1276}
1277
1278/*
1279 * reads new debug level
1280 */
1281
Michael Holzheu66a464d2005-06-25 14:55:33 -07001282static int
1283debug_input_level_fn(debug_info_t * id, struct debug_view *view,
1284 struct file *file, const char __user *user_buf,
1285 size_t user_len, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001287 char *str;
1288 int rc,new_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Michael Holzheu66a464d2005-06-25 14:55:33 -07001290 if (user_len > 0x10000)
1291 user_len = 0x10000;
1292 if (*offset != 0){
1293 rc = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto out;
1295 }
Michael Holzheu66a464d2005-06-25 14:55:33 -07001296 str = debug_get_user_string(user_buf,user_len);
1297 if(IS_ERR(str)){
1298 rc = PTR_ERR(str);
1299 goto out;
1300 }
1301 if(str[0] == '-'){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 debug_set_level(id, DEBUG_OFF_LEVEL);
Michael Holzheu66a464d2005-06-25 14:55:33 -07001303 rc = user_len;
1304 goto free_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 } else {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001306 new_level = debug_get_uint(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Michael Holzheu66a464d2005-06-25 14:55:33 -07001308 if(new_level < 0) {
1309 printk(KERN_INFO "debug: level `%s` is not valid\n", str);
1310 rc = -EINVAL;
1311 } else {
1312 debug_set_level(id, new_level);
1313 rc = user_len;
1314 }
1315free_str:
1316 kfree(str);
1317out:
1318 *offset += user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return rc; /* number of input characters */
1320}
1321
1322
1323/*
1324 * flushes debug areas
1325 */
1326
Heiko Carstens2b67fc42007-02-05 21:16:47 +01001327static void debug_flush(debug_info_t* id, int area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 unsigned long flags;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001330 int i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Michael Holzheu66a464d2005-06-25 14:55:33 -07001332 if(!id || !id->areas)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return;
1334 spin_lock_irqsave(&id->lock,flags);
1335 if(area == DEBUG_FLUSH_ALL){
1336 id->active_area = 0;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001337 memset(id->active_entries, 0, id->nr_areas * sizeof(int));
1338 for (i = 0; i < id->nr_areas; i++) {
1339 id->active_pages[i] = 0;
1340 for(j = 0; j < id->pages_per_area; j++) {
1341 memset(id->areas[i][j], 0, PAGE_SIZE);
1342 }
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 printk(KERN_INFO "debug: %s: all areas flushed\n",id->name);
1345 } else if(area >= 0 && area < id->nr_areas) {
Michael Holzheu66a464d2005-06-25 14:55:33 -07001346 id->active_entries[area] = 0;
1347 id->active_pages[area] = 0;
1348 for(i = 0; i < id->pages_per_area; i++) {
1349 memset(id->areas[area][i],0,PAGE_SIZE);
1350 }
1351 printk(KERN_INFO "debug: %s: area %i has been flushed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 id->name, area);
1353 } else {
1354 printk(KERN_INFO
Michael Holzheu66a464d2005-06-25 14:55:33 -07001355 "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 id->name, area, 0, id->nr_areas-1);
1357 }
1358 spin_unlock_irqrestore(&id->lock,flags);
1359}
1360
1361/*
1362 * view function: flushes debug areas
1363 */
1364
Michael Holzheu66a464d2005-06-25 14:55:33 -07001365static int
1366debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
1367 struct file *file, const char __user *user_buf,
1368 size_t user_len, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 char input_buf[1];
Michael Holzheu66a464d2005-06-25 14:55:33 -07001371 int rc = user_len;
1372
1373 if (user_len > 0x10000)
1374 user_len = 0x10000;
1375 if (*offset != 0){
1376 rc = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto out;
Michael Holzheu66a464d2005-06-25 14:55:33 -07001378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (copy_from_user(input_buf, user_buf, 1)){
1380 rc = -EFAULT;
1381 goto out;
1382 }
1383 if(input_buf[0] == '-') {
1384 debug_flush(id, DEBUG_FLUSH_ALL);
1385 goto out;
1386 }
1387 if (isdigit(input_buf[0])) {
1388 int area = ((int) input_buf[0] - (int) '0');
1389 debug_flush(id, area);
1390 goto out;
1391 }
1392
1393 printk(KERN_INFO "debug: area `%c` is not valid\n", input_buf[0]);
1394
Michael Holzheu66a464d2005-06-25 14:55:33 -07001395out:
1396 *offset += user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return rc; /* number of input characters */
1398}
1399
1400/*
1401 * prints debug header in raw format
1402 */
1403
Michael Holzheu66a464d2005-06-25 14:55:33 -07001404static int
1405debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
1406 int area, debug_entry_t * entry, char *out_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 int rc;
1409
1410 rc = sizeof(debug_entry_t);
1411 memcpy(out_buf,entry,sizeof(debug_entry_t));
1412 return rc;
1413}
1414
1415/*
1416 * prints debug data in raw format
1417 */
1418
Michael Holzheu66a464d2005-06-25 14:55:33 -07001419static int
1420debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 char *out_buf, const char *in_buf)
1422{
1423 int rc;
1424
1425 rc = id->buf_size;
1426 memcpy(out_buf, in_buf, id->buf_size);
1427 return rc;
1428}
1429
1430/*
1431 * prints debug data in hex/ascii format
1432 */
1433
Michael Holzheu66a464d2005-06-25 14:55:33 -07001434static int
1435debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
1436 char *out_buf, const char *in_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 int i, rc = 0;
1439
1440 for (i = 0; i < id->buf_size; i++) {
1441 rc += sprintf(out_buf + rc, "%02x ",
1442 ((unsigned char *) in_buf)[i]);
1443 }
1444 rc += sprintf(out_buf + rc, "| ");
1445 for (i = 0; i < id->buf_size; i++) {
1446 unsigned char c = in_buf[i];
1447 if (!isprint(c))
1448 rc += sprintf(out_buf + rc, ".");
1449 else
1450 rc += sprintf(out_buf + rc, "%c", c);
1451 }
1452 rc += sprintf(out_buf + rc, "\n");
1453 return rc;
1454}
1455
1456/*
1457 * prints header for debug entry
1458 */
1459
Michael Holzheu66a464d2005-06-25 14:55:33 -07001460int
1461debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 int area, debug_entry_t * entry, char *out_buf)
1463{
Michael Holzheu942eaab2005-09-03 15:57:58 -07001464 struct timespec time_spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 unsigned long long time;
1466 char *except_str;
1467 unsigned long caller;
1468 int rc = 0;
1469 unsigned int level;
1470
1471 level = entry->id.fields.level;
1472 time = entry->id.stck;
1473 /* adjust todclock to 1970 */
1474 time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
Michael Holzheu942eaab2005-09-03 15:57:58 -07001475 tod_to_timeval(time, &time_spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 if (entry->id.fields.exception)
1478 except_str = "*";
1479 else
1480 except_str = "-";
1481 caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
1482 rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
Michael Holzheu942eaab2005-09-03 15:57:58 -07001483 area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 except_str, entry->id.fields.cpuid, (void *) caller);
1485 return rc;
1486}
1487
1488/*
1489 * prints debug data sprintf-formated:
1490 * debug_sprinf_event/exception calls must be used together with this view
1491 */
1492
1493#define DEBUG_SPRINTF_MAX_ARGS 10
1494
Michael Holzheu66a464d2005-06-25 14:55:33 -07001495static int
1496debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
1497 char *out_buf, debug_sprintf_entry_t *curr_event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 int num_longs, num_used_args = 0,i, rc = 0;
1500 int index[DEBUG_SPRINTF_MAX_ARGS];
1501
1502 /* count of longs fit into one entry */
1503 num_longs = id->buf_size / sizeof(long);
1504
1505 if(num_longs < 1)
1506 goto out; /* bufsize of entry too small */
1507 if(num_longs == 1) {
1508 /* no args, we use only the string */
1509 strcpy(out_buf, curr_event->string);
1510 rc = strlen(curr_event->string);
1511 goto out;
1512 }
1513
1514 /* number of arguments used for sprintf (without the format string) */
1515 num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
1516
1517 memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
1518
1519 for(i = 0; i < num_used_args; i++)
1520 index[i] = i;
1521
1522 rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
1523 curr_event->args[index[1]], curr_event->args[index[2]],
1524 curr_event->args[index[3]], curr_event->args[index[4]],
1525 curr_event->args[index[5]], curr_event->args[index[6]],
1526 curr_event->args[index[7]], curr_event->args[index[8]],
1527 curr_event->args[index[9]]);
1528
1529out:
1530
1531 return rc;
1532}
1533
1534/*
1535 * clean up module
1536 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +01001537static void __exit debug_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001539 debugfs_remove(debug_debugfs_root_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 unregister_sysctl_table(s390dbf_sysctl_header);
1541 return;
1542}
1543
1544/*
1545 * module definitions
1546 */
Michael Holzheu66a464d2005-06-25 14:55:33 -07001547postcore_initcall(debug_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548module_exit(debug_exit);
1549MODULE_LICENSE("GPL");
1550
1551EXPORT_SYMBOL(debug_register);
1552EXPORT_SYMBOL(debug_unregister);
1553EXPORT_SYMBOL(debug_set_level);
1554EXPORT_SYMBOL(debug_stop_all);
1555EXPORT_SYMBOL(debug_register_view);
1556EXPORT_SYMBOL(debug_unregister_view);
1557EXPORT_SYMBOL(debug_event_common);
1558EXPORT_SYMBOL(debug_exception_common);
1559EXPORT_SYMBOL(debug_hex_ascii_view);
1560EXPORT_SYMBOL(debug_raw_view);
1561EXPORT_SYMBOL(debug_dflt_header_fn);
1562EXPORT_SYMBOL(debug_sprintf_view);
1563EXPORT_SYMBOL(debug_sprintf_exception);
1564EXPORT_SYMBOL(debug_sprintf_event);