patch-2.4.4 linux/arch/s390x/kernel/debug.c

Next file: linux/arch/s390x/kernel/ebcdic.c
Previous file: linux/arch/s390x/kernel/cpprintk.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.4.3/linux/arch/s390x/kernel/debug.c linux/arch/s390x/kernel/debug.c
@@ -15,45 +15,53 @@
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/malloc.h>
-#include <linux/vmalloc.h>
 #include <linux/ctype.h>
 #include <linux/version.h>
 #include <asm/uaccess.h>
 #include <asm/semaphore.h>
 
-#ifdef MODULE
 #include <linux/module.h>
-#endif
 
 #include <asm/debug.h>
 
 #define MIN(a,b) (((a)<(b))?(a):(b))
-
-#if defined(CONFIG_ARCH_S390X)
-#define DEBUG_PROC_HEADER_SIZE 46
-#else
-#define DEBUG_PROC_HEADER_SIZE 38
-#endif
-
-#define ADD_BUFFER 1000
+#define DEBUG_PROLOG_ENTRY -1
 
 /* typedefs */
 
 typedef struct file_private_info {
-	loff_t len;			/* length of output in byte */
-	int size;			/* size of buffer for output */
-	char *data;			/* buffer for output */
-	debug_info_t *debug_info;	/* the debug information struct */
+	loff_t offset;			/* offset of last read in file */
+	int    act_area;                /* number of last formated area */
+	int    act_entry;               /* last formated entry (offset */
+                                        /* relative to beginning of last */
+                                        /* formated area) */ 
+	size_t act_entry_offset;        /* up to this offset we copied */
+					/* in last read the last formated */
+					/* entry to userland */
+	char   temp_buf[2048];		/* buffer for output */
+	debug_info_t *debug_info_org;   /* original debug information */
+	debug_info_t *debug_info_snap;	/* snapshot of debug information */
 	struct debug_view *view;	/* used view of debug info */
 } file_private_info_t;
 
+typedef struct
+{
+	char *string;
+	/* 
+	 * This assumes that all args are converted into longs 
+	 * on L/390 this is the case for all types of parameter 
+	 * except of floats, and long long (32 bit) 
+         *
+         */
+	long args[0];
+} debug_sprintf_entry;
+
+
 extern void tod_to_timeval(uint64_t todval, struct timeval *xtime);
 
 /* internal function prototyes */
 
 static int debug_init(void);
-static int debug_format_output(debug_info_t * debug_area, char *buf,
-			       int size, struct debug_view *view);
 static ssize_t debug_output(struct file *file, char *user_buf,
 			    size_t user_len, loff_t * offset);
 static ssize_t debug_input(struct file *file, const char *user_buf,
@@ -83,6 +91,9 @@
 static int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
                          int area, debug_entry_t * entry, char *out_buf);
 
+static int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
+				   char *out_buf, debug_sprintf_entry *curr_event);
+
 /* globals */
 
 struct debug_view debug_raw_view = {
@@ -90,6 +101,7 @@
 	NULL,
 	&debug_raw_header_fn,
 	&debug_raw_format_fn,
+	NULL,
 	NULL
 };
 
@@ -98,6 +110,7 @@
 	NULL,
 	&debug_dflt_header_fn,
 	&debug_hex_ascii_format_fn,
+	NULL,
 	NULL
 };
 
@@ -106,9 +119,22 @@
 	&debug_prolog_level_fn,
 	NULL,
 	NULL,
-	&debug_input_level_fn
+	&debug_input_level_fn,
+	NULL
+};
+
+struct debug_view debug_sprintf_view = {
+	"sprintf",
+	NULL,
+	&debug_dflt_header_fn,
+	(debug_format_proc_t*)&debug_sprintf_format_fn,
+	NULL,
+	NULL
 };
 
+
+unsigned int debug_feature_version = __DEBUG_FEATURE_VERSION;
+
 /* static globals */
 
 static debug_info_t *debug_area_first = NULL;
@@ -137,64 +163,108 @@
 
 static struct proc_dir_entry *debug_proc_root_entry;
 
-
 /* functions */
 
 /*
- * debug_info_create
- * - create new debug-info
+ * debug_info_alloc
+ * - alloc new debug-info
  */
 
-static debug_info_t*  debug_info_create(char *name, int page_order, 
+static debug_info_t*  debug_info_alloc(char *name, int page_order,
                                         int nr_areas, int buf_size)
 {
 	debug_info_t* rc;
 	int i;
 
-        /* alloc everything */
+	/* alloc everything */
 
-        rc = (debug_info_t*) kmalloc(sizeof(debug_info_t), GFP_ATOMIC);
-        if(!rc) 
+	rc = (debug_info_t*) kmalloc(sizeof(debug_info_t), GFP_ATOMIC);
+	if(!rc)
 		goto fail_malloc_rc;
 	rc->active_entry = (int*)kmalloc(nr_areas * sizeof(int), GFP_ATOMIC);
 	if(!rc->active_entry)
 		goto fail_malloc_active_entry;
 	memset(rc->active_entry, 0, nr_areas * sizeof(int));
-        rc->areas = (debug_entry_t **) kmalloc(nr_areas *
-                                               sizeof(debug_entry_t *),
-                                               GFP_ATOMIC);
-        if (!rc->areas) 
-                goto fail_malloc_areas;
-        for (i = 0; i < nr_areas; i++) {
-                rc->areas[i] =
-                    (debug_entry_t *) __get_free_pages(GFP_ATOMIC,
-                                                       page_order);
-                if (!rc->areas[i]) {
-                        for (i--; i >= 0; i--) {
-                                free_pages((unsigned long) rc->areas[i],
-                                           page_order);
-                        }
-                        goto fail_malloc_areas2;
-                } else {
-                        memset(rc->areas[i], 0, PAGE_SIZE << page_order);
-                }
-        }
+	rc->areas = (debug_entry_t **) kmalloc(nr_areas *
+						sizeof(debug_entry_t *),
+						GFP_ATOMIC);
+	if (!rc->areas)
+		goto fail_malloc_areas;
+	for (i = 0; i < nr_areas; i++) {
+		rc->areas[i] = (debug_entry_t *) __get_free_pages(GFP_ATOMIC,
+								page_order);
+		if (!rc->areas[i]) {
+			for (i--; i >= 0; i--) {
+				free_pages((unsigned long) rc->areas[i],
+						page_order);
+			}
+			goto fail_malloc_areas2;
+		} else {
+			memset(rc->areas[i], 0, PAGE_SIZE << page_order);
+		}
+	}
+
+	/* initialize members */
 
-        /* initialize members */
+	spin_lock_init(&rc->lock);
+	rc->page_order  = page_order;
+	rc->nr_areas    = nr_areas;
+	rc->active_area = 0;
+	rc->level       = DEBUG_DEFAULT_LEVEL;
+	rc->buf_size    = buf_size;
+	rc->entry_size  = sizeof(debug_entry_t) + buf_size;
+	strncpy(rc->name, name, MIN(strlen(name), (DEBUG_MAX_PROCF_LEN - 1)));
+	rc->name[MIN(strlen(name), (DEBUG_MAX_PROCF_LEN - 1))] = 0;
+	memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
+	memset(rc->proc_entries, 0 ,DEBUG_MAX_VIEWS *
+		sizeof(struct proc_dir_entry*));
+	atomic_set(&(rc->ref_count), 0);
+
+	return rc;
+
+fail_malloc_areas2:
+	kfree(rc->areas);
+fail_malloc_areas:
+	kfree(rc->active_entry);
+fail_malloc_active_entry:
+	kfree(rc);
+fail_malloc_rc:
+	return NULL;
+}
+
+/*
+ * debug_info_free
+ * - free memory debug-info
+ */
+
+static void debug_info_free(debug_info_t* db_info){
+	int i;
+	for (i = 0; i < db_info->nr_areas; i++) {
+		free_pages((unsigned long) db_info->areas[i],
+		db_info->page_order);
+	}
+	kfree(db_info->areas);
+	kfree(db_info->active_entry);
+	kfree(db_info);
+}
+
+/*
+ * debug_info_create
+ * - create new debug-info
+ */
+
+static debug_info_t*  debug_info_create(char *name, int page_order, 
+                                        int nr_areas, int buf_size)
+{
+	debug_info_t* rc;
+
+        rc = debug_info_alloc(name, page_order, nr_areas, buf_size);
+        if(!rc) 
+		goto out;
+
+
+	/* create proc rood directory */
 
-        spin_lock_init(&rc->lock);
-        rc->page_order  = page_order;
-        rc->nr_areas    = nr_areas;
-        rc->active_area = 0;
-        rc->level       = DEBUG_DEFAULT_LEVEL;
-        rc->buf_size    = buf_size;
-        rc->entry_size  = sizeof(debug_entry_t) + buf_size;
-        strncpy(rc->name, name, MIN(strlen(name), (DEBUG_MAX_PROCF_LEN - 1)));
-        rc->name[MIN(strlen(name), (DEBUG_MAX_PROCF_LEN - 1))] = 0;
-        memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
-	memset(rc->proc_entries, 0 ,DEBUG_MAX_VIEWS * 
-               sizeof(struct proc_dir_entry*));
-        atomic_set(&(rc->ref_count), 0);
         rc->proc_root_entry =
             debug_create_proc_dir_entry(debug_proc_root_entry, rc->name,
                                         S_IFDIR | S_IRUGO | S_IXUGO |
@@ -216,16 +286,29 @@
         rc->next = NULL;
 
 	debug_info_get(rc);
+out:
 	return rc;
+}
 
-fail_malloc_areas2:
-	kfree(rc->areas);
-fail_malloc_areas:
-	kfree(rc->active_entry);
-fail_malloc_active_entry:
-	kfree(rc);
-fail_malloc_rc:
-	return NULL;
+/*
+ * debug_info_copy
+ * - copy debug-info
+ */
+
+static debug_info_t* debug_info_copy(debug_info_t* in)
+{
+        int i;
+        debug_info_t* rc;
+        rc = debug_info_alloc(in->name, in->page_order, 
+                                in->nr_areas, in->buf_size);
+        if(!rc)
+                goto out;
+
+        for(i = 0; i < in->nr_areas; i++){
+                memcpy(rc->areas[i],in->areas[i], PAGE_SIZE << in->page_order);
+        }
+out:
+        return rc;
 }
 
 /*
@@ -261,49 +344,120 @@
 		}
 		debug_delete_proc_dir_entry(debug_proc_root_entry,
 					    db_info->proc_root_entry);
-		for (i = 0; i < db_info->nr_areas; i++) {
-			free_pages((unsigned long) db_info->areas[i],
-				   db_info->page_order);
-		}
-		kfree(db_info->areas);
-		kfree(db_info->active_entry);
 		if(db_info == debug_area_first)
 			debug_area_first = db_info->next;
 		if(db_info == debug_area_last)
 			debug_area_last = db_info->prev;
 		if(db_info->prev) db_info->prev->next = db_info->next;
 		if(db_info->next) db_info->next->prev = db_info->prev;
-		kfree(db_info);
+		debug_info_free(db_info);
 	}
 }
 
+/*
+ * debug_format_entry:
+ * - format one debug entry and return size of formated data
+ */
+
+static int debug_format_entry(file_private_info_t *p_info)
+{
+	debug_info_t *id_org    = p_info->debug_info_org;
+	debug_info_t *id_snap   = p_info->debug_info_snap;
+	struct debug_view *view = p_info->view;
+	debug_entry_t *act_entry;
+	size_t len = 0;
+	if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
+		/* print prolog */
+        	if (view->prolog_proc)
+                	len += view->prolog_proc(id_org, view,p_info->temp_buf);
+		goto out;
+	}
+
+	act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area] +
+					p_info->act_entry);
+                        
+	if (act_entry->id.stck == 0LL)
+			goto out;  /* empty entry */
+	if (view->header_proc)
+		len += view->header_proc(id_org, view, p_info->act_area, 
+					act_entry, p_info->temp_buf + len);
+	if (view->format_proc)
+		len += view->format_proc(id_org, view, p_info->temp_buf + len,
+						DEBUG_DATA(act_entry));
+      out:
+        return len;
+}
+
+/*
+ * debug_next_entry:
+ * - goto next entry in p_info
+ */
+
+extern inline int debug_next_entry(file_private_info_t *p_info)
+{
+	debug_info_t *id = p_info->debug_info_snap;
+	if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
+		p_info->act_entry = 0;
+		goto out;
+	}
+	if ((p_info->act_entry += id->entry_size)
+		> ((PAGE_SIZE << (id->page_order)) 
+		- id->entry_size)){
+
+		/* next area */
+		p_info->act_entry = 0;
+        	p_info->act_area++;
+        	if(p_info->act_area >= id->nr_areas)
+			return 1;
+	}
+out:
+	return 0;	
+}
 
 /*
  * debug_output:
  * - called for user read()
- * - copies formated output form private_data of the file
- *   handle to the user buffer
+ * - copies formated debug entries to the user buffer
  */
 
 static ssize_t debug_output(struct file *file,	/* file descriptor */
 			    char *user_buf,	/* user buffer */
-			    size_t user_len,	/* length of buffer */
+			    size_t  len,	/* length of buffer */
 			    loff_t *offset	/* offset in the file */ )
 {
-	loff_t len;
+	size_t count = 0;
+	size_t entry_offset, size = 0;
 	int rc;
 	file_private_info_t *p_info;
 
 	p_info = ((file_private_info_t *) file->private_data);
-	if (*offset >= p_info->len) {
-		return 0;	/* EOF */
-	} else {
-		len = MIN(user_len, (p_info->len - *offset));
-		if ((rc = copy_to_user(user_buf, &(p_info->data[*offset]),len)))
-			return rc;;
-		(*offset) += len;
-		return len;	/* number of bytes "read" */
+	if (*offset != p_info->offset) 
+		return -EPIPE;
+	if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
+		return 0;
+
+	entry_offset = p_info->act_entry_offset;
+
+	while(count < len){
+		size = debug_format_entry(p_info);
+		size = MIN((len - count), (size - entry_offset));
+
+		if(size){
+			if ((rc = copy_to_user(user_buf + count, 
+					p_info->temp_buf + entry_offset, size)))
+			return rc;
+		}
+		count += size;
+		entry_offset = 0;
+		if(count != len)
+			if(debug_next_entry(p_info)) 
+				goto out;
 	}
+out:
+	p_info->offset           = *offset + count;
+	p_info->act_entry_offset = size;	
+	*offset = p_info->offset;
+	return count;
 }
 
 /*
@@ -322,68 +476,16 @@
 	down(&debug_lock);
 	p_info = ((file_private_info_t *) file->private_data);
 	if (p_info->view->input_proc)
-		rc = p_info->view->input_proc(p_info->debug_info,
+		rc = p_info->view->input_proc(p_info->debug_info_org,
 					      p_info->view, file, user_buf,
 					      length, offset);
+	else
+		rc = -EPERM;
 	up(&debug_lock);
 	return rc;		/* number of input characters */
 }
 
 /*
- * debug_format_output:
- * - calls prolog, header and format functions of view to format output
- */
-
-static int debug_format_output(debug_info_t * debug_area, char *buf,
-			       int size, struct debug_view *view)
-{
-	int len = 0;
-	int i, j;
-	int nr_of_entries;
-	debug_entry_t *act_entry;
-
-	/* print prolog */
-	if (view->prolog_proc)
-		len += view->prolog_proc(debug_area, view, buf);
-	/* print debug records */
-	if (!(view->format_proc) && !(view->header_proc))
-		goto out;
-	nr_of_entries = PAGE_SIZE / debug_area->entry_size
-			<< debug_area->page_order;
-	for (i = 0; i < debug_area->nr_areas; i++) {
-		act_entry = debug_area->areas[i];
-		for (j = 0; j < nr_of_entries; j++) {
-			if (act_entry->id.fields.used == 0)
-				break;	/* empty entry */
-			if (view->header_proc)
-				len += view->header_proc(debug_area, view, i,
-						         act_entry, buf + len);
-			if (view->format_proc)
-				len += view->format_proc(debug_area, view,
-						         buf + len,
-						         DEBUG_DATA(act_entry));
-			if (len > size) {
-				printk(KERN_ERR
-				"debug: error -- memory exceeded for (%s/%s)\n",
-				debug_area->name, view->name);
-				printk(KERN_ERR "debug: fix view %s!!\n",
-				       view->name);
-				printk(KERN_ERR
-				"debug: area: %i (0 - %i) entry: %i (0 - %i)\n",
-				i, debug_area->nr_areas - 1, j,
-				nr_of_entries - 1);
-				goto out;
-			}
-			act_entry = (debug_entry_t *) (((char *) act_entry) +
-					       debug_area->entry_size);
-		}
-	}
-      out:
-	return len;
-}
-
-
-/*
  * debug_open:
  * - called for user open()
  * - copies formated output to private_data area of the file
@@ -392,17 +494,14 @@
 
 static int debug_open(struct inode *inode, struct file *file)
 {
-	int i = 0, size = 0, rc = 0, f_entry_size = 0;
+	int i = 0, rc = 0;
 	file_private_info_t *p_info;
-	debug_info_t* debug_info;
+	debug_info_t *debug_info, *debug_info_snapshot;
 
 #ifdef DEBUG
 	printk("debug_open\n");
 #endif
-
-#ifdef MODULE
 	MOD_INC_USE_COUNT;
-#endif
 	down(&debug_lock);
 
 	/* find debug log and view */
@@ -422,87 +521,41 @@
 	/* no entry found */
 	rc = -EINVAL;
 	goto out;
+
       found:
-	if ((file->private_data =
-	     kmalloc(sizeof(file_private_info_t), GFP_ATOMIC)) == 0) {
-		printk(KERN_ERR "debug_open: kmalloc failed\n");
+
+	/* make snapshot of current debug areas to get it consistent */
+
+	debug_info_snapshot = debug_info_copy(debug_info);
+
+	if(!debug_info_snapshot){
+		printk(KERN_ERR "debug_open: debug_info_copy failed (out of mem)\n");
 		rc = -ENOMEM;
 		goto out;
 	}
-	p_info = (file_private_info_t *) file->private_data;
-
-	/*
-	 * the size for the formated output is calculated
-	 * with the following formula:
-	 *
-	 *   prolog-size
-	 *   +
-	 *   (record header size + record data field size)
-	 *   * number of entries per page
-	 *   * number of pages per area
-	 *   * number of areas
-	 */
-
-	if (debug_info->views[i]->prolog_proc)
-		size +=
-		    debug_info->views[i]->prolog_proc(debug_info,
-							 debug_info->
-							 views[i], NULL);
-
-	if (debug_info->views[i]->header_proc)
-		f_entry_size =
-		    debug_info->views[i]->header_proc(debug_info,
-							 debug_info->
-							 views[i], 0, NULL,
-							 NULL);
-	if (debug_info->views[i]->format_proc)
-		f_entry_size +=
-		    debug_info->views[i]->format_proc(debug_info,
-							 debug_info->
-							 views[i], NULL,
-							 NULL);
-
-	size += f_entry_size 
-		* (PAGE_SIZE / debug_info->entry_size
-		<< debug_info->page_order)
-		* debug_info->nr_areas + 1;	/* terminating \0 */
-#ifdef DEBUG
-	printk("debug_open: size: %i\n", size);
-#endif
 
-	/* alloc some bytes more to be safe against bad views */
-	if ((p_info->data = vmalloc(size + ADD_BUFFER)) == 0) {
-		printk(KERN_ERR "debug_open: vmalloc failed\n");
-		vfree(file->private_data);
+	if ((file->private_data =
+	     kmalloc(sizeof(file_private_info_t), GFP_ATOMIC)) == 0) {
+		printk(KERN_ERR "debug_open: kmalloc failed\n");
+		debug_info_free(debug_info_snapshot);	
 		rc = -ENOMEM;
 		goto out;
 	}
-
-	p_info->size = size;
-	p_info->debug_info = debug_info;
+	p_info = (file_private_info_t *) file->private_data;
+	p_info->offset = 0;
+	p_info->debug_info_snap = debug_info_snapshot;
+	p_info->debug_info_org  = debug_info;
 	p_info->view = debug_info->views[i];
+	p_info->act_area = 0;
+	p_info->act_entry = DEBUG_PROLOG_ENTRY;
+	p_info->act_entry_offset = 0;
 
-	spin_lock_irq(&debug_info->lock);
-
-	p_info->len =
-	    debug_format_output(debug_info, p_info->data, size,
-				debug_info->views[i]);
-#ifdef DEBUG
-	{
-		int ilen = p_info->len;
-		printk("debug_open: len: %i\n", ilen);
-	}
-#endif
-
-	spin_unlock_irq(&debug_info->lock);
 	debug_info_get(debug_info);
 
       out:
 	up(&debug_lock);
-#ifdef MODULE
 	if (rc != 0)
 		MOD_DEC_USE_COUNT;
-#endif
 	return rc;
 }
 
@@ -518,18 +571,11 @@
 #ifdef DEBUG
 	printk("debug_close\n");
 #endif
-	down(&debug_lock);
 	p_info = (file_private_info_t *) file->private_data;
-	debug_info_put(p_info->debug_info);
-	if (p_info->data) {
-		vfree(p_info->data);
-		kfree(file->private_data);
-	}
-	up(&debug_lock);
-
-#ifdef MODULE
+	debug_info_free(p_info->debug_info_snap);
+	debug_info_put(p_info->debug_info_org);
+	kfree(file->private_data);
 	MOD_DEC_USE_COUNT;
-#endif
 	return 0;		/* success */
 }
 
@@ -607,9 +653,7 @@
 {
 	debug_info_t *rc = NULL;
 
-#ifdef MODULE
 	MOD_INC_USE_COUNT;
-#endif
 	if (!initialized)
 		debug_init();
 	down(&debug_lock);
@@ -626,9 +670,7 @@
       out:
         if (rc == NULL){
 		printk(KERN_ERR "debug: debug_register failed for %s\n",name);
-#ifdef MODULE
 		MOD_DEC_USE_COUNT;
-#endif
         }
 	up(&debug_lock);
 	return rc;
@@ -648,9 +690,7 @@
 	debug_info_put(id);
 	up(&debug_lock);
 
-#ifdef MODULE
 	MOD_DEC_USE_COUNT;
-#endif
       out:
 	return;
 }
@@ -662,7 +702,7 @@
 
 void debug_set_level(debug_info_t* id, int new_level)
 {
-	long flags;
+	unsigned long flags;
 	if(!id)
 		return;	
 	spin_lock_irqsave(&id->lock,flags);
@@ -687,7 +727,7 @@
  * - set active entry to next in the ring buffer
  */
 
-static inline void proceed_active_entry(debug_info_t * id)
+extern inline void proceed_active_entry(debug_info_t * id)
 {
 	if ((id->active_entry[id->active_area] += id->entry_size)
 	    > ((PAGE_SIZE << (id->page_order)) - id->entry_size))
@@ -699,7 +739,7 @@
  * - set active area to next in the ring buffer
  */
 
-static inline void proceed_active_area(debug_info_t * id)
+extern inline void proceed_active_area(debug_info_t * id)
 {
 	id->active_area++;
 	id->active_area = id->active_area % id->nr_areas;
@@ -709,7 +749,7 @@
  * get_active_entry:
  */
 
-static inline debug_entry_t *get_active_entry(debug_info_t * id)
+extern inline debug_entry_t *get_active_entry(debug_info_t * id)
 {
 	return (debug_entry_t *) ((char *) id->areas[id->active_area] +
 				  id->active_entry[id->active_area]);
@@ -720,160 +760,126 @@
  * - set timestamp, caller address, cpu number etc.
  */
 
-static inline debug_entry_t *debug_common(debug_info_t * id)
+extern inline debug_entry_t *debug_common(debug_info_t * id, int level, 
+                                    const void *buf, int len, int exception)
 {
+	unsigned long flags;
 	debug_entry_t *active;
 
+	spin_lock_irqsave(&id->lock, flags);
 	active = get_active_entry(id);
 	STCK(active->id.stck);
 	active->id.fields.cpuid = smp_processor_id();
-	active->id.fields.used = 1;
 	active->caller = __builtin_return_address(0);
-	return active;
-}
-
-/*
- * debug_event:
- */
-
-debug_entry_t *debug_event(debug_info_t * id, int level, void *buf,
-			   int len)
-{
-	long flags;
-	debug_entry_t *active = NULL;
-
-	if ((!id) || (level > id->level))
-		goto out;
-	spin_lock_irqsave(&id->lock, flags);
-	active = debug_common(id);
-	active->id.fields.exception = 0;
+	active->id.fields.exception = exception;
+	active->id.fields.level     = level;
 	memset(DEBUG_DATA(active), 0, id->buf_size);
 	memcpy(DEBUG_DATA(active), buf, MIN(len, id->buf_size));
 	proceed_active_entry(id);
+	if(exception)
+		proceed_active_area(id);
 	spin_unlock_irqrestore(&id->lock, flags);
-      out:
+
 	return active;
 }
 
 /*
- * debug_int_event:
+ * debug_event_common:
+ * - write debug entry with given size
  */
 
-debug_entry_t *debug_int_event(debug_info_t * id, int level,
-			       unsigned int tag)
+debug_entry_t *debug_event_common(debug_info_t * id, int level, const void *buf,
+			          int len)
 {
-	long flags;
-	debug_entry_t *active = NULL;
-
-	if ((!id) || (level > id->level))
-		goto out;
-	spin_lock_irqsave(&id->lock, flags);
-	active = debug_common(id);
-	active->id.fields.exception = 0;
-	memset(DEBUG_DATA(active), 0, id->buf_size);
-	memcpy(DEBUG_DATA(active), &tag, MIN(sizeof(unsigned int), id->buf_size));
-	proceed_active_entry(id);
-	spin_unlock_irqrestore(&id->lock, flags);
-      out:
-	return active;
+	return debug_common(id, level, buf, len, 0);
 }
 
 /*
- * debug_text_event:
+ * debug_exception_common:
+ * - write debug entry with given size and switch to next debug area
  */
 
-debug_entry_t *debug_text_event(debug_info_t * id, int level,
-				const char *txt)
+debug_entry_t *debug_exception_common(debug_info_t * id, int level, 
+                                      const void *buf, int len)
 {
-	long flags;
-	debug_entry_t *active = NULL;
-
-	if ((!id) || (level > id->level))
-		goto out;
-	spin_lock_irqsave(&id->lock, flags);
-	active = debug_common(id);
-	memset(DEBUG_DATA(active), 0, id->buf_size);
-	strncpy(DEBUG_DATA(active), txt, MIN(strlen(txt), id->buf_size));
-	active->id.fields.exception = 0;
-	proceed_active_entry(id);
-	spin_unlock_irqrestore(&id->lock, flags);
-      out:
-	return active;
-
+	return debug_common(id, level, buf, len, 1);
 }
 
 /*
- * debug_exception:
+ * counts arguments in format string for sprintf view
  */
 
-debug_entry_t *debug_exception(debug_info_t * id, int level, void *buf,
-			       int len)
+extern inline int debug_count_numargs(char *string)
 {
-	long flags;
-	debug_entry_t *active = NULL;
+	int numargs=0;
 
-	if ((!id) || (level > id->level))
-		goto out;
-	spin_lock_irqsave(&id->lock, flags);
-	active = debug_common(id);
-	active->id.fields.exception = 1;
-	memset(DEBUG_DATA(active), 0, id->buf_size);
-	memcpy(DEBUG_DATA(active), buf, MIN(len, id->buf_size));
-	proceed_active_entry(id);
-	proceed_active_area(id);
-	spin_unlock_irqrestore(&id->lock, flags);
-      out:
-	return active;
+	while(*string) {
+		if(*string++=='%')
+			numargs++;
+	}
+	return(numargs);
 }
 
 /*
- * debug_int_exception:
+ * debug_sprintf_event:
  */
 
-debug_entry_t *debug_int_exception(debug_info_t * id, int level,
-				   unsigned int tag)
+debug_entry_t *debug_sprintf_event(debug_info_t* id,
+                                   int level,char *string,...)
 {
-	long flags;
-	debug_entry_t *active = NULL;
+	va_list   ap;
+	int numargs,alloc_size,idx;
+	debug_sprintf_entry *curr_event;
+	debug_entry_t *retval = NULL;
 
-	if ((!id) || (level > id->level))
-		goto out;
-	spin_lock_irqsave(&id->lock, flags);
-	active = debug_common(id);
-	active->id.fields.exception = 1;
-	memset(DEBUG_DATA(active), 0, id->buf_size);
-	memcpy(DEBUG_DATA(active), &tag,
-	       MIN(sizeof(unsigned int), id->buf_size));
-	proceed_active_entry(id);
-	proceed_active_area(id);
-	spin_unlock_irqrestore(&id->lock, flags);
-      out:
-	return active;
+	if((!id) || (level > id->level))
+		return NULL;
+	else {
+		numargs=debug_count_numargs(string);
+		alloc_size=offsetof(debug_sprintf_entry,args[numargs]);
+		curr_event=alloca(alloc_size);
+
+		if(curr_event){
+			va_start(ap,string);
+			curr_event->string=string;
+			for(idx=0;idx<numargs;idx++)
+				curr_event->args[idx]=va_arg(ap,long);
+			retval=debug_common(id,level, curr_event,alloc_size,0);
+			va_end(ap);
+		}
+		return retval;
+	}
 }
 
 /*
- * debug_text_exception:
+ * debug_sprintf_exception:
  */
 
-debug_entry_t *debug_text_exception(debug_info_t * id, int level,
-				    const char *txt)
+debug_entry_t *debug_sprintf_exception(debug_info_t* id,
+                                       int level,char *string,...)
 {
-	long flags;
-	debug_entry_t *active = NULL;
-
-	if ((!id) || (level > id->level))
-		goto out;
-	spin_lock_irqsave(&id->lock, flags);
-	active = debug_common(id);
-	memset(DEBUG_DATA(active), 0, id->buf_size);
-	strncpy(DEBUG_DATA(active), txt, MIN(strlen(txt), id->buf_size));
-	active->id.fields.exception = 1;
-	proceed_active_entry(id);
-	proceed_active_area(id);
-	spin_unlock_irqrestore(&id->lock, flags);
-      out:
-	return active;
+	va_list   ap;
+	int numargs,alloc_size,idx;
+	debug_sprintf_entry *curr_event;
+	debug_entry_t *retval = NULL;
 
+	if((!id) || (level > id->level))
+		return NULL;
+	else {
+		numargs=debug_count_numargs(string);
+		alloc_size=offsetof(debug_sprintf_entry,args[numargs]);
+		curr_event=alloca(alloc_size);
+
+		if(curr_event){
+			va_start(ap,string);
+			curr_event->string=string;
+			for(idx=0;idx<numargs;idx++)
+				curr_event->args[idx]=va_arg(ap,long);
+			retval=debug_common(id,level, curr_event,alloc_size,1);
+			va_end(ap);
+		}
+		return retval;
+	}
 }
 
 /*
@@ -908,7 +914,7 @@
 {
 	int rc = 0;
 	int i;
-	long flags;
+	unsigned long flags;
 	mode_t mode = S_IFREG;
 
 	if (!id)
@@ -951,7 +957,7 @@
 {
 	int rc = 0;
 	int i;
-	long flags;
+	unsigned long flags;
 
 	if (!id)
 		goto out;
@@ -987,13 +993,8 @@
 {
 	int rc = 0;
 
-	if (out_buf == NULL) {
-		rc = 2;
-		goto out;
-	}
 	if(id->level == -1) rc = sprintf(out_buf,"-\n");
 	else rc = sprintf(out_buf, "%i\n", id->level);
-      out:
 	return rc;
 }
 
@@ -1010,8 +1011,10 @@
 
 	if (*offset != 0)
 		goto out;
-	if ((rc = copy_from_user(input_buf, user_buf, 1)))
+	if (copy_from_user(input_buf, user_buf, 1)){
+		rc = -EFAULT;
 		goto out;
+	}
 	if (isdigit(input_buf[0])) {
 		int new_level = ((int) input_buf[0] - (int) '0');
 		debug_set_level(id, new_level);
@@ -1036,10 +1039,7 @@
         int rc;
 
 	rc = sizeof(debug_entry_t);
-        if (out_buf == NULL)
-                goto out;
 	memcpy(out_buf,entry,sizeof(debug_entry_t));
-      out:
         return rc;
 }
 
@@ -1053,10 +1053,7 @@
 	int rc;
 
 	rc = id->buf_size;
-	if (out_buf == NULL || in_buf == NULL)
-		goto out;
 	memcpy(out_buf, in_buf, id->buf_size);
-      out:
 	return rc;
 }
 
@@ -1069,10 +1066,6 @@
 {
 	int i, rc = 0;
 
-	if (out_buf == NULL || in_buf == NULL) {
-		rc = id->buf_size * 4 + 3;
-		goto out;
-	}
 	for (i = 0; i < id->buf_size; i++) {
                 rc += sprintf(out_buf + rc, "%02x ",
                               ((unsigned char *) in_buf)[i]);
@@ -1086,7 +1079,6 @@
 			rc += sprintf(out_buf + rc, "%c", c);
 	}
 	rc += sprintf(out_buf + rc, "\n");
-      out:
 	return rc;
 }
 
@@ -1102,12 +1094,9 @@
 	char *except_str;
 	unsigned long caller;
 	int rc = 0;
+	unsigned int level;
 
-	if (out_buf == NULL) {
-		rc = DEBUG_PROC_HEADER_SIZE;
-		goto out;
-	}
-
+	level = entry->id.fields.level;
 	time = entry->id.stck;
 	/* adjust todclock to 1970 */
 	time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
@@ -1119,18 +1108,60 @@
 		except_str = "-";
 	caller = (unsigned long) entry->caller;
 #if defined(CONFIG_ARCH_S390X)
-	rc += sprintf(out_buf, "%02i %011lu:%06lu %1s %02i %016lx  ",
-		      area, time_val.tv_sec,
-		      time_val.tv_usec, except_str,
-		      entry->id.fields.cpuid, caller);
+	rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %016lx  ",
+		      area, time_val.tv_sec, time_val.tv_usec, level,
+		      except_str, entry->id.fields.cpuid, caller);
 #else
 	caller &= 0x7fffffff;
-	rc += sprintf(out_buf, "%02i %011lu:%06lu %1s %02i %08lx  ",
-		      area, time_val.tv_sec,
-		      time_val.tv_usec, except_str,
-		      entry->id.fields.cpuid, caller);
+	rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %08lx  ",
+		      area, time_val.tv_sec, time_val.tv_usec, level,
+		      except_str, entry->id.fields.cpuid, caller);
 #endif
-      out:
+	return rc;
+}
+
+/*
+ * prints debug data sprintf-formated:
+ * debug_sprinf_event/exception calls must be used together with this view
+ */
+
+#define DEBUG_SPRINTF_MAX_ARGS 10
+
+int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
+                            char *out_buf, debug_sprintf_entry *curr_event)
+{
+	int num_longs, num_used_args = 0,i, rc = 0;
+	int index[DEBUG_SPRINTF_MAX_ARGS];
+
+	/* count of longs fit into one entry */
+	num_longs = id->buf_size /  sizeof(long); 
+
+	if(num_longs < 1)
+		goto out; /* bufsize of entry too small */
+	if(num_longs == 1) {
+		/* no args, we use only the string */
+		strcpy(out_buf, curr_event->string);
+		rc = strlen(curr_event->string);
+		goto out;
+	}
+
+	/* number of arguments used for sprintf (without the format string) */
+	num_used_args   = MIN(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
+
+	memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
+
+	for(i = 0; i < num_used_args; i++)
+		index[i] = i;
+
+	rc =  sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
+		curr_event->args[index[1]], curr_event->args[index[2]],
+		curr_event->args[index[3]], curr_event->args[index[4]],
+		curr_event->args[index[5]], curr_event->args[index[6]],
+		curr_event->args[index[7]], curr_event->args[index[8]],
+		curr_event->args[index[9]]);
+
+out:
+
 	return rc;
 }
 
@@ -1165,3 +1196,17 @@
 }
 
 #endif			/* MODULE */
+
+EXPORT_SYMBOL(debug_register);
+EXPORT_SYMBOL(debug_unregister); 
+EXPORT_SYMBOL(debug_set_level);
+EXPORT_SYMBOL(debug_register_view);
+EXPORT_SYMBOL(debug_unregister_view);
+EXPORT_SYMBOL(debug_event_common);
+EXPORT_SYMBOL(debug_exception_common);
+EXPORT_SYMBOL(debug_hex_ascii_view);
+EXPORT_SYMBOL(debug_raw_view);
+EXPORT_SYMBOL(debug_dflt_header_fn);
+EXPORT_SYMBOL(debug_sprintf_view);
+EXPORT_SYMBOL(debug_sprintf_exception);
+EXPORT_SYMBOL(debug_sprintf_event);

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)