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