cregit-Linux how code gets into the kernel

Release 4.8 lib/dynamic_debug.c

Directory: lib
/*
 * 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.
 * Copyright (C) 2013 Du, Changbin <changbin.du@gmail.com>
 */


#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/parser.h>
#include <linux/string_helpers.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;
	
const 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;
module_param(verbose, int, 0644);

/* Return the path relative to source root */

static inline const char *trim_prefix(const char *path) { int skip = strlen(__FILE__) - strlen("lib/dynamic_debug.c"); if (strncmp(path, __FILE__, skip)) skip = 0; /* prefix mismatch, don't skip */ return path + skip; }

Contributors

PersonTokensPropCommitsCommitProp
jim cromiejim cromie48100.00%1100.00%
Total48100.00%1100.00%

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' }, { _DPRINTK_FLAGS_NONE, '_' }, }; /* 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 < 6); 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; }

Contributors

PersonTokensPropCommitsCommitProp
jason baronjason baron6265.26%133.33%
bart van asschebart van assche3132.63%133.33%
jim cromiejim cromie22.11%133.33%
Total95100.00%3100.00%

#define vpr_info(fmt, ...) \ do { \ if (verbose) \ pr_info(fmt, ##__VA_ARGS__); \ } while (0)
static void vpr_info_dq(const struct ddebug_query *query, const char *msg) { /* trim any trailing newlines */ int fmtlen = 0; if (query->format) { fmtlen = strlen(query->format); while (fmtlen && query->format[fmtlen - 1] == '\n') fmtlen--; } vpr_info("%s: func=\"%s\" file=\"%s\" module=\"%s\" format=\"%.*s\" lineno=%u-%u\n", msg, query->function ? query->function : "", query->filename ? query->filename : "", query->module ? query->module : "", fmtlen, query->format ? query->format : "", query->first_lineno, query->last_lineno); }

Contributors

PersonTokensPropCommitsCommitProp
joe perchesjoe perches11095.65%150.00%
jim cromiejim cromie54.35%150.00%
Total115100.00%2100.00%

/* * Search the tables for _ddebug's which match the given `query' and * apply the `flags' and `mask' to them. Returns number of matching * callsites, normally the same as number of changes. If verbose, * logs the changes. Takes ddebug_lock. */
static int 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[10]; /* search for matching ddebugs */ mutex_lock(&ddebug_lock); list_for_each_entry(dt, &ddebug_tables, link) { /* match against the module name */ if (query->module && !match_wildcard(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 && !match_wildcard(query->filename, dp->filename) && !match_wildcard(query->filename, kbasename(dp->filename)) && !match_wildcard(query->filename, trim_prefix(dp->filename))) continue; /* match against the function */ if (query->function && !match_wildcard(query->function, dp->function)) continue; /* match against the format */ if (query->format && !strstr(dp->format, query->format)) 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; #ifdef HAVE_JUMP_LABEL if (dp->flags & _DPRINTK_FLAGS_PRINT) { if (!(flags & _DPRINTK_FLAGS_PRINT)) static_branch_disable(&dp->key.dd_key_true); } else if (flags & _DPRINTK_FLAGS_PRINT) static_branch_enable(&dp->key.dd_key_true); #endif dp->flags = newflags; vpr_info("changed %s:%d [%s]%s =%s\n", trim_prefix(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"); return nfound; }

Contributors

PersonTokensPropCommitsCommitProp
jason baronjason baron33390.00%220.00%
jim cromiejim cromie246.49%550.00%
changbin duchangbin du102.70%110.00%
joe perchesjoe perches20.54%110.00%
andy shevchenkoandy shevchenko10.27%110.00%
Total370100.00%10100.00%

/* * 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 */ if (*buf == '#') break; /* token starts comment, skip rest of line */ /* find `end' of word, whitespace separated or quoted */ if (*buf == '"' || *buf == '\'') { int quote = *buf++; for (end = buf; *end && *end != quote; end++) ; if (!*end) { pr_err("unclosed quote: %s\n", buf); return -EINVAL; /* unclosed quote */ } } else { for (end = buf; *end && !isspace(*end); end++) ; BUG_ON(end == buf); } /* `buf' is start of word, `end' is one past its end */ if (nwords == maxwords) { pr_err("too many words, legal max <=%d\n", 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; }

Contributors

PersonTokensPropCommitsCommitProp
greg banksgreg banks12552.30%114.29%
jason baronjason baron7933.05%114.29%
jim cromiejim cromie2912.13%342.86%
joe perchesjoe perches41.67%114.29%
andre goddard rosaandre goddard rosa20.84%114.29%
Total239100.00%7100.00%

/* * 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) { BUG_ON(str == NULL); if (*str == '\0') { *val = 0; return 0; } if (kstrtouint(str, 10, val) < 0) { pr_err("bad line-number: %s\n", str); return -EINVAL; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jason baronjason baron5071.43%133.33%
jim cromiejim cromie1318.57%133.33%
andrey ryabininandrey ryabinin710.00%133.33%
Total70100.00%3100.00%


static int check_set(const char **dest, char *src, char *name) { int rc = 0; if (*dest) { rc = -EINVAL; pr_err("match-spec:%s val:%s overridden by %s\n", name, *dest, src); } *dest = src; return rc; }

Contributors

PersonTokensPropCommitsCommitProp
jim cromiejim cromie5698.25%150.00%
joe perchesjoe perches11.75%150.00%
Total57100.00%2100.00%

/* * 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 * * Only 1 of each type is allowed. * Returns 0 on success, <0 on error. */
static int ddebug_parse_query(char *words[], int nwords, struct ddebug_query *query, const char *modname) { unsigned int i; int rc = 0; /* check we have an even number of words */ if (nwords % 2 != 0) { pr_err("expecting pairs of match-spec <value>\n"); return -EINVAL; } memset(query, 0, sizeof(*query)); if (modname) /* support $modname.dyndbg=<multiple queries> */ query->module = modname; for (i = 0; i < nwords; i += 2) { if (!strcmp(words[i], "func")) { rc = check_set(&query->function, words[i+1], "func"); } else if (!strcmp(words[i], "file")) { rc = check_set(&query->filename, words[i+1], "file"); } else if (!strcmp(words[i], "module")) { rc = check_set(&query->module, words[i+1], "module"); } else if (!strcmp(words[i], "format")) { string_unescape_inplace(words[i+1], UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_SPECIAL); rc = check_set(&query->format, words[i+1], "format"); } else if (!strcmp(words[i], "line")) { char *first = words[i+1]; char *last = strchr(first, '-'); if (query->first_lineno || query->last_lineno) { pr_err("match-spec: line used 2x\n"); return -EINVAL; } if (last) *last++ = '\0'; if (parse_lineno(first, &query->first_lineno) < 0) return -EINVAL; if (last) { /* range <first>-<last> */ if (parse_lineno(last, &query->last_lineno) < 0) return -EINVAL; if (query->last_lineno < query->first_lineno) { pr_err("last-line:%d < 1st-line:%d\n", query->last_lineno, query->first_lineno); return -EINVAL; } } else { query->last_lineno = query->first_lineno; } } else { pr_err("unknown keyword \"%s\"\n", words[i]); return -EINVAL; } if (rc) return rc; } vpr_info_dq(query, "parsed"); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jason baronjason baron28665.90%220.00%
jim cromiejim cromie11025.35%440.00%
andy shevchenkoandy shevchenko163.69%110.00%
andrey ryabininandrey ryabinin122.76%110.00%
joe perchesjoe perches102.30%220.00%
Total434100.00%10100.00%

/* * 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: pr_err("bad flag-op %c, at start of %s\n", *str, str); return -EINVAL; } vpr_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) { pr_err("unknown flag '%c' in \"%s\"\n", *str, str); return -EINVAL; } } vpr_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; } vpr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jason baronjason baron15668.72%120.00%
bart van asschebart van assche4318.94%120.00%
jim cromiejim cromie2511.01%240.00%
joe perchesjoe perches31.32%120.00%
Total227100.00%5100.00%


static int ddebug_exec_query(char *query_string, const char *modname) { unsigned int flags = 0, mask = 0; struct ddebug_query query; #define MAXWORDS 9 int nwords, nfound; char *words[MAXWORDS]; nwords = ddebug_tokenize(query_string, words, MAXWORDS); if (nwords <= 0) { pr_err("tokenize failed\n"); return -EINVAL; } /* check flags 1st (last arg) so query is pairs of spec,val */ if (ddebug_parse_flags(words[nwords-1], &flags, &mask)) { pr_err("flags parse failed\n"); return -EINVAL; } if (ddebug_parse_query(words, nwords-1, &query, modname)) { pr_err("query parse failed\n"); return -EINVAL; } /* actually go and implement the change */ nfound = ddebug_change(&query, flags, mask); vpr_info_dq(&query, nfound ? "applied" : "no-match"); return nfound; }

Contributors

PersonTokensPropCommitsCommitProp
thomas renningerthomas renninger6742.41%120.00%
jim cromiejim cromie5736.08%360.00%
jason baronjason baron3421.52%120.00%
Total158100.00%5100.00%

/* handle multiple queries in query string, continue on error, return last error or number of matching callsites. Module name is either in param (for boot arg) or perhaps in query string. */
static int ddebug_exec_queries(char *query, const char *modname) { char *split; int i, errs = 0, exitcode = 0, rc, nfound = 0; for (i = 0; query; query = split) { split = strpbrk(query, ";\n"); if (split) *split++ = '\0'; query = skip_spaces(query); if (!query || !*query || *query == '#') continue; vpr_info("query %d: \"%s\"\n", i, query); rc = ddebug_exec_query(query, modname); if (rc < 0) { errs++; exitcode = rc; } else { nfound += rc; } i++; } vpr_info("processed %d queries, with %d matches, %d errs\n", i, nfound, errs); if (exitcode) return exitcode; return nfound; }

Contributors

PersonTokensPropCommitsCommitProp
jim cromiejim cromie15096.77%360.00%
thomas renningerthomas renninger31.94%120.00%
joe perchesjoe perches21.29%120.00%
Total155100.00%5100.00%

#define PREFIX_SIZE 64
static int remaining(int wrote) { if (PREFIX_SIZE - wrote > 0) return PREFIX_SIZE - wrote; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jason baronjason baron2392.00%133.33%
bart van asschebart van assche14.00%133.33%
joe perchesjoe perches14.00%133.33%
Total25100.00%3100.00%


static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf) { int pos_after_tid; int pos = 0; *buf = '\0'; if (desc->flags & _DPRINTK_FLAGS_INCL_TID) { if (in_interrupt()) pos += snprintf(buf + pos, remaining(pos), "<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; }

Contributors

PersonTokensPropCommitsCommitProp
jason baronjason baron9645.50%120.00%
bart van asschebart van assche9042.65%120.00%
joe perchesjoe perches2511.85%360.00%
Total211100.00%5100.00%


void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...) { va_list args; struct va_format vaf; char buf[PREFIX_SIZE]; BUG_ON(!descriptor); BUG_ON(!fmt); va_start(args, fmt); vaf.fmt = fmt; vaf.va = &args; printk(KERN_DEBUG "%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf); va_end(args); }

Contributors

PersonTokensPropCommitsCommitProp
joe perchesjoe perches4048.19%360.00%
jason baronjason baron3036.14%120.00%
bart van asschebart van assche1315.66%120.00%
Total83100.00%5100.00%

EXPORT_SYMBOL(__dynamic_pr_debug);
void __dynamic_dev_dbg(struct _ddebug *descriptor, const struct device *dev, const char *fmt, ...) { struct va_format vaf; va_list args; BUG_ON(!descriptor); BUG_ON(!fmt); va_start(args, fmt); vaf.fmt = fmt; vaf.va = &args; if (!dev) { printk(KERN_DEBUG "(NULL device *): %pV", &vaf); } else { char buf[PREFIX_SIZE]; dev_printk_emit(LOGLEVEL_DEBUG, dev, "%s%s %s: %pV", dynamic_emit_prefix(descriptor, buf), dev_driver_string(dev), dev_name(dev), &vaf); } va_end(args); }

Contributors

PersonTokensPropCommitsCommitProp
joe perchesjoe perches11897.52%685.71%
jason baronjason baron32.48%114.29%
Total121100.00%7100.00%

EXPORT_SYMBOL(__dynamic_dev_dbg); #ifdef CONFIG_NET
void __dynamic_netdev_dbg(struct _ddebug *descriptor, const struct net_device *dev, const char *fmt, ...) { struct va_format vaf; va_list args; BUG_ON(!descriptor); BUG_ON(!fmt); va_start(args, fmt); vaf.fmt = fmt; vaf.va = &args; if (dev && dev->dev.parent) { char buf[PREFIX_SIZE]; dev_printk_emit(LOGLEVEL_DEBUG, dev->dev.parent, "%s%s %s %s%s: %pV", dynamic_emit_prefix(descriptor, buf), dev_driver_string(dev->dev.parent), dev_name(dev->dev.parent), netdev_name(dev), netdev_reg_state(dev), &vaf); } else if (dev) { printk(KERN_DEBUG "%s%s: %pV", netdev_name(dev), netdev_reg_state(dev), &vaf); } else { printk(KERN_DEBUG "(NULL net_device): %pV", &vaf); } va_end(args); }

Contributors

PersonTokensPropCommitsCommitProp
joe perchesjoe perches8347.70%457.14%
jason baronjason baron7945.40%228.57%
veaceslav falicoveaceslav falico126.90%114.29%
Total174100.00%7100.00%

EXPORT_SYMBOL(__dynamic_netdev_dbg); #endif #define DDEBUG_STRING_SIZE 1024 static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE];
static __init int ddebug_setup_query(char *str) { if (strlen(str) >= DDEBUG_STRING_SIZE) { pr_warn("ddebug boot param string too large\n"); return 0; } strlcpy