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