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. | |
10 | */ | |
11 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/moduleparam.h> | |
15 | #include <linux/kallsyms.h> | |
16 | #include <linux/version.h> | |
17 | #include <linux/types.h> | |
18 | #include <linux/mutex.h> | |
19 | #include <linux/proc_fs.h> | |
20 | #include <linux/seq_file.h> | |
21 | #include <linux/list.h> | |
22 | #include <linux/sysctl.h> | |
23 | #include <linux/ctype.h> | |
e7d2860b | 24 | #include <linux/string.h> |
e9d376f0 JB |
25 | #include <linux/uaccess.h> |
26 | #include <linux/dynamic_debug.h> | |
27 | #include <linux/debugfs.h> | |
28 | ||
29 | extern struct _ddebug __start___verbose[]; | |
30 | extern struct _ddebug __stop___verbose[]; | |
31 | ||
32 | /* dynamic_debug_enabled, and dynamic_debug_enabled2 are bitmasks in which | |
33 | * bit n is set to 1 if any modname hashes into the bucket n, 0 otherwise. They | |
34 | * use independent hash functions, to reduce the chance of false positives. | |
35 | */ | |
36 | long long dynamic_debug_enabled; | |
37 | EXPORT_SYMBOL_GPL(dynamic_debug_enabled); | |
38 | long long dynamic_debug_enabled2; | |
39 | EXPORT_SYMBOL_GPL(dynamic_debug_enabled2); | |
40 | ||
41 | struct ddebug_table { | |
42 | struct list_head link; | |
43 | char *mod_name; | |
44 | unsigned int num_ddebugs; | |
45 | unsigned int num_enabled; | |
46 | struct _ddebug *ddebugs; | |
47 | }; | |
48 | ||
49 | struct ddebug_query { | |
50 | const char *filename; | |
51 | const char *module; | |
52 | const char *function; | |
53 | const char *format; | |
54 | unsigned int first_lineno, last_lineno; | |
55 | }; | |
56 | ||
57 | struct ddebug_iter { | |
58 | struct ddebug_table *table; | |
59 | unsigned int idx; | |
60 | }; | |
61 | ||
62 | static DEFINE_MUTEX(ddebug_lock); | |
63 | static LIST_HEAD(ddebug_tables); | |
64 | static int verbose = 0; | |
65 | ||
66 | /* Return the last part of a pathname */ | |
67 | static inline const char *basename(const char *path) | |
68 | { | |
69 | const char *tail = strrchr(path, '/'); | |
70 | return tail ? tail+1 : path; | |
71 | } | |
72 | ||
73 | /* format a string into buf[] which describes the _ddebug's flags */ | |
74 | static char *ddebug_describe_flags(struct _ddebug *dp, char *buf, | |
75 | size_t maxlen) | |
76 | { | |
77 | char *p = buf; | |
78 | ||
79 | BUG_ON(maxlen < 4); | |
80 | if (dp->flags & _DPRINTK_FLAGS_PRINT) | |
81 | *p++ = 'p'; | |
82 | if (p == buf) | |
83 | *p++ = '-'; | |
84 | *p = '\0'; | |
85 | ||
86 | return buf; | |
87 | } | |
88 | ||
89 | /* | |
90 | * must be called with ddebug_lock held | |
91 | */ | |
92 | ||
93 | static int disabled_hash(char hash, bool first_table) | |
94 | { | |
95 | struct ddebug_table *dt; | |
96 | char table_hash_value; | |
97 | ||
98 | list_for_each_entry(dt, &ddebug_tables, link) { | |
99 | if (first_table) | |
100 | table_hash_value = dt->ddebugs->primary_hash; | |
101 | else | |
102 | table_hash_value = dt->ddebugs->secondary_hash; | |
103 | if (dt->num_enabled && (hash == table_hash_value)) | |
104 | return 0; | |
105 | } | |
106 | return 1; | |
107 | } | |
108 | ||
109 | /* | |
110 | * Search the tables for _ddebug's which match the given | |
111 | * `query' and apply the `flags' and `mask' to them. Tells | |
112 | * the user which ddebug's were changed, or whether none | |
113 | * were matched. | |
114 | */ | |
115 | static void ddebug_change(const struct ddebug_query *query, | |
116 | unsigned int flags, unsigned int mask) | |
117 | { | |
118 | int i; | |
119 | struct ddebug_table *dt; | |
120 | unsigned int newflags; | |
121 | unsigned int nfound = 0; | |
122 | char flagbuf[8]; | |
123 | ||
124 | /* search for matching ddebugs */ | |
125 | mutex_lock(&ddebug_lock); | |
126 | list_for_each_entry(dt, &ddebug_tables, link) { | |
127 | ||
128 | /* match against the module name */ | |
129 | if (query->module != NULL && | |
130 | strcmp(query->module, dt->mod_name)) | |
131 | continue; | |
132 | ||
133 | for (i = 0 ; i < dt->num_ddebugs ; i++) { | |
134 | struct _ddebug *dp = &dt->ddebugs[i]; | |
135 | ||
136 | /* match against the source filename */ | |
137 | if (query->filename != NULL && | |
138 | strcmp(query->filename, dp->filename) && | |
139 | strcmp(query->filename, basename(dp->filename))) | |
140 | continue; | |
141 | ||
142 | /* match against the function */ | |
143 | if (query->function != NULL && | |
144 | strcmp(query->function, dp->function)) | |
145 | continue; | |
146 | ||
147 | /* match against the format */ | |
148 | if (query->format != NULL && | |
149 | strstr(dp->format, query->format) == NULL) | |
150 | continue; | |
151 | ||
152 | /* match against the line number range */ | |
153 | if (query->first_lineno && | |
154 | dp->lineno < query->first_lineno) | |
155 | continue; | |
156 | if (query->last_lineno && | |
157 | dp->lineno > query->last_lineno) | |
158 | continue; | |
159 | ||
160 | nfound++; | |
161 | ||
162 | newflags = (dp->flags & mask) | flags; | |
163 | if (newflags == dp->flags) | |
164 | continue; | |
165 | ||
166 | if (!newflags) | |
167 | dt->num_enabled--; | |
4df7b3e0 | 168 | else if (!dp->flags) |
e9d376f0 JB |
169 | dt->num_enabled++; |
170 | dp->flags = newflags; | |
171 | if (newflags) { | |
172 | dynamic_debug_enabled |= | |
173 | (1LL << dp->primary_hash); | |
174 | dynamic_debug_enabled2 |= | |
175 | (1LL << dp->secondary_hash); | |
176 | } else { | |
177 | if (disabled_hash(dp->primary_hash, true)) | |
178 | dynamic_debug_enabled &= | |
179 | ~(1LL << dp->primary_hash); | |
180 | if (disabled_hash(dp->secondary_hash, false)) | |
181 | dynamic_debug_enabled2 &= | |
182 | ~(1LL << dp->secondary_hash); | |
183 | } | |
184 | if (verbose) | |
185 | printk(KERN_INFO | |
186 | "ddebug: changed %s:%d [%s]%s %s\n", | |
187 | dp->filename, dp->lineno, | |
188 | dt->mod_name, dp->function, | |
189 | ddebug_describe_flags(dp, flagbuf, | |
190 | sizeof(flagbuf))); | |
191 | } | |
192 | } | |
193 | mutex_unlock(&ddebug_lock); | |
194 | ||
195 | if (!nfound && verbose) | |
196 | printk(KERN_INFO "ddebug: no matches for query\n"); | |
197 | } | |
198 | ||
e9d376f0 JB |
199 | /* |
200 | * Split the buffer `buf' into space-separated words. | |
9898abb3 GB |
201 | * Handles simple " and ' quoting, i.e. without nested, |
202 | * embedded or escaped \". Return the number of words | |
203 | * or <0 on error. | |
e9d376f0 JB |
204 | */ |
205 | static int ddebug_tokenize(char *buf, char *words[], int maxwords) | |
206 | { | |
207 | int nwords = 0; | |
208 | ||
9898abb3 GB |
209 | while (*buf) { |
210 | char *end; | |
211 | ||
212 | /* Skip leading whitespace */ | |
e7d2860b | 213 | buf = skip_spaces(buf); |
9898abb3 GB |
214 | if (!*buf) |
215 | break; /* oh, it was trailing whitespace */ | |
216 | ||
217 | /* Run `end' over a word, either whitespace separated or quoted */ | |
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 | } | |
229 | /* Here `buf' is the start of the word, `end' is one past the end */ | |
230 | ||
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; | |
241 | printk(KERN_INFO "%s: split into words:", __func__); | |
242 | for (i = 0 ; i < nwords ; i++) | |
243 | printk(" \"%s\"", words[i]); | |
244 | printk("\n"); | |
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 | ||
309 | /* | |
310 | * Parse words[] as a ddebug query specification, which is a series | |
311 | * of (keyword, value) pairs chosen from these possibilities: | |
312 | * | |
313 | * func <function-name> | |
314 | * file <full-pathname> | |
315 | * file <base-filename> | |
316 | * module <module-name> | |
317 | * format <escaped-string-to-find-in-format> | |
318 | * line <lineno> | |
319 | * line <first-lineno>-<last-lineno> // where either may be empty | |
320 | */ | |
321 | static int ddebug_parse_query(char *words[], int nwords, | |
322 | struct ddebug_query *query) | |
323 | { | |
324 | unsigned int i; | |
325 | ||
326 | /* check we have an even number of words */ | |
327 | if (nwords % 2 != 0) | |
328 | return -EINVAL; | |
329 | memset(query, 0, sizeof(*query)); | |
330 | ||
331 | for (i = 0 ; i < nwords ; i += 2) { | |
332 | if (!strcmp(words[i], "func")) | |
333 | query->function = words[i+1]; | |
334 | else if (!strcmp(words[i], "file")) | |
335 | query->filename = words[i+1]; | |
336 | else if (!strcmp(words[i], "module")) | |
337 | query->module = words[i+1]; | |
338 | else if (!strcmp(words[i], "format")) | |
339 | query->format = unescape(words[i+1]); | |
340 | else if (!strcmp(words[i], "line")) { | |
341 | char *first = words[i+1]; | |
342 | char *last = strchr(first, '-'); | |
343 | if (last) | |
344 | *last++ = '\0'; | |
345 | if (parse_lineno(first, &query->first_lineno) < 0) | |
346 | return -EINVAL; | |
347 | if (last != NULL) { | |
348 | /* range <first>-<last> */ | |
349 | if (parse_lineno(last, &query->last_lineno) < 0) | |
350 | return -EINVAL; | |
351 | } else { | |
352 | query->last_lineno = query->first_lineno; | |
353 | } | |
354 | } else { | |
355 | if (verbose) | |
356 | printk(KERN_ERR "%s: unknown keyword \"%s\"\n", | |
357 | __func__, words[i]); | |
358 | return -EINVAL; | |
359 | } | |
360 | } | |
361 | ||
362 | if (verbose) | |
363 | printk(KERN_INFO "%s: q->function=\"%s\" q->filename=\"%s\" " | |
364 | "q->module=\"%s\" q->format=\"%s\" q->lineno=%u-%u\n", | |
365 | __func__, query->function, query->filename, | |
366 | query->module, query->format, query->first_lineno, | |
367 | query->last_lineno); | |
368 | ||
369 | return 0; | |
370 | } | |
371 | ||
372 | /* | |
373 | * Parse `str' as a flags specification, format [-+=][p]+. | |
374 | * Sets up *maskp and *flagsp to be used when changing the | |
375 | * flags fields of matched _ddebug's. Returns 0 on success | |
376 | * or <0 on error. | |
377 | */ | |
378 | static int ddebug_parse_flags(const char *str, unsigned int *flagsp, | |
379 | unsigned int *maskp) | |
380 | { | |
381 | unsigned flags = 0; | |
382 | int op = '='; | |
383 | ||
384 | switch (*str) { | |
385 | case '+': | |
386 | case '-': | |
387 | case '=': | |
388 | op = *str++; | |
389 | break; | |
390 | default: | |
391 | return -EINVAL; | |
392 | } | |
393 | if (verbose) | |
394 | printk(KERN_INFO "%s: op='%c'\n", __func__, op); | |
395 | ||
396 | for ( ; *str ; ++str) { | |
397 | switch (*str) { | |
398 | case 'p': | |
399 | flags |= _DPRINTK_FLAGS_PRINT; | |
400 | break; | |
401 | default: | |
402 | return -EINVAL; | |
403 | } | |
404 | } | |
405 | if (flags == 0) | |
406 | return -EINVAL; | |
407 | if (verbose) | |
408 | printk(KERN_INFO "%s: flags=0x%x\n", __func__, flags); | |
409 | ||
410 | /* calculate final *flagsp, *maskp according to mask and op */ | |
411 | switch (op) { | |
412 | case '=': | |
413 | *maskp = 0; | |
414 | *flagsp = flags; | |
415 | break; | |
416 | case '+': | |
417 | *maskp = ~0U; | |
418 | *flagsp = flags; | |
419 | break; | |
420 | case '-': | |
421 | *maskp = ~flags; | |
422 | *flagsp = 0; | |
423 | break; | |
424 | } | |
425 | if (verbose) | |
426 | printk(KERN_INFO "%s: *flagsp=0x%x *maskp=0x%x\n", | |
427 | __func__, *flagsp, *maskp); | |
428 | return 0; | |
429 | } | |
430 | ||
431 | /* | |
432 | * File_ops->write method for <debugfs>/dynamic_debug/conrol. Gathers the | |
433 | * command text from userspace, parses and executes it. | |
434 | */ | |
435 | static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf, | |
436 | size_t len, loff_t *offp) | |
437 | { | |
438 | unsigned int flags = 0, mask = 0; | |
439 | struct ddebug_query query; | |
440 | #define MAXWORDS 9 | |
441 | int nwords; | |
442 | char *words[MAXWORDS]; | |
443 | char tmpbuf[256]; | |
444 | ||
445 | if (len == 0) | |
446 | return 0; | |
447 | /* we don't check *offp -- multiple writes() are allowed */ | |
448 | if (len > sizeof(tmpbuf)-1) | |
449 | return -E2BIG; | |
450 | if (copy_from_user(tmpbuf, ubuf, len)) | |
451 | return -EFAULT; | |
452 | tmpbuf[len] = '\0'; | |
453 | if (verbose) | |
454 | printk(KERN_INFO "%s: read %d bytes from userspace\n", | |
455 | __func__, (int)len); | |
456 | ||
457 | nwords = ddebug_tokenize(tmpbuf, words, MAXWORDS); | |
458 | if (nwords < 0) | |
459 | return -EINVAL; | |
460 | if (ddebug_parse_query(words, nwords-1, &query)) | |
461 | return -EINVAL; | |
462 | if (ddebug_parse_flags(words[nwords-1], &flags, &mask)) | |
463 | return -EINVAL; | |
464 | ||
465 | /* actually go and implement the change */ | |
466 | ddebug_change(&query, flags, mask); | |
467 | ||
468 | *offp += len; | |
469 | return len; | |
470 | } | |
471 | ||
472 | /* | |
473 | * Set the iterator to point to the first _ddebug object | |
474 | * and return a pointer to that first object. Returns | |
475 | * NULL if there are no _ddebugs at all. | |
476 | */ | |
477 | static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter) | |
478 | { | |
479 | if (list_empty(&ddebug_tables)) { | |
480 | iter->table = NULL; | |
481 | iter->idx = 0; | |
482 | return NULL; | |
483 | } | |
484 | iter->table = list_entry(ddebug_tables.next, | |
485 | struct ddebug_table, link); | |
486 | iter->idx = 0; | |
487 | return &iter->table->ddebugs[iter->idx]; | |
488 | } | |
489 | ||
490 | /* | |
491 | * Advance the iterator to point to the next _ddebug | |
492 | * object from the one the iterator currently points at, | |
493 | * and returns a pointer to the new _ddebug. Returns | |
494 | * NULL if the iterator has seen all the _ddebugs. | |
495 | */ | |
496 | static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter) | |
497 | { | |
498 | if (iter->table == NULL) | |
499 | return NULL; | |
500 | if (++iter->idx == iter->table->num_ddebugs) { | |
501 | /* iterate to next table */ | |
502 | iter->idx = 0; | |
503 | if (list_is_last(&iter->table->link, &ddebug_tables)) { | |
504 | iter->table = NULL; | |
505 | return NULL; | |
506 | } | |
507 | iter->table = list_entry(iter->table->link.next, | |
508 | struct ddebug_table, link); | |
509 | } | |
510 | return &iter->table->ddebugs[iter->idx]; | |
511 | } | |
512 | ||
513 | /* | |
514 | * Seq_ops start method. Called at the start of every | |
515 | * read() call from userspace. Takes the ddebug_lock and | |
516 | * seeks the seq_file's iterator to the given position. | |
517 | */ | |
518 | static void *ddebug_proc_start(struct seq_file *m, loff_t *pos) | |
519 | { | |
520 | struct ddebug_iter *iter = m->private; | |
521 | struct _ddebug *dp; | |
522 | int n = *pos; | |
523 | ||
524 | if (verbose) | |
525 | printk(KERN_INFO "%s: called m=%p *pos=%lld\n", | |
526 | __func__, m, (unsigned long long)*pos); | |
527 | ||
528 | mutex_lock(&ddebug_lock); | |
529 | ||
530 | if (!n) | |
531 | return SEQ_START_TOKEN; | |
532 | if (n < 0) | |
533 | return NULL; | |
534 | dp = ddebug_iter_first(iter); | |
535 | while (dp != NULL && --n > 0) | |
536 | dp = ddebug_iter_next(iter); | |
537 | return dp; | |
538 | } | |
539 | ||
540 | /* | |
541 | * Seq_ops next method. Called several times within a read() | |
542 | * call from userspace, with ddebug_lock held. Walks to the | |
543 | * next _ddebug object with a special case for the header line. | |
544 | */ | |
545 | static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos) | |
546 | { | |
547 | struct ddebug_iter *iter = m->private; | |
548 | struct _ddebug *dp; | |
549 | ||
550 | if (verbose) | |
551 | printk(KERN_INFO "%s: called m=%p p=%p *pos=%lld\n", | |
552 | __func__, m, p, (unsigned long long)*pos); | |
553 | ||
554 | if (p == SEQ_START_TOKEN) | |
555 | dp = ddebug_iter_first(iter); | |
556 | else | |
557 | dp = ddebug_iter_next(iter); | |
558 | ++*pos; | |
559 | return dp; | |
560 | } | |
561 | ||
562 | /* | |
563 | * Seq_ops show method. Called several times within a read() | |
564 | * call from userspace, with ddebug_lock held. Formats the | |
565 | * current _ddebug as a single human-readable line, with a | |
566 | * special case for the header line. | |
567 | */ | |
568 | static int ddebug_proc_show(struct seq_file *m, void *p) | |
569 | { | |
570 | struct ddebug_iter *iter = m->private; | |
571 | struct _ddebug *dp = p; | |
572 | char flagsbuf[8]; | |
573 | ||
574 | if (verbose) | |
575 | printk(KERN_INFO "%s: called m=%p p=%p\n", | |
576 | __func__, m, p); | |
577 | ||
578 | if (p == SEQ_START_TOKEN) { | |
579 | seq_puts(m, | |
580 | "# filename:lineno [module]function flags format\n"); | |
581 | return 0; | |
582 | } | |
583 | ||
584 | seq_printf(m, "%s:%u [%s]%s %s \"", | |
585 | dp->filename, dp->lineno, | |
586 | iter->table->mod_name, dp->function, | |
587 | ddebug_describe_flags(dp, flagsbuf, sizeof(flagsbuf))); | |
588 | seq_escape(m, dp->format, "\t\r\n\""); | |
589 | seq_puts(m, "\"\n"); | |
590 | ||
591 | return 0; | |
592 | } | |
593 | ||
594 | /* | |
595 | * Seq_ops stop method. Called at the end of each read() | |
596 | * call from userspace. Drops ddebug_lock. | |
597 | */ | |
598 | static void ddebug_proc_stop(struct seq_file *m, void *p) | |
599 | { | |
600 | if (verbose) | |
601 | printk(KERN_INFO "%s: called m=%p p=%p\n", | |
602 | __func__, m, p); | |
603 | mutex_unlock(&ddebug_lock); | |
604 | } | |
605 | ||
606 | static const struct seq_operations ddebug_proc_seqops = { | |
607 | .start = ddebug_proc_start, | |
608 | .next = ddebug_proc_next, | |
609 | .show = ddebug_proc_show, | |
610 | .stop = ddebug_proc_stop | |
611 | }; | |
612 | ||
613 | /* | |
614 | * File_ops->open method for <debugfs>/dynamic_debug/control. Does the seq_file | |
615 | * setup dance, and also creates an iterator to walk the _ddebugs. | |
616 | * Note that we create a seq_file always, even for O_WRONLY files | |
617 | * where it's not needed, as doing so simplifies the ->release method. | |
618 | */ | |
619 | static int ddebug_proc_open(struct inode *inode, struct file *file) | |
620 | { | |
621 | struct ddebug_iter *iter; | |
622 | int err; | |
623 | ||
624 | if (verbose) | |
625 | printk(KERN_INFO "%s: called\n", __func__); | |
626 | ||
627 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); | |
628 | if (iter == NULL) | |
629 | return -ENOMEM; | |
630 | ||
631 | err = seq_open(file, &ddebug_proc_seqops); | |
632 | if (err) { | |
633 | kfree(iter); | |
634 | return err; | |
635 | } | |
636 | ((struct seq_file *) file->private_data)->private = iter; | |
637 | return 0; | |
638 | } | |
639 | ||
640 | static const struct file_operations ddebug_proc_fops = { | |
641 | .owner = THIS_MODULE, | |
642 | .open = ddebug_proc_open, | |
643 | .read = seq_read, | |
644 | .llseek = seq_lseek, | |
645 | .release = seq_release_private, | |
646 | .write = ddebug_proc_write | |
647 | }; | |
648 | ||
649 | /* | |
650 | * Allocate a new ddebug_table for the given module | |
651 | * and add it to the global list. | |
652 | */ | |
653 | int ddebug_add_module(struct _ddebug *tab, unsigned int n, | |
654 | const char *name) | |
655 | { | |
656 | struct ddebug_table *dt; | |
657 | char *new_name; | |
658 | ||
659 | dt = kzalloc(sizeof(*dt), GFP_KERNEL); | |
660 | if (dt == NULL) | |
661 | return -ENOMEM; | |
662 | new_name = kstrdup(name, GFP_KERNEL); | |
663 | if (new_name == NULL) { | |
664 | kfree(dt); | |
665 | return -ENOMEM; | |
666 | } | |
667 | dt->mod_name = new_name; | |
668 | dt->num_ddebugs = n; | |
669 | dt->num_enabled = 0; | |
670 | dt->ddebugs = tab; | |
671 | ||
672 | mutex_lock(&ddebug_lock); | |
673 | list_add_tail(&dt->link, &ddebug_tables); | |
674 | mutex_unlock(&ddebug_lock); | |
675 | ||
676 | if (verbose) | |
677 | printk(KERN_INFO "%u debug prints in module %s\n", | |
678 | n, dt->mod_name); | |
679 | return 0; | |
680 | } | |
681 | EXPORT_SYMBOL_GPL(ddebug_add_module); | |
682 | ||
683 | static void ddebug_table_free(struct ddebug_table *dt) | |
684 | { | |
685 | list_del_init(&dt->link); | |
686 | kfree(dt->mod_name); | |
687 | kfree(dt); | |
688 | } | |
689 | ||
690 | /* | |
691 | * Called in response to a module being unloaded. Removes | |
692 | * any ddebug_table's which point at the module. | |
693 | */ | |
694 | int ddebug_remove_module(char *mod_name) | |
695 | { | |
696 | struct ddebug_table *dt, *nextdt; | |
697 | int ret = -ENOENT; | |
698 | ||
699 | if (verbose) | |
700 | printk(KERN_INFO "%s: removing module \"%s\"\n", | |
701 | __func__, mod_name); | |
702 | ||
703 | mutex_lock(&ddebug_lock); | |
704 | list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) { | |
705 | if (!strcmp(dt->mod_name, mod_name)) { | |
706 | ddebug_table_free(dt); | |
707 | ret = 0; | |
708 | } | |
709 | } | |
710 | mutex_unlock(&ddebug_lock); | |
711 | return ret; | |
712 | } | |
713 | EXPORT_SYMBOL_GPL(ddebug_remove_module); | |
714 | ||
715 | static void ddebug_remove_all_tables(void) | |
716 | { | |
717 | mutex_lock(&ddebug_lock); | |
718 | while (!list_empty(&ddebug_tables)) { | |
719 | struct ddebug_table *dt = list_entry(ddebug_tables.next, | |
720 | struct ddebug_table, | |
721 | link); | |
722 | ddebug_table_free(dt); | |
723 | } | |
724 | mutex_unlock(&ddebug_lock); | |
725 | } | |
726 | ||
727 | static int __init dynamic_debug_init(void) | |
728 | { | |
729 | struct dentry *dir, *file; | |
730 | struct _ddebug *iter, *iter_start; | |
731 | const char *modname = NULL; | |
732 | int ret = 0; | |
733 | int n = 0; | |
734 | ||
735 | dir = debugfs_create_dir("dynamic_debug", NULL); | |
736 | if (!dir) | |
737 | return -ENOMEM; | |
738 | file = debugfs_create_file("control", 0644, dir, NULL, | |
739 | &ddebug_proc_fops); | |
740 | if (!file) { | |
741 | debugfs_remove(dir); | |
742 | return -ENOMEM; | |
743 | } | |
744 | if (__start___verbose != __stop___verbose) { | |
745 | iter = __start___verbose; | |
746 | modname = iter->modname; | |
747 | iter_start = iter; | |
748 | for (; iter < __stop___verbose; iter++) { | |
749 | if (strcmp(modname, iter->modname)) { | |
750 | ret = ddebug_add_module(iter_start, n, modname); | |
751 | if (ret) | |
752 | goto out_free; | |
753 | n = 0; | |
754 | modname = iter->modname; | |
755 | iter_start = iter; | |
756 | } | |
757 | n++; | |
758 | } | |
759 | ret = ddebug_add_module(iter_start, n, modname); | |
760 | } | |
761 | out_free: | |
762 | if (ret) { | |
763 | ddebug_remove_all_tables(); | |
764 | debugfs_remove(dir); | |
765 | debugfs_remove(file); | |
766 | } | |
767 | return 0; | |
768 | } | |
769 | module_init(dynamic_debug_init); |