📄 debug.c
字号:
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); } 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); 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_finish_entry: * - set timestamp, caller address, cpu number etc. */extern inline void debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level, int exception){ 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; 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 int s390dbf_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;void debug_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) 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) 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 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,idx; unsigned long flags; debug_sprintf_entry_t *curr_event; debug_entry_t *active; if((!id) || (level > id->level)) return NULL; if (!debug_active) 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -