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