📄 debug.c
字号:
}/* * debug_output: * - called for user read() * - copies formated debug entries to the user buffer */static ssize_tdebug_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; 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){ int formatted_line_size; int formatted_line_residue; int user_buf_residue; size_t copy_size; formatted_line_size = debug_format_entry(p_info); formatted_line_residue = formatted_line_size - entry_offset; user_buf_residue = len-count; copy_size = min(user_buf_residue, formatted_line_residue); if(copy_size){ if (copy_to_user(user_buf + count, p_info->temp_buf + entry_offset, copy_size)) return -EFAULT; count += copy_size; entry_offset += copy_size; } if(copy_size == formatted_line_residue){ entry_offset = 0; if(debug_next_entry(p_info)) goto out; } }out: p_info->offset = *offset + count; p_info->act_entry_offset = entry_offset; *offset = p_info->offset; return count;}/* * debug_input: * - called for user write() * - calls input function of view */static ssize_tdebug_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 intdebug_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; down(&debug_lock); debug_info = (struct debug_info*)file->f_dentry->d_inode->u.generic_ip; /* find debug view */ for (i = 0; i < DEBUG_MAX_VIEWS; i++) { if (!debug_info->views[i]) continue; else if (debug_info->debugfs_entries[i] == file->f_dentry) { goto found; /* found view ! */ } } /* no entry found */ rc = -EINVAL; goto out;found: /* Make snapshot of current debug areas to get it consistent. */ /* To copy all the areas is only needed, if we have a view which */ /* formats the debug areas. */ if(!debug_info->views[i]->format_proc && !debug_info->views[i]->header_proc){ debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS); } else { debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS); } if(!debug_info_snapshot){ rc = -ENOMEM; goto out; } p_info = (file_private_info_t *) kmalloc(sizeof(file_private_info_t), GFP_KERNEL); if(!p_info){ if(debug_info_snapshot) debug_info_free(debug_info_snapshot); rc = -ENOMEM; goto out; } 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_page = 0; p_info->act_entry = DEBUG_PROLOG_ENTRY; p_info->act_entry_offset = 0; file->private_data = p_info; 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 intdebug_close(struct inode *inode, struct file *file){ file_private_info_t *p_info; p_info = (file_private_info_t *) file->private_data; if(p_info->debug_info_snap) 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 pages_per_area, 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, pages_per_area, nr_areas, buf_size); if(!rc) goto out; debug_register_view(rc, &debug_level_view); debug_register_view(rc, &debug_flush_view); debug_register_view(rc, &debug_pages_view);out: if (!rc){ printk(KERN_ERR "debug: debug_register failed for %s\n",name); } up(&debug_lock); return rc;}/* * debug_unregister: * - give back debug area */voiddebug_unregister(debug_info_t * id){ if (!id) goto out; down(&debug_lock); debug_info_put(id); up(&debug_lock);out: return;}/* * debug_set_size: * - set area size (number of pages) and number of areas */static intdebug_set_size(debug_info_t* id, int nr_areas, int pages_per_area){ unsigned long flags; debug_entry_t *** new_areas; int rc=0; if(!id || (nr_areas <= 0) || (pages_per_area < 0)) return -EINVAL; if(pages_per_area > 0){ new_areas = debug_areas_alloc(pages_per_area, nr_areas); if(!new_areas) { printk(KERN_WARNING "debug: could not allocate memory "\ "for pagenumber: %i\n",pages_per_area); rc = -ENOMEM; goto out; } } else { new_areas = NULL; } spin_lock_irqsave(&id->lock,flags); debug_areas_free(id); id->areas = new_areas; id->nr_areas = nr_areas; id->pages_per_area = pages_per_area; id->active_area = 0; memset(id->active_entries,0,sizeof(int)*id->nr_areas); memset(id->active_pages, 0, sizeof(int)*id->nr_areas); spin_unlock_irqrestore(&id->lock,flags); printk(KERN_INFO "debug: %s: set new size (%i pages)\n"\ ,id->name, pages_per_area);out: return rc;}/* * debug_set_level: * - set actual debug level */voiddebug_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; } spin_unlock_irqrestore(&id->lock,flags);}/* * proceed_active_entry: * - set active entry to next in the ring buffer */extern inline voidproceed_active_entry(debug_info_t * id){ if ((id->active_entries[id->active_area] += id->entry_size) > (PAGE_SIZE - id->entry_size)){ id->active_entries[id->active_area] = 0; id->active_pages[id->active_area] = (id->active_pages[id->active_area] + 1) % id->pages_per_area; }}/* * proceed_active_area: * - set active area to next in the ring buffer */extern inline voidproceed_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_pages[id->active_area]]) + id->active_entries[id->active_area]);}/* * debug_finish_entry: * - set timestamp, caller address, cpu number etc. */extern inline voiddebug_finish_entry(debug_info_t * id, debug_entry_t* active, int level, int exception){ active->id.stck = get_clock(); active->id.fields.cpuid = smp_processor_id(); active->caller = __builtin_return_address(0); active->id.fields.exception = exception; active->id.fields.level = level; proceed_active_entry(id); if(exception) proceed_active_area(id);}static int debug_stoppable=1;static int debug_active=1;#define CTL_S390DBF 5677#define CTL_S390DBF_STOPPABLE 5678#define CTL_S390DBF_ACTIVE 5679/* * proc handler for the running debug_active sysctl * always allow read, allow write only if debug_stoppable is set or * if debug_active is already off */static ints390dbf_procactive(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ if (!write || debug_stoppable || !debug_active) return proc_dointvec(table, write, filp, buffer, lenp, ppos); else return 0;}static struct ctl_table s390dbf_table[] = { { .ctl_name = CTL_S390DBF_STOPPABLE, .procname = "debug_stoppable", .data = &debug_stoppable, .maxlen = sizeof(int), .mode = S_IRUGO | S_IWUSR, .proc_handler = &proc_dointvec, .strategy = &sysctl_intvec, }, { .ctl_name = CTL_S390DBF_ACTIVE, .procname = "debug_active", .data = &debug_active, .maxlen = sizeof(int), .mode = S_IRUGO | S_IWUSR, .proc_handler = &s390dbf_procactive, .strategy = &sysctl_intvec, }, { .ctl_name = 0 }};static struct ctl_table s390dbf_dir_table[] = { { .ctl_name = CTL_S390DBF, .procname = "s390dbf", .maxlen = 0, .mode = S_IRUGO | S_IXUGO, .child = s390dbf_table, }, { .ctl_name = 0 }};struct ctl_table_header *s390dbf_sysctl_header;voiddebug_stop_all(void){ if (debug_stoppable) debug_active = 0;}/* * 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){ unsigned long flags; debug_entry_t *active; if (!debug_active || !id->areas) return NULL; spin_lock_irqsave(&id->lock, flags); active = get_active_entry(id); memset(DEBUG_DATA(active), 0, id->buf_size); memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size)); debug_finish_entry(id, active, level, 0); spin_unlock_irqrestore(&id->lock, flags); return active;}/* * 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){ unsigned long flags; debug_entry_t *active; if (!debug_active || !id->areas) return NULL; spin_lock_irqsave(&id->lock, flags); active = get_active_entry(id); memset(DEBUG_DATA(active), 0, id->buf_size); memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size)); debug_finish_entry(id, active, level, 1); spin_unlock_irqrestore(&id->lock, flags); return active;}/* * counts arguments in format string for sprintf view */extern inline intdebug_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,idx; unsigned long flags; debug_sprintf_entry_t *curr_event; debug_entry_t *active; if((!id) || (level > id->level)) return NULL; if (!debug_active || !id->areas) return NULL; numargs=debug_count_numargs(string); spin_lock_irqsave(&id->lock, flags); active = get_active_entry(id); curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active); va_start(ap,string); curr_event->string=string; for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++) curr_event->args[idx]=va_arg(ap,long); va_end(ap); debug_finish_entry(id, active, level, 0); spin_unlock_irqrestore(&id->lock, flags); return active;}/* * debug_sprintf_exception: */debug_entry_t*debug_sprintf_exception(debug_info_t* id, int level,char *string,...){ va_list ap; int numargs,idx; unsigned long flags; debug_sprintf_entry_t *curr_event; debug_entry_t *active; if((!id) || (level > id->level)) return NULL; if (!debug_active || !id->areas)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -