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 | |
| 279 | /* |
| 280 | * Parse words[] as a ddebug query specification, which is a series |
| 281 | * of (keyword, value) pairs chosen from these possibilities: |
| 282 | * |
| 283 | * func <function-name> |
| 284 | * file <full-pathname> |
| 285 | * file <base-filename> |
| 286 | * module <module-name> |
| 287 | * format <escaped-string-to-find-in-format> |
| 288 | * line <lineno> |
| 289 | * line <first-lineno>-<last-lineno> // where either may be empty |
| 290 | */ |
| 291 | static int ddebug_parse_query(char *words[], int nwords, |
| 292 | struct ddebug_query *query) |
| 293 | { |
| 294 | unsigned int i; |
| 295 | |
| 296 | /* check we have an even number of words */ |
| 297 | if (nwords % 2 != 0) |
| 298 | return -EINVAL; |
| 299 | memset(query, 0, sizeof(*query)); |
| 300 | |
| 301 | for (i = 0 ; i < nwords ; i += 2) { |
| 302 | if (!strcmp(words[i], "func")) |
| 303 | query->function = words[i+1]; |
| 304 | else if (!strcmp(words[i], "file")) |
| 305 | query->filename = words[i+1]; |
| 306 | else if (!strcmp(words[i], "module")) |
| 307 | query->module = words[i+1]; |
| 308 | else if (!strcmp(words[i], "format")) |
| 309 | query->format = unescape(words[i+1]); |
| 310 | else if (!strcmp(words[i], "line")) { |
| 311 | char *first = words[i+1]; |
| 312 | char *last = strchr(first, '-'); |
| 313 | if (last) |
| 314 | *last++ = '\0'; |
| 315 | if (parse_lineno(first, &query->first_lineno) < 0) |
| 316 | return -EINVAL; |
| 317 | if (last != NULL) { |
| 318 | /* range <first>-<last> */ |
| 319 | if (parse_lineno(last, &query->last_lineno) < 0) |
| 320 | return -EINVAL; |
| 321 | } else { |
| 322 | query->last_lineno = query->first_lineno; |
| 323 | } |
| 324 | } else { |
Jim Cromie | ae27f86 | 2011-12-19 17:12:34 -0500 | [diff] [blame] | 325 | pr_err("unknown keyword \"%s\"\n", words[i]); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 326 | return -EINVAL; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 331 | pr_info("q->function=\"%s\" q->filename=\"%s\" " |
| 332 | "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n", |
| 333 | query->function, query->filename, |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 334 | query->module, query->format, query->first_lineno, |
| 335 | query->last_lineno); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Parse `str' as a flags specification, format [-+=][p]+. |
| 342 | * Sets up *maskp and *flagsp to be used when changing the |
| 343 | * flags fields of matched _ddebug's. Returns 0 on success |
| 344 | * or <0 on error. |
| 345 | */ |
| 346 | static int ddebug_parse_flags(const char *str, unsigned int *flagsp, |
| 347 | unsigned int *maskp) |
| 348 | { |
| 349 | unsigned flags = 0; |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 350 | int op = '=', i; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 351 | |
| 352 | switch (*str) { |
| 353 | case '+': |
| 354 | case '-': |
| 355 | case '=': |
| 356 | op = *str++; |
| 357 | break; |
| 358 | default: |
| 359 | return -EINVAL; |
| 360 | } |
| 361 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 362 | pr_info("op='%c'\n", op); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 363 | |
| 364 | for ( ; *str ; ++str) { |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 365 | for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) { |
| 366 | if (*str == opt_array[i].opt_char) { |
| 367 | flags |= opt_array[i].flag; |
| 368 | break; |
| 369 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 370 | } |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 371 | if (i < 0) |
| 372 | return -EINVAL; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 373 | } |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 374 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 375 | pr_info("flags=0x%x\n", flags); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 376 | |
| 377 | /* calculate final *flagsp, *maskp according to mask and op */ |
| 378 | switch (op) { |
| 379 | case '=': |
| 380 | *maskp = 0; |
| 381 | *flagsp = flags; |
| 382 | break; |
| 383 | case '+': |
| 384 | *maskp = ~0U; |
| 385 | *flagsp = flags; |
| 386 | break; |
| 387 | case '-': |
| 388 | *maskp = ~flags; |
| 389 | *flagsp = 0; |
| 390 | break; |
| 391 | } |
| 392 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 393 | pr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 397 | static int ddebug_exec_query(char *query_string) |
| 398 | { |
| 399 | unsigned int flags = 0, mask = 0; |
| 400 | struct ddebug_query query; |
| 401 | #define MAXWORDS 9 |
| 402 | int nwords; |
| 403 | char *words[MAXWORDS]; |
| 404 | |
| 405 | nwords = ddebug_tokenize(query_string, words, MAXWORDS); |
| 406 | if (nwords <= 0) |
| 407 | return -EINVAL; |
| 408 | if (ddebug_parse_query(words, nwords-1, &query)) |
| 409 | return -EINVAL; |
| 410 | if (ddebug_parse_flags(words[nwords-1], &flags, &mask)) |
| 411 | return -EINVAL; |
| 412 | |
| 413 | /* actually go and implement the change */ |
| 414 | ddebug_change(&query, flags, mask); |
| 415 | return 0; |
| 416 | } |
| 417 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 418 | #define PREFIX_SIZE 64 |
| 419 | |
| 420 | static int remaining(int wrote) |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 421 | { |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 422 | if (PREFIX_SIZE - wrote > 0) |
| 423 | return PREFIX_SIZE - wrote; |
| 424 | return 0; |
| 425 | } |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 426 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 427 | static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf) |
| 428 | { |
| 429 | int pos_after_tid; |
| 430 | int pos = 0; |
| 431 | |
| 432 | pos += snprintf(buf + pos, remaining(pos), "%s", KERN_DEBUG); |
| 433 | if (desc->flags & _DPRINTK_FLAGS_INCL_TID) { |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 434 | if (in_interrupt()) |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 435 | pos += snprintf(buf + pos, remaining(pos), "%s ", |
| 436 | "<intr>"); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 437 | else |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 438 | pos += snprintf(buf + pos, remaining(pos), "[%d] ", |
| 439 | task_pid_vnr(current)); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 440 | } |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 441 | pos_after_tid = pos; |
| 442 | if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME) |
| 443 | pos += snprintf(buf + pos, remaining(pos), "%s:", |
| 444 | desc->modname); |
| 445 | if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) |
| 446 | pos += snprintf(buf + pos, remaining(pos), "%s:", |
| 447 | desc->function); |
| 448 | if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO) |
Jim Cromie | 07100be | 2011-12-19 17:11:09 -0500 | [diff] [blame] | 449 | pos += snprintf(buf + pos, remaining(pos), "%d:", |
| 450 | desc->lineno); |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 451 | if (pos - pos_after_tid) |
| 452 | pos += snprintf(buf + pos, remaining(pos), " "); |
| 453 | if (pos >= PREFIX_SIZE) |
| 454 | buf[PREFIX_SIZE - 1] = '\0'; |
Joe Perches | 6c2140e | 2011-08-11 14:36:25 -0400 | [diff] [blame] | 455 | |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 456 | return buf; |
Joe Perches | 6c2140e | 2011-08-11 14:36:25 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 459 | int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...) |
| 460 | { |
| 461 | va_list args; |
| 462 | int res; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 463 | struct va_format vaf; |
| 464 | char buf[PREFIX_SIZE]; |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 465 | |
| 466 | BUG_ON(!descriptor); |
| 467 | BUG_ON(!fmt); |
| 468 | |
| 469 | va_start(args, fmt); |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 470 | vaf.fmt = fmt; |
| 471 | vaf.va = &args; |
| 472 | res = printk("%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf); |
Bart Van Assche | 8ba6ebf5 | 2011-01-23 17:17:24 +0100 | [diff] [blame] | 473 | va_end(args); |
| 474 | |
| 475 | return res; |
| 476 | } |
| 477 | EXPORT_SYMBOL(__dynamic_pr_debug); |
| 478 | |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 479 | int __dynamic_dev_dbg(struct _ddebug *descriptor, |
| 480 | const struct device *dev, const char *fmt, ...) |
| 481 | { |
| 482 | struct va_format vaf; |
| 483 | va_list args; |
| 484 | int res; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 485 | char buf[PREFIX_SIZE]; |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 486 | |
| 487 | BUG_ON(!descriptor); |
| 488 | BUG_ON(!fmt); |
| 489 | |
| 490 | va_start(args, fmt); |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 491 | vaf.fmt = fmt; |
| 492 | vaf.va = &args; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 493 | res = __dev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf); |
Joe Perches | cbc4663 | 2011-08-11 14:36:21 -0400 | [diff] [blame] | 494 | va_end(args); |
| 495 | |
| 496 | return res; |
| 497 | } |
| 498 | EXPORT_SYMBOL(__dynamic_dev_dbg); |
| 499 | |
Jason Baron | 0feefd9 | 2011-10-04 14:13:22 -0700 | [diff] [blame] | 500 | #ifdef CONFIG_NET |
| 501 | |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 502 | int __dynamic_netdev_dbg(struct _ddebug *descriptor, |
| 503 | const struct net_device *dev, const char *fmt, ...) |
| 504 | { |
| 505 | struct va_format vaf; |
| 506 | va_list args; |
| 507 | int res; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 508 | char buf[PREFIX_SIZE]; |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 509 | |
| 510 | BUG_ON(!descriptor); |
| 511 | BUG_ON(!fmt); |
| 512 | |
| 513 | va_start(args, fmt); |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 514 | vaf.fmt = fmt; |
| 515 | vaf.va = &args; |
Jason Baron | 431625d | 2011-10-04 14:13:19 -0700 | [diff] [blame] | 516 | res = __netdev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf); |
Jason Baron | ffa10cb | 2011-08-11 14:36:48 -0400 | [diff] [blame] | 517 | va_end(args); |
| 518 | |
| 519 | return res; |
| 520 | } |
| 521 | EXPORT_SYMBOL(__dynamic_netdev_dbg); |
| 522 | |
Jason Baron | 0feefd9 | 2011-10-04 14:13:22 -0700 | [diff] [blame] | 523 | #endif |
| 524 | |
Jim Cromie | bc757f6 | 2011-12-19 17:12:29 -0500 | [diff] [blame] | 525 | #define DDEBUG_STRING_SIZE 1024 |
| 526 | static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE]; |
| 527 | |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 528 | static __init int ddebug_setup_query(char *str) |
| 529 | { |
Jim Cromie | bc757f6 | 2011-12-19 17:12:29 -0500 | [diff] [blame] | 530 | if (strlen(str) >= DDEBUG_STRING_SIZE) { |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 531 | pr_warn("ddebug boot param string too large\n"); |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 532 | return 0; |
| 533 | } |
Jim Cromie | bc757f6 | 2011-12-19 17:12:29 -0500 | [diff] [blame] | 534 | strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE); |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 535 | return 1; |
| 536 | } |
| 537 | |
| 538 | __setup("ddebug_query=", ddebug_setup_query); |
| 539 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 540 | /* |
| 541 | * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the |
| 542 | * command text from userspace, parses and executes it. |
| 543 | */ |
| 544 | static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf, |
| 545 | size_t len, loff_t *offp) |
| 546 | { |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 547 | char tmpbuf[256]; |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 548 | int ret; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 549 | |
| 550 | if (len == 0) |
| 551 | return 0; |
| 552 | /* we don't check *offp -- multiple writes() are allowed */ |
| 553 | if (len > sizeof(tmpbuf)-1) |
| 554 | return -E2BIG; |
| 555 | if (copy_from_user(tmpbuf, ubuf, len)) |
| 556 | return -EFAULT; |
| 557 | tmpbuf[len] = '\0'; |
| 558 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 559 | pr_info("read %d bytes from userspace\n", (int)len); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 560 | |
Thomas Renninger | fd89cfb | 2010-08-06 16:11:01 +0200 | [diff] [blame] | 561 | ret = ddebug_exec_query(tmpbuf); |
| 562 | if (ret) |
| 563 | return ret; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 564 | |
| 565 | *offp += len; |
| 566 | return len; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Set the iterator to point to the first _ddebug object |
| 571 | * and return a pointer to that first object. Returns |
| 572 | * NULL if there are no _ddebugs at all. |
| 573 | */ |
| 574 | static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter) |
| 575 | { |
| 576 | if (list_empty(&ddebug_tables)) { |
| 577 | iter->table = NULL; |
| 578 | iter->idx = 0; |
| 579 | return NULL; |
| 580 | } |
| 581 | iter->table = list_entry(ddebug_tables.next, |
| 582 | struct ddebug_table, link); |
| 583 | iter->idx = 0; |
| 584 | return &iter->table->ddebugs[iter->idx]; |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * Advance the iterator to point to the next _ddebug |
| 589 | * object from the one the iterator currently points at, |
| 590 | * and returns a pointer to the new _ddebug. Returns |
| 591 | * NULL if the iterator has seen all the _ddebugs. |
| 592 | */ |
| 593 | static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter) |
| 594 | { |
| 595 | if (iter->table == NULL) |
| 596 | return NULL; |
| 597 | if (++iter->idx == iter->table->num_ddebugs) { |
| 598 | /* iterate to next table */ |
| 599 | iter->idx = 0; |
| 600 | if (list_is_last(&iter->table->link, &ddebug_tables)) { |
| 601 | iter->table = NULL; |
| 602 | return NULL; |
| 603 | } |
| 604 | iter->table = list_entry(iter->table->link.next, |
| 605 | struct ddebug_table, link); |
| 606 | } |
| 607 | return &iter->table->ddebugs[iter->idx]; |
| 608 | } |
| 609 | |
| 610 | /* |
| 611 | * Seq_ops start method. Called at the start of every |
| 612 | * read() call from userspace. Takes the ddebug_lock and |
| 613 | * seeks the seq_file's iterator to the given position. |
| 614 | */ |
| 615 | static void *ddebug_proc_start(struct seq_file *m, loff_t *pos) |
| 616 | { |
| 617 | struct ddebug_iter *iter = m->private; |
| 618 | struct _ddebug *dp; |
| 619 | int n = *pos; |
| 620 | |
| 621 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 622 | 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] | 623 | |
| 624 | mutex_lock(&ddebug_lock); |
| 625 | |
| 626 | if (!n) |
| 627 | return SEQ_START_TOKEN; |
| 628 | if (n < 0) |
| 629 | return NULL; |
| 630 | dp = ddebug_iter_first(iter); |
| 631 | while (dp != NULL && --n > 0) |
| 632 | dp = ddebug_iter_next(iter); |
| 633 | return dp; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Seq_ops next method. Called several times within a read() |
| 638 | * call from userspace, with ddebug_lock held. Walks to the |
| 639 | * next _ddebug object with a special case for the header line. |
| 640 | */ |
| 641 | static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos) |
| 642 | { |
| 643 | struct ddebug_iter *iter = m->private; |
| 644 | struct _ddebug *dp; |
| 645 | |
| 646 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 647 | pr_info("called m=%p p=%p *pos=%lld\n", |
| 648 | m, p, (unsigned long long)*pos); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 649 | |
| 650 | if (p == SEQ_START_TOKEN) |
| 651 | dp = ddebug_iter_first(iter); |
| 652 | else |
| 653 | dp = ddebug_iter_next(iter); |
| 654 | ++*pos; |
| 655 | return dp; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Seq_ops show method. Called several times within a read() |
| 660 | * call from userspace, with ddebug_lock held. Formats the |
| 661 | * current _ddebug as a single human-readable line, with a |
| 662 | * special case for the header line. |
| 663 | */ |
| 664 | static int ddebug_proc_show(struct seq_file *m, void *p) |
| 665 | { |
| 666 | struct ddebug_iter *iter = m->private; |
| 667 | struct _ddebug *dp = p; |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame^] | 668 | char flagsbuf[10]; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 669 | |
| 670 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 671 | pr_info("called m=%p p=%p\n", m, p); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 672 | |
| 673 | if (p == SEQ_START_TOKEN) { |
| 674 | seq_puts(m, |
| 675 | "# filename:lineno [module]function flags format\n"); |
| 676 | return 0; |
| 677 | } |
| 678 | |
Jim Cromie | 5ca7d2a | 2011-12-19 17:12:44 -0500 | [diff] [blame^] | 679 | seq_printf(m, "%s:%u [%s]%s =%s \"", |
| 680 | dp->filename, dp->lineno, |
| 681 | iter->table->mod_name, dp->function, |
| 682 | ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf))); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 683 | seq_escape(m, dp->format, "\t\r\n\""); |
| 684 | seq_puts(m, "\"\n"); |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Seq_ops stop method. Called at the end of each read() |
| 691 | * call from userspace. Drops ddebug_lock. |
| 692 | */ |
| 693 | static void ddebug_proc_stop(struct seq_file *m, void *p) |
| 694 | { |
| 695 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 696 | pr_info("called m=%p p=%p\n", m, p); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 697 | mutex_unlock(&ddebug_lock); |
| 698 | } |
| 699 | |
| 700 | static const struct seq_operations ddebug_proc_seqops = { |
| 701 | .start = ddebug_proc_start, |
| 702 | .next = ddebug_proc_next, |
| 703 | .show = ddebug_proc_show, |
| 704 | .stop = ddebug_proc_stop |
| 705 | }; |
| 706 | |
| 707 | /* |
Jim Cromie | 07100be | 2011-12-19 17:11:09 -0500 | [diff] [blame] | 708 | * File_ops->open method for <debugfs>/dynamic_debug/control. Does |
| 709 | * the seq_file setup dance, and also creates an iterator to walk the |
| 710 | * _ddebugs. Note that we create a seq_file always, even for O_WRONLY |
| 711 | * files where it's not needed, as doing so simplifies the ->release |
| 712 | * method. |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 713 | */ |
| 714 | static int ddebug_proc_open(struct inode *inode, struct file *file) |
| 715 | { |
| 716 | struct ddebug_iter *iter; |
| 717 | int err; |
| 718 | |
| 719 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 720 | pr_info("called\n"); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 721 | |
| 722 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 723 | if (iter == NULL) |
| 724 | return -ENOMEM; |
| 725 | |
| 726 | err = seq_open(file, &ddebug_proc_seqops); |
| 727 | if (err) { |
| 728 | kfree(iter); |
| 729 | return err; |
| 730 | } |
| 731 | ((struct seq_file *) file->private_data)->private = iter; |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | static const struct file_operations ddebug_proc_fops = { |
| 736 | .owner = THIS_MODULE, |
| 737 | .open = ddebug_proc_open, |
| 738 | .read = seq_read, |
| 739 | .llseek = seq_lseek, |
| 740 | .release = seq_release_private, |
| 741 | .write = ddebug_proc_write |
| 742 | }; |
| 743 | |
| 744 | /* |
| 745 | * Allocate a new ddebug_table for the given module |
| 746 | * and add it to the global list. |
| 747 | */ |
| 748 | int ddebug_add_module(struct _ddebug *tab, unsigned int n, |
| 749 | const char *name) |
| 750 | { |
| 751 | struct ddebug_table *dt; |
| 752 | char *new_name; |
| 753 | |
| 754 | dt = kzalloc(sizeof(*dt), GFP_KERNEL); |
| 755 | if (dt == NULL) |
| 756 | return -ENOMEM; |
| 757 | new_name = kstrdup(name, GFP_KERNEL); |
| 758 | if (new_name == NULL) { |
| 759 | kfree(dt); |
| 760 | return -ENOMEM; |
| 761 | } |
| 762 | dt->mod_name = new_name; |
| 763 | dt->num_ddebugs = n; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 764 | dt->ddebugs = tab; |
| 765 | |
| 766 | mutex_lock(&ddebug_lock); |
| 767 | list_add_tail(&dt->link, &ddebug_tables); |
| 768 | mutex_unlock(&ddebug_lock); |
| 769 | |
| 770 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 771 | 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] | 772 | return 0; |
| 773 | } |
| 774 | EXPORT_SYMBOL_GPL(ddebug_add_module); |
| 775 | |
| 776 | static void ddebug_table_free(struct ddebug_table *dt) |
| 777 | { |
| 778 | list_del_init(&dt->link); |
| 779 | kfree(dt->mod_name); |
| 780 | kfree(dt); |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Called in response to a module being unloaded. Removes |
| 785 | * any ddebug_table's which point at the module. |
| 786 | */ |
Yehuda Sadeh | ff49d74 | 2010-07-03 13:07:35 +1000 | [diff] [blame] | 787 | int ddebug_remove_module(const char *mod_name) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 788 | { |
| 789 | struct ddebug_table *dt, *nextdt; |
| 790 | int ret = -ENOENT; |
| 791 | |
| 792 | if (verbose) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 793 | pr_info("removing module \"%s\"\n", mod_name); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 794 | |
| 795 | mutex_lock(&ddebug_lock); |
| 796 | list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) { |
| 797 | if (!strcmp(dt->mod_name, mod_name)) { |
| 798 | ddebug_table_free(dt); |
| 799 | ret = 0; |
| 800 | } |
| 801 | } |
| 802 | mutex_unlock(&ddebug_lock); |
| 803 | return ret; |
| 804 | } |
| 805 | EXPORT_SYMBOL_GPL(ddebug_remove_module); |
| 806 | |
| 807 | static void ddebug_remove_all_tables(void) |
| 808 | { |
| 809 | mutex_lock(&ddebug_lock); |
| 810 | while (!list_empty(&ddebug_tables)) { |
| 811 | struct ddebug_table *dt = list_entry(ddebug_tables.next, |
| 812 | struct ddebug_table, |
| 813 | link); |
| 814 | ddebug_table_free(dt); |
| 815 | } |
| 816 | mutex_unlock(&ddebug_lock); |
| 817 | } |
| 818 | |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 819 | static __initdata int ddebug_init_success; |
| 820 | |
| 821 | static int __init dynamic_debug_init_debugfs(void) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 822 | { |
| 823 | struct dentry *dir, *file; |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 824 | |
| 825 | if (!ddebug_init_success) |
| 826 | return -ENODEV; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 827 | |
| 828 | dir = debugfs_create_dir("dynamic_debug", NULL); |
| 829 | if (!dir) |
| 830 | return -ENOMEM; |
| 831 | file = debugfs_create_file("control", 0644, dir, NULL, |
| 832 | &ddebug_proc_fops); |
| 833 | if (!file) { |
| 834 | debugfs_remove(dir); |
| 835 | return -ENOMEM; |
| 836 | } |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | static int __init dynamic_debug_init(void) |
| 841 | { |
| 842 | struct _ddebug *iter, *iter_start; |
| 843 | const char *modname = NULL; |
| 844 | int ret = 0; |
| 845 | int n = 0; |
| 846 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 847 | if (__start___verbose != __stop___verbose) { |
| 848 | iter = __start___verbose; |
| 849 | modname = iter->modname; |
| 850 | iter_start = iter; |
| 851 | for (; iter < __stop___verbose; iter++) { |
| 852 | if (strcmp(modname, iter->modname)) { |
| 853 | ret = ddebug_add_module(iter_start, n, modname); |
| 854 | if (ret) |
| 855 | goto out_free; |
| 856 | n = 0; |
| 857 | modname = iter->modname; |
| 858 | iter_start = iter; |
| 859 | } |
| 860 | n++; |
| 861 | } |
| 862 | ret = ddebug_add_module(iter_start, n, modname); |
| 863 | } |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 864 | |
| 865 | /* ddebug_query boot param got passed -> set it up */ |
| 866 | if (ddebug_setup_string[0] != '\0') { |
| 867 | ret = ddebug_exec_query(ddebug_setup_string); |
| 868 | if (ret) |
Joe Perches | 4ad275e | 2011-08-11 14:36:33 -0400 | [diff] [blame] | 869 | pr_warn("Invalid ddebug boot param %s", |
| 870 | ddebug_setup_string); |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 871 | else |
| 872 | pr_info("ddebug initialized with string %s", |
| 873 | ddebug_setup_string); |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 874 | } |
Thomas Renninger | a648ec0 | 2010-08-06 16:11:02 +0200 | [diff] [blame] | 875 | |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 876 | out_free: |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 877 | if (ret) |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 878 | ddebug_remove_all_tables(); |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 879 | else |
| 880 | ddebug_init_success = 1; |
Jason Baron | e9d376f | 2009-02-05 11:51:38 -0500 | [diff] [blame] | 881 | return 0; |
| 882 | } |
Thomas Renninger | 6a5c083 | 2010-08-06 16:11:03 +0200 | [diff] [blame] | 883 | /* Allow early initialization for boot messages via boot param */ |
| 884 | arch_initcall(dynamic_debug_init); |
| 885 | /* Debugfs setup must be done later */ |
| 886 | module_init(dynamic_debug_init_debugfs); |