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