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