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