blob: c983049e986d2acb9f0bbb69c02ff777db29cdf9 [file] [log] [blame]
Jason Barone9d376f2009-02-05 11:51:38 -05001/*
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 Assche8ba6ebf52011-01-23 17:17:24 +010010 * Copyright (C) 2011 Bart Van Assche. All Rights Reserved.
Du, Changbin578b1e02014-01-23 15:54:14 -080011 * Copyright (C) 2013 Du, Changbin <changbin.du@gmail.com>
Jason Barone9d376f2009-02-05 11:51:38 -050012 */
13
Joe Perches4ad275e2011-08-11 14:36:33 -040014#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
15
Jason Barone9d376f2009-02-05 11:51:38 -050016#include <linux/kernel.h>
17#include <linux/module.h>
Greg Kroah-Hartmanfef15d22012-05-07 16:47:32 -070018#include <linux/moduleparam.h>
19#include <linux/kallsyms.h>
20#include <linux/types.h>
Jason Barone9d376f2009-02-05 11:51:38 -050021#include <linux/mutex.h>
Greg Kroah-Hartmanfef15d22012-05-07 16:47:32 -070022#include <linux/proc_fs.h>
Jason Barone9d376f2009-02-05 11:51:38 -050023#include <linux/seq_file.h>
Greg Kroah-Hartmanfef15d22012-05-07 16:47:32 -070024#include <linux/list.h>
25#include <linux/sysctl.h>
Jason Barone9d376f2009-02-05 11:51:38 -050026#include <linux/ctype.h>
Greg Kroah-Hartmanfef15d22012-05-07 16:47:32 -070027#include <linux/string.h>
Du, Changbin578b1e02014-01-23 15:54:14 -080028#include <linux/parser.h>
Andy Shevchenkod338b132013-04-30 15:27:32 -070029#include <linux/string_helpers.h>
Greg Kroah-Hartmanfef15d22012-05-07 16:47:32 -070030#include <linux/uaccess.h>
Jason Barone9d376f2009-02-05 11:51:38 -050031#include <linux/dynamic_debug.h>
32#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Greg Kroah-Hartmanfef15d22012-05-07 16:47:32 -070034#include <linux/jump_label.h>
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010035#include <linux/hardirq.h>
Greg Kroah-Hartmane8d97922011-02-03 15:59:58 -080036#include <linux/sched.h>
Greg Kroah-Hartmanfef15d22012-05-07 16:47:32 -070037#include <linux/device.h>
Jason Baronffa10cb2011-08-11 14:36:48 -040038#include <linux/netdevice.h>
Jason Barone9d376f2009-02-05 11:51:38 -050039
Gal Pressman923abb92019-05-01 13:48:13 +030040#include <rdma/ib_verbs.h>
41
Jim Cromiee5ebffe2020-07-19 17:10:45 -060042extern struct _ddebug __start___dyndbg[];
43extern struct _ddebug __stop___dyndbg[];
Jason Barone9d376f2009-02-05 11:51:38 -050044
Jason Barone9d376f2009-02-05 11:51:38 -050045struct ddebug_table {
46 struct list_head link;
Rasmus Villemoes3e406b12015-11-06 16:30:15 -080047 const char *mod_name;
Jason Barone9d376f2009-02-05 11:51:38 -050048 unsigned int num_ddebugs;
Jason Barone9d376f2009-02-05 11:51:38 -050049 struct _ddebug *ddebugs;
50};
51
52struct ddebug_query {
53 const char *filename;
54 const char *module;
55 const char *function;
56 const char *format;
57 unsigned int first_lineno, last_lineno;
58};
59
60struct ddebug_iter {
61 struct ddebug_table *table;
62 unsigned int idx;
63};
64
Jim Cromie84da83a2020-07-19 17:10:55 -060065struct flag_settings {
66 unsigned int flags;
67 unsigned int mask;
68};
69
Jason Barone9d376f2009-02-05 11:51:38 -050070static DEFINE_MUTEX(ddebug_lock);
71static LIST_HEAD(ddebug_tables);
Joe Perchesf657fd22012-12-05 16:48:26 -050072static int verbose;
Jim Cromie74df1382011-12-19 17:12:24 -050073module_param(verbose, int, 0644);
Jason Barone9d376f2009-02-05 11:51:38 -050074
Jim Cromie2b678312011-12-19 17:13:12 -050075/* Return the path relative to source root */
76static 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 Assche8ba6ebf52011-01-23 17:17:24 +010086static 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 Cromie5ca7d2a2011-12-19 17:12:44 -050092 { _DPRINTK_FLAGS_NONE, '_' },
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +010093};
94
Jim Cromief678ce82020-07-19 17:10:47 -060095struct flagsbuf { char buf[ARRAY_SIZE(opt_array)+1]; };
96
Jason Barone9d376f2009-02-05 11:51:38 -050097/* format a string into buf[] which describes the _ddebug's flags */
Jim Cromief678ce82020-07-19 17:10:47 -060098static char *ddebug_describe_flags(unsigned int flags, struct flagsbuf *fb)
Jason Barone9d376f2009-02-05 11:51:38 -050099{
Jim Cromief678ce82020-07-19 17:10:47 -0600100 char *p = fb->buf;
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100101 int i;
Jason Barone9d376f2009-02-05 11:51:38 -0500102
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100103 for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
Jim Cromief678ce82020-07-19 17:10:47 -0600104 if (flags & opt_array[i].flag)
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100105 *p++ = opt_array[i].opt_char;
Jim Cromief678ce82020-07-19 17:10:47 -0600106 if (p == fb->buf)
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500107 *p++ = '_';
Jason Barone9d376f2009-02-05 11:51:38 -0500108 *p = '\0';
109
Jim Cromief678ce82020-07-19 17:10:47 -0600110 return fb->buf;
Jason Barone9d376f2009-02-05 11:51:38 -0500111}
112
Jim Cromie481c0e32020-07-19 17:10:44 -0600113#define vnpr_info(lvl, fmt, ...) \
Jim Cromieb8ccd5d2012-04-27 14:30:32 -0600114do { \
Jim Cromie481c0e32020-07-19 17:10:44 -0600115 if (verbose >= lvl) \
Joe Perchesf657fd22012-12-05 16:48:26 -0500116 pr_info(fmt, ##__VA_ARGS__); \
Jim Cromie574b3722011-12-19 17:13:16 -0500117} while (0)
118
Jim Cromie481c0e32020-07-19 17:10:44 -0600119#define vpr_info(fmt, ...) vnpr_info(1, fmt, ##__VA_ARGS__)
120#define v2pr_info(fmt, ...) vnpr_info(2, fmt, ##__VA_ARGS__)
121
Joe Perchesf657fd22012-12-05 16:48:26 -0500122static 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 Cromief62fc082020-07-19 17:10:51 -0600135 query->function ?: "",
136 query->filename ?: "",
137 query->module ?: "",
138 fmtlen, query->format ?: "",
Joe Perchesf657fd22012-12-05 16:48:26 -0500139 query->first_lineno, query->last_lineno);
140}
141
Jason Barone9d376f2009-02-05 11:51:38 -0500142/*
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500143 * 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 Barone9d376f2009-02-05 11:51:38 -0500147 */
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500148static int ddebug_change(const struct ddebug_query *query,
Jim Cromie84da83a2020-07-19 17:10:55 -0600149 struct flag_settings *modifiers)
Jason Barone9d376f2009-02-05 11:51:38 -0500150{
151 int i;
152 struct ddebug_table *dt;
153 unsigned int newflags;
154 unsigned int nfound = 0;
Jim Cromief678ce82020-07-19 17:10:47 -0600155 struct flagsbuf fbuf;
Jason Barone9d376f2009-02-05 11:51:38 -0500156
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, Changbin578b1e02014-01-23 15:54:14 -0800162 if (query->module &&
163 !match_wildcard(query->module, dt->mod_name))
Jason Barone9d376f2009-02-05 11:51:38 -0500164 continue;
165
Joe Perchesf657fd22012-12-05 16:48:26 -0500166 for (i = 0; i < dt->num_ddebugs; i++) {
Jason Barone9d376f2009-02-05 11:51:38 -0500167 struct _ddebug *dp = &dt->ddebugs[i];
168
169 /* match against the source filename */
Jim Cromied6a238d2011-12-19 17:12:39 -0500170 if (query->filename &&
Du, Changbin578b1e02014-01-23 15:54:14 -0800171 !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 Barone9d376f2009-02-05 11:51:38 -0500176 continue;
177
178 /* match against the function */
Jim Cromied6a238d2011-12-19 17:12:39 -0500179 if (query->function &&
Du, Changbin578b1e02014-01-23 15:54:14 -0800180 !match_wildcard(query->function, dp->function))
Jason Barone9d376f2009-02-05 11:51:38 -0500181 continue;
182
183 /* match against the format */
Jim Cromied6a238d2011-12-19 17:12:39 -0500184 if (query->format &&
185 !strstr(dp->format, query->format))
Jason Barone9d376f2009-02-05 11:51:38 -0500186 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 Cromie84da83a2020-07-19 17:10:55 -0600198 newflags = (dp->flags & modifiers->mask) | modifiers->flags;
Jason Barone9d376f2009-02-05 11:51:38 -0500199 if (newflags == dp->flags)
200 continue;
Masahiro Yamadae9666d12018-12-31 00:14:15 +0900201#ifdef CONFIG_JUMP_LABEL
Jason Baron9049fc72016-08-03 13:46:39 -0700202 if (dp->flags & _DPRINTK_FLAGS_PRINT) {
Jim Cromie84da83a2020-07-19 17:10:55 -0600203 if (!(modifiers->flags & _DPRINTK_FLAGS_PRINT))
Jason Baron9049fc72016-08-03 13:46:39 -0700204 static_branch_disable(&dp->key.dd_key_true);
Jim Cromie84da83a2020-07-19 17:10:55 -0600205 } else if (modifiers->flags & _DPRINTK_FLAGS_PRINT)
Jason Baron9049fc72016-08-03 13:46:39 -0700206 static_branch_enable(&dp->key.dd_key_true);
207#endif
Jason Barone9d376f2009-02-05 11:51:38 -0500208 dp->flags = newflags;
Jim Cromie481c0e32020-07-19 17:10:44 -0600209 v2pr_info("changed %s:%d [%s]%s =%s\n",
Joe Perchesf657fd22012-12-05 16:48:26 -0500210 trim_prefix(dp->filename), dp->lineno,
211 dt->mod_name, dp->function,
Jim Cromief678ce82020-07-19 17:10:47 -0600212 ddebug_describe_flags(dp->flags, &fbuf));
Jason Barone9d376f2009-02-05 11:51:38 -0500213 }
214 }
215 mutex_unlock(&ddebug_lock);
216
217 if (!nfound && verbose)
Joe Perches4ad275e2011-08-11 14:36:33 -0400218 pr_info("no matches for query\n");
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500219
220 return nfound;
Jason Barone9d376f2009-02-05 11:51:38 -0500221}
222
223/*
Jason Barone9d376f2009-02-05 11:51:38 -0500224 * Split the buffer `buf' into space-separated words.
Greg Banks9898abb2009-02-06 12:54:26 +1100225 * Handles simple " and ' quoting, i.e. without nested,
226 * embedded or escaped \". Return the number of words
227 * or <0 on error.
Jason Barone9d376f2009-02-05 11:51:38 -0500228 */
229static int ddebug_tokenize(char *buf, char *words[], int maxwords)
230{
231 int nwords = 0;
232
Greg Banks9898abb2009-02-06 12:54:26 +1100233 while (*buf) {
234 char *end;
235
236 /* Skip leading whitespace */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800237 buf = skip_spaces(buf);
Greg Banks9898abb2009-02-06 12:54:26 +1100238 if (!*buf)
239 break; /* oh, it was trailing whitespace */
Jim Cromie8bd60262011-12-19 17:13:03 -0500240 if (*buf == '#')
241 break; /* token starts comment, skip rest of line */
Greg Banks9898abb2009-02-06 12:54:26 +1100242
Jim Cromie07100be2011-12-19 17:11:09 -0500243 /* find `end' of word, whitespace separated or quoted */
Greg Banks9898abb2009-02-06 12:54:26 +1100244 if (*buf == '"' || *buf == '\'') {
245 int quote = *buf++;
Joe Perchesf657fd22012-12-05 16:48:26 -0500246 for (end = buf; *end && *end != quote; end++)
Greg Banks9898abb2009-02-06 12:54:26 +1100247 ;
Jim Cromie18c216c2012-12-05 16:48:27 -0500248 if (!*end) {
249 pr_err("unclosed quote: %s\n", buf);
Greg Banks9898abb2009-02-06 12:54:26 +1100250 return -EINVAL; /* unclosed quote */
Jim Cromie18c216c2012-12-05 16:48:27 -0500251 }
Greg Banks9898abb2009-02-06 12:54:26 +1100252 } else {
Joe Perchesf657fd22012-12-05 16:48:26 -0500253 for (end = buf; *end && !isspace(*end); end++)
Greg Banks9898abb2009-02-06 12:54:26 +1100254 ;
255 BUG_ON(end == buf);
256 }
Greg Banks9898abb2009-02-06 12:54:26 +1100257
Jim Cromie07100be2011-12-19 17:11:09 -0500258 /* `buf' is start of word, `end' is one past its end */
Jim Cromie18c216c2012-12-05 16:48:27 -0500259 if (nwords == maxwords) {
260 pr_err("too many words, legal max <=%d\n", maxwords);
Greg Banks9898abb2009-02-06 12:54:26 +1100261 return -EINVAL; /* ran out of words[] before bytes */
Jim Cromie18c216c2012-12-05 16:48:27 -0500262 }
Greg Banks9898abb2009-02-06 12:54:26 +1100263 if (*end)
264 *end++ = '\0'; /* terminate the word */
265 words[nwords++] = buf;
266 buf = end;
267 }
Jason Barone9d376f2009-02-05 11:51:38 -0500268
269 if (verbose) {
270 int i;
Joe Perches4ad275e2011-08-11 14:36:33 -0400271 pr_info("split into words:");
Joe Perchesf657fd22012-12-05 16:48:26 -0500272 for (i = 0; i < nwords; i++)
Joe Perches4ad275e2011-08-11 14:36:33 -0400273 pr_cont(" \"%s\"", words[i]);
274 pr_cont("\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500275 }
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 */
285static inline int parse_lineno(const char *str, unsigned int *val)
286{
Jason Barone9d376f2009-02-05 11:51:38 -0500287 BUG_ON(str == NULL);
288 if (*str == '\0') {
289 *val = 0;
290 return 0;
291 }
Andrey Ryabinin45925992014-01-27 17:06:59 -0800292 if (kstrtouint(str, 10, val) < 0) {
Jim Cromie18c216c2012-12-05 16:48:27 -0500293 pr_err("bad line-number: %s\n", str);
294 return -EINVAL;
295 }
296 return 0;
Jason Barone9d376f2009-02-05 11:51:38 -0500297}
298
Jim Cromie80370722020-07-19 17:10:52 -0600299static 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 Cromie820874c2011-12-19 17:12:49 -0500334static int check_set(const char **dest, char *src, char *name)
335{
336 int rc = 0;
337
338 if (*dest) {
339 rc = -EINVAL;
Joe Perchesf657fd22012-12-05 16:48:26 -0500340 pr_err("match-spec:%s val:%s overridden by %s\n",
341 name, *dest, src);
Jim Cromie820874c2011-12-19 17:12:49 -0500342 }
343 *dest = src;
344 return rc;
345}
346
Jason Barone9d376f2009-02-05 11:51:38 -0500347/*
348 * Parse words[] as a ddebug query specification, which is a series
Jim Cromie14775b02020-07-19 17:10:54 -0600349 * of (keyword, value) pairs or combined keyword=value terms,
350 * chosen from these possibilities:
Jason Barone9d376f2009-02-05 11:51:38 -0500351 *
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 Cromie820874c2011-12-19 17:12:49 -0500359 *
360 * Only 1 of each type is allowed.
361 * Returns 0 on success, <0 on error.
Jason Barone9d376f2009-02-05 11:51:38 -0500362 */
363static int ddebug_parse_query(char *words[], int nwords,
Jim Cromie8e59b5cf2012-04-27 14:30:40 -0600364 struct ddebug_query *query, const char *modname)
Jason Barone9d376f2009-02-05 11:51:38 -0500365{
366 unsigned int i;
jbaron@akamai.combd8c154a2013-08-27 16:50:03 +0000367 int rc = 0;
Jim Cromieaaebe322020-07-19 17:10:53 -0600368 char *fline;
Jim Cromie14775b02020-07-19 17:10:54 -0600369 char *keyword, *arg;
Jason Barone9d376f2009-02-05 11:51:38 -0500370
Jim Cromie8e59b5cf2012-04-27 14:30:40 -0600371 if (modname)
372 /* support $modname.dyndbg=<multiple queries> */
373 query->module = modname;
374
Jim Cromie14775b02020-07-19 17:10:54 -0600375 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 Cromieaaebe322020-07-19 17:10:53 -0600397 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 Cromie14775b02020-07-19 17:10:54 -0600412 } 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 Shevchenkod338b132013-04-30 15:27:32 -0700416 UNESCAPE_OCTAL |
417 UNESCAPE_SPECIAL);
Jim Cromie14775b02020-07-19 17:10:54 -0600418 rc = check_set(&query->format, arg, "format");
419 } else if (!strcmp(keyword, "line")) {
420 if (parse_linerange(query, arg))
Jim Cromie820874c2011-12-19 17:12:49 -0500421 return -EINVAL;
Jason Barone9d376f2009-02-05 11:51:38 -0500422 } else {
Jim Cromie14775b02020-07-19 17:10:54 -0600423 pr_err("unknown keyword \"%s\"\n", keyword);
Jason Barone9d376f2009-02-05 11:51:38 -0500424 return -EINVAL;
425 }
Jim Cromie820874c2011-12-19 17:12:49 -0500426 if (rc)
427 return rc;
Jason Barone9d376f2009-02-05 11:51:38 -0500428 }
Jim Cromie574b3722011-12-19 17:13:16 -0500429 vpr_info_dq(query, "parsed");
Jason Barone9d376f2009-02-05 11:51:38 -0500430 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 Cromie84da83a2020-07-19 17:10:55 -0600439static int ddebug_parse_flags(const char *str, struct flag_settings *modifiers)
Jason Barone9d376f2009-02-05 11:51:38 -0500440{
Jim Cromie84da83a2020-07-19 17:10:55 -0600441 int op, i;
Jason Barone9d376f2009-02-05 11:51:38 -0500442
443 switch (*str) {
444 case '+':
445 case '-':
446 case '=':
447 op = *str++;
448 break;
449 default:
Jim Cromie18c216c2012-12-05 16:48:27 -0500450 pr_err("bad flag-op %c, at start of %s\n", *str, str);
Jason Barone9d376f2009-02-05 11:51:38 -0500451 return -EINVAL;
452 }
Jim Cromieb8ccd5d2012-04-27 14:30:32 -0600453 vpr_info("op='%c'\n", op);
Jason Barone9d376f2009-02-05 11:51:38 -0500454
Joe Perchesf657fd22012-12-05 16:48:26 -0500455 for (; *str ; ++str) {
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100456 for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
457 if (*str == opt_array[i].opt_char) {
Jim Cromie84da83a2020-07-19 17:10:55 -0600458 modifiers->flags |= opt_array[i].flag;
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100459 break;
460 }
Jason Barone9d376f2009-02-05 11:51:38 -0500461 }
Jim Cromie18c216c2012-12-05 16:48:27 -0500462 if (i < 0) {
Jim Cromie0b8f96b2020-07-19 17:10:48 -0600463 pr_err("unknown flag '%c'\n", *str);
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100464 return -EINVAL;
Jim Cromie18c216c2012-12-05 16:48:27 -0500465 }
Jason Barone9d376f2009-02-05 11:51:38 -0500466 }
Jim Cromie84da83a2020-07-19 17:10:55 -0600467 vpr_info("flags=0x%x\n", modifiers->flags);
Jason Barone9d376f2009-02-05 11:51:38 -0500468
Jim Cromie84da83a2020-07-19 17:10:55 -0600469 /* calculate final flags, mask based upon op */
Jason Barone9d376f2009-02-05 11:51:38 -0500470 switch (op) {
471 case '=':
Jim Cromie84da83a2020-07-19 17:10:55 -0600472 /* modifiers->flags already set */
473 modifiers->mask = 0;
Jason Barone9d376f2009-02-05 11:51:38 -0500474 break;
475 case '+':
Jim Cromie84da83a2020-07-19 17:10:55 -0600476 modifiers->mask = ~0U;
Jason Barone9d376f2009-02-05 11:51:38 -0500477 break;
478 case '-':
Jim Cromie84da83a2020-07-19 17:10:55 -0600479 modifiers->mask = ~modifiers->flags;
480 modifiers->flags = 0;
Jason Barone9d376f2009-02-05 11:51:38 -0500481 break;
482 }
Jim Cromie84da83a2020-07-19 17:10:55 -0600483 vpr_info("*flagsp=0x%x *maskp=0x%x\n", modifiers->flags, modifiers->mask);
484
Jason Barone9d376f2009-02-05 11:51:38 -0500485 return 0;
486}
487
Jim Cromie8e59b5cf2012-04-27 14:30:40 -0600488static int ddebug_exec_query(char *query_string, const char *modname)
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200489{
Jim Cromie84da83a2020-07-19 17:10:55 -0600490 struct flag_settings modifiers = {};
Jim Cromie9c9d0ac2020-07-19 17:10:49 -0600491 struct ddebug_query query = {};
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200492#define MAXWORDS 9
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500493 int nwords, nfound;
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200494 char *words[MAXWORDS];
495
496 nwords = ddebug_tokenize(query_string, words, MAXWORDS);
Jim Cromie18c216c2012-12-05 16:48:27 -0500497 if (nwords <= 0) {
498 pr_err("tokenize failed\n");
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200499 return -EINVAL;
Jim Cromie18c216c2012-12-05 16:48:27 -0500500 }
501 /* check flags 1st (last arg) so query is pairs of spec,val */
Jim Cromie84da83a2020-07-19 17:10:55 -0600502 if (ddebug_parse_flags(words[nwords-1], &modifiers)) {
Jim Cromie18c216c2012-12-05 16:48:27 -0500503 pr_err("flags parse failed\n");
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200504 return -EINVAL;
Jim Cromie18c216c2012-12-05 16:48:27 -0500505 }
506 if (ddebug_parse_query(words, nwords-1, &query, modname)) {
507 pr_err("query parse failed\n");
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200508 return -EINVAL;
Jim Cromie18c216c2012-12-05 16:48:27 -0500509 }
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200510 /* actually go and implement the change */
Jim Cromie84da83a2020-07-19 17:10:55 -0600511 nfound = ddebug_change(&query, &modifiers);
Joe Perchesf657fd22012-12-05 16:48:26 -0500512 vpr_info_dq(&query, nfound ? "applied" : "no-match");
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500513
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 Cromie8e59b5cf2012-04-27 14:30:40 -0600521static int ddebug_exec_queries(char *query, const char *modname)
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500522{
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 Cromieb8ccd5d2012-04-27 14:30:32 -0600535 vpr_info("query %d: \"%s\"\n", i, query);
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500536
Jim Cromie8e59b5cf2012-04-27 14:30:40 -0600537 rc = ddebug_exec_query(query, modname);
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500538 if (rc < 0) {
539 errs++;
540 exitcode = rc;
Joe Perchesf657fd22012-12-05 16:48:26 -0500541 } else {
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500542 nfound += rc;
Joe Perchesf657fd22012-12-05 16:48:26 -0500543 }
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500544 i++;
545 }
Jim Cromieb8ccd5d2012-04-27 14:30:32 -0600546 vpr_info("processed %d queries, with %d matches, %d errs\n",
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500547 i, nfound, errs);
548
549 if (exitcode)
550 return exitcode;
551 return nfound;
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200552}
553
Jason Baron431625d2011-10-04 14:13:19 -0700554#define PREFIX_SIZE 64
555
556static int remaining(int wrote)
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100557{
Jason Baron431625d2011-10-04 14:13:19 -0700558 if (PREFIX_SIZE - wrote > 0)
559 return PREFIX_SIZE - wrote;
560 return 0;
561}
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100562
Jason Baron431625d2011-10-04 14:13:19 -0700563static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf)
564{
565 int pos_after_tid;
566 int pos = 0;
567
Joe Perches798efc62012-09-12 20:11:29 -0700568 *buf = '\0';
569
Jason Baron431625d2011-10-04 14:13:19 -0700570 if (desc->flags & _DPRINTK_FLAGS_INCL_TID) {
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100571 if (in_interrupt())
Joe Perches798efc62012-09-12 20:11:29 -0700572 pos += snprintf(buf + pos, remaining(pos), "<intr> ");
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100573 else
Jason Baron431625d2011-10-04 14:13:19 -0700574 pos += snprintf(buf + pos, remaining(pos), "[%d] ",
Joe Perches798efc62012-09-12 20:11:29 -0700575 task_pid_vnr(current));
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100576 }
Jason Baron431625d2011-10-04 14:13:19 -0700577 pos_after_tid = pos;
578 if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME)
579 pos += snprintf(buf + pos, remaining(pos), "%s:",
Joe Perches798efc62012-09-12 20:11:29 -0700580 desc->modname);
Jason Baron431625d2011-10-04 14:13:19 -0700581 if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
582 pos += snprintf(buf + pos, remaining(pos), "%s:",
Joe Perches798efc62012-09-12 20:11:29 -0700583 desc->function);
Jason Baron431625d2011-10-04 14:13:19 -0700584 if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO)
Jim Cromie07100be2011-12-19 17:11:09 -0500585 pos += snprintf(buf + pos, remaining(pos), "%d:",
Joe Perches798efc62012-09-12 20:11:29 -0700586 desc->lineno);
Jason Baron431625d2011-10-04 14:13:19 -0700587 if (pos - pos_after_tid)
588 pos += snprintf(buf + pos, remaining(pos), " ");
589 if (pos >= PREFIX_SIZE)
590 buf[PREFIX_SIZE - 1] = '\0';
Joe Perches6c2140e2011-08-11 14:36:25 -0400591
Jason Baron431625d2011-10-04 14:13:19 -0700592 return buf;
Joe Perches6c2140e2011-08-11 14:36:25 -0400593}
594
Joe Perches906d2012014-09-24 11:17:56 -0700595void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100596{
597 va_list args;
Jason Baron431625d2011-10-04 14:13:19 -0700598 struct va_format vaf;
599 char buf[PREFIX_SIZE];
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100600
601 BUG_ON(!descriptor);
602 BUG_ON(!fmt);
603
604 va_start(args, fmt);
Joe Perches798efc62012-09-12 20:11:29 -0700605
Jason Baron431625d2011-10-04 14:13:19 -0700606 vaf.fmt = fmt;
607 vaf.va = &args;
Joe Perches798efc62012-09-12 20:11:29 -0700608
Joe Perches906d2012014-09-24 11:17:56 -0700609 printk(KERN_DEBUG "%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf);
Joe Perches798efc62012-09-12 20:11:29 -0700610
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100611 va_end(args);
Bart Van Assche8ba6ebf52011-01-23 17:17:24 +0100612}
613EXPORT_SYMBOL(__dynamic_pr_debug);
614
Joe Perches906d2012014-09-24 11:17:56 -0700615void __dynamic_dev_dbg(struct _ddebug *descriptor,
Joe Perchescbc46632011-08-11 14:36:21 -0400616 const struct device *dev, const char *fmt, ...)
617{
618 struct va_format vaf;
619 va_list args;
Joe Perchescbc46632011-08-11 14:36:21 -0400620
621 BUG_ON(!descriptor);
622 BUG_ON(!fmt);
623
624 va_start(args, fmt);
Joe Perches798efc62012-09-12 20:11:29 -0700625
Joe Perchescbc46632011-08-11 14:36:21 -0400626 vaf.fmt = fmt;
627 vaf.va = &args;
Joe Perches798efc62012-09-12 20:11:29 -0700628
629 if (!dev) {
Joe Perches906d2012014-09-24 11:17:56 -0700630 printk(KERN_DEBUG "(NULL device *): %pV", &vaf);
Joe Perches798efc62012-09-12 20:11:29 -0700631 } else {
632 char buf[PREFIX_SIZE];
Joe Perches798efc62012-09-12 20:11:29 -0700633
Joe Perchesa39d4a82014-12-10 15:50:15 -0800634 dev_printk_emit(LOGLEVEL_DEBUG, dev, "%s%s %s: %pV",
Joe Perches906d2012014-09-24 11:17:56 -0700635 dynamic_emit_prefix(descriptor, buf),
636 dev_driver_string(dev), dev_name(dev),
637 &vaf);
Joe Perches798efc62012-09-12 20:11:29 -0700638 }
639
Joe Perchescbc46632011-08-11 14:36:21 -0400640 va_end(args);
Joe Perchescbc46632011-08-11 14:36:21 -0400641}
642EXPORT_SYMBOL(__dynamic_dev_dbg);
643
Jason Baron0feefd92011-10-04 14:13:22 -0700644#ifdef CONFIG_NET
645
Joe Perches906d2012014-09-24 11:17:56 -0700646void __dynamic_netdev_dbg(struct _ddebug *descriptor,
647 const struct net_device *dev, const char *fmt, ...)
Jason Baronffa10cb2011-08-11 14:36:48 -0400648{
649 struct va_format vaf;
650 va_list args;
Jason Baronffa10cb2011-08-11 14:36:48 -0400651
652 BUG_ON(!descriptor);
653 BUG_ON(!fmt);
654
655 va_start(args, fmt);
Joe Perchesb004ff42012-09-12 20:12:19 -0700656
Jason Baronffa10cb2011-08-11 14:36:48 -0400657 vaf.fmt = fmt;
658 vaf.va = &args;
Joe Perchesb004ff42012-09-12 20:12:19 -0700659
660 if (dev && dev->dev.parent) {
661 char buf[PREFIX_SIZE];
Joe Perchesb004ff42012-09-12 20:12:19 -0700662
Joe Perchesa39d4a82014-12-10 15:50:15 -0800663 dev_printk_emit(LOGLEVEL_DEBUG, dev->dev.parent,
Joe Perches906d2012014-09-24 11:17:56 -0700664 "%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 Perchesb004ff42012-09-12 20:12:19 -0700670 } else if (dev) {
Joe Perches906d2012014-09-24 11:17:56 -0700671 printk(KERN_DEBUG "%s%s: %pV", netdev_name(dev),
672 netdev_reg_state(dev), &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -0700673 } else {
Joe Perches906d2012014-09-24 11:17:56 -0700674 printk(KERN_DEBUG "(NULL net_device): %pV", &vaf);
Joe Perchesb004ff42012-09-12 20:12:19 -0700675 }
676
Jason Baronffa10cb2011-08-11 14:36:48 -0400677 va_end(args);
Jason Baronffa10cb2011-08-11 14:36:48 -0400678}
679EXPORT_SYMBOL(__dynamic_netdev_dbg);
680
Jason Baron0feefd92011-10-04 14:13:22 -0700681#endif
682
Gal Pressman923abb92019-05-01 13:48:13 +0300683#if IS_ENABLED(CONFIG_INFINIBAND)
684
685void __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}
714EXPORT_SYMBOL(__dynamic_ibdev_dbg);
715
716#endif
717
Jim Cromiebc757f62011-12-19 17:12:29 -0500718#define DDEBUG_STRING_SIZE 1024
719static __initdata char ddebug_setup_string[DDEBUG_STRING_SIZE];
720
Thomas Renningera648ec02010-08-06 16:11:02 +0200721static __init int ddebug_setup_query(char *str)
722{
Jim Cromiebc757f62011-12-19 17:12:29 -0500723 if (strlen(str) >= DDEBUG_STRING_SIZE) {
Joe Perches4ad275e2011-08-11 14:36:33 -0400724 pr_warn("ddebug boot param string too large\n");
Thomas Renningera648ec02010-08-06 16:11:02 +0200725 return 0;
726 }
Jim Cromiebc757f62011-12-19 17:12:29 -0500727 strlcpy(ddebug_setup_string, str, DDEBUG_STRING_SIZE);
Thomas Renningera648ec02010-08-06 16:11:02 +0200728 return 1;
729}
730
731__setup("ddebug_query=", ddebug_setup_query);
732
Jason Barone9d376f2009-02-05 11:51:38 -0500733/*
Masatake YAMATO231821d2014-12-15 12:04:16 +0900734 * File_ops->write method for <debugfs>/dynamic_debug/control. Gathers the
Jason Barone9d376f2009-02-05 11:51:38 -0500735 * command text from userspace, parses and executes it.
736 */
Jim Cromie72814912011-12-19 17:13:07 -0500737#define USER_BUF_PAGE 4096
Jason Barone9d376f2009-02-05 11:51:38 -0500738static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
739 size_t len, loff_t *offp)
740{
Jim Cromie72814912011-12-19 17:13:07 -0500741 char *tmpbuf;
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200742 int ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500743
744 if (len == 0)
745 return 0;
Jim Cromie72814912011-12-19 17:13:07 -0500746 if (len > USER_BUF_PAGE - 1) {
747 pr_warn("expected <%d bytes into control\n", USER_BUF_PAGE);
Jason Barone9d376f2009-02-05 11:51:38 -0500748 return -E2BIG;
Jim Cromie72814912011-12-19 17:13:07 -0500749 }
Al Viro16e5c1f2015-12-24 00:06:05 -0500750 tmpbuf = memdup_user_nul(ubuf, len);
751 if (IS_ERR(tmpbuf))
752 return PTR_ERR(tmpbuf);
Jim Cromieb8ccd5d2012-04-27 14:30:32 -0600753 vpr_info("read %d bytes from userspace\n", (int)len);
Jason Barone9d376f2009-02-05 11:51:38 -0500754
Jim Cromie8e59b5cf2012-04-27 14:30:40 -0600755 ret = ddebug_exec_queries(tmpbuf, NULL);
Jim Cromie72814912011-12-19 17:13:07 -0500756 kfree(tmpbuf);
Jim Cromie85f7f6c2011-12-19 17:13:21 -0500757 if (ret < 0)
Thomas Renningerfd89cfb2010-08-06 16:11:01 +0200758 return ret;
Jason Barone9d376f2009-02-05 11:51:38 -0500759
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 */
769static 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 */
788static 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 */
810static 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 Barone9d376f2009-02-05 11:51:38 -0500816 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 */
833static 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 Barone9d376f2009-02-05 11:51:38 -0500838 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 */
852static int ddebug_proc_show(struct seq_file *m, void *p)
853{
854 struct ddebug_iter *iter = m->private;
855 struct _ddebug *dp = p;
Jim Cromief678ce82020-07-19 17:10:47 -0600856 struct flagsbuf flags;
Jason Barone9d376f2009-02-05 11:51:38 -0500857
Jason Barone9d376f2009-02-05 11:51:38 -0500858 if (p == SEQ_START_TOKEN) {
859 seq_puts(m,
Joe Perchesf657fd22012-12-05 16:48:26 -0500860 "# filename:lineno [module]function flags format\n");
Jason Barone9d376f2009-02-05 11:51:38 -0500861 return 0;
862 }
863
Jim Cromie5ca7d2a2011-12-19 17:12:44 -0500864 seq_printf(m, "%s:%u [%s]%s =%s \"",
Joe Perchesf657fd22012-12-05 16:48:26 -0500865 trim_prefix(dp->filename), dp->lineno,
866 iter->table->mod_name, dp->function,
Jim Cromief678ce82020-07-19 17:10:47 -0600867 ddebug_describe_flags(dp->flags, &flags));
Jason Barone9d376f2009-02-05 11:51:38 -0500868 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 */
878static void ddebug_proc_stop(struct seq_file *m, void *p)
879{
Jason Barone9d376f2009-02-05 11:51:38 -0500880 mutex_unlock(&ddebug_lock);
881}
882
883static 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 Barone9d376f2009-02-05 11:51:38 -0500890static int ddebug_proc_open(struct inode *inode, struct file *file)
891{
Jim Cromieb8ccd5d2012-04-27 14:30:32 -0600892 vpr_info("called\n");
Rob Jones4bad78c2014-10-13 15:51:32 -0700893 return seq_open_private(file, &ddebug_proc_seqops,
894 sizeof(struct ddebug_iter));
Jason Barone9d376f2009-02-05 11:51:38 -0500895}
896
897static 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-Hartman239a5792020-02-10 13:11:42 -0800906static 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 Barone9d376f2009-02-05 11:51:38 -0500914/*
915 * Allocate a new ddebug_table for the given module
916 * and add it to the global list.
917 */
918int ddebug_add_module(struct _ddebug *tab, unsigned int n,
919 const char *name)
920{
921 struct ddebug_table *dt;
Jason Barone9d376f2009-02-05 11:51:38 -0500922
923 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
Rasmus Villemoes513770f2019-03-07 16:27:48 -0800924 if (dt == NULL) {
925 pr_err("error adding module: %s\n", name);
Jason Barone9d376f2009-02-05 11:51:38 -0500926 return -ENOMEM;
Rasmus Villemoes513770f2019-03-07 16:27:48 -0800927 }
Rasmus Villemoescdf6d002019-03-07 16:27:37 -0800928 /*
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 Barone9d376f2009-02-05 11:51:38 -0500935 dt->num_ddebugs = n;
Jason Barone9d376f2009-02-05 11:51:38 -0500936 dt->ddebugs = tab;
937
938 mutex_lock(&ddebug_lock);
Jim Cromie47e9f5a2020-07-19 17:10:50 -0600939 list_add(&dt->link, &ddebug_tables);
Jason Barone9d376f2009-02-05 11:51:38 -0500940 mutex_unlock(&ddebug_lock);
941
Jim Cromie481c0e32020-07-19 17:10:44 -0600942 v2pr_info("%u debug prints in module %s\n", n, dt->mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -0500943 return 0;
944}
Jason Barone9d376f2009-02-05 11:51:38 -0500945
Jim Cromie6ab676e2012-04-27 14:30:37 -0600946/* helper for ddebug_dyndbg_(boot|module)_param_cb */
947static int ddebug_dyndbg_param_cb(char *param, char *val,
948 const char *modname, int on_err)
Jim Cromieb48420c2012-04-27 14:30:35 -0600949{
Jim Cromieb48420c2012-04-27 14:30:35 -0600950 char *sep;
951
952 sep = strchr(param, '.');
953 if (sep) {
Jim Cromie6ab676e2012-04-27 14:30:37 -0600954 /* needed only for ddebug_dyndbg_boot_param_cb */
Jim Cromieb48420c2012-04-27 14:30:35 -0600955 *sep = '\0';
956 modname = param;
957 param = sep + 1;
958 }
959 if (strcmp(param, "dyndbg"))
Jim Cromie6ab676e2012-04-27 14:30:37 -0600960 return on_err; /* determined by caller */
Jim Cromieb48420c2012-04-27 14:30:35 -0600961
Jim Cromie8e59b5cf2012-04-27 14:30:40 -0600962 ddebug_exec_queries((val ? val : "+p"), modname);
963
Jim Cromieb48420c2012-04-27 14:30:35 -0600964 return 0; /* query failure shouldnt stop module load */
965}
966
Jim Cromie6ab676e2012-04-27 14:30:37 -0600967/* handle both dyndbg and $module.dyndbg params at boot */
968static int ddebug_dyndbg_boot_param_cb(char *param, char *val,
Luis R. Rodriguezecc86172015-03-30 16:20:03 -0700969 const char *unused, void *arg)
Jim Cromieb48420c2012-04-27 14:30:35 -0600970{
Jim Cromie6ab676e2012-04-27 14:30:37 -0600971 vpr_info("%s=\"%s\"\n", param, val);
972 return ddebug_dyndbg_param_cb(param, val, NULL, 0);
973}
Jim Cromieb48420c2012-04-27 14:30:35 -0600974
Jim Cromie6ab676e2012-04-27 14:30:37 -0600975/*
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 */
980int 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 Cromieb48420c2012-04-27 14:30:35 -0600984}
985
Jason Barone9d376f2009-02-05 11:51:38 -0500986static void ddebug_table_free(struct ddebug_table *dt)
987{
988 list_del_init(&dt->link);
Jason Barone9d376f2009-02-05 11:51:38 -0500989 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 Sadehff49d742010-07-03 13:07:35 +1000996int ddebug_remove_module(const char *mod_name)
Jason Barone9d376f2009-02-05 11:51:38 -0500997{
998 struct ddebug_table *dt, *nextdt;
999 int ret = -ENOENT;
1000
Jim Cromie481c0e32020-07-19 17:10:44 -06001001 v2pr_info("removing module \"%s\"\n", mod_name);
Jason Barone9d376f2009-02-05 11:51:38 -05001002
1003 mutex_lock(&ddebug_lock);
1004 list_for_each_entry_safe(dt, nextdt, &ddebug_tables, link) {
Rasmus Villemoes4573fe12019-03-07 16:27:41 -08001005 if (dt->mod_name == mod_name) {
Jason Barone9d376f2009-02-05 11:51:38 -05001006 ddebug_table_free(dt);
1007 ret = 0;
Rasmus Villemoes4573fe12019-03-07 16:27:41 -08001008 break;
Jason Barone9d376f2009-02-05 11:51:38 -05001009 }
1010 }
1011 mutex_unlock(&ddebug_lock);
1012 return ret;
1013}
Jason Barone9d376f2009-02-05 11:51:38 -05001014
1015static 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 Renninger6a5c0832010-08-06 16:11:03 +02001027static __initdata int ddebug_init_success;
1028
Greg Kroah-Hartman239a5792020-02-10 13:11:42 -08001029static int __init dynamic_debug_init_control(void)
Jason Barone9d376f2009-02-05 11:51:38 -05001030{
Greg Kroah-Hartman239a5792020-02-10 13:11:42 -08001031 struct proc_dir_entry *procfs_dir;
1032 struct dentry *debugfs_dir;
Thomas Renninger6a5c0832010-08-06 16:11:03 +02001033
1034 if (!ddebug_init_success)
1035 return -ENODEV;
Jason Barone9d376f2009-02-05 11:51:38 -05001036
Greg Kroah-Hartman239a5792020-02-10 13:11:42 -08001037 /* 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-Hartman9fd714c2019-06-12 17:35:34 +02001048
Thomas Renninger6a5c0832010-08-06 16:11:03 +02001049 return 0;
1050}
1051
1052static int __init dynamic_debug_init(void)
1053{
1054 struct _ddebug *iter, *iter_start;
1055 const char *modname = NULL;
Jim Cromieb48420c2012-04-27 14:30:35 -06001056 char *cmdline;
Thomas Renninger6a5c0832010-08-06 16:11:03 +02001057 int ret = 0;
Jim Cromie41076922012-04-27 14:30:39 -06001058 int n = 0, entries = 0, modct = 0;
Thomas Renninger6a5c0832010-08-06 16:11:03 +02001059
Jim Cromiee5ebffe2020-07-19 17:10:45 -06001060 if (&__start___dyndbg == &__stop___dyndbg) {
Orson Zhaiceabef72020-06-07 21:40:14 -07001061 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 Barone9d376f2009-02-05 11:51:38 -05001068 }
Jim Cromiee5ebffe2020-07-19 17:10:45 -06001069 iter = __start___dyndbg;
Jim Cromieb5b78f82011-12-19 17:12:54 -05001070 modname = iter->modname;
1071 iter_start = iter;
Jim Cromiee5ebffe2020-07-19 17:10:45 -06001072 for (; iter < __stop___dyndbg; iter++) {
Jim Cromie41076922012-04-27 14:30:39 -06001073 entries++;
Jim Cromieb5b78f82011-12-19 17:12:54 -05001074 if (strcmp(modname, iter->modname)) {
Jim Cromie41076922012-04-27 14:30:39 -06001075 modct++;
Jim Cromieb5b78f82011-12-19 17:12:54 -05001076 ret = ddebug_add_module(iter_start, n, modname);
1077 if (ret)
Jim Cromieaf442392012-04-27 14:30:38 -06001078 goto out_err;
Jim Cromieb5b78f82011-12-19 17:12:54 -05001079 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 Cromieaf442392012-04-27 14:30:38 -06001087 goto out_err;
Thomas Renningera648ec02010-08-06 16:11:02 +02001088
Jim Cromieaf442392012-04-27 14:30:38 -06001089 ddebug_init_success = 1;
Jim Cromie81d0c2c2020-07-19 17:10:46 -06001090 vpr_info("%d modules, %d entries and %d bytes in ddebug tables, %d bytes in __dyndbg section\n",
Joe Perchesf657fd22012-12-05 16:48:26 -05001091 modct, entries, (int)(modct * sizeof(struct ddebug_table)),
Jim Cromie81d0c2c2020-07-19 17:10:46 -06001092 (int)(entries * sizeof(struct _ddebug)));
Jim Cromieaf442392012-04-27 14:30:38 -06001093
1094 /* apply ddebug_query boot param, dont unload tables on err */
Thomas Renningera648ec02010-08-06 16:11:02 +02001095 if (ddebug_setup_string[0] != '\0') {
Joe Perchesf657fd22012-12-05 16:48:26 -05001096 pr_warn("ddebug_query param name is deprecated, change it to dyndbg\n");
Jim Cromie8e59b5cf2012-04-27 14:30:40 -06001097 ret = ddebug_exec_queries(ddebug_setup_string, NULL);
Jim Cromie85f7f6c2011-12-19 17:13:21 -05001098 if (ret < 0)
Joe Perchesf657fd22012-12-05 16:48:26 -05001099 pr_warn("Invalid ddebug boot param %s\n",
Joe Perches4ad275e2011-08-11 14:36:33 -04001100 ddebug_setup_string);
Thomas Renningera648ec02010-08-06 16:11:02 +02001101 else
Jim Cromie85f7f6c2011-12-19 17:13:21 -05001102 pr_info("%d changes by ddebug_query\n", ret);
Jason Barone9d376f2009-02-05 11:51:38 -05001103 }
Jim Cromieb48420c2012-04-27 14:30:35 -06001104 /* 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. Rodriguezecc86172015-03-30 16:20:03 -07001114 0, 0, 0, NULL, &ddebug_dyndbg_boot_param_cb);
Jim Cromieb48420c2012-04-27 14:30:35 -06001115 kfree(cmdline);
Jim Cromieaf442392012-04-27 14:30:38 -06001116 return 0;
Thomas Renningera648ec02010-08-06 16:11:02 +02001117
Jim Cromieaf442392012-04-27 14:30:38 -06001118out_err:
1119 ddebug_remove_all_tables();
Jason Barone9d376f2009-02-05 11:51:38 -05001120 return 0;
1121}
Thomas Renninger6a5c0832010-08-06 16:11:03 +02001122/* Allow early initialization for boot messages via boot param */
Jim Cromie3ec56522012-04-27 14:30:42 -06001123early_initcall(dynamic_debug_init);
Jim Cromieb48420c2012-04-27 14:30:35 -06001124
Thomas Renninger6a5c0832010-08-06 16:11:03 +02001125/* Debugfs setup must be done later */
Greg Kroah-Hartman239a5792020-02-10 13:11:42 -08001126fs_initcall(dynamic_debug_init_control);