]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - lib/dynamic_debug.c
dyndbg: use gcc ?: to reduce word count
[mirror_ubuntu-hirsute-kernel.git] / lib / dynamic_debug.c
CommitLineData
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.
578b1e07 11 * Copyright (C) 2013 Du, Changbin <changbin.du@gmail.com>
e9d376f0
JB
12 */
13
4ad275e5
JP
14#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
15
e9d376f0
JB
16#include <linux/kernel.h>
17#include <linux/module.h>
fef15d2f
GKH
18#include <linux/moduleparam.h>
19#include <linux/kallsyms.h>
20#include <linux/types.h>
e9d376f0 21#include <linux/mutex.h>
fef15d2f 22#include <linux/proc_fs.h>
e9d376f0 23#include <linux/seq_file.h>
fef15d2f
GKH
24#include <linux/list.h>
25#include <linux/sysctl.h>
e9d376f0 26#include <linux/ctype.h>
fef15d2f 27#include <linux/string.h>
578b1e07 28#include <linux/parser.h>
d338b137 29#include <linux/string_helpers.h>
fef15d2f 30#include <linux/uaccess.h>
e9d376f0
JB
31#include <linux/dynamic_debug.h>
32#include <linux/debugfs.h>
5a0e3ad6 33#include <linux/slab.h>
fef15d2f 34#include <linux/jump_label.h>
8ba6ebf5 35#include <linux/hardirq.h>
e8d9792a 36#include <linux/sched.h>
fef15d2f 37#include <linux/device.h>
ffa10cb4 38#include <linux/netdevice.h>
e9d376f0 39
923abb9d
GP
40#include <rdma/ib_verbs.h>
41
e5ebffe1
JC
42extern struct _ddebug __start___dyndbg[];
43extern struct _ddebug __stop___dyndbg[];
e9d376f0 44
e9d376f0
JB
45struct ddebug_table {
46 struct list_head link;
3e406b1d 47 const char *mod_name;
e9d376f0 48 unsigned int num_ddebugs;
e9d376f0
JB
49 struct _ddebug *ddebugs;
50};
51
52struct ddebug_query {
53 const char *filename;
54 const char *module;
55 const char *function;
56 const char *format;
57 unsigned int first_lineno, last_lineno;
58};
59
60struct ddebug_iter {
61 struct ddebug_table *table;
62 unsigned int idx;
63};
64
65static DEFINE_MUTEX(ddebug_lock);
66static LIST_HEAD(ddebug_tables);
f657fd21 67static int verbose;
74df138d 68module_param(verbose, int, 0644);
e9d376f0 69
2b678319
JC
70/* Return the path relative to source root */
71static inline const char *trim_prefix(const char *path)
72{
73 int skip = strlen(__FILE__) - strlen("lib/dynamic_debug.c");
74
75 if (strncmp(path, __FILE__, skip))
76 skip = 0; /* prefix mismatch, don't skip */
77
78 return path + skip;
79}
80
8ba6ebf5
BVA
81static struct { unsigned flag:8; char opt_char; } opt_array[] = {
82 { _DPRINTK_FLAGS_PRINT, 'p' },
83 { _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
84 { _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
85 { _DPRINTK_FLAGS_INCL_LINENO, 'l' },
86 { _DPRINTK_FLAGS_INCL_TID, 't' },
5ca7d2a6 87 { _DPRINTK_FLAGS_NONE, '_' },
8ba6ebf5
BVA
88};
89
f678ce8c
JC
90struct flagsbuf { char buf[ARRAY_SIZE(opt_array)+1]; };
91
e9d376f0 92/* format a string into buf[] which describes the _ddebug's flags */
f678ce8c 93static char *ddebug_describe_flags(unsigned int flags, struct flagsbuf *fb)
e9d376f0 94{
f678ce8c 95 char *p = fb->buf;
8ba6ebf5 96 int i;
e9d376f0 97
8ba6ebf5 98 for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
f678ce8c 99 if (flags & opt_array[i].flag)
8ba6ebf5 100 *p++ = opt_array[i].opt_char;
f678ce8c 101 if (p == fb->buf)
5ca7d2a6 102 *p++ = '_';
e9d376f0
JB
103 *p = '\0';
104
f678ce8c 105 return fb->buf;
e9d376f0
JB
106}
107
481c0e33 108#define vnpr_info(lvl, fmt, ...) \
b8ccd5de 109do { \
481c0e33 110 if (verbose >= lvl) \
f657fd21 111 pr_info(fmt, ##__VA_ARGS__); \
574b3725
JC
112} while (0)
113
481c0e33
JC
114#define vpr_info(fmt, ...) vnpr_info(1, fmt, ##__VA_ARGS__)
115#define v2pr_info(fmt, ...) vnpr_info(2, fmt, ##__VA_ARGS__)
116
f657fd21
JP
117static void vpr_info_dq(const struct ddebug_query *query, const char *msg)
118{
119 /* trim any trailing newlines */
120 int fmtlen = 0;
121
122 if (query->format) {
123 fmtlen = strlen(query->format);
124 while (fmtlen && query->format[fmtlen - 1] == '\n')
125 fmtlen--;
126 }
127
128 vpr_info("%s: func=\"%s\" file=\"%s\" module=\"%s\" format=\"%.*s\" lineno=%u-%u\n",
129 msg,
f62fc08f
JC
130 query->function ?: "",
131 query->filename ?: "",
132 query->module ?: "",
133 fmtlen, query->format ?: "",
f657fd21
JP
134 query->first_lineno, query->last_lineno);
135}
136
e9d376f0 137/*
85f7f6c0
JC
138 * Search the tables for _ddebug's which match the given `query' and
139 * apply the `flags' and `mask' to them. Returns number of matching
140 * callsites, normally the same as number of changes. If verbose,
141 * logs the changes. Takes ddebug_lock.
e9d376f0 142 */
85f7f6c0
JC
143static int ddebug_change(const struct ddebug_query *query,
144 unsigned int flags, unsigned int mask)
e9d376f0
JB
145{
146 int i;
147 struct ddebug_table *dt;
148 unsigned int newflags;
149 unsigned int nfound = 0;
f678ce8c 150 struct flagsbuf fbuf;
e9d376f0
JB
151
152 /* search for matching ddebugs */
153 mutex_lock(&ddebug_lock);
154 list_for_each_entry(dt, &ddebug_tables, link) {
155
156 /* match against the module name */
578b1e07
CD
157 if (query->module &&
158 !match_wildcard(query->module, dt->mod_name))
e9d376f0
JB
159 continue;
160
f657fd21 161 for (i = 0; i < dt->num_ddebugs; i++) {
e9d376f0
JB
162 struct _ddebug *dp = &dt->ddebugs[i];
163
164 /* match against the source filename */
d6a238d2 165 if (query->filename &&
578b1e07
CD
166 !match_wildcard(query->filename, dp->filename) &&
167 !match_wildcard(query->filename,
168 kbasename(dp->filename)) &&
169 !match_wildcard(query->filename,
170 trim_prefix(dp->filename)))
e9d376f0
JB
171 continue;
172
173 /* match against the function */
d6a238d2 174 if (query->function &&
578b1e07 175 !match_wildcard(query->function, dp->function))
e9d376f0
JB
176 continue;
177
178 /* match against the format */
d6a238d2
JC
179 if (query->format &&
180 !strstr(dp->format, query->format))
e9d376f0
JB
181 continue;
182
183 /* match against the line number range */
184 if (query->first_lineno &&
185 dp->lineno < query->first_lineno)
186 continue;
187 if (query->last_lineno &&
188 dp->lineno > query->last_lineno)
189 continue;
190
191 nfound++;
192
193 newflags = (dp->flags & mask) | flags;
194 if (newflags == dp->flags)
195 continue;
e9666d10 196#ifdef CONFIG_JUMP_LABEL
9049fc74
JB
197 if (dp->flags & _DPRINTK_FLAGS_PRINT) {
198 if (!(flags & _DPRINTK_FLAGS_PRINT))
199 static_branch_disable(&dp->key.dd_key_true);
200 } else if (flags & _DPRINTK_FLAGS_PRINT)
201 static_branch_enable(&dp->key.dd_key_true);
202#endif
e9d376f0 203 dp->flags = newflags;
481c0e33 204 v2pr_info("changed %s:%d [%s]%s =%s\n",
f657fd21
JP
205 trim_prefix(dp->filename), dp->lineno,
206 dt->mod_name, dp->function,
f678ce8c 207 ddebug_describe_flags(dp->flags, &fbuf));
e9d376f0
JB
208 }
209 }
210 mutex_unlock(&ddebug_lock);
211
212 if (!nfound && verbose)
4ad275e5 213 pr_info("no matches for query\n");
85f7f6c0
JC
214
215 return nfound;
e9d376f0
JB
216}
217
e9d376f0
JB
218/*
219 * Split the buffer `buf' into space-separated words.
9898abb3
GB
220 * Handles simple " and ' quoting, i.e. without nested,
221 * embedded or escaped \". Return the number of words
222 * or <0 on error.
e9d376f0
JB
223 */
224static int ddebug_tokenize(char *buf, char *words[], int maxwords)
225{
226 int nwords = 0;
227
9898abb3
GB
228 while (*buf) {
229 char *end;
230
231 /* Skip leading whitespace */
e7d2860b 232 buf = skip_spaces(buf);
9898abb3
GB
233 if (!*buf)
234 break; /* oh, it was trailing whitespace */
8bd6026e
JC
235 if (*buf == '#')
236 break; /* token starts comment, skip rest of line */
9898abb3 237
07100be7 238 /* find `end' of word, whitespace separated or quoted */
9898abb3
GB
239 if (*buf == '"' || *buf == '\'') {
240 int quote = *buf++;
f657fd21 241 for (end = buf; *end && *end != quote; end++)
9898abb3 242 ;
18c216c5
JC
243 if (!*end) {
244 pr_err("unclosed quote: %s\n", buf);
9898abb3 245 return -EINVAL; /* unclosed quote */
18c216c5 246 }
9898abb3 247 } else {
f657fd21 248 for (end = buf; *end && !isspace(*end); end++)
9898abb3
GB
249 ;
250 BUG_ON(end == buf);
251 }
9898abb3 252
07100be7 253 /* `buf' is start of word, `end' is one past its end */
18c216c5
JC
254 if (nwords == maxwords) {
255 pr_err("too many words, legal max <=%d\n", maxwords);
9898abb3 256 return -EINVAL; /* ran out of words[] before bytes */
18c216c5 257 }
9898abb3
GB
258 if (*end)
259 *end++ = '\0'; /* terminate the word */
260 words[nwords++] = buf;
261 buf = end;
262 }
e9d376f0
JB
263
264 if (verbose) {
265 int i;
4ad275e5 266 pr_info("split into words:");
f657fd21 267 for (i = 0; i < nwords; i++)
4ad275e5
JP
268 pr_cont(" \"%s\"", words[i]);
269 pr_cont("\n");
e9d376f0
JB
270 }
271
272 return nwords;
273}
274
275/*
276 * Parse a single line number. Note that the empty string ""
277 * is treated as a special case and converted to zero, which
278 * is later treated as a "don't care" value.
279 */
280static inline int parse_lineno(const char *str, unsigned int *val)
281{
e9d376f0
JB
282 BUG_ON(str == NULL);
283 if (*str == '\0') {
284 *val = 0;
285 return 0;
286 }
4592599a 287 if (kstrtouint(str, 10, val) < 0) {
18c216c5
JC
288 pr_err("bad line-number: %s\n", str);
289 return -EINVAL;
290 }
291 return 0;
e9d376f0
JB
292}
293
820874c7
JC
294static int check_set(const char **dest, char *src, char *name)
295{
296 int rc = 0;
297
298 if (*dest) {
299 rc = -EINVAL;
f657fd21
JP
300 pr_err("match-spec:%s val:%s overridden by %s\n",
301 name, *dest, src);
820874c7
JC
302 }
303 *dest = src;
304 return rc;
305}
306
e9d376f0
JB
307/*
308 * Parse words[] as a ddebug query specification, which is a series
309 * of (keyword, value) pairs chosen from these possibilities:
310 *
311 * func <function-name>
312 * file <full-pathname>
313 * file <base-filename>
314 * module <module-name>
315 * format <escaped-string-to-find-in-format>
316 * line <lineno>
317 * line <first-lineno>-<last-lineno> // where either may be empty
820874c7
JC
318 *
319 * Only 1 of each type is allowed.
320 * Returns 0 on success, <0 on error.
e9d376f0
JB
321 */
322static int ddebug_parse_query(char *words[], int nwords,
8e59b5cf 323 struct ddebug_query *query, const char *modname)
e9d376f0
JB
324{
325 unsigned int i;
bd8c154a 326 int rc = 0;
e9d376f0
JB
327
328 /* check we have an even number of words */
18c216c5
JC
329 if (nwords % 2 != 0) {
330 pr_err("expecting pairs of match-spec <value>\n");
e9d376f0 331 return -EINVAL;
18c216c5 332 }
e9d376f0 333
8e59b5cf
JC
334 if (modname)
335 /* support $modname.dyndbg=<multiple queries> */
336 query->module = modname;
337
f657fd21
JP
338 for (i = 0; i < nwords; i += 2) {
339 if (!strcmp(words[i], "func")) {
820874c7 340 rc = check_set(&query->function, words[i+1], "func");
f657fd21 341 } else if (!strcmp(words[i], "file")) {
820874c7 342 rc = check_set(&query->filename, words[i+1], "file");
f657fd21 343 } else if (!strcmp(words[i], "module")) {
820874c7 344 rc = check_set(&query->module, words[i+1], "module");
f657fd21 345 } else if (!strcmp(words[i], "format")) {
d338b137
AS
346 string_unescape_inplace(words[i+1], UNESCAPE_SPACE |
347 UNESCAPE_OCTAL |
348 UNESCAPE_SPECIAL);
349 rc = check_set(&query->format, words[i+1], "format");
f657fd21 350 } else if (!strcmp(words[i], "line")) {
e9d376f0
JB
351 char *first = words[i+1];
352 char *last = strchr(first, '-');
820874c7 353 if (query->first_lineno || query->last_lineno) {
18c216c5 354 pr_err("match-spec: line used 2x\n");
820874c7
JC
355 return -EINVAL;
356 }
e9d376f0
JB
357 if (last)
358 *last++ = '\0';
d9e133e6 359 if (parse_lineno(first, &query->first_lineno) < 0)
e9d376f0 360 return -EINVAL;
820874c7 361 if (last) {
e9d376f0 362 /* range <first>-<last> */
3ace678f
AR
363 if (parse_lineno(last, &query->last_lineno) < 0)
364 return -EINVAL;
365
1f3c790b
RD
366 /* special case for last lineno not specified */
367 if (query->last_lineno == 0)
368 query->last_lineno = UINT_MAX;
369
3ace678f 370 if (query->last_lineno < query->first_lineno) {
18c216c5
JC
371 pr_err("last-line:%d < 1st-line:%d\n",
372 query->last_lineno,
373 query->first_lineno);
e9d376f0 374 return -EINVAL;
820874c7 375 }
e9d376f0
JB
376 } else {
377 query->last_lineno = query->first_lineno;
378 }
379 } else {
ae27f86a 380 pr_err("unknown keyword \"%s\"\n", words[i]);
e9d376f0
JB
381 return -EINVAL;
382 }
820874c7
JC
383 if (rc)
384 return rc;
e9d376f0 385 }
574b3725 386 vpr_info_dq(query, "parsed");
e9d376f0
JB
387 return 0;
388}
389
390/*
391 * Parse `str' as a flags specification, format [-+=][p]+.
392 * Sets up *maskp and *flagsp to be used when changing the
393 * flags fields of matched _ddebug's. Returns 0 on success
394 * or <0 on error.
395 */
396static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
397 unsigned int *maskp)
398{
399 unsigned flags = 0;
8ba6ebf5 400 int op = '=', i;
e9d376f0
JB
401
402 switch (*str) {
403 case '+':
404 case '-':
405 case '=':
406 op = *str++;
407 break;
408 default:
18c216c5 409 pr_err("bad flag-op %c, at start of %s\n", *str, str);
e9d376f0
JB
410 return -EINVAL;
411 }
b8ccd5de 412 vpr_info("op='%c'\n", op);
e9d376f0 413
f657fd21 414 for (; *str ; ++str) {
8ba6ebf5
BVA
415 for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
416 if (*str == opt_array[i].opt_char) {
417 flags |= opt_array[i].flag;
418 break;
419 }
e9d376f0 420 }
18c216c5 421 if (i < 0) {
0b8f96be 422 pr_err("unknown flag '%c'\n", *str);
8ba6ebf5 423 return -EINVAL;
18c216c5 424 }
e9d376f0 425 }
b8ccd5de 426 vpr_info("flags=0x%x\n", flags);
e9d376f0
JB
427
428 /* calculate final *flagsp, *maskp according to mask and op */
429 switch (op) {
430 case '=':
431 *maskp = 0;
432 *flagsp = flags;
433 break;
434 case '+':
435 *maskp = ~0U;
436 *flagsp = flags;
437 break;
438 case '-':
439 *maskp = ~flags;
440 *flagsp = 0;
441 break;
442 }
b8ccd5de 443 vpr_info("*flagsp=0x%x *maskp=0x%x\n", *flagsp, *maskp);
e9d376f0
JB
444 return 0;
445}
446
8e59b5cf 447static int ddebug_exec_query(char *query_string, const char *modname)
fd89cfb8
TR
448{
449 unsigned int flags = 0, mask = 0;
9c9d0acb 450 struct ddebug_query query = {};
fd89cfb8 451#define MAXWORDS 9
85f7f6c0 452 int nwords, nfound;
fd89cfb8
TR
453 char *words[MAXWORDS];
454
455 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
18c216c5
JC
456 if (nwords <= 0) {
457 pr_err("tokenize failed\n");
fd89cfb8 458 return -EINVAL;
18c216c5
JC
459 }
460 /* check flags 1st (last arg) so query is pairs of spec,val */
461 if (ddebug_parse_flags(words[nwords-1], &flags, &mask)) {
462 pr_err("flags parse failed\n");
fd89cfb8 463 return -EINVAL;
18c216c5
JC
464 }
465 if (ddebug_parse_query(words, nwords-1, &query, modname)) {
466 pr_err("query parse failed\n");
fd89cfb8 467 return -EINVAL;
18c216c5 468 }
fd89cfb8 469 /* actually go and implement the change */
85f7f6c0 470 nfound = ddebug_change(&query, flags, mask);
f657fd21 471 vpr_info_dq(&query, nfound ? "applied" : "no-match");
85f7f6c0
JC
472
473 return nfound;
474}
475
476/* handle multiple queries in query string, continue on error, return
477 last error or number of matching callsites. Module name is either
478 in param (for boot arg) or perhaps in query string.
479*/
8e59b5cf 480static int ddebug_exec_queries(char *query, const char *modname)
85f7f6c0
JC
481{
482 char *split;
483 int i, errs = 0, exitcode = 0, rc, nfound = 0;
484
485 for (i = 0; query; query = split) {
486 split = strpbrk(query, ";\n");
487 if (split)
488 *split++ = '\0';
489
490 query = skip_spaces(query);
491 if (!query || !*query || *query == '#')
492 continue;
493
b8ccd5de 494 vpr_info("query %d: \"%s\"\n", i, query);
85f7f6c0 495
8e59b5cf 496 rc = ddebug_exec_query(query, modname);
85f7f6c0
JC
497 if (rc < 0) {
498 errs++;
499 exitcode = rc;
f657fd21 500 } else {
85f7f6c0 501 nfound += rc;
f657fd21 502 }
85f7f6c0
JC
503 i++;
504 }
b8ccd5de 505 vpr_info("processed %d queries, with %d matches, %d errs\n",
85f7f6c0
JC
506 i, nfound, errs);
507
508 if (exitcode)
509 return exitcode;
510 return nfound;
fd89cfb8
TR
511}
512
431625da
JB
513#define PREFIX_SIZE 64
514
515static int remaining(int wrote)
516{
517 if (PREFIX_SIZE - wrote > 0)
518 return PREFIX_SIZE - wrote;
519 return 0;
520}
521
522static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf)
8ba6ebf5 523{
431625da
JB
524 int pos_after_tid;
525 int pos = 0;
8ba6ebf5 526
798efc60
JP
527 *buf = '\0';
528
431625da 529 if (desc->flags & _DPRINTK_FLAGS_INCL_TID) {
8ba6ebf5 530 if (in_interrupt())
798efc60 531 pos += snprintf(buf + pos, remaining(pos), "<intr> ");
8ba6ebf5 532 else
431625da 533 pos += snprintf(buf + pos, remaining(pos), "[%d] ",
798efc60 534 task_pid_vnr(current));
8ba6ebf5 535 }
431625da
JB
536 pos_after_tid = pos;
537 if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME)
538 pos += snprintf(buf + pos, remaining(pos), "%s:",
798efc60 539 desc->modname);
431625da
JB
540 if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
541 pos += snprintf(buf + pos, remaining(pos), "%s:",
798efc60 542 desc->function);
431625da 543 if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO)
07100be7 544 pos += snprintf(buf + pos, remaining(pos), "%d:",
798efc60 545 desc->lineno);
431625da
JB
546 if (pos - pos_after_tid)
547 pos += snprintf(buf + pos, remaining(pos), " ");
548 if (pos >= PREFIX_SIZE)
549 buf[PREFIX_SIZE - 1] = '\0';
6c2140ee 550
431625da 551 return buf;
6c2140ee
JP
552}
553
906d2015 554void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
8ba6ebf5
BVA
555{
556 va_list args;
431625da
JB
557 struct va_format vaf;
558 char buf[PREFIX_SIZE];
8ba6ebf5
BVA
559
560 BUG_ON(!descriptor);
561 BUG_ON(!fmt);
562
563 va_start(args, fmt);
798efc60 564
431625da
JB
565 vaf.fmt = fmt;
566 vaf.va = &args;
798efc60 567
906d2015 568 printk(KERN_DEBUG "%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf);
798efc60 569
8ba6ebf5 570 va_end(args);
8ba6ebf5
BVA
571}
572EXPORT_SYMBOL(__dynamic_pr_debug);
573
906d2015 574void __dynamic_dev_dbg(struct _ddebug *descriptor,
cbc46635
JP
575 const struct device *dev, const char *fmt, ...)
576{
577 struct va_format vaf;
578 va_list args;
cbc46635
JP
579
580 BUG_ON(!descriptor);
581 BUG_ON(!fmt);
582
583 va_start(args, fmt);
798efc60 584
cbc46635
JP
585 vaf.fmt = fmt;
586 vaf.va = &args;
798efc60
JP
587
588 if (!dev) {
906d2015 589 printk(KERN_DEBUG "(NULL device *): %pV", &vaf);
798efc60
JP
590 } else {
591 char buf[PREFIX_SIZE];
798efc60 592
a39d4a85 593 dev_printk_emit(LOGLEVEL_DEBUG, dev, "%s%s %s: %pV",
906d2015
JP
594 dynamic_emit_prefix(descriptor, buf),
595 dev_driver_string(dev), dev_name(dev),
596 &vaf);
798efc60
JP
597 }
598
cbc46635 599 va_end(args);
cbc46635
JP
600}
601EXPORT_SYMBOL(__dynamic_dev_dbg);
602
0feefd97
JB
603#ifdef CONFIG_NET
604
906d2015
JP
605void __dynamic_netdev_dbg(struct _ddebug *descriptor,
606 const struct net_device *dev, const char *fmt, ...)
ffa10cb4
JB
607{
608 struct va_format vaf;
609 va_list args;
ffa10cb4
JB
610
611 BUG_ON(!descriptor);
612 BUG_ON(!fmt);
613
614 va_start(args, fmt);
b004ff49 615
ffa10cb4
JB
616 vaf.fmt = fmt;
617 vaf.va = &args;
b004ff49
JP
618
619 if (dev && dev->dev.parent) {
620 char buf[PREFIX_SIZE];
666f355f 621
a39d4a85 622 dev_printk_emit(LOGLEVEL_DEBUG, dev->dev.parent,
906d2015
JP
623 "%s%s %s %s%s: %pV",
624 dynamic_emit_prefix(descriptor, buf),
625 dev_driver_string(dev->dev.parent),
626 dev_name(dev->dev.parent),
627 netdev_name(dev), netdev_reg_state(dev),
628 &vaf);
b004ff49 629 } else if (dev) {
906d2015
JP
630 printk(KERN_DEBUG "%s%s: %pV", netdev_name(dev),
631 netdev_reg_state(dev), &vaf);
b004ff49 632 } else {
906d2015 633 printk(KERN_DEBUG "(NULL net_device): %pV", &vaf);
b004ff49
JP
634 }
635
ffa10cb4 636 va_end(args);
ffa10cb4
JB
637}
638EXPORT_SYMBOL(__dynamic_netdev_dbg);
639
0feefd97
JB
640#endif
641
923abb9d
GP
642#if IS_ENABLED(CONFIG_INFINIBAND)
643
644void __dynamic_ibdev_dbg(struct _ddebug *descriptor,
645 const struct ib_device *ibdev, const char *fmt, ...)
646{
647 struct va_format vaf;
648 va_list args;
649
650 va_start(args, fmt);
651
652 vaf.fmt = fmt;
653 vaf.va = &args;
654
655 if (ibdev && ibdev->dev.parent) {
656 char buf[PREFIX_SIZE];
657
658 dev_printk_emit(LOGLEVEL_DEBUG, ibdev->dev.parent,
659 "%s%s %s %s: %pV",
660 dynamic_emit_prefix(descriptor, buf),
661 dev_driver_string(ibdev->dev.parent),
662 dev_name(ibdev->dev.parent),
663 dev_name(&ibdev->dev),
664 &vaf);
665 } else if (ibdev) {
666 printk(KERN_DEBUG "%s: %pV", dev_name(&ibdev->dev), &vaf);
667 } else {
668 printk(KERN_DEBUG "(NULL ib_device): %pV", &vaf);
669 }
670
671 va_end(args);
672}
673EXPORT_SYMBOL(__dynamic_ibdev_dbg);
674
675#endif
676
bc757f6f
JC
677#define DDEBUG_STRING_SIZE 1024
678static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE];
679
a648ec05
TR
680static __init int ddebug_setup_query(char *str)
681{
bc757f6f 682 if (strlen(str) >= DDEBUG_STRING_SIZE) {
4ad275e5 683 pr_warn("ddebug boot param string too large\n");
a648ec05
TR
684 return 0;
685 }
bc757f6f 686 strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE);
a648ec05
TR
687 return 1;
688}
689
690__setup("ddebug_query=", ddebug_setup_query);
691
e9d376f0 692/*
231821d4 693 * File_ops->write method for <debugfs>/dynamic_debug/control. Gathers the
e9d376f0
JB
694 * command text from userspace, parses and executes it.
695 */
7281491c 696#define USER_BUF_PAGE 4096
e9d376f0
JB
697static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
698 size_t len, loff_t *offp)
699{
7281491c 700 char *tmpbuf;
fd89cfb8 701 int ret;
e9d376f0
JB
702
703 if (len == 0)
704 return 0;
7281491c
JC
705 if (len > USER_BUF_PAGE - 1) {
706 pr_warn("expected <%d bytes into control\n", USER_BUF_PAGE);
e9d376f0 707 return -E2BIG;
7281491c 708 }
16e5c1fc
AV
709 tmpbuf = memdup_user_nul(ubuf, len);
710 if (IS_ERR(tmpbuf))
711 return PTR_ERR(tmpbuf);
b8ccd5de 712 vpr_info("read %d bytes from userspace\n", (int)len);
e9d376f0 713
8e59b5cf 714 ret = ddebug_exec_queries(tmpbuf, NULL);
7281491c 715 kfree(tmpbuf);
85f7f6c0 716 if (ret < 0)
fd89cfb8 717 return ret;
e9d376f0
JB
718
719 *offp += len;
720 return len;
721}
722
723/*
724 * Set the iterator to point to the first _ddebug object
725 * and return a pointer to that first object. Returns
726 * NULL if there are no _ddebugs at all.
727 */
728static struct _ddebug *ddebug_iter_first(struct ddebug_iter *iter)
729{
730 if (list_empty(&ddebug_tables)) {
731 iter->table = NULL;
732 iter->idx = 0;
733 return NULL;
734 }
735 iter->table = list_entry(ddebug_tables.next,
736 struct ddebug_table, link);
737 iter->idx = 0;
738 return &iter->table->ddebugs[iter->idx];
739}
740
741/*
742 * Advance the iterator to point to the next _ddebug
743 * object from the one the iterator currently points at,
744 * and returns a pointer to the new _ddebug. Returns
745 * NULL if the iterator has seen all the _ddebugs.
746 */
747static struct _ddebug *ddebug_iter_next(struct ddebug_iter *iter)
748{
749 if (iter->table == NULL)
750 return NULL;
751 if (++iter->idx == iter->table->num_ddebugs) {
752 /* iterate to next table */
753 iter->idx = 0;
754 if (list_is_last(&iter->table->link, &ddebug_tables)) {
755 iter->table = NULL;
756 return NULL;
757 }
758 iter->table = list_entry(iter->table->link.next,
759 struct ddebug_table, link);
760 }
761 return &iter->table->ddebugs[iter->idx];
762}
763
764/*
765 * Seq_ops start method. Called at the start of every
766 * read() call from userspace. Takes the ddebug_lock and
767 * seeks the seq_file's iterator to the given position.
768 */
769static void *ddebug_proc_start(struct seq_file *m, loff_t *pos)
770{
771 struct ddebug_iter *iter = m->private;
772 struct _ddebug *dp;
773 int n = *pos;
774
e9d376f0
JB
775 mutex_lock(&ddebug_lock);
776
777 if (!n)
778 return SEQ_START_TOKEN;
779 if (n < 0)
780 return NULL;
781 dp = ddebug_iter_first(iter);
782 while (dp != NULL && --n > 0)
783 dp = ddebug_iter_next(iter);
784 return dp;
785}
786
787/*
788 * Seq_ops next method. Called several times within a read()
789 * call from userspace, with ddebug_lock held. Walks to the
790 * next _ddebug object with a special case for the header line.
791 */
792static void *ddebug_proc_next(struct seq_file *m, void *p, loff_t *pos)
793{
794 struct ddebug_iter *iter = m->private;
795 struct _ddebug *dp;
796
e9d376f0
JB
797 if (p == SEQ_START_TOKEN)
798 dp = ddebug_iter_first(iter);
799 else
800 dp = ddebug_iter_next(iter);
801 ++*pos;
802 return dp;
803}
804
805/*
806 * Seq_ops show method. Called several times within a read()
807 * call from userspace, with ddebug_lock held. Formats the
808 * current _ddebug as a single human-readable line, with a
809 * special case for the header line.
810 */
811static int ddebug_proc_show(struct seq_file *m, void *p)
812{
813 struct ddebug_iter *iter = m->private;
814 struct _ddebug *dp = p;
f678ce8c 815 struct flagsbuf flags;
e9d376f0 816
e9d376f0
JB
817 if (p == SEQ_START_TOKEN) {
818 seq_puts(m,
f657fd21 819 "# filename:lineno [module]function flags format\n");
e9d376f0
JB
820 return 0;
821 }
822
5ca7d2a6 823 seq_printf(m, "%s:%u [%s]%s =%s \"",
f657fd21
JP
824 trim_prefix(dp->filename), dp->lineno,
825 iter->table->mod_name, dp->function,
f678ce8c 826 ddebug_describe_flags(dp->flags, &flags));
e9d376f0
JB
827 seq_escape(m, dp->format, "\t\r\n\"");
828 seq_puts(m, "\"\n");
829
830 return 0;
831}
832
833/*
834 * Seq_ops stop method. Called at the end of each read()
835 * call from userspace. Drops ddebug_lock.
836 */
837static void ddebug_proc_stop(struct seq_file *m, void *p)
838{
e9d376f0
JB
839 mutex_unlock(&ddebug_lock);
840}
841
842static const struct seq_operations ddebug_proc_seqops = {
843 .start = ddebug_proc_start,
844 .next = ddebug_proc_next,
845 .show = ddebug_proc_show,
846 .stop = ddebug_proc_stop
847};
848
e9d376f0
JB
849static int ddebug_proc_open(struct inode *inode, struct file *file)
850{
b8ccd5de 851 vpr_info("called\n");
4bad78c5
RJ
852 return seq_open_private(file, &ddebug_proc_seqops,
853 sizeof(struct ddebug_iter));
e9d376f0
JB
854}
855
856static const struct file_operations ddebug_proc_fops = {
857 .owner = THIS_MODULE,
858 .open = ddebug_proc_open,
859 .read = seq_read,
860 .llseek = seq_lseek,
861 .release = seq_release_private,
862 .write = ddebug_proc_write
863};
864
239a5791
GKH
865static const struct proc_ops proc_fops = {
866 .proc_open = ddebug_proc_open,
867 .proc_read = seq_read,
868 .proc_lseek = seq_lseek,
869 .proc_release = seq_release_private,
870 .proc_write = ddebug_proc_write
871};
872
e9d376f0
JB
873/*
874 * Allocate a new ddebug_table for the given module
875 * and add it to the global list.
876 */
877int ddebug_add_module(struct _ddebug *tab, unsigned int n,
878 const char *name)
879{
880 struct ddebug_table *dt;
e9d376f0
JB
881
882 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
513770f5
RV
883 if (dt == NULL) {
884 pr_err("error adding module: %s\n", name);
e9d376f0 885 return -ENOMEM;
513770f5 886 }
cdf6d006
RV
887 /*
888 * For built-in modules, name lives in .rodata and is
889 * immortal. For loaded modules, name points at the name[]
890 * member of struct module, which lives at least as long as
891 * this struct ddebug_table.
892 */
893 dt->mod_name = name;
e9d376f0 894 dt->num_ddebugs = n;
e9d376f0
JB
895 dt->ddebugs = tab;
896
897 mutex_lock(&ddebug_lock);
47e9f5a8 898 list_add(&dt->link, &ddebug_tables);
e9d376f0
JB
899 mutex_unlock(&ddebug_lock);
900
481c0e33 901 v2pr_info("%u debug prints in module %s\n", n, dt->mod_name);
e9d376f0
JB
902 return 0;
903}
e9d376f0 904
6ab676e9
JC
905/* helper for ddebug_dyndbg_(boot|module)_param_cb */
906static int ddebug_dyndbg_param_cb(char *param, char *val,
907 const char *modname, int on_err)
b48420c1 908{
b48420c1
JC
909 char *sep;
910
911 sep = strchr(param, '.');
912 if (sep) {
6ab676e9 913 /* needed only for ddebug_dyndbg_boot_param_cb */
b48420c1
JC
914 *sep = '\0';
915 modname = param;
916 param = sep + 1;
917 }
918 if (strcmp(param, "dyndbg"))
6ab676e9 919 return on_err; /* determined by caller */
b48420c1 920
8e59b5cf
JC
921 ddebug_exec_queries((val ? val : "+p"), modname);
922
b48420c1
JC
923 return 0; /* query failure shouldnt stop module load */
924}
925
6ab676e9
JC
926/* handle both dyndbg and $module.dyndbg params at boot */
927static int ddebug_dyndbg_boot_param_cb(char *param, char *val,
ecc86170 928 const char *unused, void *arg)
b48420c1 929{
6ab676e9
JC
930 vpr_info("%s=\"%s\"\n", param, val);
931 return ddebug_dyndbg_param_cb(param, val, NULL, 0);
932}
b48420c1 933
6ab676e9
JC
934/*
935 * modprobe foo finds foo.params in boot-args, strips "foo.", and
936 * passes them to load_module(). This callback gets unknown params,
937 * processes dyndbg params, rejects others.
938 */
939int ddebug_dyndbg_module_param_cb(char *param, char *val, const char *module)
940{
941 vpr_info("module: %s %s=\"%s\"\n", module, param, val);
942 return ddebug_dyndbg_param_cb(param, val, module, -ENOENT);
b48420c1
JC
943}
944
e9d376f0
JB
945static void ddebug_table_free(struct ddebug_table *dt)
946{
947 list_del_init(&dt->link);
e9d376f0
JB
948 kfree(dt);
949}
950
951/*
952 * Called in response to a module being unloaded. Removes
953 * any ddebug_table's which point at the module.
954 */
ff49d74a 955int ddebug_remove_module(const char *mod_name)
e9d376f0
JB
956{
957 struct ddebug_table *dt, *nextdt;
958 int ret = -ENOENT;
959
481c0e33 960 v2pr_info("removing module \"%s\"\n", mod_name);
e9d376f0
JB
961
962 mutex_lock(&ddebug_lock);
963 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
4573fe15 964 if (dt->mod_name == mod_name) {
e9d376f0
JB
965 ddebug_table_free(dt);
966 ret = 0;
4573fe15 967 break;
e9d376f0
JB
968 }
969 }
970 mutex_unlock(&ddebug_lock);
971 return ret;
972}
e9d376f0
JB
973
974static void ddebug_remove_all_tables(void)
975{
976 mutex_lock(&ddebug_lock);
977 while (!list_empty(&ddebug_tables)) {
978 struct ddebug_table *dt = list_entry(ddebug_tables.next,
979 struct ddebug_table,
980 link);
981 ddebug_table_free(dt);
982 }
983 mutex_unlock(&ddebug_lock);
984}
985
6a5c083d
TR
986static __initdata int ddebug_init_success;
987
239a5791 988static int __init dynamic_debug_init_control(void)
e9d376f0 989{
239a5791
GKH
990 struct proc_dir_entry *procfs_dir;
991 struct dentry *debugfs_dir;
6a5c083d
TR
992
993 if (!ddebug_init_success)
994 return -ENODEV;
e9d376f0 995
239a5791
GKH
996 /* Create the control file in debugfs if it is enabled */
997 if (debugfs_initialized()) {
998 debugfs_dir = debugfs_create_dir("dynamic_debug", NULL);
999 debugfs_create_file("control", 0644, debugfs_dir, NULL,
1000 &ddebug_proc_fops);
1001 }
1002
1003 /* Also create the control file in procfs */
1004 procfs_dir = proc_mkdir("dynamic_debug", NULL);
1005 if (procfs_dir)
1006 proc_create("control", 0644, procfs_dir, &proc_fops);
9fd714cd 1007
6a5c083d
TR
1008 return 0;
1009}
1010
1011static int __init dynamic_debug_init(void)
1012{
1013 struct _ddebug *iter, *iter_start;
1014 const char *modname = NULL;
b48420c1 1015 char *cmdline;
6a5c083d 1016 int ret = 0;
41076927 1017 int n = 0, entries = 0, modct = 0;
6a5c083d 1018
e5ebffe1 1019 if (&__start___dyndbg == &__stop___dyndbg) {
ceabef7d
OZ
1020 if (IS_ENABLED(CONFIG_DYNAMIC_DEBUG)) {
1021 pr_warn("_ddebug table is empty in a CONFIG_DYNAMIC_DEBUG build\n");
1022 return 1;
1023 }
1024 pr_info("Ignore empty _ddebug table in a CONFIG_DYNAMIC_DEBUG_CORE build\n");
1025 ddebug_init_success = 1;
1026 return 0;
b5b78f83 1027 }
e5ebffe1 1028 iter = __start___dyndbg;
b5b78f83
JC
1029 modname = iter->modname;
1030 iter_start = iter;
e5ebffe1 1031 for (; iter < __stop___dyndbg; iter++) {
41076927 1032 entries++;
b5b78f83 1033 if (strcmp(modname, iter->modname)) {
41076927 1034 modct++;
b5b78f83
JC
1035 ret = ddebug_add_module(iter_start, n, modname);
1036 if (ret)
af442399 1037 goto out_err;
b5b78f83
JC
1038 n = 0;
1039 modname = iter->modname;
1040 iter_start = iter;
e9d376f0 1041 }
b5b78f83 1042 n++;
e9d376f0 1043 }
b5b78f83
JC
1044 ret = ddebug_add_module(iter_start, n, modname);
1045 if (ret)
af442399 1046 goto out_err;
a648ec05 1047
af442399 1048 ddebug_init_success = 1;
81d0c2c6 1049 vpr_info("%d modules, %d entries and %d bytes in ddebug tables, %d bytes in __dyndbg section\n",
f657fd21 1050 modct, entries, (int)(modct * sizeof(struct ddebug_table)),
81d0c2c6 1051 (int)(entries * sizeof(struct _ddebug)));
af442399
JC
1052
1053 /* apply ddebug_query boot param, dont unload tables on err */
a648ec05 1054 if (ddebug_setup_string[0] != '\0') {
f657fd21 1055 pr_warn("ddebug_query param name is deprecated, change it to dyndbg\n");
8e59b5cf 1056 ret = ddebug_exec_queries(ddebug_setup_string, NULL);
85f7f6c0 1057 if (ret < 0)
f657fd21 1058 pr_warn("Invalid ddebug boot param %s\n",
4ad275e5 1059 ddebug_setup_string);
a648ec05 1060 else
85f7f6c0 1061 pr_info("%d changes by ddebug_query\n", ret);
a648ec05 1062 }
b48420c1
JC
1063 /* now that ddebug tables are loaded, process all boot args
1064 * again to find and activate queries given in dyndbg params.
1065 * While this has already been done for known boot params, it
1066 * ignored the unknown ones (dyndbg in particular). Reusing
1067 * parse_args avoids ad-hoc parsing. This will also attempt
1068 * to activate queries for not-yet-loaded modules, which is
1069 * slightly noisy if verbose, but harmless.
1070 */
1071 cmdline = kstrdup(saved_command_line, GFP_KERNEL);
1072 parse_args("dyndbg params", cmdline, NULL,
ecc86170 1073 0, 0, 0, NULL, &ddebug_dyndbg_boot_param_cb);
b48420c1 1074 kfree(cmdline);
af442399 1075 return 0;
a648ec05 1076
af442399
JC
1077out_err:
1078 ddebug_remove_all_tables();
e9d376f0
JB
1079 return 0;
1080}
6a5c083d 1081/* Allow early initialization for boot messages via boot param */
3ec5652a 1082early_initcall(dynamic_debug_init);
b48420c1 1083
6a5c083d 1084/* Debugfs setup must be done later */
239a5791 1085fs_initcall(dynamic_debug_init_control);