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. |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 11 | */ |
| 12 | |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ |
| 14 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/kallsyms.h> |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 19 | #include <linux/types.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/proc_fs.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/sysctl.h> |
| 25 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 26 | #include <linux/string.h> |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
| 28 | #include <linux/dynamic_debug.h> |
| 29 | #include <linux/debugfs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Jason Baron | 52159d9 | 2010-09-17 11:09:17 -0400 | [diff] [blame] | 31 | #include <linux/jump_label.h> |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 32 | #include <linux/hardirq.h> |
Greg Kroah-Hartman | e8d9792 | 2011-02-03 15:59:58 -0800 | [diff] [blame] | 33 | #include <linux/sched.h> |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 34 | #include <linux/device.h> |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 35 | #include <linux/netdevice.h> |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 36 | |
| 37 | extern struct _ddebug __start___verbose[]; |
| 38 | extern struct _ddebug __stop___verbose[]; |
| 39 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 40 | struct ddebug_table { |
| 41 | struct list_head link; |
| 42 | char *mod_name; |
| 43 | unsigned int num_ddebugs; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 44 | struct _ddebug *ddebugs; |
| 45 | }; |
| 46 | |
| 47 | struct ddebug_query { |
| 48 | const char *filename; |
| 49 | const char *module; |
| 50 | const char *function; |
| 51 | const char *format; |
| 52 | unsigned int first_lineno, last_lineno; |
| 53 | }; |
| 54 | |
| 55 | struct ddebug_iter { |
| 56 | struct ddebug_table *table; |
| 57 | unsigned int idx; |
| 58 | }; |
| 59 | |
| 60 | static DEFINE_MUTEX(ddebug_lock); |
| 61 | static LIST_HEAD(ddebug_tables); |
| 62 | static int verbose = 0; |
Jim Cromie | 74df138 | 2011-12-19 17:12:24 -0500 | [diff] [blame] | 63 | module_param(verbose, int, 0644); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 64 | |
| 65 | /* Return the last part of a pathname */ |
| 66 | static inline const char *basename(const char *path) |
| 67 | { |
| 68 | const char *tail = strrchr(path, '/'); |
| 69 | return tail ? tail+1 : path; |
| 70 | } |
| 71 | |
Jim Cromie | 2b67831 | 2011-12-19 17:13:12 -0500 | [diff] [blame^] | 72 | /* Return the path relative to source root */ |
| 73 | static inline const char *trim_prefix(const char *path) |
| 74 | { |
| 75 | int skip = strlen(__FILE__) - strlen("lib/dynamic_debug.c"); |
| 76 | |
| 77 | if (strncmp(path, __FILE__, skip)) |
| 78 | skip = 0; /* prefix mismatch, don't skip */ |
| 79 | |
| 80 | return path + skip; |
| 81 | } |
| 82 | |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 83 | static struct { unsigned flag:8; char opt_char; } opt_array[] = { |
| 84 | { _DPRINTK_FLAGS_PRINT, 'p' }, |
| 85 | { _DPRINTK_FLAGS_INCL_MODNAME, 'm' }, |
| 86 | { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' }, |
| 87 | { _DPRINTK_FLAGS_INCL_LINENO, 'l' }, |
| 88 | { _DPRINTK_FLAGS_INCL_TID, 't' }, |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 89 | { _DPRINTK_FLAGS_NONE, '_' }, |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 90 | }; |
| 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 */ |
| 93 | static char *ddebug_describe_flags(struct _ddebug *dp, char *buf, |
| 94 | size_t maxlen) |
| 95 | { |
| 96 | char *p = buf; |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 97 | int i; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 98 | |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 99 | BUG_ON(maxlen < 6); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 100 | for (i = 0; i < ARRAY_SIZE(opt_array); ++i) |
| 101 | if (dp->flags & opt_array[i].flag) |
| 102 | *p++ = opt_array[i].opt_char; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 103 | if (p == buf) |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 104 | *p++ = '_'; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 105 | *p = '\0'; |
| 106 | |
| 107 | return buf; |
| 108 | } |
| 109 | |
| 110 | /* |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 111 | * Search the tables for _ddebug's which match the given |
| 112 | * `query' and apply the `flags' and `mask' to them. Tells |
| 113 | * the user which ddebug's were changed, or whether none |
| 114 | * were matched. |
| 115 | */ |
| 116 | static void ddebug_change(const struct ddebug_query *query, |
| 117 | unsigned int flags, unsigned int mask) |
| 118 | { |
| 119 | int i; |
| 120 | struct ddebug_table *dt; |
| 121 | unsigned int newflags; |
| 122 | unsigned int nfound = 0; |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 123 | char flagbuf[10]; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 124 | |
| 125 | /* search for matching ddebugs */ |
| 126 | mutex_lock(&ddebug_lock); |
| 127 | list_for_each_entry(dt, &ddebug_tables, link) { |
| 128 | |
| 129 | /* match against the module name */ |
Jim Cromie | d6a238d | 2011-12-19 17:12:39 -0500 | [diff] [blame] | 130 | if (query->module && strcmp(query->module, dt->mod_name)) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 131 | continue; |
| 132 | |
| 133 | for (i = 0 ; i < dt->num_ddebugs ; i++) { |
| 134 | struct _ddebug *dp = &dt->ddebugs[i]; |
| 135 | |
| 136 | /* match against the source filename */ |
Jim Cromie | d6a238d | 2011-12-19 17:12:39 -0500 | [diff] [blame] | 137 | if (query->filename && |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 138 | strcmp(query->filename, dp->filename) && |
Jim Cromie | 2b67831 | 2011-12-19 17:13:12 -0500 | [diff] [blame^] | 139 | strcmp(query->filename, basename(dp->filename)) && |
| 140 | strcmp(query->filename, trim_prefix(dp->filename))) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 141 | continue; |
| 142 | |
| 143 | /* match against the function */ |
Jim Cromie | d6a238d | 2011-12-19 17:12:39 -0500 | [diff] [blame] | 144 | if (query->function && |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 145 | strcmp(query->function, dp->function)) |
| 146 | continue; |
| 147 | |
| 148 | /* match against the format */ |
Jim Cromie | d6a238d | 2011-12-19 17:12:39 -0500 | [diff] [blame] | 149 | if (query->format && |
| 150 | !strstr(dp->format, query->format)) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 151 | continue; |
| 152 | |
| 153 | /* match against the line number range */ |
| 154 | if (query->first_lineno && |
| 155 | dp->lineno < query->first_lineno) |
| 156 | continue; |
| 157 | if (query->last_lineno && |
| 158 | dp->lineno > query->last_lineno) |
| 159 | continue; |
| 160 | |
| 161 | nfound++; |
| 162 | |
| 163 | newflags = (dp->flags & mask) | flags; |
| 164 | if (newflags == dp->flags) |
| 165 | continue; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 166 | dp->flags = newflags; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 167 | if (verbose) |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 168 | pr_info("changed %s:%d [%s]%s =%s\n", |
Jim Cromie | 2b67831 | 2011-12-19 17:13:12 -0500 | [diff] [blame^] | 169 | trim_prefix(dp->filename), dp->lineno, |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 170 | dt->mod_name, dp->function, |
| 171 | ddebug_describe_flags(dp, flagbuf, |
| 172 | sizeof(flagbuf))); |
| 173 | } |
| 174 | } |
| 175 | mutex_unlock(&ddebug_lock); |
| 176 | |
| 177 | if (!nfound && verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 178 | pr_info("no matches for query\n"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 182 | * Split the buffer `buf' into space-separated words. |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 183 | * Handles simple " and ' quoting, i.e. without nested, |
| 184 | * embedded or escaped \". Return the number of words |
| 185 | * or <0 on error. |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 186 | */ |
| 187 | static int ddebug_tokenize(char *buf, char *words[], int maxwords) |
| 188 | { |
| 189 | int nwords = 0; |
| 190 | |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 191 | while (*buf) { |
| 192 | char *end; |
| 193 | |
| 194 | /* Skip leading whitespace */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 195 | buf = skip_spaces(buf); |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 196 | if (!*buf) |
| 197 | break; /* oh, it was trailing whitespace */ |
Jim Cromie | 8bd6026 | 2011-12-19 17:13:03 -0500 | [diff] [blame] | 198 | if (*buf == '#') |
| 199 | break; /* token starts comment, skip rest of line */ |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 200 | |
Jim Cromie | 07100be | 2011-12-19 17:11:09 -0500 | [diff] [blame] | 201 | /* find `end' of word, whitespace separated or quoted */ |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 202 | if (*buf == '"' || *buf == '\'') { |
| 203 | int quote = *buf++; |
| 204 | for (end = buf ; *end && *end != quote ; end++) |
| 205 | ; |
| 206 | if (!*end) |
| 207 | return -EINVAL; /* unclosed quote */ |
| 208 | } else { |
| 209 | for (end = buf ; *end && !isspace(*end) ; end++) |
| 210 | ; |
| 211 | BUG_ON(end == buf); |
| 212 | } |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 213 | |
Jim Cromie | 07100be | 2011-12-19 17:11:09 -0500 | [diff] [blame] | 214 | /* `buf' is start of word, `end' is one past its end */ |
Greg Banks | 9898abb | 2009-02-06 12:54:26 +1100 | [diff] [blame] | 215 | if (nwords == maxwords) |
| 216 | return -EINVAL; /* ran out of words[] before bytes */ |
| 217 | if (*end) |
| 218 | *end++ = '\0'; /* terminate the word */ |
| 219 | words[nwords++] = buf; |
| 220 | buf = end; |
| 221 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 222 | |
| 223 | if (verbose) { |
| 224 | int i; |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 225 | pr_info("split into words:"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 226 | for (i = 0 ; i < nwords ; i++) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 227 | pr_cont(" \"%s\"", words[i]); |
| 228 | pr_cont("\n"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return nwords; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Parse a single line number. Note that the empty string "" |
| 236 | * is treated as a special case and converted to zero, which |
| 237 | * is later treated as a "don't care" value. |
| 238 | */ |
| 239 | static inline int parse_lineno(const char *str, unsigned int *val) |
| 240 | { |
| 241 | char *end = NULL; |
| 242 | BUG_ON(str == NULL); |
| 243 | if (*str == '\0') { |
| 244 | *val = 0; |
| 245 | return 0; |
| 246 | } |
| 247 | *val = simple_strtoul(str, &end, 10); |
| 248 | return end == NULL || end == str || *end != '\0' ? -EINVAL : 0; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Undo octal escaping in a string, inplace. This is useful to |
| 253 | * allow the user to express a query which matches a format |
| 254 | * containing embedded spaces. |
| 255 | */ |
| 256 | #define isodigit(c) ((c) >= '0' && (c) <= '7') |
| 257 | static char *unescape(char *str) |
| 258 | { |
| 259 | char *in = str; |
| 260 | char *out = str; |
| 261 | |
| 262 | while (*in) { |
| 263 | if (*in == '\\') { |
| 264 | if (in[1] == '\\') { |
| 265 | *out++ = '\\'; |
| 266 | in += 2; |
| 267 | continue; |
| 268 | } else if (in[1] == 't') { |
| 269 | *out++ = '\t'; |
| 270 | in += 2; |
| 271 | continue; |
| 272 | } else if (in[1] == 'n') { |
| 273 | *out++ = '\n'; |
| 274 | in += 2; |
| 275 | continue; |
| 276 | } else if (isodigit(in[1]) && |
| 277 | isodigit(in[2]) && |
| 278 | isodigit(in[3])) { |
| 279 | *out++ = ((in[1] - '0')<<6) | |
| 280 | ((in[2] - '0')<<3) | |
| 281 | (in[3] - '0'); |
| 282 | in += 4; |
| 283 | continue; |
| 284 | } |
| 285 | } |
| 286 | *out++ = *in++; |
| 287 | } |
| 288 | *out = '\0'; |
| 289 | |
| 290 | return str; |
| 291 | } |
| 292 | |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 293 | static int check_set(const char **dest, char *src, char *name) |
| 294 | { |
| 295 | int rc = 0; |
| 296 | |
| 297 | if (*dest) { |
| 298 | rc = -EINVAL; |
| 299 | pr_err("match-spec:%s val:%s overridden by %s", |
| 300 | name, *dest, src); |
| 301 | } |
| 302 | *dest = src; |
| 303 | return rc; |
| 304 | } |
| 305 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 306 | /* |
| 307 | * Parse words[] as a ddebug query specification, which is a series |
| 308 | * of (keyword, value) pairs chosen from these possibilities: |
| 309 | * |
| 310 | * func <function-name> |
| 311 | * file <full-pathname> |
| 312 | * file <base-filename> |
| 313 | * module <module-name> |
| 314 | * format <escaped-string-to-find-in-format> |
| 315 | * line <lineno> |
| 316 | * line <first-lineno>-<last-lineno> // where either may be empty |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 317 | * |
| 318 | * Only 1 of each type is allowed. |
| 319 | * Returns 0 on success, <0 on error. |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 320 | */ |
| 321 | static int ddebug_parse_query(char *words[], int nwords, |
| 322 | struct ddebug_query *query) |
| 323 | { |
| 324 | unsigned int i; |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 325 | int rc; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 326 | |
| 327 | /* check we have an even number of words */ |
| 328 | if (nwords % 2 != 0) |
| 329 | return -EINVAL; |
| 330 | memset(query, 0, sizeof(*query)); |
| 331 | |
| 332 | for (i = 0 ; i < nwords ; i += 2) { |
| 333 | if (!strcmp(words[i], "func")) |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 334 | rc = check_set(&query->function, words[i+1], "func"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 335 | else if (!strcmp(words[i], "file")) |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 336 | rc = check_set(&query->filename, words[i+1], "file"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 337 | else if (!strcmp(words[i], "module")) |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 338 | rc = check_set(&query->module, words[i+1], "module"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 339 | else if (!strcmp(words[i], "format")) |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 340 | rc = check_set(&query->format, unescape(words[i+1]), |
| 341 | "format"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 342 | else if (!strcmp(words[i], "line")) { |
| 343 | char *first = words[i+1]; |
| 344 | char *last = strchr(first, '-'); |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 345 | if (query->first_lineno || query->last_lineno) { |
| 346 | pr_err("match-spec:line given 2 times\n"); |
| 347 | return -EINVAL; |
| 348 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 349 | if (last) |
| 350 | *last++ = '\0'; |
| 351 | if (parse_lineno(first, &query->first_lineno) < 0) |
| 352 | return -EINVAL; |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 353 | if (last) { |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 354 | /* range <first>-<last> */ |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 355 | if (parse_lineno(last, &query->last_lineno) |
| 356 | < query->first_lineno) { |
| 357 | pr_err("last-line < 1st-line\n"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 358 | return -EINVAL; |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 359 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 360 | } else { |
| 361 | query->last_lineno = query->first_lineno; |
| 362 | } |
| 363 | } else { |
Jim Cromie | ae27f86 | 2011-12-19 17:12:34 -0500 | [diff] [blame] | 364 | pr_err("unknown keyword \"%s\"\n", words[i]); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 365 | return -EINVAL; |
| 366 | } |
Jim Cromie | 820874c | 2011-12-19 17:12:49 -0500 | [diff] [blame] | 367 | if (rc) |
| 368 | return rc; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 372 | pr_info("q->function=\"%s\" q->filename=\"%s\" " |
| 373 | "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n", |
| 374 | query->function, query->filename, |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 375 | query->module, query->format, query->first_lineno, |
| 376 | query->last_lineno); |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * Parse `str' as a flags specification, format [-+=][p]+. |
| 383 | * Sets up *maskp and *flagsp to be used when changing the |
| 384 | * flags fields of matched _ddebug's. Returns 0 on success |
| 385 | * or <0 on error. |
| 386 | */ |
| 387 | static int ddebug_parse_flags(const char *str, unsigned int *flagsp, |
| 388 | unsigned int *maskp) |
| 389 | { |
| 390 | unsigned flags = 0; |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 391 | int op = '=', i; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 392 | |
| 393 | switch (*str) { |
| 394 | case '+': |
| 395 | case '-': |
| 396 | case '=': |
| 397 | op = *str++; |
| 398 | break; |
| 399 | default: |
| 400 | return -EINVAL; |
| 401 | } |
| 402 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 403 | pr_info("op='%c'\n", op); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 404 | |
| 405 | for ( ; *str ; ++str) { |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 406 | for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) { |
| 407 | if (*str == opt_array[i].opt_char) { |
| 408 | flags |= opt_array[i].flag; |
| 409 | break; |
| 410 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 411 | } |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 412 | if (i < 0) |
| 413 | return -EINVAL; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 414 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 415 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 416 | pr_info("flags=0x%x\n", flags); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 417 | |
| 418 | /* calculate final *flagsp, *maskp according to mask and op */ |
| 419 | switch (op) { |
| 420 | case '=': |
| 421 | *maskp = 0; |
| 422 | *flagsp = flags; |
| 423 | break; |
| 424 | case '+': |
| 425 | *maskp = ~0U; |
| 426 | *flagsp = flags; |
| 427 | break; |
| 428 | case '-': |
| 429 | *maskp = ~flags; |
| 430 | *flagsp = 0; |
| 431 | break; |
| 432 | } |
| 433 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 434 | pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 438 | static int ddebug_exec_query(char *query_string) |
| 439 | { |
| 440 | unsigned int flags = 0, mask = 0; |
| 441 | struct ddebug_query query; |
| 442 | #define MAXWORDS 9 |
| 443 | int nwords; |
| 444 | char *words[MAXWORDS]; |
| 445 | |
| 446 | nwords = ddebug_tokenize(query_string, words, MAXWORDS); |
| 447 | if (nwords <= 0) |
| 448 | return -EINVAL; |
| 449 | if (ddebug_parse_query(words, nwords-1, &query)) |
| 450 | return -EINVAL; |
| 451 | if (ddebug_parse_flags(words[nwords-1], &flags, &mask)) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | /* actually go and implement the change */ |
| 455 | ddebug_change(&query, flags, mask); |
| 456 | return 0; |
| 457 | } |
| 458 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 459 | #define PREFIX_SIZE 64 |
| 460 | |
| 461 | static int remaining(int wrote) |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 462 | { |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 463 | if (PREFIX_SIZE - wrote > 0) |
| 464 | return PREFIX_SIZE - wrote; |
| 465 | return 0; |
| 466 | } |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 467 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 468 | static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf) |
| 469 | { |
| 470 | int pos_after_tid; |
| 471 | int pos = 0; |
| 472 | |
| 473 | pos += snprintf(buf + pos, remaining(pos), "%s", KERN_DEBUG); |
| 474 | if (desc->flags & _DPRINTK_FLAGS_INCL_TID) { |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 475 | if (in_interrupt()) |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 476 | pos += snprintf(buf + pos, remaining(pos), "%s ", |
| 477 | "<intr>"); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 478 | else |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 479 | pos += snprintf(buf + pos, remaining(pos), "[%d] ", |
| 480 | task_pid_vnr(current)); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 481 | } |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 482 | pos_after_tid = pos; |
| 483 | if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME) |
| 484 | pos += snprintf(buf + pos, remaining(pos), "%s:", |
| 485 | desc->modname); |
| 486 | if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) |
| 487 | pos += snprintf(buf + pos, remaining(pos), "%s:", |
| 488 | desc->function); |
| 489 | if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO) |
Jim Cromie | 07100be | 2011-12-19 17:11:09 -0500 | [diff] [blame] | 490 | pos += snprintf(buf + pos, remaining(pos), "%d:", |
| 491 | desc->lineno); |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 492 | if (pos - pos_after_tid) |
| 493 | pos += snprintf(buf + pos, remaining(pos), " "); |
| 494 | if (pos >= PREFIX_SIZE) |
| 495 | buf[PREFIX_SIZE - 1] = '\0'; |
Joe Perches | 6c2140e | 2011-08-11 14:36:25 -0400 | [diff] [blame] | 496 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 497 | return buf; |
Joe Perches | 6c2140e | 2011-08-11 14:36:25 -0400 | [diff] [blame] | 498 | } |
| 499 | |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 500 | int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...) |
| 501 | { |
| 502 | va_list args; |
| 503 | int res; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 504 | struct va_format vaf; |
| 505 | char buf[PREFIX_SIZE]; |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 506 | |
| 507 | BUG_ON(!descriptor); |
| 508 | BUG_ON(!fmt); |
| 509 | |
| 510 | va_start(args, fmt); |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 511 | vaf.fmt = fmt; |
| 512 | vaf.va = &args; |
| 513 | res = printk("%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 514 | va_end(args); |
| 515 | |
| 516 | return res; |
| 517 | } |
| 518 | EXPORT_SYMBOL(__dynamic_pr_debug); |
| 519 | |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 520 | int __dynamic_dev_dbg(struct _ddebug *descriptor, |
| 521 | const struct device *dev, const char *fmt, ...) |
| 522 | { |
| 523 | struct va_format vaf; |
| 524 | va_list args; |
| 525 | int res; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 526 | char buf[PREFIX_SIZE]; |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 527 | |
| 528 | BUG_ON(!descriptor); |
| 529 | BUG_ON(!fmt); |
| 530 | |
| 531 | va_start(args, fmt); |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 532 | vaf.fmt = fmt; |
| 533 | vaf.va = &args; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 534 | res = __dev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf); |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 535 | va_end(args); |
| 536 | |
| 537 | return res; |
| 538 | } |
| 539 | EXPORT_SYMBOL(__dynamic_dev_dbg); |
| 540 | |
Jason Baron | 0feefd9 | 2011-10-04 14:13:22 -0700 | [diff] [blame] | 541 | #ifdef CONFIG_NET |
| 542 | |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 543 | int __dynamic_netdev_dbg(struct _ddebug *descriptor, |
| 544 | const struct net_device *dev, const char *fmt, ...) |
| 545 | { |
| 546 | struct va_format vaf; |
| 547 | va_list args; |
| 548 | int res; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 549 | char buf[PREFIX_SIZE]; |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 550 | |
| 551 | BUG_ON(!descriptor); |
| 552 | BUG_ON(!fmt); |
| 553 | |
| 554 | va_start(args, fmt); |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 555 | vaf.fmt = fmt; |
| 556 | vaf.va = &args; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 557 | res = __netdev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf); |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 558 | va_end(args); |
| 559 | |
| 560 | return res; |
| 561 | } |
| 562 | EXPORT_SYMBOL(__dynamic_netdev_dbg); |
| 563 | |
Jason Baron | 0feefd9 | 2011-10-04 14:13:22 -0700 | [diff] [blame] | 564 | #endif |
| 565 | |
Jim Cromie | bc757f6 | 2011-12-19 17:12:29 -0500 | [diff] [blame] | 566 | #define DDEBUG_STRING_SIZE 1024 |
| 567 | static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE]; |
| 568 | |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 569 | static __init int ddebug_setup_query(char *str) |
| 570 | { |
Jim Cromie | bc757f6 | 2011-12-19 17:12:29 -0500 | [diff] [blame] | 571 | if (strlen(str) >= DDEBUG_STRING_SIZE) { |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 572 | pr_warn("ddebug boot param string too large\n"); |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 573 | return 0; |
| 574 | } |
Jim Cromie | bc757f6 | 2011-12-19 17:12:29 -0500 | [diff] [blame] | 575 | strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE); |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 576 | return 1; |
| 577 | } |
| 578 | |
| 579 | __setup("ddebug_query=", ddebug_setup_query); |
| 580 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 581 | /* |
| 582 | * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the |
| 583 | * command text from userspace, parses and executes it. |
| 584 | */ |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 585 | #define USER_BUF_PAGE 4096 |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 586 | static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf, |
| 587 | size_t len, loff_t *offp) |
| 588 | { |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 589 | char *tmpbuf; |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 590 | int ret; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 591 | |
| 592 | if (len == 0) |
| 593 | return 0; |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 594 | if (len > USER_BUF_PAGE - 1) { |
| 595 | pr_warn("expected <%d bytes into control\n", USER_BUF_PAGE); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 596 | return -E2BIG; |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 597 | } |
| 598 | tmpbuf = kmalloc(len + 1, GFP_KERNEL); |
| 599 | if (!tmpbuf) |
| 600 | return -ENOMEM; |
| 601 | if (copy_from_user(tmpbuf, ubuf, len)) { |
| 602 | kfree(tmpbuf); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 603 | return -EFAULT; |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 604 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 605 | tmpbuf[len] = '\0'; |
| 606 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 607 | pr_info("read %d bytes from userspace\n", (int)len); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 608 | |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 609 | ret = ddebug_exec_query(tmpbuf); |
Jim Cromie | 7281491 | 2011-12-19 17:13:07 -0500 | [diff] [blame] | 610 | kfree(tmpbuf); |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 611 | if (ret) |
| 612 | return ret; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 613 | |
| 614 | *offp += len; |
| 615 | return len; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Set the iterator to point to the first _ddebug object |
| 620 | * and return a pointer to that first object. Returns |
| 621 | * NULL if there are no _ddebugs at all. |
| 622 | */ |
| 623 | static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter) |
| 624 | { |
| 625 | if (list_empty(&ddebug_tables)) { |
| 626 | iter->table = NULL; |
| 627 | iter->idx = 0; |
| 628 | return NULL; |
| 629 | } |
| 630 | iter->table = list_entry(ddebug_tables.next, |
| 631 | struct ddebug_table, link); |
| 632 | iter->idx = 0; |
| 633 | return &iter->table->ddebugs[iter->idx]; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Advance the iterator to point to the next _ddebug |
| 638 | * object from the one the iterator currently points at, |
| 639 | * and returns a pointer to the new _ddebug. Returns |
| 640 | * NULL if the iterator has seen all the _ddebugs. |
| 641 | */ |
| 642 | static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter) |
| 643 | { |
| 644 | if (iter->table == NULL) |
| 645 | return NULL; |
| 646 | if (++iter->idx == iter->table->num_ddebugs) { |
| 647 | /* iterate to next table */ |
| 648 | iter->idx = 0; |
| 649 | if (list_is_last(&iter->table->link, &ddebug_tables)) { |
| 650 | iter->table = NULL; |
| 651 | return NULL; |
| 652 | } |
| 653 | iter->table = list_entry(iter->table->link.next, |
| 654 | struct ddebug_table, link); |
| 655 | } |
| 656 | return &iter->table->ddebugs[iter->idx]; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Seq_ops start method. Called at the start of every |
| 661 | * read() call from userspace. Takes the ddebug_lock and |
| 662 | * seeks the seq_file's iterator to the given position. |
| 663 | */ |
| 664 | static void *ddebug_proc_start(struct seq_file *m, loff_t *pos) |
| 665 | { |
| 666 | struct ddebug_iter *iter = m->private; |
| 667 | struct _ddebug *dp; |
| 668 | int n = *pos; |
| 669 | |
| 670 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 671 | pr_info("called m=%p *pos=%lld\n", m, (unsigned long long)*pos); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 672 | |
| 673 | mutex_lock(&ddebug_lock); |
| 674 | |
| 675 | if (!n) |
| 676 | return SEQ_START_TOKEN; |
| 677 | if (n < 0) |
| 678 | return NULL; |
| 679 | dp = ddebug_iter_first(iter); |
| 680 | while (dp != NULL && --n > 0) |
| 681 | dp = ddebug_iter_next(iter); |
| 682 | return dp; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Seq_ops next method. Called several times within a read() |
| 687 | * call from userspace, with ddebug_lock held. Walks to the |
| 688 | * next _ddebug object with a special case for the header line. |
| 689 | */ |
| 690 | static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos) |
| 691 | { |
| 692 | struct ddebug_iter *iter = m->private; |
| 693 | struct _ddebug *dp; |
| 694 | |
| 695 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 696 | pr_info("called m=%p p=%p *pos=%lld\n", |
| 697 | m, p, (unsigned long long)*pos); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 698 | |
| 699 | if (p == SEQ_START_TOKEN) |
| 700 | dp = ddebug_iter_first(iter); |
| 701 | else |
| 702 | dp = ddebug_iter_next(iter); |
| 703 | ++*pos; |
| 704 | return dp; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Seq_ops show method. Called several times within a read() |
| 709 | * call from userspace, with ddebug_lock held. Formats the |
| 710 | * current _ddebug as a single human-readable line, with a |
| 711 | * special case for the header line. |
| 712 | */ |
| 713 | static int ddebug_proc_show(struct seq_file *m, void *p) |
| 714 | { |
| 715 | struct ddebug_iter *iter = m->private; |
| 716 | struct _ddebug *dp = p; |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 717 | char flagsbuf[10]; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 718 | |
| 719 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 720 | pr_info("called m=%p p=%p\n", m, p); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 721 | |
| 722 | if (p == SEQ_START_TOKEN) { |
| 723 | seq_puts(m, |
| 724 | "# filename:lineno [module]function flags format\n"); |
| 725 | return 0; |
| 726 | } |
| 727 | |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 728 | seq_printf(m, "%s:%u [%s]%s =%s \"", |
Jim Cromie | 2b67831 | 2011-12-19 17:13:12 -0500 | [diff] [blame^] | 729 | trim_prefix(dp->filename), dp->lineno, |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame] | 730 | iter->table->mod_name, dp->function, |
| 731 | ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf))); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 732 | seq_escape(m, dp->format, "\t\r\n\""); |
| 733 | seq_puts(m, "\"\n"); |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Seq_ops stop method. Called at the end of each read() |
| 740 | * call from userspace. Drops ddebug_lock. |
| 741 | */ |
| 742 | static void ddebug_proc_stop(struct seq_file *m, void *p) |
| 743 | { |
| 744 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 745 | pr_info("called m=%p p=%p\n", m, p); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 746 | mutex_unlock(&ddebug_lock); |
| 747 | } |
| 748 | |
| 749 | static const struct seq_operations ddebug_proc_seqops = { |
| 750 | .start = ddebug_proc_start, |
| 751 | .next = ddebug_proc_next, |
| 752 | .show = ddebug_proc_show, |
| 753 | .stop = ddebug_proc_stop |
| 754 | }; |
| 755 | |
| 756 | /* |
Jim Cromie | 07100be | 2011-12-19 17:11:09 -0500 | [diff] [blame] | 757 | * File_ops->open method for <debugfs>/dynamic_debug/control. Does |
| 758 | * the seq_file setup dance, and also creates an iterator to walk the |
| 759 | * _ddebugs. Note that we create a seq_file always, even for O_WRONLY |
| 760 | * files where it's not needed, as doing so simplifies the ->release |
| 761 | * method. |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 762 | */ |
| 763 | static int ddebug_proc_open(struct inode *inode, struct file *file) |
| 764 | { |
| 765 | struct ddebug_iter *iter; |
| 766 | int err; |
| 767 | |
| 768 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 769 | pr_info("called\n"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 770 | |
| 771 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 772 | if (iter == NULL) |
| 773 | return -ENOMEM; |
| 774 | |
| 775 | err = seq_open(file, &ddebug_proc_seqops); |
| 776 | if (err) { |
| 777 | kfree(iter); |
| 778 | return err; |
| 779 | } |
| 780 | ((struct seq_file *) file->private_data)->private = iter; |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static const struct file_operations ddebug_proc_fops = { |
| 785 | .owner = THIS_MODULE, |
| 786 | .open = ddebug_proc_open, |
| 787 | .read = seq_read, |
| 788 | .llseek = seq_lseek, |
| 789 | .release = seq_release_private, |
| 790 | .write = ddebug_proc_write |
| 791 | }; |
| 792 | |
| 793 | /* |
| 794 | * Allocate a new ddebug_table for the given module |
| 795 | * and add it to the global list. |
| 796 | */ |
| 797 | int ddebug_add_module(struct _ddebug *tab, unsigned int n, |
| 798 | const char *name) |
| 799 | { |
| 800 | struct ddebug_table *dt; |
| 801 | char *new_name; |
| 802 | |
| 803 | dt = kzalloc(sizeof(*dt), GFP_KERNEL); |
| 804 | if (dt == NULL) |
| 805 | return -ENOMEM; |
| 806 | new_name = kstrdup(name, GFP_KERNEL); |
| 807 | if (new_name == NULL) { |
| 808 | kfree(dt); |
| 809 | return -ENOMEM; |
| 810 | } |
| 811 | dt->mod_name = new_name; |
| 812 | dt->num_ddebugs = n; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 813 | dt->ddebugs = tab; |
| 814 | |
| 815 | mutex_lock(&ddebug_lock); |
| 816 | list_add_tail(&dt->link, &ddebug_tables); |
| 817 | mutex_unlock(&ddebug_lock); |
| 818 | |
| 819 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 820 | pr_info("%u debug prints in module %s\n", n, dt->mod_name); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | EXPORT_SYMBOL_GPL(ddebug_add_module); |
| 824 | |
| 825 | static void ddebug_table_free(struct ddebug_table *dt) |
| 826 | { |
| 827 | list_del_init(&dt->link); |
| 828 | kfree(dt->mod_name); |
| 829 | kfree(dt); |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | * Called in response to a module being unloaded. Removes |
| 834 | * any ddebug_table's which point at the module. |
| 835 | */ |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 836 | int ddebug_remove_module(const char *mod_name) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 837 | { |
| 838 | struct ddebug_table *dt, *nextdt; |
| 839 | int ret = -ENOENT; |
| 840 | |
| 841 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 842 | pr_info("removing module \"%s\"\n", mod_name); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 843 | |
| 844 | mutex_lock(&ddebug_lock); |
| 845 | list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) { |
| 846 | if (!strcmp(dt->mod_name, mod_name)) { |
| 847 | ddebug_table_free(dt); |
| 848 | ret = 0; |
| 849 | } |
| 850 | } |
| 851 | mutex_unlock(&ddebug_lock); |
| 852 | return ret; |
| 853 | } |
| 854 | EXPORT_SYMBOL_GPL(ddebug_remove_module); |
| 855 | |
| 856 | static void ddebug_remove_all_tables(void) |
| 857 | { |
| 858 | mutex_lock(&ddebug_lock); |
| 859 | while (!list_empty(&ddebug_tables)) { |
| 860 | struct ddebug_table *dt = list_entry(ddebug_tables.next, |
| 861 | struct ddebug_table, |
| 862 | link); |
| 863 | ddebug_table_free(dt); |
| 864 | } |
| 865 | mutex_unlock(&ddebug_lock); |
| 866 | } |
| 867 | |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 868 | static __initdata int ddebug_init_success; |
| 869 | |
| 870 | static int __init dynamic_debug_init_debugfs(void) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 871 | { |
| 872 | struct dentry *dir, *file; |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 873 | |
| 874 | if (!ddebug_init_success) |
| 875 | return -ENODEV; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 876 | |
| 877 | dir = debugfs_create_dir("dynamic_debug", NULL); |
| 878 | if (!dir) |
| 879 | return -ENOMEM; |
| 880 | file = debugfs_create_file("control", 0644, dir, NULL, |
| 881 | &ddebug_proc_fops); |
| 882 | if (!file) { |
| 883 | debugfs_remove(dir); |
| 884 | return -ENOMEM; |
| 885 | } |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | static int __init dynamic_debug_init(void) |
| 890 | { |
| 891 | struct _ddebug *iter, *iter_start; |
| 892 | const char *modname = NULL; |
| 893 | int ret = 0; |
| 894 | int n = 0; |
| 895 | |
Jim Cromie | b5b78f8 | 2011-12-19 17:12:54 -0500 | [diff] [blame] | 896 | if (__start___verbose == __stop___verbose) { |
| 897 | pr_warn("_ddebug table is empty in a " |
| 898 | "CONFIG_DYNAMIC_DEBUG build"); |
| 899 | return 1; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 900 | } |
Jim Cromie | b5b78f8 | 2011-12-19 17:12:54 -0500 | [diff] [blame] | 901 | iter = __start___verbose; |
| 902 | modname = iter->modname; |
| 903 | iter_start = iter; |
| 904 | for (; iter < __stop___verbose; iter++) { |
| 905 | if (strcmp(modname, iter->modname)) { |
| 906 | ret = ddebug_add_module(iter_start, n, modname); |
| 907 | if (ret) |
| 908 | goto out_free; |
| 909 | n = 0; |
| 910 | modname = iter->modname; |
| 911 | iter_start = iter; |
| 912 | } |
| 913 | n++; |
| 914 | } |
| 915 | ret = ddebug_add_module(iter_start, n, modname); |
| 916 | if (ret) |
| 917 | goto out_free; |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 918 | |
| 919 | /* ddebug_query boot param got passed -> set it up */ |
| 920 | if (ddebug_setup_string[0] != '\0') { |
| 921 | ret = ddebug_exec_query(ddebug_setup_string); |
| 922 | if (ret) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 923 | pr_warn("Invalid ddebug boot param %s", |
| 924 | ddebug_setup_string); |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 925 | else |
| 926 | pr_info("ddebug initialized with string %s", |
| 927 | ddebug_setup_string); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 928 | } |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 929 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 930 | out_free: |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 931 | if (ret) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 932 | ddebug_remove_all_tables(); |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 933 | else |
| 934 | ddebug_init_success = 1; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 935 | return 0; |
| 936 | } |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 937 | /* Allow early initialization for boot messages via boot param */ |
| 938 | arch_initcall(dynamic_debug_init); |
| 939 | /* Debugfs setup must be done later */ |
| 940 | module_init(dynamic_debug_init_debugfs); |