⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debug.c

📁 优龙2410linux2.6.8内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  arch/s390/kernel/debug.c *   S/390 debug facility * *    Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH, *                             IBM Corporation *    Author(s): Michael Holzheu (holzheu@de.ibm.com), *               Holger Smolinski (Holger.Smolinski@de.ibm.com) * *    Bugreports to: <Linux390@de.ibm.com> */#include <linux/config.h>#include <linux/stddef.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/slab.h>#include <linux/ctype.h>#include <asm/uaccess.h>#include <asm/semaphore.h>#include <linux/module.h>#include <linux/init.h>#include <asm/debug.h>#define MIN(a,b) (((a)<(b))?(a):(b))#define DEBUG_PROLOG_ENTRY -1/* typedefs */typedef struct file_private_info {	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_t;extern void tod_to_timeval(uint64_t todval, struct timeval *xtime);/* internal function prototyes */static int debug_init(void);static ssize_t debug_output(struct file *file, char __user *user_buf,			    size_t user_len, loff_t * offset);static ssize_t debug_input(struct file *file, const char __user *user_buf,			   size_t user_len, loff_t * offset);static int debug_open(struct inode *inode, struct file *file);static int debug_close(struct inode *inode, struct file *file);static debug_info_t*  debug_info_create(char *name, int page_order, int nr_areas, int buf_size);static void debug_info_get(debug_info_t *);static void debug_info_put(debug_info_t *);static int debug_prolog_level_fn(debug_info_t * id,				 struct debug_view *view, char *out_buf);static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,				struct file *file, const char __user *user_buf,				size_t user_buf_size, loff_t * offset);static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,                                struct file *file, const char __user *user_buf,                                size_t user_buf_size, loff_t * offset);static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,                                char *out_buf, const char *in_buf);static int debug_raw_format_fn(debug_info_t * id,				 struct debug_view *view, char *out_buf,				 const char *in_buf);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_t *curr_event);/* globals */struct debug_view debug_raw_view = {	"raw",	NULL,	&debug_raw_header_fn,	&debug_raw_format_fn,	NULL,	NULL};struct debug_view debug_hex_ascii_view = {	"hex_ascii",	NULL,	&debug_dflt_header_fn,	&debug_hex_ascii_format_fn,	NULL,	NULL};struct debug_view debug_level_view = {	"level",	&debug_prolog_level_fn,	NULL,	NULL,	&debug_input_level_fn,	NULL};struct debug_view debug_flush_view = {        "flush",        NULL,        NULL,        NULL,        &debug_input_flush_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;static debug_info_t *debug_area_last = NULL;DECLARE_MUTEX(debug_lock);static int initialized;static struct file_operations debug_file_ops = {	.owner	 = THIS_MODULE,	.read    = debug_output,	.write   = debug_input,		.open    = debug_open,	.release = debug_close,};static struct proc_dir_entry *debug_proc_root_entry;/* functions *//* * debug_info_alloc * - alloc new debug-info */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 */	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);		}	}	/* 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;	strlcpy(rc->name, name, sizeof(rc->name));	memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));#ifdef CONFIG_PROC_FS	memset(rc->proc_entries, 0 ,DEBUG_MAX_VIEWS *		sizeof(struct proc_dir_entry*));#endif /* CONFIG_PROC_FS */	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 */        rc->proc_root_entry = proc_mkdir(rc->name, debug_proc_root_entry);	/* append new element to linked list */        if (debug_area_first == NULL) {                /* first element in list */                debug_area_first = rc;                rc->prev = NULL;        } else {                /* append element to end of list */                debug_area_last->next = rc;                rc->prev = debug_area_last;        }        debug_area_last = rc;        rc->next = NULL;	debug_info_get(rc);out:	return rc;}/* * 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;}/* * debug_info_get * - increments reference count for debug-info */static void debug_info_get(debug_info_t * db_info){	if (db_info)		atomic_inc(&db_info->ref_count);}/* * debug_info_put: * - decreases reference count for debug-info and frees it if necessary */static void debug_info_put(debug_info_t *db_info){	int i;	if (!db_info)		return;	if (atomic_dec_and_test(&db_info->ref_count)) {#ifdef DEBUG		printk(KERN_INFO "debug: freeing debug area %p (%s)\n",		       db_info, db_info->name);#endif		for (i = 0; i < DEBUG_MAX_VIEWS; i++) {			if (db_info->views[i] == NULL)				continue;#ifdef CONFIG_PROC_FS			remove_proc_entry(db_info->proc_entries[i]->name,					  db_info->proc_root_entry);#endif		}#ifdef CONFIG_PROC_FS		remove_proc_entry(db_info->proc_root_entry->name,				  debug_proc_root_entry);#endif		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;		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 debug entries to the user buffer */static ssize_t debug_output(struct file *file,		/* file descriptor */			    char __user *user_buf,	/* user buffer */			    size_t  len,		/* length of buffer */			    loff_t *offset)	      /* offset in the file */{	size_t count = 0;	size_t entry_offset, size = 0;	file_private_info_t *p_info;	p_info = ((file_private_info_t *) file->private_data);	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 (copy_to_user(user_buf + count, 					p_info->temp_buf + entry_offset, size))			return -EFAULT;		}		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;}/* * debug_input: * - called for user write() * - calls input function of view */static ssize_t debug_input(struct file *file,			   const char __user *user_buf, size_t length,			   loff_t *offset){	int rc = 0;	file_private_info_t *p_info;	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_org,					      p_info->view, file, user_buf,					      length, offset);	else		rc = -EPERM;	up(&debug_lock);	return rc;		/* number of input characters */}/* * debug_open: * - called for user open() * - copies formated output to private_data area of the file *   handle */static int debug_open(struct inode *inode, struct file *file){	int i = 0, rc = 0;	file_private_info_t *p_info;	debug_info_t *debug_info, *debug_info_snapshot;#ifdef DEBUG	printk("debug_open\n");#endif	down(&debug_lock);	/* find debug log and view */	debug_info = debug_area_first;	while(debug_info != NULL){		for (i = 0; i < DEBUG_MAX_VIEWS; i++) {			if (debug_info->views[i] == NULL)				continue;			else if (debug_info->proc_entries[i] ==				 PDE(file->f_dentry->d_inode)) {				goto found;	/* found view ! */			}		}		debug_info = debug_info->next;	}	/* no entry found */	rc = -EINVAL;	goto out;      found:	/* make snapshot of current debug areas to get it consistent */	debug_info_snapshot = debug_info_copy(debug_info);	if(!debug_info_snapshot){#ifdef DEBUG		printk(KERN_ERR "debug_open: debug_info_copy failed (out of mem)\n");#endif		rc = -ENOMEM;		goto out;	}	if ((file->private_data =	     kmalloc(sizeof(file_private_info_t), GFP_ATOMIC)) == 0) {#ifdef DEBUG		printk(KERN_ERR "debug_open: kmalloc failed\n");#endif		debug_info_free(debug_info_snapshot);			rc = -ENOMEM;		goto out;	}	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;	debug_info_get(debug_info);      out:	up(&debug_lock);	return rc;}/* * debug_close: * - called for user close() * - deletes  private_data area of the file handle */static int debug_close(struct inode *inode, struct file *file){	file_private_info_t *p_info;#ifdef DEBUG	printk("debug_close\n");#endif	p_info = (file_private_info_t *) file->private_data;	debug_info_free(p_info->debug_info_snap);	debug_info_put(p_info->debug_info_org);	kfree(file->private_data);	return 0;		/* success */}/* * debug_register: * - creates and initializes debug area for the caller * - returns handle for debug area */debug_info_t *debug_register    (char *name, int page_order, int nr_areas, int buf_size) {	debug_info_t *rc = NULL;	if (!initialized)		BUG();	down(&debug_lock);        /* create new debug_info */	rc = debug_info_create(name, page_order, nr_areas, buf_size);	if(!rc) 		goto out;	debug_register_view(rc, &debug_level_view);        debug_register_view(rc, &debug_flush_view);#ifdef DEBUG	printk(KERN_INFO	       "debug: reserved %d areas of %d pages for debugging %s\n",	       nr_areas, 1 << page_order, rc->name);#endif      out:        if (rc == NULL){		printk(KERN_ERR "debug: debug_register failed for %s\n",name);        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -