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