blob: e82cff5c842c6e5c8ac961cd6beae0ab0a3ab304 [file] [log] [blame]
Lai Jiangshan1427cdf2009-03-06 17:21:47 +01001/*
2 * trace binary printk
3 *
4 * Copyright (C) 2008 Lai Jiangshan <laijs@cn.fujitsu.com>
5 *
6 */
Steven Rostedt7975a2b2009-03-12 14:23:17 -04007#include <linux/seq_file.h>
Steven Rostedt7975a2b2009-03-12 14:23:17 -04008#include <linux/uaccess.h>
Lai Jiangshan1427cdf2009-03-06 17:21:47 +01009#include <linux/kernel.h>
10#include <linux/ftrace.h>
11#include <linux/string.h>
Steven Rostedt7975a2b2009-03-12 14:23:17 -040012#include <linux/module.h>
Steven Rostedt7975a2b2009-03-12 14:23:17 -040013#include <linux/mutex.h>
Lai Jiangshan1427cdf2009-03-06 17:21:47 +010014#include <linux/ctype.h>
15#include <linux/list.h>
Lai Jiangshan1427cdf2009-03-06 17:21:47 +010016#include <linux/slab.h>
Lai Jiangshan1427cdf2009-03-06 17:21:47 +010017
18#include "trace.h"
19
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010020#ifdef CONFIG_MODULES
21
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010022/*
Frederic Weisbecker769b0442009-03-06 17:21:49 +010023 * modules trace_printk()'s formats are autosaved in struct trace_bprintk_fmt
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010024 * which are queued on trace_bprintk_fmt_list.
25 */
26static LIST_HEAD(trace_bprintk_fmt_list);
27
Frederic Weisbecker769b0442009-03-06 17:21:49 +010028/* serialize accesses to trace_bprintk_fmt_list */
29static DEFINE_MUTEX(btrace_mutex);
30
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010031struct trace_bprintk_fmt {
32 struct list_head list;
Steven Rostedt0588fa32011-03-21 22:59:21 -040033 const char *fmt;
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010034};
35
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010036static inline struct trace_bprintk_fmt *lookup_format(const char *fmt)
Lai Jiangshan1427cdf2009-03-06 17:21:47 +010037{
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010038 struct trace_bprintk_fmt *pos;
Steven Rostedt (Red Hat)70c82172016-06-17 16:10:42 -040039
40 if (!fmt)
41 return ERR_PTR(-EINVAL);
42
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010043 list_for_each_entry(pos, &trace_bprintk_fmt_list, list) {
44 if (!strcmp(pos->fmt, fmt))
45 return pos;
46 }
47 return NULL;
48}
49
50static
51void hold_module_trace_bprintk_format(const char **start, const char **end)
52{
53 const char **iter;
Steven Rostedt0588fa32011-03-21 22:59:21 -040054 char *fmt;
Frederic Weisbecker769b0442009-03-06 17:21:49 +010055
Steven Rostedt07d777f2011-09-22 14:01:55 -040056 /* allocate the trace_printk per cpu buffers */
57 if (start != end)
58 trace_printk_init_buffers();
59
Frederic Weisbecker769b0442009-03-06 17:21:49 +010060 mutex_lock(&btrace_mutex);
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010061 for (iter = start; iter < end; iter++) {
62 struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);
63 if (tb_fmt) {
Steven Rostedt (Red Hat)70c82172016-06-17 16:10:42 -040064 if (!IS_ERR(tb_fmt))
65 *iter = tb_fmt->fmt;
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010066 continue;
67 }
68
Steven Rostedt3a301d72011-08-08 21:39:39 -040069 fmt = NULL;
Steven Rostedt0588fa32011-03-21 22:59:21 -040070 tb_fmt = kmalloc(sizeof(*tb_fmt), GFP_KERNEL);
Steven Rostedt3a301d72011-08-08 21:39:39 -040071 if (tb_fmt) {
Steven Rostedt0588fa32011-03-21 22:59:21 -040072 fmt = kmalloc(strlen(*iter) + 1, GFP_KERNEL);
Steven Rostedt3a301d72011-08-08 21:39:39 -040073 if (fmt) {
74 list_add_tail(&tb_fmt->list, &trace_bprintk_fmt_list);
75 strcpy(fmt, *iter);
76 tb_fmt->fmt = fmt;
77 } else
78 kfree(tb_fmt);
Steven Rostedt0588fa32011-03-21 22:59:21 -040079 }
Steven Rostedt3a301d72011-08-08 21:39:39 -040080 *iter = fmt;
81
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010082 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +010083 mutex_unlock(&btrace_mutex);
Lai Jiangshan1427cdf2009-03-06 17:21:47 +010084}
85
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010086static int module_trace_bprintk_format_notify(struct notifier_block *self,
87 unsigned long val, void *data)
Lai Jiangshan1427cdf2009-03-06 17:21:47 +010088{
Lai Jiangshan1ba28e02009-03-06 17:21:48 +010089 struct module *mod = data;
90 if (mod->num_trace_bprintk_fmt) {
91 const char **start = mod->trace_bprintk_fmt_start;
92 const char **end = start + mod->num_trace_bprintk_fmt;
93
94 if (val == MODULE_STATE_COMING)
95 hold_module_trace_bprintk_format(start, end);
96 }
97 return 0;
Lai Jiangshan1427cdf2009-03-06 17:21:47 +010098}
99
Steven Rostedt1813dc32011-03-21 23:36:31 -0400100/*
101 * The debugfs/tracing/printk_formats file maps the addresses with
102 * the ASCII formats that are used in the bprintk events in the
103 * buffer. For userspace tools to be able to decode the events from
104 * the buffer, they need to be able to map the address with the format.
105 *
106 * The addresses of the bprintk formats are in their own section
107 * __trace_printk_fmt. But for modules we copy them into a link list.
108 * The code to print the formats and their addresses passes around the
109 * address of the fmt string. If the fmt address passed into the seq
110 * functions is within the kernel core __trace_printk_fmt section, then
111 * it simply uses the next pointer in the list.
112 *
113 * When the fmt pointer is outside the kernel core __trace_printk_fmt
114 * section, then we need to read the link list pointers. The trick is
115 * we pass the address of the string to the seq function just like
116 * we do for the kernel core formats. To get back the structure that
117 * holds the format, we simply use containerof() and then go to the
118 * next format in the list.
119 */
120static const char **
121find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos)
122{
123 struct trace_bprintk_fmt *mod_fmt;
124
125 if (list_empty(&trace_bprintk_fmt_list))
126 return NULL;
127
128 /*
129 * v will point to the address of the fmt record from t_next
130 * v will be NULL from t_start.
131 * If this is the first pointer or called from start
132 * then we need to walk the list.
133 */
134 if (!v || start_index == *pos) {
135 struct trace_bprintk_fmt *p;
136
137 /* search the module list */
138 list_for_each_entry(p, &trace_bprintk_fmt_list, list) {
139 if (start_index == *pos)
140 return &p->fmt;
141 start_index++;
142 }
143 /* pos > index */
144 return NULL;
145 }
146
147 /*
148 * v points to the address of the fmt field in the mod list
149 * structure that holds the module print format.
150 */
151 mod_fmt = container_of(v, typeof(*mod_fmt), fmt);
152 if (mod_fmt->list.next == &trace_bprintk_fmt_list)
153 return NULL;
154
155 mod_fmt = container_of(mod_fmt->list.next, typeof(*mod_fmt), list);
156
157 return &mod_fmt->fmt;
158}
159
160static void format_mod_start(void)
161{
162 mutex_lock(&btrace_mutex);
163}
164
165static void format_mod_stop(void)
166{
167 mutex_unlock(&btrace_mutex);
168}
169
Lai Jiangshan1ba28e02009-03-06 17:21:48 +0100170#else /* !CONFIG_MODULES */
171__init static int
172module_trace_bprintk_format_notify(struct notifier_block *self,
173 unsigned long val, void *data)
174{
175 return 0;
176}
Steven Rostedt1813dc32011-03-21 23:36:31 -0400177static inline const char **
178find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos)
179{
180 return NULL;
181}
182static inline void format_mod_start(void) { }
183static inline void format_mod_stop(void) { }
Lai Jiangshan1ba28e02009-03-06 17:21:48 +0100184#endif /* CONFIG_MODULES */
185
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -0400186static bool __read_mostly trace_printk_enabled = true;
187
188void trace_printk_control(bool enabled)
189{
190 trace_printk_enabled = enabled;
191}
Lai Jiangshan1ba28e02009-03-06 17:21:48 +0100192
193__initdata_or_module static
194struct notifier_block module_trace_bprintk_format_nb = {
195 .notifier_call = module_trace_bprintk_format_notify,
196};
197
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100198int __trace_bprintk(unsigned long ip, const char *fmt, ...)
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100199 {
200 int ret;
201 va_list ap;
Lai Jiangshan1427cdf2009-03-06 17:21:47 +0100202
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100203 if (unlikely(!fmt))
204 return 0;
Lai Jiangshan1427cdf2009-03-06 17:21:47 +0100205
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -0400206 if (!trace_printk_enabled)
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100207 return 0;
Lai Jiangshan1427cdf2009-03-06 17:21:47 +0100208
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100209 va_start(ap, fmt);
Steven Rostedt40ce74f2009-03-19 14:03:53 -0400210 ret = trace_vbprintk(ip, fmt, ap);
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100211 va_end(ap);
212 return ret;
213}
214EXPORT_SYMBOL_GPL(__trace_bprintk);
215
216int __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap)
217 {
218 if (unlikely(!fmt))
219 return 0;
220
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -0400221 if (!trace_printk_enabled)
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100222 return 0;
223
Steven Rostedt40ce74f2009-03-19 14:03:53 -0400224 return trace_vbprintk(ip, fmt, ap);
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100225}
226EXPORT_SYMBOL_GPL(__ftrace_vbprintk);
227
228int __trace_printk(unsigned long ip, const char *fmt, ...)
229{
230 int ret;
231 va_list ap;
232
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -0400233 if (!trace_printk_enabled)
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100234 return 0;
235
236 va_start(ap, fmt);
Steven Rostedt40ce74f2009-03-19 14:03:53 -0400237 ret = trace_vprintk(ip, fmt, ap);
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100238 va_end(ap);
Lai Jiangshan1ba28e02009-03-06 17:21:48 +0100239 return ret;
Lai Jiangshan1427cdf2009-03-06 17:21:47 +0100240}
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100241EXPORT_SYMBOL_GPL(__trace_printk);
Lai Jiangshan1427cdf2009-03-06 17:21:47 +0100242
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100243int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100244{
Steven Rostedt (Red Hat)b9f91082015-09-29 18:21:35 -0400245 if (!trace_printk_enabled)
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100246 return 0;
247
Steven Rostedt40ce74f2009-03-19 14:03:53 -0400248 return trace_vprintk(ip, fmt, ap);
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100249}
250EXPORT_SYMBOL_GPL(__ftrace_vprintk);
251
Steven Rostedt1813dc32011-03-21 23:36:31 -0400252static const char **find_next(void *v, loff_t *pos)
253{
254 const char **fmt = v;
255 int start_index;
Steven Rostedt (Red Hat)102c9322013-07-12 17:07:27 -0400256 int last_index;
Steven Rostedt1813dc32011-03-21 23:36:31 -0400257
Steven Rostedt1813dc32011-03-21 23:36:31 -0400258 start_index = __stop___trace_bprintk_fmt - __start___trace_bprintk_fmt;
259
260 if (*pos < start_index)
Steven Rostedtdb5e7ec2011-06-09 08:40:59 -0400261 return __start___trace_bprintk_fmt + *pos;
Steven Rostedt1813dc32011-03-21 23:36:31 -0400262
Steven Rostedt (Red Hat)102c9322013-07-12 17:07:27 -0400263 /*
264 * The __tracepoint_str section is treated the same as the
265 * __trace_printk_fmt section. The difference is that the
266 * __trace_printk_fmt section should only be used by trace_printk()
267 * in a debugging environment, as if anything exists in that section
268 * the trace_prink() helper buffers are allocated, which would just
269 * waste space in a production environment.
270 *
271 * The __tracepoint_str sections on the other hand are used by
272 * tracepoints which need to map pointers to their strings to
273 * the ASCII text for userspace.
274 */
275 last_index = start_index;
276 start_index = __stop___tracepoint_str - __start___tracepoint_str;
277
278 if (*pos < last_index + start_index)
279 return __start___tracepoint_str + (*pos - last_index);
280
Qiu Peiyangf36d1be2015-12-31 13:11:28 +0800281 start_index += last_index;
Steven Rostedt1813dc32011-03-21 23:36:31 -0400282 return find_next_mod_format(start_index, v, fmt, pos);
283}
284
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400285static void *
Li Zefanc8961ec2009-06-24 09:52:58 +0800286t_start(struct seq_file *m, loff_t *pos)
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400287{
Steven Rostedt1813dc32011-03-21 23:36:31 -0400288 format_mod_start();
289 return find_next(NULL, pos);
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400290}
291
Li Zefanc8961ec2009-06-24 09:52:58 +0800292static void *t_next(struct seq_file *m, void * v, loff_t *pos)
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400293{
Li Zefanc8961ec2009-06-24 09:52:58 +0800294 (*pos)++;
Steven Rostedt1813dc32011-03-21 23:36:31 -0400295 return find_next(v, pos);
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400296}
297
298static int t_show(struct seq_file *m, void *v)
299{
300 const char **fmt = v;
301 const char *str = *fmt;
302 int i;
303
Steven Rostedt (Red Hat)3debb0a2016-03-22 17:30:58 -0400304 if (!*fmt)
305 return 0;
306
Nick Desaulniersf9b19a92017-03-03 15:40:12 -0800307 seq_printf(m, "0x%lx : \"", 0L);
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400308
309 /*
310 * Tabs and new lines need to be converted.
311 */
312 for (i = 0; str[i]; i++) {
313 switch (str[i]) {
314 case '\n':
315 seq_puts(m, "\\n");
316 break;
317 case '\t':
318 seq_puts(m, "\\t");
319 break;
320 case '\\':
Rasmus Villemoes1177e432014-11-08 21:42:12 +0100321 seq_putc(m, '\\');
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400322 break;
323 case '"':
324 seq_puts(m, "\\\"");
325 break;
326 default:
327 seq_putc(m, str[i]);
328 }
329 }
330 seq_puts(m, "\"\n");
331
332 return 0;
333}
334
335static void t_stop(struct seq_file *m, void *p)
336{
Steven Rostedt1813dc32011-03-21 23:36:31 -0400337 format_mod_stop();
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400338}
339
340static const struct seq_operations show_format_seq_ops = {
341 .start = t_start,
342 .next = t_next,
343 .show = t_show,
344 .stop = t_stop,
345};
346
347static int
348ftrace_formats_open(struct inode *inode, struct file *file)
349{
Li Zefanc8961ec2009-06-24 09:52:58 +0800350 return seq_open(file, &show_format_seq_ops);
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400351}
352
353static const struct file_operations ftrace_formats_fops = {
354 .open = ftrace_formats_open,
355 .read = seq_read,
356 .llseek = seq_lseek,
357 .release = seq_release,
358};
359
360static __init int init_trace_printk_function_export(void)
361{
362 struct dentry *d_tracer;
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400363
364 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -0500365 if (IS_ERR(d_tracer))
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400366 return 0;
367
Frederic Weisbecker5452af62009-03-27 00:25:38 +0100368 trace_create_file("printk_formats", 0444, d_tracer,
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400369 NULL, &ftrace_formats_fops);
Steven Rostedt7975a2b2009-03-12 14:23:17 -0400370
371 return 0;
372}
373
374fs_initcall(init_trace_printk_function_export);
375
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100376static __init int init_trace_printk(void)
377{
378 return register_module_notifier(&module_trace_bprintk_format_nb);
379}
380
381early_initcall(init_trace_printk);