/* * lib/dynamic_debug.c * * make pr_debug()/dev_dbg() calls runtime configurable based upon their * source module. * * Copyright (C) 2008 Jason Baron <jbaron@redhat.com> * By Greg Banks <gnb@melbourne.sgi.com> * Copyright (c) 2008 Silicon Graphics Inc. All Rights Reserved. * Copyright (C) 2011 Bart Van Assche. All Rights Reserved. */ #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ #include <linux/kernel.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/kallsyms.h>#include <linux/types.h>#include <linux/mutex.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <linux/list.h>#include <linux/sysctl.h>#include <linux/ctype.h>#include <linux/string.h>#include <linux/uaccess.h>#include <linux/dynamic_debug.h>#include <linux/debugfs.h>#include <linux/slab.h>#include <linux/jump_label.h>#include <linux/hardirq.h>#include <linux/sched.h>#include <linux/device.h>#include <linux/netdevice.h> extern struct _ddebug __start___verbose[];extern struct _ddebug __stop___verbose[]; struct ddebug_table { struct list_head link; char *mod_name; unsigned int num_ddebugs; struct _ddebug *ddebugs;}; struct ddebug_query { const char *filename; const char *module; const char *function; const char *format; unsigned int first_lineno, last_lineno;}; struct ddebug_iter { struct ddebug_table *table; unsigned int idx;}; static DEFINE_MUTEX(ddebug_lock);static LIST_HEAD(ddebug_tables);static int verbose = 0; /* Return the last part of a pathname */static inline const char *basename(const char *path){ const char *tail = strrchr(path, '/'); return tail ? tail+1 : path;} static struct { unsigned flag:8; char opt_char; } opt_array[] = { { _DPRINTK_FLAGS_PRINT, 'p' }, { _DPRINTK_FLAGS_INCL_MODNAME, 'm' }, { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' }, { _DPRINTK_FLAGS_INCL_LINENO, 'l' }, { _DPRINTK_FLAGS_INCL_TID, 't' },}; /* format a string into buf[] which describes the _ddebug's flags */static char *ddebug_describe_flags(struct _ddebug *dp, char *buf, size_t maxlen){ char *p = buf; int i; BUG_ON(maxlen < 4); for (i = 0; i < ARRAY_SIZE(opt_array); ++i) if (dp->flags & opt_array[i].flag) *p++ = opt_array[i].opt_char; if (p == buf) *p++ = '-'; *p = '\0'; return buf;} /* * Search the tables for _ddebug's which match the given * `query' and apply the `flags' and `mask' to them. Tells * the user which ddebug's were changed, or whether none * were matched. */static void ddebug_change(const struct ddebug_query *query, unsigned int flags, unsigned int mask){ int i; struct ddebug_table *dt; unsigned int newflags; unsigned int nfound = 0; char flagbuf[8]; /* search for matching ddebugs */ mutex_lock(&ddebug_lock); list_for_each_entry(dt, &ddebug_tables, link) { /* match against the module name */ if (query->module != NULL && strcmp(query->module, dt->mod_name)) continue; for (i = 0 ; i < dt->num_ddebugs ; i++) { struct _ddebug *dp = &dt->ddebugs[i]; /* match against the source filename */ if (query->filename != NULL && strcmp(query->filename, dp->filename) && strcmp(query->filename, basename(dp->filename))) continue; /* match against the function */ if (query->function != NULL && strcmp(query->function, dp->function)) continue; /* match against the format */ if (query->format != NULL && strstr(dp->format, query->format) == NULL) continue; /* match against the line number range */ if (query->first_lineno && dp->lineno < query->first_lineno) continue; if (query->last_lineno && dp->lineno > query->last_lineno) continue; nfound++; newflags = (dp->flags & mask) | flags; if (newflags == dp->flags) continue; dp->flags = newflags; if (newflags) dp->enabled = 1; else dp->enabled = 0; if (verbose) pr_info("changed %s:%d [%s]%s %s\n", dp->filename, dp->lineno, dt->mod_name, dp->function, ddebug_describe_flags(dp, flagbuf, sizeof(flagbuf))); } } mutex_unlock(&ddebug_lock); if (!nfound && verbose) pr_info("no matches for query\n");} /* * Split the buffer `buf' into space-separated words. * Handles simple " and ' quoting, i.e. without nested, * embedded or escaped \". Return the number of words * or <0 on error. */static int ddebug_tokenize(char *buf, char *words[], int maxwords){ int nwords = 0; while (*buf) { char *end; /* Skip leading whitespace */ buf = skip_spaces(buf); if (!*buf) break; /* oh, it was trailing whitespace */ /* Run `end' over a word, either whitespace separated or quoted */ if (*buf == '"' || *buf == '\'') { int quote = *buf++; for (end = buf ; *end && *end != quote ; end++) ; if (!*end) return -EINVAL; /* unclosed quote */ } else { for (end = buf ; *end && !isspace(*end) ; end++) ; BUG_ON(end == buf); } /* Here `buf' is the start of the word, `end' is one past the end */ if (nwords == maxwords) return -EINVAL; /* ran out of words[] before bytes */ if (*end) *end++ = '\0'; /* terminate the word */ words[nwords++] = buf; buf = end; } if (verbose) { int i; pr_info("split into words:"); for (i = 0 ; i < nwords ; i++) pr_cont(" \"%s\"", words[i]); pr_cont("\n"); } return nwords;} /* * Parse a single line number. Note that the empty string "" * is treated as a special case and converted to zero, which * is later treated as a "don't care" value. */static inline int parse_lineno(const char *str, unsigned int *val){ char *end = NULL; BUG_ON(str == NULL); if (*str == '\0') { *val = 0; return 0; } *val = simple_strtoul(str, &end, 10); return end == NULL || end == str || *end != '\0' ? -EINVAL : 0;} /* * Undo octal escaping in a string, inplace. This is useful to * allow the user to express a query which matches a format * containing embedded spaces. */#define isodigit(c) ((c) >= '0' && (c) <= '7')static char *unescape(char *str){ char *in = str; char *out = str; while (*in) { if (*in == '\\') { if (in[1] == '\\') { *out++ = '\\'; in += 2; continue; } else if (in[1] == 't') { *out++ = '\t'; in += 2; continue; } else if (in[1] == 'n') { *out++ = '\n'; in += 2; continue; } else if (isodigit(in[1]) && isodigit(in[2]) && isodigit(in[3])) { *out++ = ((in[1] - '0')<<6) | ((in[2] - '0')<<3) | (in[3] - '0'); in += 4; continue; } } *out++ = *in++; } *out = '\0'; return str;} /* * Parse words[] as a ddebug query specification, which is a series * of (keyword, value) pairs chosen from these possibilities: * * func <function-name> * file <full-pathname> * file <base-filename> * module <module-name> * format <escaped-string-to-find-in-format> * line <lineno> * line <first-lineno>-<last-lineno> // where either may be empty */static int ddebug_parse_query(char *words[], int nwords, struct ddebug_query *query){ unsigned int i; /* check we have an even number of words */ if (nwords % 2 != 0) return -EINVAL; memset(query, 0, sizeof(*query)); for (i = 0 ; i < nwords ; i += 2) { if (!strcmp(words[i], "func")) query->function = words[i+1]; else if (!strcmp(words[i], "file")) query->filename = words[i+1]; else if (!strcmp(words[i], "module")) query->module = words[i+1]; else if (!strcmp(words[i], "format")) query->format = unescape(words[i+1]); else if (!strcmp(words[i], "line")) { char *first = words[i+1]; char *last = strchr(first, '-'); if (last) *last++ = '\0'; if (parse_lineno(first, &query->first_lineno) < 0) return -EINVAL; if (last != NULL) { /* range <first>-<last> */ if (parse_lineno(last, &query->last_lineno) < 0) return -EINVAL; } else { query->last_lineno = query->first_lineno; } } else { if (verbose) pr_err("unknown keyword \"%s\"\n", words[i]); return -EINVAL; } } if (verbose) pr_info("q->function=\"%s\" q->filename=\"%s\" " "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n", query->function, query->filename, query->module, query->format, query->first_lineno, query->last_lineno); return 0;} /* * Parse `str' as a flags specification, format [-+=][p]+. * Sets up *maskp and *flagsp to be used when changing the * flags fields of matched _ddebug's. Returns 0 on success * or <0 on error. */static int ddebug_parse_flags(const char *str, unsigned int *flagsp, unsigned int *maskp){ unsigned flags = 0; int op = '=', i; switch (*str) { case '+': case '-': case '=': op = *str++; break; default: return -EINVAL; } if (verbose) pr_info("op='%c'\n", op); for ( ; *str ; ++str) { for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) { if (*str == opt_array[i].opt_char) { flags |= opt_array[i].flag; break; } } if (i < 0) return -EINVAL; } if (flags == 0) return -EINVAL; if (verbose) pr_info("flags=0x%x\n", flags); /* calculate final *flagsp, *maskp according to mask and op */ switch (op) { case '=': *maskp = 0; *flagsp = flags; break; case '+': *maskp = ~0U; *flagsp = flags; break; case '-': *maskp = ~flags; *flagsp = 0; break; } if (verbose) pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp); return 0;} static int ddebug_exec_query(char *query_string){ unsigned int flags = 0, mask = 0; struct ddebug_query query;#define MAXWORDS 9 int nwords; char *words[MAXWORDS]; nwords = ddebug_tokenize(query_string, words, MAXWORDS); if (nwords <= 0) return -EINVAL; if (ddebug_parse_query(words, nwords-1, &query)) return -EINVAL; if (ddebug_parse_flags(words[nwords-1], &flags, &mask)) return -EINVAL; /* actually go and implement the change */ ddebug_change(&query, flags, mask); return 0;} #define PREFIX_SIZE 64 static int remaining(int wrote){ if (PREFIX_SIZE - wrote > 0) return PREFIX_SIZE - wrote; return 0;} static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf){ int pos_after_tid; int pos = 0; pos += snprintf(buf + pos, remaining(pos), "%s", KERN_DEBUG); if (desc->flags & _DPRINTK_FLAGS_INCL_TID) { if (in_interrupt()) pos += snprintf(buf + pos, remaining(pos), "%s ", "<intr>"); else pos += snprintf(buf + pos, remaining(pos), "[%d] ", task_pid_vnr(current)); } pos_after_tid = pos; if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME) pos += snprintf(buf + pos, remaining(pos), "%s:", desc->modname); if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) pos += snprintf(buf + pos, remaining(pos), "%s:", desc->function); if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO) pos += snprintf(buf + pos, remaining(pos), "%d:", desc->lineno); if (pos - pos_after_tid) pos += snprintf(buf + pos, remaining(pos), " "); if (pos >= PREFIX_SIZE) buf[PREFIX_SIZE - 1] = '\0'; return buf;} int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...){ va_list args; int res; struct va_format vaf; char buf[PREFIX_SIZE]; BUG_ON(!descriptor); BUG_ON(!fmt); va_start(args, fmt); vaf.fmt = fmt; vaf.va = &args; res = printk("%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf); va_end(args); return res;}EXPORT_SYMBOL(__dynamic_pr_debug); int __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, const char *fmt, ...){ struct va_format vaf; va_list args; int res; char buf[PREFIX_SIZE]; BUG_ON(!descriptor); BUG_ON(!fmt); va_start(args, fmt); vaf.fmt = fmt; vaf.va = &args; res = __dev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf); va_end(args); Lines 1–500 of 888. Load more
/* * lib/dynamic_debug.c * * make pr_debug()/dev_dbg() calls runtime configurable based upon their * source module. * * Copyright (C) 2008 Jason Baron <jbaron@redhat.com> * By Greg Banks <gnb@melbourne.sgi.com> * Copyright (c) 2008 Silicon Graphics Inc. All Rights Reserved. * Copyright (C) 2011 Bart Van Assche. All Rights Reserved. */ #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ #include <linux/kernel.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/kallsyms.h> #include <linux/types.h> #include <linux/mutex.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/list.h> #include <linux/sysctl.h> #include <linux/ctype.h> #include <linux/string.h> #include <linux/uaccess.h> #include <linux/dynamic_debug.h> #include <linux/debugfs.h> #include <linux/slab.h> #include <linux/jump_label.h> #include <linux/hardirq.h> #include <linux/sched.h> #include <linux/device.h> #include <linux/netdevice.h> extern struct _ddebug __start___verbose[]; extern struct _ddebug __stop___verbose[]; struct ddebug_table { struct list_head link; char *mod_name; unsigned int num_ddebugs; struct _ddebug *ddebugs; }; struct ddebug_query { const char *filename; const char *module; const char *function; const char *format; unsigned int first_lineno, last_lineno; }; struct ddebug_iter { struct ddebug_table *table; unsigned int idx; }; static DEFINE_MUTEX(ddebug_lock); static LIST_HEAD(ddebug_tables); static int verbose = 0; /* Return the last part of a pathname */ static inline const char *basename(const char *path) { const char *tail = strrchr(path, '/'); return tail ? tail+1 : path; } static struct { unsigned flag:8; char opt_char; } opt_array[] = { { _DPRINTK_FLAGS_PRINT, 'p' }, { _DPRINTK_FLAGS_INCL_MODNAME, 'm' }, { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' }, { _DPRINTK_FLAGS_INCL_LINENO, 'l' }, { _DPRINTK_FLAGS_INCL_TID, 't' }, }; /* format a string into buf[] which describes the _ddebug's flags */ static char *ddebug_describe_flags(struct _ddebug *dp, char *buf, size_t maxlen) { char *p = buf; int i; BUG_ON(maxlen < 4); for (i = 0; i < ARRAY_SIZE(opt_array); ++i) if (dp->flags & opt_array[i].flag) *p++ = opt_array[i].opt_char; if (p == buf) *p++ = '-'; *p = '\0'; return buf; } /* * Search the tables for _ddebug's which match the given * `query' and apply the `flags' and `mask' to them. Tells * the user which ddebug's were changed, or whether none * were matched. */ static void ddebug_change(const struct ddebug_query *query, unsigned int flags, unsigned int mask) { int i; struct ddebug_table *dt; unsigned int newflags; unsigned int nfound = 0; char flagbuf[8]; /* search for matching ddebugs */ mutex_lock(&ddebug_lock); list_for_each_entry(dt, &ddebug_tables, link) { /* match against the module name */ if (query->module != NULL && strcmp(query->module, dt->mod_name)) continue; for (i = 0 ; i < dt->num_ddebugs ; i++) { struct _ddebug *dp = &dt->ddebugs[i]; /* match against the source filename */ if (query->filename != NULL && strcmp(query->filename, dp->filename) && strcmp(query->filename, basename(dp->filename))) continue; /* match against the function */ if (query->function != NULL && strcmp(query->function, dp->function)) continue; /* match against the format */ if (query->format != NULL && strstr(dp->format, query->format) == NULL) continue; /* match against the line number range */ if (query->first_lineno && dp->lineno < query->first_lineno) continue; if (query->last_lineno && dp->lineno > query->last_lineno) continue; nfound++; newflags = (dp->flags & mask) | flags; if (newflags == dp->flags) continue; dp->flags = newflags; if (newflags) dp->enabled = 1; else dp->enabled = 0; if (verbose) pr_info("changed %s:%d [%s]%s %s\n", dp->filename, dp->lineno, dt->mod_name, dp->function, ddebug_describe_flags(dp, flagbuf, sizeof(flagbuf))); } } mutex_unlock(&ddebug_lock); if (!nfound && verbose) pr_info("no matches for query\n"); } /* * Split the buffer `buf' into space-separated words. * Handles simple " and ' quoting, i.e. without nested, * embedded or escaped \". Return the number of words * or <0 on error. */ static int ddebug_tokenize(char *buf, char *words[], int maxwords) { int nwords = 0; while (*buf) { char *end; /* Skip leading whitespace */ buf = skip_spaces(buf); if (!*buf) break; /* oh, it was trailing whitespace */ /* Run `end' over a word, either whitespace separated or quoted */ if (*buf == '"' || *buf == '\'') { int quote = *buf++; for (end = buf ; *end && *end != quote ; end++) ; if (!*end) return -EINVAL; /* unclosed quote */ } else { for (end = buf ; *end && !isspace(*end) ; end++) ; BUG_ON(end == buf); } /* Here `buf' is the start of the word, `end' is one past the end */ if (nwords == maxwords) return -EINVAL; /* ran out of words[] before bytes */ if (*end) *end++ = '\0'; /* terminate the word */ words[nwords++] = buf; buf = end; } if (verbose) { int i; pr_info("split into words:"); for (i = 0 ; i < nwords ; i++) pr_cont(" \"%s\"", words[i]); pr_cont("\n"); } return nwords; } /* * Parse a single line number. Note that the empty string "" * is treated as a special case and converted to zero, which * is later treated as a "don't care" value. */ static inline int parse_lineno(const char *str, unsigned int *val) { char *end = NULL; BUG_ON(str == NULL); if (*str == '\0') { *val = 0; return 0; } *val = simple_strtoul(str, &end, 10); return end == NULL || end == str || *end != '\0' ? -EINVAL : 0; } /* * Undo octal escaping in a string, inplace. This is useful to * allow the user to express a query which matches a format * containing embedded spaces. */ #define isodigit(c) ((c) >= '0' && (c) <= '7') static char *unescape(char *str) { char *in = str; char *out = str; while (*in) { if (*in == '\\') { if (in[1] == '\\') { *out++ = '\\'; in += 2; continue; } else if (in[1] == 't') { *out++ = '\t'; in += 2; continue; } else if (in[1] == 'n') { *out++ = '\n'; in += 2; continue; } else if (isodigit(in[1]) && isodigit(in[2]) && isodigit(in[3])) { *out++ = ((in[1] - '0')<<6) | ((in[2] - '0')<<3) | (in[3] - '0'); in += 4; continue; } } *out++ = *in++; } *out = '\0'; return str; } /* * Parse words[] as a ddebug query specification, which is a series * of (keyword, value) pairs chosen from these possibilities: * * func <function-name> * file <full-pathname> * file <base-filename> * module <module-name> * format <escaped-string-to-find-in-format> * line <lineno> * line <first-lineno>-<last-lineno> // where either may be empty */ static int ddebug_parse_query(char *words[], int nwords, struct ddebug_query *query) { unsigned int i; /* check we have an even number of words */ if (nwords % 2 != 0) return -EINVAL; memset(query, 0, sizeof(*query)); for (i = 0 ; i < nwords ; i += 2) { if (!strcmp(words[i], "func")) query->function = words[i+1]; else if (!strcmp(words[i], "file")) query->filename = words[i+1]; else if (!strcmp(words[i], "module")) query->module = words[i+1]; else if (!strcmp(words[i], "format")) query->format = unescape(words[i+1]); else if (!strcmp(words[i], "line")) { char *first = words[i+1]; char *last = strchr(first, '-'); if (last) *last++ = '\0'; if (parse_lineno(first, &query->first_lineno) < 0) return -EINVAL; if (last != NULL) { /* range <first>-<last> */ if (parse_lineno(last, &query->last_lineno) < 0) return -EINVAL; } else { query->last_lineno = query->first_lineno; } } else { if (verbose) pr_err("unknown keyword \"%s\"\n", words[i]); return -EINVAL; } } if (verbose) pr_info("q->function=\"%s\" q->filename=\"%s\" " "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n", query->function, query->filename, query->module, query->format, query->first_lineno, query->last_lineno); return 0; } /* * Parse `str' as a flags specification, format [-+=][p]+. * Sets up *maskp and *flagsp to be used when changing the * flags fields of matched _ddebug's. Returns 0 on success * or <0 on error. */ static int ddebug_parse_flags(const char *str, unsigned int *flagsp, unsigned int *maskp) { unsigned flags = 0; int op = '=', i; switch (*str) { case '+': case '-': case '=': op = *str++; break; default: return -EINVAL; } if (verbose) pr_info("op='%c'\n", op); for ( ; *str ; ++str) { for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) { if (*str == opt_array[i].opt_char) { flags |= opt_array[i].flag; break; } } if (i < 0) return -EINVAL; } if (flags == 0) return -EINVAL; if (verbose) pr_info("flags=0x%x\n", flags); /* calculate final *flagsp, *maskp according to mask and op */ switch (op) { case '=': *maskp = 0; *flagsp = flags; break; case '+': *maskp = ~0U; *flagsp = flags; break; case '-': *maskp = ~flags; *flagsp = 0; break; } if (verbose) pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp); return 0; } static int ddebug_exec_query(char *query_string) { unsigned int flags = 0, mask = 0; struct ddebug_query query; #define MAXWORDS 9 int nwords; char *words[MAXWORDS]; nwords = ddebug_tokenize(query_string, words, MAXWORDS); if (nwords <= 0) return -EINVAL; if (ddebug_parse_query(words, nwords-1, &query)) return -EINVAL; if (ddebug_parse_flags(words[nwords-1], &flags, &mask)) return -EINVAL; /* actually go and implement the change */ ddebug_change(&query, flags, mask); return 0; } #define PREFIX_SIZE 64 static int remaining(int wrote) { if (PREFIX_SIZE - wrote > 0) return PREFIX_SIZE - wrote; return 0; } static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf) { int pos_after_tid; int pos = 0; pos += snprintf(buf + pos, remaining(pos), "%s", KERN_DEBUG); if (desc->flags & _DPRINTK_FLAGS_INCL_TID) { if (in_interrupt()) pos += snprintf(buf + pos, remaining(pos), "%s ", "<intr>"); else pos += snprintf(buf + pos, remaining(pos), "[%d] ", task_pid_vnr(current)); } pos_after_tid = pos; if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME) pos += snprintf(buf + pos, remaining(pos), "%s:", desc->modname); if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) pos += snprintf(buf + pos, remaining(pos), "%s:", desc->function); if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO) pos += snprintf(buf + pos, remaining(pos), "%d:", desc->lineno); if (pos - pos_after_tid) pos += snprintf(buf + pos, remaining(pos), " "); if (pos >= PREFIX_SIZE) buf[PREFIX_SIZE - 1] = '\0'; return buf; } int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...) { va_list args; int res; struct va_format vaf; char buf[PREFIX_SIZE]; BUG_ON(!descriptor); BUG_ON(!fmt); va_start(args, fmt); vaf.fmt = fmt; vaf.va = &args; res = printk("%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf); va_end(args); return res; } EXPORT_SYMBOL(__dynamic_pr_debug); int __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, const char *fmt, ...) { struct va_format vaf; va_list args; int res; char buf[PREFIX_SIZE]; BUG_ON(!descriptor); BUG_ON(!fmt); va_start(args, fmt); vaf.fmt = fmt; vaf.va = &args; res = __dev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf); va_end(args);