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