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