📄 debug.c
字号:
{ 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->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 += 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_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 MOD_INC_USE_COUNT; 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]->low_ino == file->f_dentry->d_inode->i_ino) { 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); if (rc != 0) MOD_DEC_USE_COUNT; 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); MOD_DEC_USE_COUNT; return 0; /* success */}/* * debug_create_proc_dir_entry: * - initializes proc-dir-entry and registers it */static struct proc_dir_entry *debug_create_proc_dir_entry (struct proc_dir_entry *root, const char *name, mode_t mode, struct inode_operations *iops, struct file_operations *fops) { struct proc_dir_entry *rc = NULL;#ifdef CONFIG_PROC_FS#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,98)) const char *fn = name; int len; len = strlen(fn); rc = (struct proc_dir_entry *) kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_ATOMIC); if (!rc) goto out; memset(rc, 0, sizeof(struct proc_dir_entry)); memcpy(((char *) rc) + sizeof(*rc), fn, len + 1); rc->name = ((char *) rc) + sizeof(*rc); rc->namelen = len; rc->low_ino = 0, rc->mode = mode; rc->nlink = 1; rc->uid = 0; rc->gid = 0; rc->size = 0; rc->get_info = NULL; rc->ops = iops; proc_register(root, rc);#else rc = create_proc_entry(name, mode, root); if (!rc) goto out; if (fops) rc->proc_fops = fops;#endif out:#endif /* CONFIG_PROC_FS */ return rc;}/* * delete_proc_dir_entry: */static void debug_delete_proc_dir_entry (struct proc_dir_entry *root, struct proc_dir_entry *proc_entry) {#ifdef CONFIG_PROC_FS#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,98)) proc_unregister(root, proc_entry->low_ino); kfree(proc_entry);#else remove_proc_entry(proc_entry->name, root);#endif#endif /* CONFIG_PROC_FS */}/* * 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; MOD_INC_USE_COUNT; if (!initialized) debug_init(); 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); MOD_DEC_USE_COUNT; } up(&debug_lock); return rc;}/* * debug_unregister: * - give back debug area */void debug_unregister(debug_info_t * id){ if (!id) goto out; down(&debug_lock);#ifdef DEBUG printk(KERN_INFO "debug: unregistering %s\n", id->name);#endif debug_info_put(id); up(&debug_lock); MOD_DEC_USE_COUNT; out: return;}/* * debug_set_level: * - set actual debug level */void debug_set_level(debug_info_t* id, int new_level){ unsigned long flags; if(!id) return; spin_lock_irqsave(&id->lock,flags); if(new_level == DEBUG_OFF_LEVEL){ id->level = DEBUG_OFF_LEVEL; printk(KERN_INFO "debug: %s: switched off\n",id->name); } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) { printk(KERN_INFO "debug: %s: level %i is out of range (%i - %i)\n", id->name, new_level, 0, DEBUG_MAX_LEVEL); } else { id->level = new_level;#ifdef DEBUG printk(KERN_INFO "debug: %s: new level %i\n",id->name,id->level);#endif } spin_unlock_irqrestore(&id->lock,flags);}/* * proceed_active_entry: * - set active entry to next in the ring buffer */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)) id->active_entry[id->active_area] = 0;}/* * proceed_active_area: * - set active area to next in the ring buffer */extern inline void proceed_active_area(debug_info_t * id){ id->active_area++; id->active_area = id->active_area % id->nr_areas;}/* * get_active_entry: */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]);}/* * debug_common: * - set timestamp, caller address, cpu number etc. */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->caller = __builtin_return_address(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); return active;}/* * debug_event_common: * - write debug entry with given size */debug_entry_t *debug_event_common(debug_info_t * id, int level, const void *buf, int len){ return debug_common(id, level, buf, len, 0);}/* * debug_exception_common: * - write debug entry with given size and switch to next debug area */debug_entry_t *debug_exception_common(debug_info_t * id, int level, const void *buf, int len){ return debug_common(id, level, buf, len, 1);}/* * counts arguments in format string for sprintf view */extern inline int debug_count_numargs(char *string){ int numargs=0; while(*string) { if(*string++=='%') numargs++; } return(numargs);}/* * debug_sprintf_event: */debug_entry_t *debug_sprintf_event(debug_info_t* id, int level,char *string,...){ 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,0); va_end(ap); } return retval; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -