Back to home page

LXR

 
 

    


0001 /*
0002  * lib/dynamic_debug.c
0003  *
0004  * make pr_debug()/dev_dbg() calls runtime configurable based upon their
0005  * source module.
0006  *
0007  * Copyright (C) 2008 Jason Baron <jbaron@redhat.com>
0008  * By Greg Banks <gnb@melbourne.sgi.com>
0009  * Copyright (c) 2008 Silicon Graphics Inc.  All Rights Reserved.
0010  * Copyright (C) 2011 Bart Van Assche.  All Rights Reserved.
0011  * Copyright (C) 2013 Du, Changbin <changbin.du@gmail.com>
0012  */
0013 
0014 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
0015 
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/moduleparam.h>
0019 #include <linux/kallsyms.h>
0020 #include <linux/types.h>
0021 #include <linux/mutex.h>
0022 #include <linux/proc_fs.h>
0023 #include <linux/seq_file.h>
0024 #include <linux/list.h>
0025 #include <linux/sysctl.h>
0026 #include <linux/ctype.h>
0027 #include <linux/string.h>
0028 #include <linux/parser.h>
0029 #include <linux/string_helpers.h>
0030 #include <linux/uaccess.h>
0031 #include <linux/dynamic_debug.h>
0032 #include <linux/debugfs.h>
0033 #include <linux/slab.h>
0034 #include <linux/jump_label.h>
0035 #include <linux/hardirq.h>
0036 #include <linux/sched.h>
0037 #include <linux/device.h>
0038 #include <linux/netdevice.h>
0039 
0040 extern struct _ddebug __start___verbose[];
0041 extern struct _ddebug __stop___verbose[];
0042 
0043 struct ddebug_table {
0044     struct list_head link;
0045     const char *mod_name;
0046     unsigned int num_ddebugs;
0047     struct _ddebug *ddebugs;
0048 };
0049 
0050 struct ddebug_query {
0051     const char *filename;
0052     const char *module;
0053     const char *function;
0054     const char *format;
0055     unsigned int first_lineno, last_lineno;
0056 };
0057 
0058 struct ddebug_iter {
0059     struct ddebug_table *table;
0060     unsigned int idx;
0061 };
0062 
0063 static DEFINE_MUTEX(ddebug_lock);
0064 static LIST_HEAD(ddebug_tables);
0065 static int verbose;
0066 module_param(verbose, int, 0644);
0067 
0068 /* Return the path relative to source root */
0069 static inline const char *trim_prefix(const char *path)
0070 {
0071     int skip = strlen(__FILE__) - strlen("lib/dynamic_debug.c");
0072 
0073     if (strncmp(path, __FILE__, skip))
0074         skip = 0; /* prefix mismatch, don't skip */
0075 
0076     return path + skip;
0077 }
0078 
0079 static struct { unsigned flag:8; char opt_char; } opt_array[] = {
0080     { _DPRINTK_FLAGS_PRINT, 'p' },
0081     { _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
0082     { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
0083     { _DPRINTK_FLAGS_INCL_LINENO, 'l' },
0084     { _DPRINTK_FLAGS_INCL_TID, 't' },
0085     { _DPRINTK_FLAGS_NONE, '_' },
0086 };
0087 
0088 /* format a string into buf[] which describes the _ddebug's flags */
0089 static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
0090                     size_t maxlen)
0091 {
0092     char *p = buf;
0093     int i;
0094 
0095     BUG_ON(maxlen < 6);
0096     for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
0097         if (dp->flags & opt_array[i].flag)
0098             *p++ = opt_array[i].opt_char;
0099     if (p == buf)
0100         *p++ = '_';
0101     *p = '\0';
0102 
0103     return buf;
0104 }
0105 
0106 #define vpr_info(fmt, ...)                  \
0107 do {                                \
0108     if (verbose)                        \
0109         pr_info(fmt, ##__VA_ARGS__);            \
0110 } while (0)
0111 
0112 static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
0113 {
0114     /* trim any trailing newlines */
0115     int fmtlen = 0;
0116 
0117     if (query->format) {
0118         fmtlen = strlen(query->format);
0119         while (fmtlen && query->format[fmtlen - 1] == '\n')
0120             fmtlen--;
0121     }
0122 
0123     vpr_info("%s: func=\"%s\" file=\"%s\" module=\"%s\" format=\"%.*s\" lineno=%u-%u\n",
0124          msg,
0125          query->function ? query->function : "",
0126          query->filename ? query->filename : "",
0127          query->module ? query->module : "",
0128          fmtlen, query->format ? query->format : "",
0129          query->first_lineno, query->last_lineno);
0130 }
0131 
0132 /*
0133  * Search the tables for _ddebug's which match the given `query' and
0134  * apply the `flags' and `mask' to them.  Returns number of matching
0135  * callsites, normally the same as number of changes.  If verbose,
0136  * logs the changes.  Takes ddebug_lock.
0137  */
0138 static int ddebug_change(const struct ddebug_query *query,
0139             unsigned int flags, unsigned int mask)
0140 {
0141     int i;
0142     struct ddebug_table *dt;
0143     unsigned int newflags;
0144     unsigned int nfound = 0;
0145     char flagbuf[10];
0146 
0147     /* search for matching ddebugs */
0148     mutex_lock(&ddebug_lock);
0149     list_for_each_entry(dt, &ddebug_tables, link) {
0150 
0151         /* match against the module name */
0152         if (query->module &&
0153             !match_wildcard(query->module, dt->mod_name))
0154             continue;
0155 
0156         for (i = 0; i < dt->num_ddebugs; i++) {
0157             struct _ddebug *dp = &dt->ddebugs[i];
0158 
0159             /* match against the source filename */
0160             if (query->filename &&
0161                 !match_wildcard(query->filename, dp->filename) &&
0162                 !match_wildcard(query->filename,
0163                        kbasename(dp->filename)) &&
0164                 !match_wildcard(query->filename,
0165                        trim_prefix(dp->filename)))
0166                 continue;
0167 
0168             /* match against the function */
0169             if (query->function &&
0170                 !match_wildcard(query->function, dp->function))
0171                 continue;
0172 
0173             /* match against the format */
0174             if (query->format &&
0175                 !strstr(dp->format, query->format))
0176                 continue;
0177 
0178             /* match against the line number range */
0179             if (query->first_lineno &&
0180                 dp->lineno < query->first_lineno)
0181                 continue;
0182             if (query->last_lineno &&
0183                 dp->lineno > query->last_lineno)
0184                 continue;
0185 
0186             nfound++;
0187 
0188             newflags = (dp->flags & mask) | flags;
0189             if (newflags == dp->flags)
0190                 continue;
0191 #ifdef HAVE_JUMP_LABEL
0192             if (dp->flags & _DPRINTK_FLAGS_PRINT) {
0193                 if (!(flags & _DPRINTK_FLAGS_PRINT))
0194                     static_branch_disable(&dp->key.dd_key_true);
0195             } else if (flags & _DPRINTK_FLAGS_PRINT)
0196                 static_branch_enable(&dp->key.dd_key_true);
0197 #endif
0198             dp->flags = newflags;
0199             vpr_info("changed %s:%d [%s]%s =%s\n",
0200                  trim_prefix(dp->filename), dp->lineno,
0201                  dt->mod_name, dp->function,
0202                  ddebug_describe_flags(dp, flagbuf,
0203                                sizeof(flagbuf)));
0204         }
0205     }
0206     mutex_unlock(&ddebug_lock);
0207 
0208     if (!nfound && verbose)
0209         pr_info("no matches for query\n");
0210 
0211     return nfound;
0212 }
0213 
0214 /*
0215  * Split the buffer `buf' into space-separated words.
0216  * Handles simple " and ' quoting, i.e. without nested,
0217  * embedded or escaped \".  Return the number of words
0218  * or <0 on error.
0219  */
0220 static int ddebug_tokenize(char *buf, char *words[], int maxwords)
0221 {
0222     int nwords = 0;
0223 
0224     while (*buf) {
0225         char *end;
0226 
0227         /* Skip leading whitespace */
0228         buf = skip_spaces(buf);
0229         if (!*buf)
0230             break;  /* oh, it was trailing whitespace */
0231         if (*buf == '#')
0232             break;  /* token starts comment, skip rest of line */
0233 
0234         /* find `end' of word, whitespace separated or quoted */
0235         if (*buf == '"' || *buf == '\'') {
0236             int quote = *buf++;
0237             for (end = buf; *end && *end != quote; end++)
0238                 ;
0239             if (!*end) {
0240                 pr_err("unclosed quote: %s\n", buf);
0241                 return -EINVAL; /* unclosed quote */
0242             }
0243         } else {
0244             for (end = buf; *end && !isspace(*end); end++)
0245                 ;
0246             BUG_ON(end == buf);
0247         }
0248 
0249         /* `buf' is start of word, `end' is one past its end */
0250         if (nwords == maxwords) {
0251             pr_err("too many words, legal max <=%d\n", maxwords);
0252             return -EINVAL; /* ran out of words[] before bytes */
0253         }
0254         if (*end)
0255             *end++ = '\0';  /* terminate the word */
0256         words[nwords++] = buf;
0257         buf = end;
0258     }
0259 
0260     if (verbose) {
0261         int i;
0262         pr_info("split into words:");
0263         for (i = 0; i < nwords; i++)
0264             pr_cont(" \"%s\"", words[i]);
0265         pr_cont("\n");
0266     }
0267 
0268     return nwords;
0269 }
0270 
0271 /*
0272  * Parse a single line number.  Note that the empty string ""
0273  * is treated as a special case and converted to zero, which
0274  * is later treated as a "don't care" value.
0275  */
0276 static inline int parse_lineno(const char *str, unsigned int *val)
0277 {
0278     BUG_ON(str == NULL);
0279     if (*str == '\0') {
0280         *val = 0;
0281         return 0;
0282     }
0283     if (kstrtouint(str, 10, val) < 0) {
0284         pr_err("bad line-number: %s\n", str);
0285         return -EINVAL;
0286     }
0287     return 0;
0288 }
0289 
0290 static int check_set(const char **dest, char *src, char *name)
0291 {
0292     int rc = 0;
0293 
0294     if (*dest) {
0295         rc = -EINVAL;
0296         pr_err("match-spec:%s val:%s overridden by %s\n",
0297                name, *dest, src);
0298     }
0299     *dest = src;
0300     return rc;
0301 }
0302 
0303 /*
0304  * Parse words[] as a ddebug query specification, which is a series
0305  * of (keyword, value) pairs chosen from these possibilities:
0306  *
0307  * func <function-name>
0308  * file <full-pathname>
0309  * file <base-filename>
0310  * module <module-name>
0311  * format <escaped-string-to-find-in-format>
0312  * line <lineno>
0313  * line <first-lineno>-<last-lineno> // where either may be empty
0314  *
0315  * Only 1 of each type is allowed.
0316  * Returns 0 on success, <0 on error.
0317  */
0318 static int ddebug_parse_query(char *words[], int nwords,
0319             struct ddebug_query *query, const char *modname)
0320 {
0321     unsigned int i;
0322     int rc = 0;
0323 
0324     /* check we have an even number of words */
0325     if (nwords % 2 != 0) {
0326         pr_err("expecting pairs of match-spec <value>\n");
0327         return -EINVAL;
0328     }
0329     memset(query, 0, sizeof(*query));
0330 
0331     if (modname)
0332         /* support $modname.dyndbg=<multiple queries> */
0333         query->module = modname;
0334 
0335     for (i = 0; i < nwords; i += 2) {
0336         if (!strcmp(words[i], "func")) {
0337             rc = check_set(&query->function, words[i+1], "func");
0338         } else if (!strcmp(words[i], "file")) {
0339             rc = check_set(&query->filename, words[i+1], "file");
0340         } else if (!strcmp(words[i], "module")) {
0341             rc = check_set(&query->module, words[i+1], "module");
0342         } else if (!strcmp(words[i], "format")) {
0343             string_unescape_inplace(words[i+1], UNESCAPE_SPACE |
0344                                 UNESCAPE_OCTAL |
0345                                 UNESCAPE_SPECIAL);
0346             rc = check_set(&query->format, words[i+1], "format");
0347         } else if (!strcmp(words[i], "line")) {
0348             char *first = words[i+1];
0349             char *last = strchr(first, '-');
0350             if (query->first_lineno || query->last_lineno) {
0351                 pr_err("match-spec: line used 2x\n");
0352                 return -EINVAL;
0353             }
0354             if (last)
0355                 *last++ = '\0';
0356             if (parse_lineno(first, &query->first_lineno) < 0)
0357                 return -EINVAL;
0358             if (last) {
0359                 /* range <first>-<last> */
0360                 if (parse_lineno(last, &query->last_lineno) < 0)
0361                     return -EINVAL;
0362 
0363                 if (query->last_lineno < query->first_lineno) {
0364                     pr_err("last-line:%d < 1st-line:%d\n",
0365                         query->last_lineno,
0366                         query->first_lineno);
0367                     return -EINVAL;
0368                 }
0369             } else {
0370                 query->last_lineno = query->first_lineno;
0371             }
0372         } else {
0373             pr_err("unknown keyword \"%s\"\n", words[i]);
0374             return -EINVAL;
0375         }
0376         if (rc)
0377             return rc;
0378     }
0379     vpr_info_dq(query, "parsed");
0380     return 0;
0381 }
0382 
0383 /*
0384  * Parse `str' as a flags specification, format [-+=][p]+.
0385  * Sets up *maskp and *flagsp to be used when changing the
0386  * flags fields of matched _ddebug's.  Returns 0 on success
0387  * or <0 on error.
0388  */
0389 static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
0390                    unsigned int *maskp)
0391 {
0392     unsigned flags = 0;
0393     int op = '=', i;
0394 
0395     switch (*str) {
0396     case '+':
0397     case '-':
0398     case '=':
0399         op = *str++;
0400         break;
0401     default:
0402         pr_err("bad flag-op %c, at start of %s\n", *str, str);
0403         return -EINVAL;
0404     }
0405     vpr_info("op='%c'\n", op);
0406 
0407     for (; *str ; ++str) {
0408         for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
0409             if (*str == opt_array[i].opt_char) {
0410                 flags |= opt_array[i].flag;
0411                 break;
0412             }
0413         }
0414         if (i < 0) {
0415             pr_err("unknown flag '%c' in \"%s\"\n", *str, str);
0416             return -EINVAL;
0417         }
0418     }
0419     vpr_info("flags=0x%x\n", flags);
0420 
0421     /* calculate final *flagsp, *maskp according to mask and op */
0422     switch (op) {
0423     case '=':
0424         *maskp = 0;
0425         *flagsp = flags;
0426         break;
0427     case '+':
0428         *maskp = ~0U;
0429         *flagsp = flags;
0430         break;
0431     case '-':
0432         *maskp = ~flags;
0433         *flagsp = 0;
0434         break;
0435     }
0436     vpr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp);
0437     return 0;
0438 }
0439 
0440 static int ddebug_exec_query(char *query_string, const char *modname)
0441 {
0442     unsigned int flags = 0, mask = 0;
0443     struct ddebug_query query;
0444 #define MAXWORDS 9
0445     int nwords, nfound;
0446     char *words[MAXWORDS];
0447 
0448     nwords = ddebug_tokenize(query_string, words, MAXWORDS);
0449     if (nwords <= 0) {
0450         pr_err("tokenize failed\n");
0451         return -EINVAL;
0452     }
0453     /* check flags 1st (last arg) so query is pairs of spec,val */
0454     if (ddebug_parse_flags(words[nwords-1], &flags, &mask)) {
0455         pr_err("flags parse failed\n");
0456         return -EINVAL;
0457     }
0458     if (ddebug_parse_query(words, nwords-1, &query, modname)) {
0459         pr_err("query parse failed\n");
0460         return -EINVAL;
0461     }
0462     /* actually go and implement the change */
0463     nfound = ddebug_change(&query, flags, mask);
0464     vpr_info_dq(&query, nfound ? "applied" : "no-match");
0465 
0466     return nfound;
0467 }
0468 
0469 /* handle multiple queries in query string, continue on error, return
0470    last error or number of matching callsites.  Module name is either
0471    in param (for boot arg) or perhaps in query string.
0472 */
0473 static int ddebug_exec_queries(char *query, const char *modname)
0474 {
0475     char *split;
0476     int i, errs = 0, exitcode = 0, rc, nfound = 0;
0477 
0478     for (i = 0; query; query = split) {
0479         split = strpbrk(query, ";\n");
0480         if (split)
0481             *split++ = '\0';
0482 
0483         query = skip_spaces(query);
0484         if (!query || !*query || *query == '#')
0485             continue;
0486 
0487         vpr_info("query %d: \"%s\"\n", i, query);
0488 
0489         rc = ddebug_exec_query(query, modname);
0490         if (rc < 0) {
0491             errs++;
0492             exitcode = rc;
0493         } else {
0494             nfound += rc;
0495         }
0496         i++;
0497     }
0498     vpr_info("processed %d queries, with %d matches, %d errs\n",
0499          i, nfound, errs);
0500 
0501     if (exitcode)
0502         return exitcode;
0503     return nfound;
0504 }
0505 
0506 #define PREFIX_SIZE 64
0507 
0508 static int remaining(int wrote)
0509 {
0510     if (PREFIX_SIZE - wrote > 0)
0511         return PREFIX_SIZE - wrote;
0512     return 0;
0513 }
0514 
0515 static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf)
0516 {
0517     int pos_after_tid;
0518     int pos = 0;
0519 
0520     *buf = '\0';
0521 
0522     if (desc->flags & _DPRINTK_FLAGS_INCL_TID) {
0523         if (in_interrupt())
0524             pos += snprintf(buf + pos, remaining(pos), "<intr> ");
0525         else
0526             pos += snprintf(buf + pos, remaining(pos), "[%d] ",
0527                     task_pid_vnr(current));
0528     }
0529     pos_after_tid = pos;
0530     if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME)
0531         pos += snprintf(buf + pos, remaining(pos), "%s:",
0532                 desc->modname);
0533     if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
0534         pos += snprintf(buf + pos, remaining(pos), "%s:",
0535                 desc->function);
0536     if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO)
0537         pos += snprintf(buf + pos, remaining(pos), "%d:",
0538                 desc->lineno);
0539     if (pos - pos_after_tid)
0540         pos += snprintf(buf + pos, remaining(pos), " ");
0541     if (pos >= PREFIX_SIZE)
0542         buf[PREFIX_SIZE - 1] = '\0';
0543 
0544     return buf;
0545 }
0546 
0547 void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
0548 {
0549     va_list args;
0550     struct va_format vaf;
0551     char buf[PREFIX_SIZE];
0552 
0553     BUG_ON(!descriptor);
0554     BUG_ON(!fmt);
0555 
0556     va_start(args, fmt);
0557 
0558     vaf.fmt = fmt;
0559     vaf.va = &args;
0560 
0561     printk(KERN_DEBUG "%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf);
0562 
0563     va_end(args);
0564 }
0565 EXPORT_SYMBOL(__dynamic_pr_debug);
0566 
0567 void __dynamic_dev_dbg(struct _ddebug *descriptor,
0568               const struct device *dev, const char *fmt, ...)
0569 {
0570     struct va_format vaf;
0571     va_list args;
0572 
0573     BUG_ON(!descriptor);
0574     BUG_ON(!fmt);
0575 
0576     va_start(args, fmt);
0577 
0578     vaf.fmt = fmt;
0579     vaf.va = &args;
0580 
0581     if (!dev) {
0582         printk(KERN_DEBUG "(NULL device *): %pV", &vaf);
0583     } else {
0584         char buf[PREFIX_SIZE];
0585 
0586         dev_printk_emit(LOGLEVEL_DEBUG, dev, "%s%s %s: %pV",
0587                 dynamic_emit_prefix(descriptor, buf),
0588                 dev_driver_string(dev), dev_name(dev),
0589                 &vaf);
0590     }
0591 
0592     va_end(args);
0593 }
0594 EXPORT_SYMBOL(__dynamic_dev_dbg);
0595 
0596 #ifdef CONFIG_NET
0597 
0598 void __dynamic_netdev_dbg(struct _ddebug *descriptor,
0599               const struct net_device *dev, const char *fmt, ...)
0600 {
0601     struct va_format vaf;
0602     va_list args;
0603 
0604     BUG_ON(!descriptor);
0605     BUG_ON(!fmt);
0606 
0607     va_start(args, fmt);
0608 
0609     vaf.fmt = fmt;
0610     vaf.va = &args;
0611 
0612     if (dev && dev->dev.parent) {
0613         char buf[PREFIX_SIZE];
0614 
0615         dev_printk_emit(LOGLEVEL_DEBUG, dev->dev.parent,
0616                 "%s%s %s %s%s: %pV",
0617                 dynamic_emit_prefix(descriptor, buf),
0618                 dev_driver_string(dev->dev.parent),
0619                 dev_name(dev->dev.parent),
0620                 netdev_name(dev), netdev_reg_state(dev),
0621                 &vaf);
0622     } else if (dev) {
0623         printk(KERN_DEBUG "%s%s: %pV", netdev_name(dev),
0624                netdev_reg_state(dev), &vaf);
0625     } else {
0626         printk(KERN_DEBUG "(NULL net_device): %pV", &vaf);
0627     }
0628 
0629     va_end(args);
0630 }
0631 EXPORT_SYMBOL(__dynamic_netdev_dbg);
0632 
0633 #endif
0634 
0635 #define DDEBUG_STRING_SIZE 1024
0636 static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE];
0637 
0638 static __init int ddebug_setup_query(char *str)
0639 {
0640     if (strlen(str) >= DDEBUG_STRING_SIZE) {
0641         pr_warn("ddebug boot param string too large\n");
0642         return 0;
0643     }
0644     strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE);
0645     return 1;
0646 }
0647 
0648 __setup("ddebug_query=", ddebug_setup_query);
0649 
0650 /*
0651  * File_ops->write method for <debugfs>/dynamic_debug/control.  Gathers the
0652  * command text from userspace, parses and executes it.
0653  */
0654 #define USER_BUF_PAGE 4096
0655 static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
0656                   size_t len, loff_t *offp)
0657 {
0658     char *tmpbuf;
0659     int ret;
0660 
0661     if (len == 0)
0662         return 0;
0663     if (len > USER_BUF_PAGE - 1) {
0664         pr_warn("expected <%d bytes into control\n", USER_BUF_PAGE);
0665         return -E2BIG;
0666     }
0667     tmpbuf = memdup_user_nul(ubuf, len);
0668     if (IS_ERR(tmpbuf))
0669         return PTR_ERR(tmpbuf);
0670     vpr_info("read %d bytes from userspace\n", (int)len);
0671 
0672     ret = ddebug_exec_queries(tmpbuf, NULL);
0673     kfree(tmpbuf);
0674     if (ret < 0)
0675         return ret;
0676 
0677     *offp += len;
0678     return len;
0679 }
0680 
0681 /*
0682  * Set the iterator to point to the first _ddebug object
0683  * and return a pointer to that first object.  Returns
0684  * NULL if there are no _ddebugs at all.
0685  */
0686 static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
0687 {
0688     if (list_empty(&ddebug_tables)) {
0689         iter->table = NULL;
0690         iter->idx = 0;
0691         return NULL;
0692     }
0693     iter->table = list_entry(ddebug_tables.next,
0694                  struct ddebug_table, link);
0695     iter->idx = 0;
0696     return &iter->table->ddebugs[iter->idx];
0697 }
0698 
0699 /*
0700  * Advance the iterator to point to the next _ddebug
0701  * object from the one the iterator currently points at,
0702  * and returns a pointer to the new _ddebug.  Returns
0703  * NULL if the iterator has seen all the _ddebugs.
0704  */
0705 static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
0706 {
0707     if (iter->table == NULL)
0708         return NULL;
0709     if (++iter->idx == iter->table->num_ddebugs) {
0710         /* iterate to next table */
0711         iter->idx = 0;
0712         if (list_is_last(&iter->table->link, &ddebug_tables)) {
0713             iter->table = NULL;
0714             return NULL;
0715         }
0716         iter->table = list_entry(iter->table->link.next,
0717                      struct ddebug_table, link);
0718     }
0719     return &iter->table->ddebugs[iter->idx];
0720 }
0721 
0722 /*
0723  * Seq_ops start method.  Called at the start of every
0724  * read() call from userspace.  Takes the ddebug_lock and
0725  * seeks the seq_file's iterator to the given position.
0726  */
0727 static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
0728 {
0729     struct ddebug_iter *iter = m->private;
0730     struct _ddebug *dp;
0731     int n = *pos;
0732 
0733     vpr_info("called m=%p *pos=%lld\n", m, (unsigned long long)*pos);
0734 
0735     mutex_lock(&ddebug_lock);
0736 
0737     if (!n)
0738         return SEQ_START_TOKEN;
0739     if (n < 0)
0740         return NULL;
0741     dp = ddebug_iter_first(iter);
0742     while (dp != NULL && --n > 0)
0743         dp = ddebug_iter_next(iter);
0744     return dp;
0745 }
0746 
0747 /*
0748  * Seq_ops next method.  Called several times within a read()
0749  * call from userspace, with ddebug_lock held.  Walks to the
0750  * next _ddebug object with a special case for the header line.
0751  */
0752 static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
0753 {
0754     struct ddebug_iter *iter = m->private;
0755     struct _ddebug *dp;
0756 
0757     vpr_info("called m=%p p=%p *pos=%lld\n",
0758          m, p, (unsigned long long)*pos);
0759 
0760     if (p == SEQ_START_TOKEN)
0761         dp = ddebug_iter_first(iter);
0762     else
0763         dp = ddebug_iter_next(iter);
0764     ++*pos;
0765     return dp;
0766 }
0767 
0768 /*
0769  * Seq_ops show method.  Called several times within a read()
0770  * call from userspace, with ddebug_lock held.  Formats the
0771  * current _ddebug as a single human-readable line, with a
0772  * special case for the header line.
0773  */
0774 static int ddebug_proc_show(struct seq_file *m, void *p)
0775 {
0776     struct ddebug_iter *iter = m->private;
0777     struct _ddebug *dp = p;
0778     char flagsbuf[10];
0779 
0780     vpr_info("called m=%p p=%p\n", m, p);
0781 
0782     if (p == SEQ_START_TOKEN) {
0783         seq_puts(m,
0784              "# filename:lineno [module]function flags format\n");
0785         return 0;
0786     }
0787 
0788     seq_printf(m, "%s:%u [%s]%s =%s \"",
0789            trim_prefix(dp->filename), dp->lineno,
0790            iter->table->mod_name, dp->function,
0791            ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf)));
0792     seq_escape(m, dp->format, "\t\r\n\"");
0793     seq_puts(m, "\"\n");
0794 
0795     return 0;
0796 }
0797 
0798 /*
0799  * Seq_ops stop method.  Called at the end of each read()
0800  * call from userspace.  Drops ddebug_lock.
0801  */
0802 static void ddebug_proc_stop(struct seq_file *m, void *p)
0803 {
0804     vpr_info("called m=%p p=%p\n", m, p);
0805     mutex_unlock(&ddebug_lock);
0806 }
0807 
0808 static const struct seq_operations ddebug_proc_seqops = {
0809     .start = ddebug_proc_start,
0810     .next = ddebug_proc_next,
0811     .show = ddebug_proc_show,
0812     .stop = ddebug_proc_stop
0813 };
0814 
0815 /*
0816  * File_ops->open method for <debugfs>/dynamic_debug/control.  Does
0817  * the seq_file setup dance, and also creates an iterator to walk the
0818  * _ddebugs.  Note that we create a seq_file always, even for O_WRONLY
0819  * files where it's not needed, as doing so simplifies the ->release
0820  * method.
0821  */
0822 static int ddebug_proc_open(struct inode *inode, struct file *file)
0823 {
0824     vpr_info("called\n");
0825     return seq_open_private(file, &ddebug_proc_seqops,
0826                 sizeof(struct ddebug_iter));
0827 }
0828 
0829 static const struct file_operations ddebug_proc_fops = {
0830     .owner = THIS_MODULE,
0831     .open = ddebug_proc_open,
0832     .read = seq_read,
0833     .llseek = seq_lseek,
0834     .release = seq_release_private,
0835     .write = ddebug_proc_write
0836 };
0837 
0838 /*
0839  * Allocate a new ddebug_table for the given module
0840  * and add it to the global list.
0841  */
0842 int ddebug_add_module(struct _ddebug *tab, unsigned int n,
0843                  const char *name)
0844 {
0845     struct ddebug_table *dt;
0846     const char *new_name;
0847 
0848     dt = kzalloc(sizeof(*dt), GFP_KERNEL);
0849     if (dt == NULL)
0850         return -ENOMEM;
0851     new_name = kstrdup_const(name, GFP_KERNEL);
0852     if (new_name == NULL) {
0853         kfree(dt);
0854         return -ENOMEM;
0855     }
0856     dt->mod_name = new_name;
0857     dt->num_ddebugs = n;
0858     dt->ddebugs = tab;
0859 
0860     mutex_lock(&ddebug_lock);
0861     list_add_tail(&dt->link, &ddebug_tables);
0862     mutex_unlock(&ddebug_lock);
0863 
0864     vpr_info("%u debug prints in module %s\n", n, dt->mod_name);
0865     return 0;
0866 }
0867 EXPORT_SYMBOL_GPL(ddebug_add_module);
0868 
0869 /* helper for ddebug_dyndbg_(boot|module)_param_cb */
0870 static int ddebug_dyndbg_param_cb(char *param, char *val,
0871                 const char *modname, int on_err)
0872 {
0873     char *sep;
0874 
0875     sep = strchr(param, '.');
0876     if (sep) {
0877         /* needed only for ddebug_dyndbg_boot_param_cb */
0878         *sep = '\0';
0879         modname = param;
0880         param = sep + 1;
0881     }
0882     if (strcmp(param, "dyndbg"))
0883         return on_err; /* determined by caller */
0884 
0885     ddebug_exec_queries((val ? val : "+p"), modname);
0886 
0887     return 0; /* query failure shouldnt stop module load */
0888 }
0889 
0890 /* handle both dyndbg and $module.dyndbg params at boot */
0891 static int ddebug_dyndbg_boot_param_cb(char *param, char *val,
0892                 const char *unused, void *arg)
0893 {
0894     vpr_info("%s=\"%s\"\n", param, val);
0895     return ddebug_dyndbg_param_cb(param, val, NULL, 0);
0896 }
0897 
0898 /*
0899  * modprobe foo finds foo.params in boot-args, strips "foo.", and
0900  * passes them to load_module().  This callback gets unknown params,
0901  * processes dyndbg params, rejects others.
0902  */
0903 int ddebug_dyndbg_module_param_cb(char *param, char *val, const char *module)
0904 {
0905     vpr_info("module: %s %s=\"%s\"\n", module, param, val);
0906     return ddebug_dyndbg_param_cb(param, val, module, -ENOENT);
0907 }
0908 
0909 static void ddebug_table_free(struct ddebug_table *dt)
0910 {
0911     list_del_init(&dt->link);
0912     kfree_const(dt->mod_name);
0913     kfree(dt);
0914 }
0915 
0916 /*
0917  * Called in response to a module being unloaded.  Removes
0918  * any ddebug_table's which point at the module.
0919  */
0920 int ddebug_remove_module(const char *mod_name)
0921 {
0922     struct ddebug_table *dt, *nextdt;
0923     int ret = -ENOENT;
0924 
0925     vpr_info("removing module \"%s\"\n", mod_name);
0926 
0927     mutex_lock(&ddebug_lock);
0928     list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
0929         if (!strcmp(dt->mod_name, mod_name)) {
0930             ddebug_table_free(dt);
0931             ret = 0;
0932         }
0933     }
0934     mutex_unlock(&ddebug_lock);
0935     return ret;
0936 }
0937 EXPORT_SYMBOL_GPL(ddebug_remove_module);
0938 
0939 static void ddebug_remove_all_tables(void)
0940 {
0941     mutex_lock(&ddebug_lock);
0942     while (!list_empty(&ddebug_tables)) {
0943         struct ddebug_table *dt = list_entry(ddebug_tables.next,
0944                               struct ddebug_table,
0945                               link);
0946         ddebug_table_free(dt);
0947     }
0948     mutex_unlock(&ddebug_lock);
0949 }
0950 
0951 static __initdata int ddebug_init_success;
0952 
0953 static int __init dynamic_debug_init_debugfs(void)
0954 {
0955     struct dentry *dir, *file;
0956 
0957     if (!ddebug_init_success)
0958         return -ENODEV;
0959 
0960     dir = debugfs_create_dir("dynamic_debug", NULL);
0961     if (!dir)
0962         return -ENOMEM;
0963     file = debugfs_create_file("control", 0644, dir, NULL,
0964                     &ddebug_proc_fops);
0965     if (!file) {
0966         debugfs_remove(dir);
0967         return -ENOMEM;
0968     }
0969     return 0;
0970 }
0971 
0972 static int __init dynamic_debug_init(void)
0973 {
0974     struct _ddebug *iter, *iter_start;
0975     const char *modname = NULL;
0976     char *cmdline;
0977     int ret = 0;
0978     int n = 0, entries = 0, modct = 0;
0979     int verbose_bytes = 0;
0980 
0981     if (__start___verbose == __stop___verbose) {
0982         pr_warn("_ddebug table is empty in a CONFIG_DYNAMIC_DEBUG build\n");
0983         return 1;
0984     }
0985     iter = __start___verbose;
0986     modname = iter->modname;
0987     iter_start = iter;
0988     for (; iter < __stop___verbose; iter++) {
0989         entries++;
0990         verbose_bytes += strlen(iter->modname) + strlen(iter->function)
0991             + strlen(iter->filename) + strlen(iter->format);
0992 
0993         if (strcmp(modname, iter->modname)) {
0994             modct++;
0995             ret = ddebug_add_module(iter_start, n, modname);
0996             if (ret)
0997                 goto out_err;
0998             n = 0;
0999             modname = iter->modname;
1000             iter_start = iter;
1001         }
1002         n++;
1003     }
1004     ret = ddebug_add_module(iter_start, n, modname);
1005     if (ret)
1006         goto out_err;
1007 
1008     ddebug_init_success = 1;
1009     vpr_info("%d modules, %d entries and %d bytes in ddebug tables, %d bytes in (readonly) verbose section\n",
1010          modct, entries, (int)(modct * sizeof(struct ddebug_table)),
1011          verbose_bytes + (int)(__stop___verbose - __start___verbose));
1012 
1013     /* apply ddebug_query boot param, dont unload tables on err */
1014     if (ddebug_setup_string[0] != '\0') {
1015         pr_warn("ddebug_query param name is deprecated, change it to dyndbg\n");
1016         ret = ddebug_exec_queries(ddebug_setup_string, NULL);
1017         if (ret < 0)
1018             pr_warn("Invalid ddebug boot param %s\n",
1019                 ddebug_setup_string);
1020         else
1021             pr_info("%d changes by ddebug_query\n", ret);
1022     }
1023     /* now that ddebug tables are loaded, process all boot args
1024      * again to find and activate queries given in dyndbg params.
1025      * While this has already been done for known boot params, it
1026      * ignored the unknown ones (dyndbg in particular).  Reusing
1027      * parse_args avoids ad-hoc parsing.  This will also attempt
1028      * to activate queries for not-yet-loaded modules, which is
1029      * slightly noisy if verbose, but harmless.
1030      */
1031     cmdline = kstrdup(saved_command_line, GFP_KERNEL);
1032     parse_args("dyndbg params", cmdline, NULL,
1033            0, 0, 0, NULL, &ddebug_dyndbg_boot_param_cb);
1034     kfree(cmdline);
1035     return 0;
1036 
1037 out_err:
1038     ddebug_remove_all_tables();
1039     return 0;
1040 }
1041 /* Allow early initialization for boot messages via boot param */
1042 early_initcall(dynamic_debug_init);
1043 
1044 /* Debugfs setup must be done later */
1045 fs_initcall(dynamic_debug_init_debugfs);