Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 1 | /* |
| 2 | * lib/dynamic_debug.c |
| 3 | * |
| 4 | * make pr_debug()/dev_dbg() calls runtime configurable based upon their |
| 5 | * source module. |
| 6 | * |
| 7 | * Copyright (C) 2008 Jason Baron <jbaron@redhat.com> |
| 8 | * By Greg Banks <gnb@melbourne.sgi.com> |
| 9 | * Copyright (c) 2008 Silicon Graphics Inc. All Rights Reserved. |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 10 | * Copyright (C) 2011 Bart Van Assche. All Rights Reserved. |
Du, Changbin | 578b1e0 | 2014-01-23 15:54:14 -0800 | [diff] [blame] | 11 | * Copyright (C) 2013 Du, Changbin <changbin.du@gmail.com> |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 12 | */ |
| 13 | |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ |
| 15 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
Greg Kroah-Hartman | fef15d2 | 2012-05-07 16:47:32 -0700 | [diff] [blame] | 18 | #include <linux/moduleparam.h> |
| 19 | #include <linux/kallsyms.h> |
| 20 | #include <linux/types.h> |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Greg Kroah-Hartman | fef15d2 | 2012-05-07 16:47:32 -0700 | [diff] [blame] | 22 | #include <linux/proc_fs.h> |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 23 | #include <linux/seq_file.h> |
Greg Kroah-Hartman | fef15d2 | 2012-05-07 16:47:32 -0700 | [diff] [blame] | 24 | #include <linux/list.h> |
| 25 | #include <linux/sysctl.h> |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 26 | #include <linux/ctype.h> |
Greg Kroah-Hartman | fef15d2 | 2012-05-07 16:47:32 -0700 | [diff] [blame] | 27 | #include <linux/string.h> |
Du, Changbin | 578b1e0 | 2014-01-23 15:54:14 -0800 | [diff] [blame] | 28 | #include <linux/parser.h> |
Andy Shevchenko | d338b13 | 2013-04-30 15:27:32 -0700 | [diff] [blame] | 29 | #include <linux/string_helpers.h> |
Greg Kroah-Hartman | fef15d2 | 2012-05-07 16:47:32 -0700 | [diff] [blame] | 30 | #include <linux/uaccess.h> |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 31 | #include <linux/dynamic_debug.h> |
| 32 | #include <linux/debugfs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Greg Kroah-Hartman | fef15d2 | 2012-05-07 16:47:32 -0700 | [diff] [blame] | 34 | #include <linux/jump_label.h> |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 35 | #include <linux/hardirq.h> |
Greg Kroah-Hartman | e8d9792 | 2011-02-03 15:59:58 -0800 | [diff] [blame] | 36 | #include <linux/sched.h> |
Greg Kroah-Hartman | fef15d2 | 2012-05-07 16:47:32 -0700 | [diff] [blame] | 37 | #include <linux/device.h> |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 38 | #include <linux/netdevice.h> |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 39 | |
Gal Pressman | 923abb9 | 2019-05-01 13:48:13 +0300 | [diff] [blame] | 40 | #include <rdma/ib_verbs.h> |
| 41 | |
Jim Cromie | e5ebffe | 2020-07-19 17:10:45 -0600 | [diff] [blame] | 42 | extern struct _ddebug __start___dyndbg[]; |
| 43 | extern struct _ddebug __stop___dyndbg[]; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 44 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 45 | struct ddebug_table { |
| 46 | struct list_head link; |
Rasmus Villemoes | 3e406b1 | 2015-11-06 16:30:15 -0800 | [diff] [blame] | 47 | const char *mod_name; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 48 | unsigned int num_ddebugs; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 49 | struct _ddebug *ddebugs; |
| 50 | }; |
| 51 | |
| 52 | struct ddebug_query { |
| 53 | const char *filename; |
| 54 | const char *module; |
| 55 | const char *function; |
| 56 | const char *format; |
| 57 | unsigned int first_lineno, last_lineno; |
| 58 | }; |
| 59 | |
| 60 | struct ddebug_iter { |
| 61 | struct ddebug_table *table; |
| 62 | unsigned int idx; |
| 63 | }; |
| 64 | |
| 65 | static DEFINE_MUTEX(ddebug_lock); |
| 66 | static LIST_HEAD(ddebug_tables); |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 67 | static int verbose; |
Jim Cromie | 74df138 | 2011-12-19 17:12:24 -0500 | [diff] [blame] | 68 | module_param(verbose, int, 0644); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 69 | |
Jim Cromie | 2b67831 | 2011-12-19 17:13:12 -0500 | [diff] [blame] | 70 | /* Return the path relative to source root */ |
| 71 | static inline const char *trim_prefix(const char *path) |
| 72 | { |
| 73 | int skip = strlen(__FILE__) - strlen("lib/dynamic_debug.c"); |
| 74 | |
| 75 | if (strncmp(path, __FILE__, skip)) |
| 76 | skip = 0; /* prefix mismatch, don't skip */ |
| 77 | |
| 78 | return path + skip; |
| 79 | } |
| 80 | |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 81 | static struct { unsigned flag:8; char opt_char; } opt_array[] = { |
| 82 | { _DPRINTK_FLAGS_PRINT, 'p' }, |
| 83 | { _DPRINTK_FLAGS_INCL_MODNAME, 'm' }, |
| 84 | { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' }, |
| 85 | { _DPRINTK_FLAGS_INCL_LINENO, 'l' }, |
| 86 | { _DPRINTK_FLAGS_INCL_TID, 't' }, |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 87 | { _DPRINTK_FLAGS_NONE, '_' }, |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
Jim Cromie | f678ce8 | 2020-07-19 17:10:47 -0600 | [diff] [blame] | 90 | struct flagsbuf { char buf[ARRAY_SIZE(opt_array)+1]; }; |
| 91 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 92 | /* format a string into buf[] which describes the _ddebug's flags */ |
Jim Cromie | f678ce8 | 2020-07-19 17:10:47 -0600 | [diff] [blame] | 93 | static char *ddebug_describe_flags(unsigned int flags, struct flagsbuf *fb) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 94 | { |
Jim Cromie | f678ce8 | 2020-07-19 17:10:47 -0600 | [diff] [blame] | 95 | char *p = fb->buf; |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 96 | int i; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 97 | |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 98 | for (i = 0; i < ARRAY_SIZE(opt_array); ++i) |
Jim Cromie | f678ce8 | 2020-07-19 17:10:47 -0600 | [diff] [blame] | 99 | if (flags & opt_array[i].flag) |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 100 | *p++ = opt_array[i].opt_char; |
Jim Cromie | f678ce8 | 2020-07-19 17:10:47 -0600 | [diff] [blame] | 101 | if (p == fb->buf) |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 102 | *p++ = '_'; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 103 | *p = '\0'; |
| 104 | |
Jim Cromie | f678ce8 | 2020-07-19 17:10:47 -0600 | [diff] [blame] | 105 | return fb->buf; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Jim Cromie | 481c0e3 | 2020-07-19 17:10:44 -0600 | [diff] [blame] | 108 | #define vnpr_info(lvl, fmt, ...) \ |
Jim Cromie | b8ccd5d | 2012-04-27 14:30:32 -0600 | [diff] [blame] | 109 | do { \ |
Jim Cromie | 481c0e3 | 2020-07-19 17:10:44 -0600 | [diff] [blame] | 110 | if (verbose >= lvl) \ |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 111 | pr_info(fmt, ##__VA_ARGS__); \ |
Jim Cromie | 574b372 | 2011-12-19 17:13:16 -0500 | [diff] [blame] | 112 | } while (0) |
| 113 | |
Jim Cromie | 481c0e3 | 2020-07-19 17:10:44 -0600 | [diff] [blame] | 114 | #define vpr_info(fmt, ...) vnpr_info(1, fmt, ##__VA_ARGS__) |
| 115 | #define v2pr_info(fmt, ...) vnpr_info(2, fmt, ##__VA_ARGS__) |
| 116 | |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 117 | static void vpr_info_dq(const struct ddebug_query *query, const char *msg) |
| 118 | { |
| 119 | /* trim any trailing newlines */ |
| 120 | int fmtlen = 0; |
| 121 | |
| 122 | if (query->format) { |
| 123 | fmtlen = strlen(query->format); |
| 124 | while (fmtlen && query->format[fmtlen - 1] == '\n') |
| 125 | fmtlen--; |
| 126 | } |
| 127 | |
| 128 | vpr_info("%s: func=\"%s\" file=\"%s\" module=\"%s\" format=\"%.*s\" lineno=%u-%u\n", |
| 129 | msg, |
Jim Cromie | f62fc08 | 2020-07-19 17:10:51 -0600 | [diff] [blame] | 130 | query->function ?: "", |
| 131 | query->filename ?: "", |
| 132 | query->module ?: "", |
| 133 | fmtlen, query->format ?: "", |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 134 | query->first_lineno, query->last_lineno); |
| 135 | } |
| 136 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 137 | /* |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 138 | * Search the tables for _ddebug's which match the given `query' and |
| 139 | * apply the `flags' and `mask' to them. Returns number of matching |
| 140 | * callsites, normally the same as number of changes. If verbose, |
| 141 | * logs the changes. Takes ddebug_lock. |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 142 | */ |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 143 | static int ddebug_change(const struct ddebug_query *query, |
| 144 | unsigned int flags, unsigned int mask) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 145 | { |
| 146 | int i; |
| 147 | struct ddebug_table *dt; |
| 148 | unsigned int newflags; |
| 149 | unsigned int nfound = 0; |
Jim Cromie | f678ce8 | 2020-07-19 17:10:47 -0600 | [diff] [blame] | 150 | struct flagsbuf fbuf; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 151 | |
| 152 | /* search for matching ddebugs */ |
| 153 | mutex_lock(&ddebug_lock); |
| 154 | list_for_each_entry(dt, &ddebug_tables, link) { |
| 155 | |
| 156 | /* match against the module name */ |
Du, Changbin | 578b1e0 | 2014-01-23 15:54:14 -0800 | [diff] [blame] | 157 | if (query->module && |
| 158 | !match_wildcard(query->module, dt->mod_name)) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 159 | continue; |
| 160 | |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 161 | for (i = 0; i < dt->num_ddebugs; i++) { |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 162 | struct _ddebug *dp = &dt->ddebugs[i]; |
| 163 | |
| 164 | /* match against the source filename */ |
Jim Cromie | d6a238d | 2011-12-19 17:12:39 -0500 | [diff] [blame] | 165 | if (query->filename && |
Du, Changbin | 578b1e0 | 2014-01-23 15:54:14 -0800 | [diff] [blame] | 166 | !match_wildcard(query->filename, dp->filename) && |
| 167 | !match_wildcard(query->filename, |
| 168 | kbasename(dp->filename)) && |
| 169 | !match_wildcard(query->filename, |
| 170 | trim_prefix(dp->filename))) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 171 | continue; |
| 172 | |
| 173 | /* match against the function */ |
Jim Cromie | d6a238d | 2011-12-19 17:12:39 -0500 | [diff] [blame] | 174 | if (query->function && |
Du, Changbin | 578b1e0 | 2014-01-23 15:54:14 -0800 | [diff] [blame] | 175 | !match_wildcard(query->function, dp->function)) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 176 | continue; |
| 177 | |
| 178 | /* match against the format */ |
Jim Cromie | d6a238d | 2011-12-19 17:12:39 -0500 | [diff] [blame] | 179 | if (query->format && |
| 180 | !strstr(dp->format, query->format)) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 181 | continue; |
| 182 | |
| 183 | /* match against the line number range */ |
| 184 | if (query->first_lineno && |
| 185 | dp->lineno < query->first_lineno) |
| 186 | continue; |
| 187 | if (query->last_lineno && |
| 188 | dp->lineno > query->last_lineno) |
| 189 | continue; |
| 190 | |
| 191 | nfound++; |
| 192 | |
| 193 | newflags = (dp->flags & mask) | flags; |
| 194 | if (newflags == dp->flags) |
| 195 | continue; |
Masahiro Yamada | e9666d1 | 2018-12-31 00:14:15 +0900 | [diff] [blame] | 196 | #ifdef CONFIG_JUMP_LABEL |
Jason Baron | 9049fc7 | 2016-08-03 13:46:39 -0700 | [diff] [blame] | 197 | if (dp->flags & _DPRINTK_FLAGS_PRINT) { |
| 198 | if (!(flags & _DPRINTK_FLAGS_PRINT)) |
| 199 | static_branch_disable(&dp->key.dd_key_true); |
| 200 | } else if (flags & _DPRINTK_FLAGS_PRINT) |
| 201 | static_branch_enable(&dp->key.dd_key_true); |
| 202 | #endif |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 203 | dp->flags = newflags; |
Jim Cromie | 481c0e3 | 2020-07-19 17:10:44 -0600 | [diff] [blame] | 204 | v2pr_info("changed %s:%d [%s]%s =%s\n", |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 205 | trim_prefix(dp->filename), dp->lineno, |
| 206 | dt->mod_name, dp->function, |
Jim Cromie | f678ce8 | 2020-07-19 17:10:47 -0600 | [diff] [blame] | 207 | ddebug_describe_flags(dp->flags, &fbuf)); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | mutex_unlock(&ddebug_lock); |
| 211 | |
| 212 | if (!nfound && verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 213 | pr_info("no matches for query\n"); |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 214 | |
| 215 | return nfound; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 219 | * Split the buffer `buf' into space-separated words. |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 220 | * Handles simple " and ' quoting, i.e. without nested, |
| 221 | * embedded or escaped \". Return the number of words |
| 222 | * or <0 on error. |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 223 | */ |
| 224 | static int ddebug_tokenize(char *buf, char *words[], int maxwords) |
| 225 | { |
| 226 | int nwords = 0; |
| 227 | |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 228 | while (*buf) { |
| 229 | char *end; |
| 230 | |
| 231 | /* Skip leading whitespace */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 232 | buf = skip_spaces(buf); |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 233 | if (!*buf) |
| 234 | break; /* oh, it was trailing whitespace */ |
Jim Cromie | 8bd6026 | 2011-12-19 17:13:03 -0500 | [diff] [blame] | 235 | if (*buf == '#') |
| 236 | break; /* token starts comment, skip rest of line */ |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 237 | |
Jim Cromie | 07100be | 2011-12-19 17:11:09 -0500 | [diff] [blame] | 238 | /* find `end' of word, whitespace separated or quoted */ |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 239 | if (*buf == '"' || *buf == '\'') { |
| 240 | int quote = *buf++; |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 241 | for (end = buf; *end && *end != quote; end++) |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 242 | ; |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 243 | if (!*end) { |
| 244 | pr_err("unclosed quote: %s\n", buf); |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 245 | return -EINVAL; /* unclosed quote */ |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 246 | } |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 247 | } else { |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 248 | for (end = buf; *end && !isspace(*end); end++) |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 249 | ; |
| 250 | BUG_ON(end == buf); |
| 251 | } |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 252 | |
Jim Cromie | 07100be | 2011-12-19 17:11:09 -0500 | [diff] [blame] | 253 | /* `buf' is start of word, `end' is one past its end */ |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 254 | if (nwords == maxwords) { |
| 255 | pr_err("too many words, legal max <=%d\n", maxwords); |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 256 | return -EINVAL; /* ran out of words[] before bytes */ |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 257 | } |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 258 | if (*end) |
| 259 | *end++ = '\0'; /* terminate the word */ |
| 260 | words[nwords++] = buf; |
| 261 | buf = end; |
| 262 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 263 | |
| 264 | if (verbose) { |
| 265 | int i; |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 266 | pr_info("split into words:"); |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 267 | for (i = 0; i < nwords; i++) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 268 | pr_cont(" \"%s\"", words[i]); |
| 269 | pr_cont("\n"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | return nwords; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Parse a single line number. Note that the empty string "" |
| 277 | * is treated as a special case and converted to zero, which |
| 278 | * is later treated as a "don't care" value. |
| 279 | */ |
| 280 | static inline int parse_lineno(const char *str, unsigned int *val) |
| 281 | { |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 282 | BUG_ON(str == NULL); |
| 283 | if (*str == '\0') { |
| 284 | *val = 0; |
| 285 | return 0; |
| 286 | } |
Andrey Ryabinin | 4592599 | 2014-01-27 17:06:59 -0800 | [diff] [blame] | 287 | if (kstrtouint(str, 10, val) < 0) { |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 288 | pr_err("bad line-number: %s\n", str); |
| 289 | return -EINVAL; |
| 290 | } |
| 291 | return 0; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 292 | } |
| 293 | |
Jim Cromie | 8037072 | 2020-07-19 17:10:52 -0600 | [diff] [blame] | 294 | static int parse_linerange(struct ddebug_query *query, const char *first) |
| 295 | { |
| 296 | char *last = strchr(first, '-'); |
| 297 | |
| 298 | if (query->first_lineno || query->last_lineno) { |
| 299 | pr_err("match-spec: line used 2x\n"); |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | if (last) |
| 303 | *last++ = '\0'; |
| 304 | if (parse_lineno(first, &query->first_lineno) < 0) |
| 305 | return -EINVAL; |
| 306 | if (last) { |
| 307 | /* range <first>-<last> */ |
| 308 | if (parse_lineno(last, &query->last_lineno) < 0) |
| 309 | return -EINVAL; |
| 310 | |
| 311 | /* special case for last lineno not specified */ |
| 312 | if (query->last_lineno == 0) |
| 313 | query->last_lineno = UINT_MAX; |
| 314 | |
| 315 | if (query->last_lineno < query->first_lineno) { |
| 316 | pr_err("last-line:%d < 1st-line:%d\n", |
| 317 | query->last_lineno, |
| 318 | query->first_lineno); |
| 319 | return -EINVAL; |
| 320 | } |
| 321 | } else { |
| 322 | query->last_lineno = query->first_lineno; |
| 323 | } |
| 324 | vpr_info("parsed line %d-%d\n", query->first_lineno, |
| 325 | query->last_lineno); |
| 326 | return 0; |
| 327 | } |
| 328 | |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 329 | static int check_set(const char **dest, char *src, char *name) |
| 330 | { |
| 331 | int rc = 0; |
| 332 | |
| 333 | if (*dest) { |
| 334 | rc = -EINVAL; |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 335 | pr_err("match-spec:%s val:%s overridden by %s\n", |
| 336 | name, *dest, src); |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 337 | } |
| 338 | *dest = src; |
| 339 | return rc; |
| 340 | } |
| 341 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 342 | /* |
| 343 | * Parse words[] as a ddebug query specification, which is a series |
| 344 | * of (keyword, value) pairs chosen from these possibilities: |
| 345 | * |
| 346 | * func <function-name> |
| 347 | * file <full-pathname> |
| 348 | * file <base-filename> |
| 349 | * module <module-name> |
| 350 | * format <escaped-string-to-find-in-format> |
| 351 | * line <lineno> |
| 352 | * line <first-lineno>-<last-lineno> // where either may be empty |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 353 | * |
| 354 | * Only 1 of each type is allowed. |
| 355 | * Returns 0 on success, <0 on error. |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 356 | */ |
| 357 | static int ddebug_parse_query(char *words[], int nwords, |
Jim Cromie | 8e59b5cf | 2012-04-27 14:30:40 -0600 | [diff] [blame] | 358 | struct ddebug_query *query, const char *modname) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 359 | { |
| 360 | unsigned int i; |
jbaron@akamai.com | bd8c154a | 2013-08-27 16:50:03 +0000 | [diff] [blame] | 361 | int rc = 0; |
Jim Cromie | aaebe32 | 2020-07-19 17:10:53 -0600 | [diff] [blame^] | 362 | char *fline; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 363 | |
| 364 | /* check we have an even number of words */ |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 365 | if (nwords % 2 != 0) { |
| 366 | pr_err("expecting pairs of match-spec <value>\n"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 367 | return -EINVAL; |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 368 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 369 | |
Jim Cromie | 8e59b5cf | 2012-04-27 14:30:40 -0600 | [diff] [blame] | 370 | if (modname) |
| 371 | /* support $modname.dyndbg=<multiple queries> */ |
| 372 | query->module = modname; |
| 373 | |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 374 | for (i = 0; i < nwords; i += 2) { |
| 375 | if (!strcmp(words[i], "func")) { |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 376 | rc = check_set(&query->function, words[i+1], "func"); |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 377 | } else if (!strcmp(words[i], "file")) { |
Jim Cromie | aaebe32 | 2020-07-19 17:10:53 -0600 | [diff] [blame^] | 378 | if (check_set(&query->filename, words[i+1], "file")) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | /* tail :$info is function or line-range */ |
| 382 | fline = strchr(query->filename, ':'); |
| 383 | if (!fline) |
| 384 | break; |
| 385 | *fline++ = '\0'; |
| 386 | if (isalpha(*fline) || *fline == '*' || *fline == '?') { |
| 387 | /* take as function name */ |
| 388 | if (check_set(&query->function, fline, "func")) |
| 389 | return -EINVAL; |
| 390 | } else { |
| 391 | if (parse_linerange(query, fline)) |
| 392 | return -EINVAL; |
| 393 | } |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 394 | } else if (!strcmp(words[i], "module")) { |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 395 | rc = check_set(&query->module, words[i+1], "module"); |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 396 | } else if (!strcmp(words[i], "format")) { |
Andy Shevchenko | d338b13 | 2013-04-30 15:27:32 -0700 | [diff] [blame] | 397 | string_unescape_inplace(words[i+1], UNESCAPE_SPACE | |
| 398 | UNESCAPE_OCTAL | |
| 399 | UNESCAPE_SPECIAL); |
| 400 | rc = check_set(&query->format, words[i+1], "format"); |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 401 | } else if (!strcmp(words[i], "line")) { |
Jim Cromie | 8037072 | 2020-07-19 17:10:52 -0600 | [diff] [blame] | 402 | if (parse_linerange(query, words[i+1])) |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 403 | return -EINVAL; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 404 | } else { |
Jim Cromie | ae27f86 | 2011-12-19 17:12:34 -0500 | [diff] [blame] | 405 | pr_err("unknown keyword \"%s\"\n", words[i]); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 406 | return -EINVAL; |
| 407 | } |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 408 | if (rc) |
| 409 | return rc; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 410 | } |
Jim Cromie | 574b372 | 2011-12-19 17:13:16 -0500 | [diff] [blame] | 411 | vpr_info_dq(query, "parsed"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Parse `str' as a flags specification, format [-+=][p]+. |
| 417 | * Sets up *maskp and *flagsp to be used when changing the |
| 418 | * flags fields of matched _ddebug's. Returns 0 on success |
| 419 | * or <0 on error. |
| 420 | */ |
| 421 | static int ddebug_parse_flags(const char *str, unsigned int *flagsp, |
| 422 | unsigned int *maskp) |
| 423 | { |
| 424 | unsigned flags = 0; |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 425 | int op = '=', i; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 426 | |
| 427 | switch (*str) { |
| 428 | case '+': |
| 429 | case '-': |
| 430 | case '=': |
| 431 | op = *str++; |
| 432 | break; |
| 433 | default: |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 434 | pr_err("bad flag-op %c, at start of %s\n", *str, str); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 435 | return -EINVAL; |
| 436 | } |
Jim Cromie | b8ccd5d | 2012-04-27 14:30:32 -0600 | [diff] [blame] | 437 | vpr_info("op='%c'\n", op); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 438 | |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 439 | for (; *str ; ++str) { |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 440 | for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) { |
| 441 | if (*str == opt_array[i].opt_char) { |
| 442 | flags |= opt_array[i].flag; |
| 443 | break; |
| 444 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 445 | } |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 446 | if (i < 0) { |
Jim Cromie | 0b8f96b | 2020-07-19 17:10:48 -0600 | [diff] [blame] | 447 | pr_err("unknown flag '%c'\n", *str); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 448 | return -EINVAL; |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 449 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 450 | } |
Jim Cromie | b8ccd5d | 2012-04-27 14:30:32 -0600 | [diff] [blame] | 451 | vpr_info("flags=0x%x\n", flags); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 452 | |
| 453 | /* calculate final *flagsp, *maskp according to mask and op */ |
| 454 | switch (op) { |
| 455 | case '=': |
| 456 | *maskp = 0; |
| 457 | *flagsp = flags; |
| 458 | break; |
| 459 | case '+': |
| 460 | *maskp = ~0U; |
| 461 | *flagsp = flags; |
| 462 | break; |
| 463 | case '-': |
| 464 | *maskp = ~flags; |
| 465 | *flagsp = 0; |
| 466 | break; |
| 467 | } |
Jim Cromie | b8ccd5d | 2012-04-27 14:30:32 -0600 | [diff] [blame] | 468 | vpr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
Jim Cromie | 8e59b5cf | 2012-04-27 14:30:40 -0600 | [diff] [blame] | 472 | static int ddebug_exec_query(char *query_string, const char *modname) |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 473 | { |
| 474 | unsigned int flags = 0, mask = 0; |
Jim Cromie | 9c9d0ac | 2020-07-19 17:10:49 -0600 | [diff] [blame] | 475 | struct ddebug_query query = {}; |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 476 | #define MAXWORDS 9 |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 477 | int nwords, nfound; |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 478 | char *words[MAXWORDS]; |
| 479 | |
| 480 | nwords = ddebug_tokenize(query_string, words, MAXWORDS); |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 481 | if (nwords <= 0) { |
| 482 | pr_err("tokenize failed\n"); |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 483 | return -EINVAL; |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 484 | } |
| 485 | /* check flags 1st (last arg) so query is pairs of spec,val */ |
| 486 | if (ddebug_parse_flags(words[nwords-1], &flags, &mask)) { |
| 487 | pr_err("flags parse failed\n"); |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 488 | return -EINVAL; |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 489 | } |
| 490 | if (ddebug_parse_query(words, nwords-1, &query, modname)) { |
| 491 | pr_err("query parse failed\n"); |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 492 | return -EINVAL; |
Jim Cromie | 18c216c | 2012-12-05 16:48:27 -0500 | [diff] [blame] | 493 | } |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 494 | /* actually go and implement the change */ |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 495 | nfound = ddebug_change(&query, flags, mask); |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 496 | vpr_info_dq(&query, nfound ? "applied" : "no-match"); |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 497 | |
| 498 | return nfound; |
| 499 | } |
| 500 | |
| 501 | /* handle multiple queries in query string, continue on error, return |
| 502 | last error or number of matching callsites. Module name is either |
| 503 | in param (for boot arg) or perhaps in query string. |
| 504 | */ |
Jim Cromie | 8e59b5cf | 2012-04-27 14:30:40 -0600 | [diff] [blame] | 505 | static int ddebug_exec_queries(char *query, const char *modname) |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 506 | { |
| 507 | char *split; |
| 508 | int i, errs = 0, exitcode = 0, rc, nfound = 0; |
| 509 | |
| 510 | for (i = 0; query; query = split) { |
| 511 | split = strpbrk(query, ";\n"); |
| 512 | if (split) |
| 513 | *split++ = '\0'; |
| 514 | |
| 515 | query = skip_spaces(query); |
| 516 | if (!query || !*query || *query == '#') |
| 517 | continue; |
| 518 | |
Jim Cromie | b8ccd5d | 2012-04-27 14:30:32 -0600 | [diff] [blame] | 519 | vpr_info("query %d: \"%s\"\n", i, query); |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 520 | |
Jim Cromie | 8e59b5cf | 2012-04-27 14:30:40 -0600 | [diff] [blame] | 521 | rc = ddebug_exec_query(query, modname); |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 522 | if (rc < 0) { |
| 523 | errs++; |
| 524 | exitcode = rc; |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 525 | } else { |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 526 | nfound += rc; |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 527 | } |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 528 | i++; |
| 529 | } |
Jim Cromie | b8ccd5d | 2012-04-27 14:30:32 -0600 | [diff] [blame] | 530 | vpr_info("processed %d queries, with %d matches, %d errs\n", |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 531 | i, nfound, errs); |
| 532 | |
| 533 | if (exitcode) |
| 534 | return exitcode; |
| 535 | return nfound; |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 536 | } |
| 537 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 538 | #define PREFIX_SIZE 64 |
| 539 | |
| 540 | static int remaining(int wrote) |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 541 | { |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 542 | if (PREFIX_SIZE - wrote > 0) |
| 543 | return PREFIX_SIZE - wrote; |
| 544 | return 0; |
| 545 | } |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 546 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 547 | static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf) |
| 548 | { |
| 549 | int pos_after_tid; |
| 550 | int pos = 0; |
| 551 | |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 552 | *buf = '\0'; |
| 553 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 554 | if (desc->flags & _DPRINTK_FLAGS_INCL_TID) { |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 555 | if (in_interrupt()) |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 556 | pos += snprintf(buf + pos, remaining(pos), "<intr> "); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 557 | else |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 558 | pos += snprintf(buf + pos, remaining(pos), "[%d] ", |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 559 | task_pid_vnr(current)); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 560 | } |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 561 | pos_after_tid = pos; |
| 562 | if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME) |
| 563 | pos += snprintf(buf + pos, remaining(pos), "%s:", |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 564 | desc->modname); |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 565 | if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) |
| 566 | pos += snprintf(buf + pos, remaining(pos), "%s:", |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 567 | desc->function); |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 568 | if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO) |
Jim Cromie | 07100be | 2011-12-19 17:11:09 -0500 | [diff] [blame] | 569 | pos += snprintf(buf + pos, remaining(pos), "%d:", |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 570 | desc->lineno); |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 571 | if (pos - pos_after_tid) |
| 572 | pos += snprintf(buf + pos, remaining(pos), " "); |
| 573 | if (pos >= PREFIX_SIZE) |
| 574 | buf[PREFIX_SIZE - 1] = '\0'; |
Joe Perches | 6c2140e | 2011-08-11 14:36:25 -0400 | [diff] [blame] | 575 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 576 | return buf; |
Joe Perches | 6c2140e | 2011-08-11 14:36:25 -0400 | [diff] [blame] | 577 | } |
| 578 | |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 579 | void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...) |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 580 | { |
| 581 | va_list args; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 582 | struct va_format vaf; |
| 583 | char buf[PREFIX_SIZE]; |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 584 | |
| 585 | BUG_ON(!descriptor); |
| 586 | BUG_ON(!fmt); |
| 587 | |
| 588 | va_start(args, fmt); |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 589 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 590 | vaf.fmt = fmt; |
| 591 | vaf.va = &args; |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 592 | |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 593 | printk(KERN_DEBUG "%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf); |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 594 | |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 595 | va_end(args); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 596 | } |
| 597 | EXPORT_SYMBOL(__dynamic_pr_debug); |
| 598 | |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 599 | void __dynamic_dev_dbg(struct _ddebug *descriptor, |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 600 | const struct device *dev, const char *fmt, ...) |
| 601 | { |
| 602 | struct va_format vaf; |
| 603 | va_list args; |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 604 | |
| 605 | BUG_ON(!descriptor); |
| 606 | BUG_ON(!fmt); |
| 607 | |
| 608 | va_start(args, fmt); |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 609 | |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 610 | vaf.fmt = fmt; |
| 611 | vaf.va = &args; |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 612 | |
| 613 | if (!dev) { |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 614 | printk(KERN_DEBUG "(NULL device *): %pV", &vaf); |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 615 | } else { |
| 616 | char buf[PREFIX_SIZE]; |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 617 | |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 618 | dev_printk_emit(LOGLEVEL_DEBUG, dev, "%s%s %s: %pV", |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 619 | dynamic_emit_prefix(descriptor, buf), |
| 620 | dev_driver_string(dev), dev_name(dev), |
| 621 | &vaf); |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 624 | va_end(args); |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 625 | } |
| 626 | EXPORT_SYMBOL(__dynamic_dev_dbg); |
| 627 | |
Jason Baron | 0feefd9 | 2011-10-04 14:13:22 -0700 | [diff] [blame] | 628 | #ifdef CONFIG_NET |
| 629 | |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 630 | void __dynamic_netdev_dbg(struct _ddebug *descriptor, |
| 631 | const struct net_device *dev, const char *fmt, ...) |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 632 | { |
| 633 | struct va_format vaf; |
| 634 | va_list args; |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 635 | |
| 636 | BUG_ON(!descriptor); |
| 637 | BUG_ON(!fmt); |
| 638 | |
| 639 | va_start(args, fmt); |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 640 | |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 641 | vaf.fmt = fmt; |
| 642 | vaf.va = &args; |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 643 | |
| 644 | if (dev && dev->dev.parent) { |
| 645 | char buf[PREFIX_SIZE]; |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 646 | |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 647 | dev_printk_emit(LOGLEVEL_DEBUG, dev->dev.parent, |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 648 | "%s%s %s %s%s: %pV", |
| 649 | dynamic_emit_prefix(descriptor, buf), |
| 650 | dev_driver_string(dev->dev.parent), |
| 651 | dev_name(dev->dev.parent), |
| 652 | netdev_name(dev), netdev_reg_state(dev), |
| 653 | &vaf); |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 654 | } else if (dev) { |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 655 | printk(KERN_DEBUG "%s%s: %pV", netdev_name(dev), |
| 656 | netdev_reg_state(dev), &vaf); |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 657 | } else { |
Joe Perches | 906d201 | 2014-09-24 11:17:56 -0700 | [diff] [blame] | 658 | printk(KERN_DEBUG "(NULL net_device): %pV", &vaf); |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 661 | va_end(args); |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 662 | } |
| 663 | EXPORT_SYMBOL(__dynamic_netdev_dbg); |
| 664 | |
Jason Baron | 0feefd9 | 2011-10-04 14:13:22 -0700 | [diff] [blame] | 665 | #endif |
| 666 | |
Gal Pressman | 923abb9 | 2019-05-01 13:48:13 +0300 | [diff] [blame] | 667 | #if IS_ENABLED(CONFIG_INFINIBAND) |
| 668 | |
| 669 | void __dynamic_ibdev_dbg(struct _ddebug *descriptor, |
| 670 | const struct ib_device *ibdev, const char *fmt, ...) |
| 671 | { |
| 672 | struct va_format vaf; |
| 673 | va_list args; |
| 674 | |
| 675 | va_start(args, fmt); |
| 676 | |
| 677 | vaf.fmt = fmt; |
| 678 | vaf.va = &args; |
| 679 | |
| 680 | if (ibdev && ibdev->dev.parent) { |
| 681 | char buf[PREFIX_SIZE]; |
| 682 | |
| 683 | dev_printk_emit(LOGLEVEL_DEBUG, ibdev->dev.parent, |
| 684 | "%s%s %s %s: %pV", |
| 685 | dynamic_emit_prefix(descriptor, buf), |
| 686 | dev_driver_string(ibdev->dev.parent), |
| 687 | dev_name(ibdev->dev.parent), |
| 688 | dev_name(&ibdev->dev), |
| 689 | &vaf); |
| 690 | } else if (ibdev) { |
| 691 | printk(KERN_DEBUG "%s: %pV", dev_name(&ibdev->dev), &vaf); |
| 692 | } else { |
| 693 | printk(KERN_DEBUG "(NULL ib_device): %pV", &vaf); |
| 694 | } |
| 695 | |
| 696 | va_end(args); |
| 697 | } |
| 698 | EXPORT_SYMBOL(__dynamic_ibdev_dbg); |
| 699 | |
| 700 | #endif |
| 701 | |
Jim Cromie | bc757f6 | 2011-12-19 17:12:29 -0500 | [diff] [blame] | 702 | #define DDEBUG_STRING_SIZE 1024 |
| 703 | static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE]; |
| 704 | |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 705 | static __init int ddebug_setup_query(char *str) |
| 706 | { |
Jim Cromie | bc757f6 | 2011-12-19 17:12:29 -0500 | [diff] [blame] | 707 | if (strlen(str) >= DDEBUG_STRING_SIZE) { |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 708 | pr_warn("ddebug boot param string too large\n"); |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 709 | return 0; |
| 710 | } |
Jim Cromie | bc757f6 | 2011-12-19 17:12:29 -0500 | [diff] [blame] | 711 | strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE); |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 712 | return 1; |
| 713 | } |
| 714 | |
| 715 | __setup("ddebug_query=", ddebug_setup_query); |
| 716 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 717 | /* |
Masatake YAMATO | 231821d | 2014-12-15 12:04:16 +0900 | [diff] [blame] | 718 | * File_ops->write method for <debugfs>/dynamic_debug/control. Gathers the |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 719 | * command text from userspace, parses and executes it. |
| 720 | */ |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 721 | #define USER_BUF_PAGE 4096 |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 722 | static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf, |
| 723 | size_t len, loff_t *offp) |
| 724 | { |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 725 | char *tmpbuf; |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 726 | int ret; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 727 | |
| 728 | if (len == 0) |
| 729 | return 0; |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 730 | if (len > USER_BUF_PAGE - 1) { |
| 731 | pr_warn("expected <%d bytes into control\n", USER_BUF_PAGE); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 732 | return -E2BIG; |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 733 | } |
Al Viro | 16e5c1f | 2015-12-24 00:06:05 -0500 | [diff] [blame] | 734 | tmpbuf = memdup_user_nul(ubuf, len); |
| 735 | if (IS_ERR(tmpbuf)) |
| 736 | return PTR_ERR(tmpbuf); |
Jim Cromie | b8ccd5d | 2012-04-27 14:30:32 -0600 | [diff] [blame] | 737 | vpr_info("read %d bytes from userspace\n", (int)len); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 738 | |
Jim Cromie | 8e59b5cf | 2012-04-27 14:30:40 -0600 | [diff] [blame] | 739 | ret = ddebug_exec_queries(tmpbuf, NULL); |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 740 | kfree(tmpbuf); |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 741 | if (ret < 0) |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 742 | return ret; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 743 | |
| 744 | *offp += len; |
| 745 | return len; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Set the iterator to point to the first _ddebug object |
| 750 | * and return a pointer to that first object. Returns |
| 751 | * NULL if there are no _ddebugs at all. |
| 752 | */ |
| 753 | static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter) |
| 754 | { |
| 755 | if (list_empty(&ddebug_tables)) { |
| 756 | iter->table = NULL; |
| 757 | iter->idx = 0; |
| 758 | return NULL; |
| 759 | } |
| 760 | iter->table = list_entry(ddebug_tables.next, |
| 761 | struct ddebug_table, link); |
| 762 | iter->idx = 0; |
| 763 | return &iter->table->ddebugs[iter->idx]; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Advance the iterator to point to the next _ddebug |
| 768 | * object from the one the iterator currently points at, |
| 769 | * and returns a pointer to the new _ddebug. Returns |
| 770 | * NULL if the iterator has seen all the _ddebugs. |
| 771 | */ |
| 772 | static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter) |
| 773 | { |
| 774 | if (iter->table == NULL) |
| 775 | return NULL; |
| 776 | if (++iter->idx == iter->table->num_ddebugs) { |
| 777 | /* iterate to next table */ |
| 778 | iter->idx = 0; |
| 779 | if (list_is_last(&iter->table->link, &ddebug_tables)) { |
| 780 | iter->table = NULL; |
| 781 | return NULL; |
| 782 | } |
| 783 | iter->table = list_entry(iter->table->link.next, |
| 784 | struct ddebug_table, link); |
| 785 | } |
| 786 | return &iter->table->ddebugs[iter->idx]; |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | * Seq_ops start method. Called at the start of every |
| 791 | * read() call from userspace. Takes the ddebug_lock and |
| 792 | * seeks the seq_file's iterator to the given position. |
| 793 | */ |
| 794 | static void *ddebug_proc_start(struct seq_file *m, loff_t *pos) |
| 795 | { |
| 796 | struct ddebug_iter *iter = m->private; |
| 797 | struct _ddebug *dp; |
| 798 | int n = *pos; |
| 799 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 800 | mutex_lock(&ddebug_lock); |
| 801 | |
| 802 | if (!n) |
| 803 | return SEQ_START_TOKEN; |
| 804 | if (n < 0) |
| 805 | return NULL; |
| 806 | dp = ddebug_iter_first(iter); |
| 807 | while (dp != NULL && --n > 0) |
| 808 | dp = ddebug_iter_next(iter); |
| 809 | return dp; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Seq_ops next method. Called several times within a read() |
| 814 | * call from userspace, with ddebug_lock held. Walks to the |
| 815 | * next _ddebug object with a special case for the header line. |
| 816 | */ |
| 817 | static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos) |
| 818 | { |
| 819 | struct ddebug_iter *iter = m->private; |
| 820 | struct _ddebug *dp; |
| 821 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 822 | if (p == SEQ_START_TOKEN) |
| 823 | dp = ddebug_iter_first(iter); |
| 824 | else |
| 825 | dp = ddebug_iter_next(iter); |
| 826 | ++*pos; |
| 827 | return dp; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Seq_ops show method. Called several times within a read() |
| 832 | * call from userspace, with ddebug_lock held. Formats the |
| 833 | * current _ddebug as a single human-readable line, with a |
| 834 | * special case for the header line. |
| 835 | */ |
| 836 | static int ddebug_proc_show(struct seq_file *m, void *p) |
| 837 | { |
| 838 | struct ddebug_iter *iter = m->private; |
| 839 | struct _ddebug *dp = p; |
Jim Cromie | f678ce8 | 2020-07-19 17:10:47 -0600 | [diff] [blame] | 840 | struct flagsbuf flags; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 841 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 842 | if (p == SEQ_START_TOKEN) { |
| 843 | seq_puts(m, |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 844 | "# filename:lineno [module]function flags format\n"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 845 | return 0; |
| 846 | } |
| 847 | |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 848 | seq_printf(m, "%s:%u [%s]%s =%s \"", |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 849 | trim_prefix(dp->filename), dp->lineno, |
| 850 | iter->table->mod_name, dp->function, |
Jim Cromie | f678ce8 | 2020-07-19 17:10:47 -0600 | [diff] [blame] | 851 | ddebug_describe_flags(dp->flags, &flags)); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 852 | seq_escape(m, dp->format, "\t\r\n\""); |
| 853 | seq_puts(m, "\"\n"); |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * Seq_ops stop method. Called at the end of each read() |
| 860 | * call from userspace. Drops ddebug_lock. |
| 861 | */ |
| 862 | static void ddebug_proc_stop(struct seq_file *m, void *p) |
| 863 | { |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 864 | mutex_unlock(&ddebug_lock); |
| 865 | } |
| 866 | |
| 867 | static const struct seq_operations ddebug_proc_seqops = { |
| 868 | .start = ddebug_proc_start, |
| 869 | .next = ddebug_proc_next, |
| 870 | .show = ddebug_proc_show, |
| 871 | .stop = ddebug_proc_stop |
| 872 | }; |
| 873 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 874 | static int ddebug_proc_open(struct inode *inode, struct file *file) |
| 875 | { |
Jim Cromie | b8ccd5d | 2012-04-27 14:30:32 -0600 | [diff] [blame] | 876 | vpr_info("called\n"); |
Rob Jones | 4bad78c | 2014-10-13 15:51:32 -0700 | [diff] [blame] | 877 | return seq_open_private(file, &ddebug_proc_seqops, |
| 878 | sizeof(struct ddebug_iter)); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static const struct file_operations ddebug_proc_fops = { |
| 882 | .owner = THIS_MODULE, |
| 883 | .open = ddebug_proc_open, |
| 884 | .read = seq_read, |
| 885 | .llseek = seq_lseek, |
| 886 | .release = seq_release_private, |
| 887 | .write = ddebug_proc_write |
| 888 | }; |
| 889 | |
Greg Kroah-Hartman | 239a579 | 2020-02-10 13:11:42 -0800 | [diff] [blame] | 890 | static const struct proc_ops proc_fops = { |
| 891 | .proc_open = ddebug_proc_open, |
| 892 | .proc_read = seq_read, |
| 893 | .proc_lseek = seq_lseek, |
| 894 | .proc_release = seq_release_private, |
| 895 | .proc_write = ddebug_proc_write |
| 896 | }; |
| 897 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 898 | /* |
| 899 | * Allocate a new ddebug_table for the given module |
| 900 | * and add it to the global list. |
| 901 | */ |
| 902 | int ddebug_add_module(struct _ddebug *tab, unsigned int n, |
| 903 | const char *name) |
| 904 | { |
| 905 | struct ddebug_table *dt; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 906 | |
| 907 | dt = kzalloc(sizeof(*dt), GFP_KERNEL); |
Rasmus Villemoes | 513770f | 2019-03-07 16:27:48 -0800 | [diff] [blame] | 908 | if (dt == NULL) { |
| 909 | pr_err("error adding module: %s\n", name); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 910 | return -ENOMEM; |
Rasmus Villemoes | 513770f | 2019-03-07 16:27:48 -0800 | [diff] [blame] | 911 | } |
Rasmus Villemoes | cdf6d00 | 2019-03-07 16:27:37 -0800 | [diff] [blame] | 912 | /* |
| 913 | * For built-in modules, name lives in .rodata and is |
| 914 | * immortal. For loaded modules, name points at the name[] |
| 915 | * member of struct module, which lives at least as long as |
| 916 | * this struct ddebug_table. |
| 917 | */ |
| 918 | dt->mod_name = name; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 919 | dt->num_ddebugs = n; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 920 | dt->ddebugs = tab; |
| 921 | |
| 922 | mutex_lock(&ddebug_lock); |
Jim Cromie | 47e9f5a | 2020-07-19 17:10:50 -0600 | [diff] [blame] | 923 | list_add(&dt->link, &ddebug_tables); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 924 | mutex_unlock(&ddebug_lock); |
| 925 | |
Jim Cromie | 481c0e3 | 2020-07-19 17:10:44 -0600 | [diff] [blame] | 926 | v2pr_info("%u debug prints in module %s\n", n, dt->mod_name); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 927 | return 0; |
| 928 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 929 | |
Jim Cromie | 6ab676e | 2012-04-27 14:30:37 -0600 | [diff] [blame] | 930 | /* helper for ddebug_dyndbg_(boot|module)_param_cb */ |
| 931 | static int ddebug_dyndbg_param_cb(char *param, char *val, |
| 932 | const char *modname, int on_err) |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 933 | { |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 934 | char *sep; |
| 935 | |
| 936 | sep = strchr(param, '.'); |
| 937 | if (sep) { |
Jim Cromie | 6ab676e | 2012-04-27 14:30:37 -0600 | [diff] [blame] | 938 | /* needed only for ddebug_dyndbg_boot_param_cb */ |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 939 | *sep = '\0'; |
| 940 | modname = param; |
| 941 | param = sep + 1; |
| 942 | } |
| 943 | if (strcmp(param, "dyndbg")) |
Jim Cromie | 6ab676e | 2012-04-27 14:30:37 -0600 | [diff] [blame] | 944 | return on_err; /* determined by caller */ |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 945 | |
Jim Cromie | 8e59b5cf | 2012-04-27 14:30:40 -0600 | [diff] [blame] | 946 | ddebug_exec_queries((val ? val : "+p"), modname); |
| 947 | |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 948 | return 0; /* query failure shouldnt stop module load */ |
| 949 | } |
| 950 | |
Jim Cromie | 6ab676e | 2012-04-27 14:30:37 -0600 | [diff] [blame] | 951 | /* handle both dyndbg and $module.dyndbg params at boot */ |
| 952 | static int ddebug_dyndbg_boot_param_cb(char *param, char *val, |
Luis R. Rodriguez | ecc8617 | 2015-03-30 16:20:03 -0700 | [diff] [blame] | 953 | const char *unused, void *arg) |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 954 | { |
Jim Cromie | 6ab676e | 2012-04-27 14:30:37 -0600 | [diff] [blame] | 955 | vpr_info("%s=\"%s\"\n", param, val); |
| 956 | return ddebug_dyndbg_param_cb(param, val, NULL, 0); |
| 957 | } |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 958 | |
Jim Cromie | 6ab676e | 2012-04-27 14:30:37 -0600 | [diff] [blame] | 959 | /* |
| 960 | * modprobe foo finds foo.params in boot-args, strips "foo.", and |
| 961 | * passes them to load_module(). This callback gets unknown params, |
| 962 | * processes dyndbg params, rejects others. |
| 963 | */ |
| 964 | int ddebug_dyndbg_module_param_cb(char *param, char *val, const char *module) |
| 965 | { |
| 966 | vpr_info("module: %s %s=\"%s\"\n", module, param, val); |
| 967 | return ddebug_dyndbg_param_cb(param, val, module, -ENOENT); |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 968 | } |
| 969 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 970 | static void ddebug_table_free(struct ddebug_table *dt) |
| 971 | { |
| 972 | list_del_init(&dt->link); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 973 | kfree(dt); |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * Called in response to a module being unloaded. Removes |
| 978 | * any ddebug_table's which point at the module. |
| 979 | */ |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 980 | int ddebug_remove_module(const char *mod_name) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 981 | { |
| 982 | struct ddebug_table *dt, *nextdt; |
| 983 | int ret = -ENOENT; |
| 984 | |
Jim Cromie | 481c0e3 | 2020-07-19 17:10:44 -0600 | [diff] [blame] | 985 | v2pr_info("removing module \"%s\"\n", mod_name); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 986 | |
| 987 | mutex_lock(&ddebug_lock); |
| 988 | list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) { |
Rasmus Villemoes | 4573fe1 | 2019-03-07 16:27:41 -0800 | [diff] [blame] | 989 | if (dt->mod_name == mod_name) { |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 990 | ddebug_table_free(dt); |
| 991 | ret = 0; |
Rasmus Villemoes | 4573fe1 | 2019-03-07 16:27:41 -0800 | [diff] [blame] | 992 | break; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | mutex_unlock(&ddebug_lock); |
| 996 | return ret; |
| 997 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 998 | |
| 999 | static void ddebug_remove_all_tables(void) |
| 1000 | { |
| 1001 | mutex_lock(&ddebug_lock); |
| 1002 | while (!list_empty(&ddebug_tables)) { |
| 1003 | struct ddebug_table *dt = list_entry(ddebug_tables.next, |
| 1004 | struct ddebug_table, |
| 1005 | link); |
| 1006 | ddebug_table_free(dt); |
| 1007 | } |
| 1008 | mutex_unlock(&ddebug_lock); |
| 1009 | } |
| 1010 | |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 1011 | static __initdata int ddebug_init_success; |
| 1012 | |
Greg Kroah-Hartman | 239a579 | 2020-02-10 13:11:42 -0800 | [diff] [blame] | 1013 | static int __init dynamic_debug_init_control(void) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 1014 | { |
Greg Kroah-Hartman | 239a579 | 2020-02-10 13:11:42 -0800 | [diff] [blame] | 1015 | struct proc_dir_entry *procfs_dir; |
| 1016 | struct dentry *debugfs_dir; |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 1017 | |
| 1018 | if (!ddebug_init_success) |
| 1019 | return -ENODEV; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 1020 | |
Greg Kroah-Hartman | 239a579 | 2020-02-10 13:11:42 -0800 | [diff] [blame] | 1021 | /* Create the control file in debugfs if it is enabled */ |
| 1022 | if (debugfs_initialized()) { |
| 1023 | debugfs_dir = debugfs_create_dir("dynamic_debug", NULL); |
| 1024 | debugfs_create_file("control", 0644, debugfs_dir, NULL, |
| 1025 | &ddebug_proc_fops); |
| 1026 | } |
| 1027 | |
| 1028 | /* Also create the control file in procfs */ |
| 1029 | procfs_dir = proc_mkdir("dynamic_debug", NULL); |
| 1030 | if (procfs_dir) |
| 1031 | proc_create("control", 0644, procfs_dir, &proc_fops); |
Greg Kroah-Hartman | 9fd714c | 2019-06-12 17:35:34 +0200 | [diff] [blame] | 1032 | |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | static int __init dynamic_debug_init(void) |
| 1037 | { |
| 1038 | struct _ddebug *iter, *iter_start; |
| 1039 | const char *modname = NULL; |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 1040 | char *cmdline; |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 1041 | int ret = 0; |
Jim Cromie | 4107692 | 2012-04-27 14:30:39 -0600 | [diff] [blame] | 1042 | int n = 0, entries = 0, modct = 0; |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 1043 | |
Jim Cromie | e5ebffe | 2020-07-19 17:10:45 -0600 | [diff] [blame] | 1044 | if (&__start___dyndbg == &__stop___dyndbg) { |
Orson Zhai | ceabef7 | 2020-06-07 21:40:14 -0700 | [diff] [blame] | 1045 | if (IS_ENABLED(CONFIG_DYNAMIC_DEBUG)) { |
| 1046 | pr_warn("_ddebug table is empty in a CONFIG_DYNAMIC_DEBUG build\n"); |
| 1047 | return 1; |
| 1048 | } |
| 1049 | pr_info("Ignore empty _ddebug table in a CONFIG_DYNAMIC_DEBUG_CORE build\n"); |
| 1050 | ddebug_init_success = 1; |
| 1051 | return 0; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 1052 | } |
Jim Cromie | e5ebffe | 2020-07-19 17:10:45 -0600 | [diff] [blame] | 1053 | iter = __start___dyndbg; |
Jim Cromie | b5b78f8 | 2011-12-19 17:12:54 -0500 | [diff] [blame] | 1054 | modname = iter->modname; |
| 1055 | iter_start = iter; |
Jim Cromie | e5ebffe | 2020-07-19 17:10:45 -0600 | [diff] [blame] | 1056 | for (; iter < __stop___dyndbg; iter++) { |
Jim Cromie | 4107692 | 2012-04-27 14:30:39 -0600 | [diff] [blame] | 1057 | entries++; |
Jim Cromie | b5b78f8 | 2011-12-19 17:12:54 -0500 | [diff] [blame] | 1058 | if (strcmp(modname, iter->modname)) { |
Jim Cromie | 4107692 | 2012-04-27 14:30:39 -0600 | [diff] [blame] | 1059 | modct++; |
Jim Cromie | b5b78f8 | 2011-12-19 17:12:54 -0500 | [diff] [blame] | 1060 | ret = ddebug_add_module(iter_start, n, modname); |
| 1061 | if (ret) |
Jim Cromie | af44239 | 2012-04-27 14:30:38 -0600 | [diff] [blame] | 1062 | goto out_err; |
Jim Cromie | b5b78f8 | 2011-12-19 17:12:54 -0500 | [diff] [blame] | 1063 | n = 0; |
| 1064 | modname = iter->modname; |
| 1065 | iter_start = iter; |
| 1066 | } |
| 1067 | n++; |
| 1068 | } |
| 1069 | ret = ddebug_add_module(iter_start, n, modname); |
| 1070 | if (ret) |
Jim Cromie | af44239 | 2012-04-27 14:30:38 -0600 | [diff] [blame] | 1071 | goto out_err; |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 1072 | |
Jim Cromie | af44239 | 2012-04-27 14:30:38 -0600 | [diff] [blame] | 1073 | ddebug_init_success = 1; |
Jim Cromie | 81d0c2c | 2020-07-19 17:10:46 -0600 | [diff] [blame] | 1074 | vpr_info("%d modules, %d entries and %d bytes in ddebug tables, %d bytes in __dyndbg section\n", |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 1075 | modct, entries, (int)(modct * sizeof(struct ddebug_table)), |
Jim Cromie | 81d0c2c | 2020-07-19 17:10:46 -0600 | [diff] [blame] | 1076 | (int)(entries * sizeof(struct _ddebug))); |
Jim Cromie | af44239 | 2012-04-27 14:30:38 -0600 | [diff] [blame] | 1077 | |
| 1078 | /* apply ddebug_query boot param, dont unload tables on err */ |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 1079 | if (ddebug_setup_string[0] != '\0') { |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 1080 | pr_warn("ddebug_query param name is deprecated, change it to dyndbg\n"); |
Jim Cromie | 8e59b5cf | 2012-04-27 14:30:40 -0600 | [diff] [blame] | 1081 | ret = ddebug_exec_queries(ddebug_setup_string, NULL); |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 1082 | if (ret < 0) |
Joe Perches | f657fd2 | 2012-12-05 16:48:26 -0500 | [diff] [blame] | 1083 | pr_warn("Invalid ddebug boot param %s\n", |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 1084 | ddebug_setup_string); |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 1085 | else |
Jim Cromie | 85f7f6c | 2011-12-19 17:13:21 -0500 | [diff] [blame] | 1086 | pr_info("%d changes by ddebug_query\n", ret); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 1087 | } |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 1088 | /* now that ddebug tables are loaded, process all boot args |
| 1089 | * again to find and activate queries given in dyndbg params. |
| 1090 | * While this has already been done for known boot params, it |
| 1091 | * ignored the unknown ones (dyndbg in particular). Reusing |
| 1092 | * parse_args avoids ad-hoc parsing. This will also attempt |
| 1093 | * to activate queries for not-yet-loaded modules, which is |
| 1094 | * slightly noisy if verbose, but harmless. |
| 1095 | */ |
| 1096 | cmdline = kstrdup(saved_command_line, GFP_KERNEL); |
| 1097 | parse_args("dyndbg params", cmdline, NULL, |
Luis R. Rodriguez | ecc8617 | 2015-03-30 16:20:03 -0700 | [diff] [blame] | 1098 | 0, 0, 0, NULL, &ddebug_dyndbg_boot_param_cb); |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 1099 | kfree(cmdline); |
Jim Cromie | af44239 | 2012-04-27 14:30:38 -0600 | [diff] [blame] | 1100 | return 0; |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 1101 | |
Jim Cromie | af44239 | 2012-04-27 14:30:38 -0600 | [diff] [blame] | 1102 | out_err: |
| 1103 | ddebug_remove_all_tables(); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 1106 | /* Allow early initialization for boot messages via boot param */ |
Jim Cromie | 3ec5652 | 2012-04-27 14:30:42 -0600 | [diff] [blame] | 1107 | early_initcall(dynamic_debug_init); |
Jim Cromie | b48420c | 2012-04-27 14:30:35 -0600 | [diff] [blame] | 1108 | |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 1109 | /* Debugfs setup must be done later */ |
Greg Kroah-Hartman | 239a579 | 2020-02-10 13:11:42 -0800 | [diff] [blame] | 1110 | fs_initcall(dynamic_debug_init_control); |