Commit | Line | Data |
---|---|---|
1427cdf0 LJ |
1 | /* |
2 | * trace binary printk | |
3 | * | |
4 | * Copyright (C) 2008 Lai Jiangshan <laijs@cn.fujitsu.com> | |
5 | * | |
6 | */ | |
7975a2be SR |
7 | #include <linux/seq_file.h> |
8 | #include <linux/debugfs.h> | |
9 | #include <linux/uaccess.h> | |
1427cdf0 LJ |
10 | #include <linux/kernel.h> |
11 | #include <linux/ftrace.h> | |
12 | #include <linux/string.h> | |
7975a2be | 13 | #include <linux/module.h> |
7975a2be | 14 | #include <linux/mutex.h> |
1427cdf0 LJ |
15 | #include <linux/ctype.h> |
16 | #include <linux/list.h> | |
1427cdf0 | 17 | #include <linux/slab.h> |
1427cdf0 | 18 | #include <linux/fs.h> |
1427cdf0 LJ |
19 | |
20 | #include "trace.h" | |
21 | ||
1ba28e02 LJ |
22 | #ifdef CONFIG_MODULES |
23 | ||
1ba28e02 | 24 | /* |
769b0441 | 25 | * modules trace_printk()'s formats are autosaved in struct trace_bprintk_fmt |
1ba28e02 LJ |
26 | * which are queued on trace_bprintk_fmt_list. |
27 | */ | |
28 | static LIST_HEAD(trace_bprintk_fmt_list); | |
29 | ||
769b0441 FW |
30 | /* serialize accesses to trace_bprintk_fmt_list */ |
31 | static DEFINE_MUTEX(btrace_mutex); | |
32 | ||
1ba28e02 LJ |
33 | struct trace_bprintk_fmt { |
34 | struct list_head list; | |
0588fa30 | 35 | const char *fmt; |
1ba28e02 LJ |
36 | }; |
37 | ||
1ba28e02 | 38 | static inline struct trace_bprintk_fmt *lookup_format(const char *fmt) |
1427cdf0 | 39 | { |
1ba28e02 LJ |
40 | struct trace_bprintk_fmt *pos; |
41 | list_for_each_entry(pos, &trace_bprintk_fmt_list, list) { | |
42 | if (!strcmp(pos->fmt, fmt)) | |
43 | return pos; | |
44 | } | |
45 | return NULL; | |
1427cdf0 LJ |
46 | } |
47 | ||
1ba28e02 LJ |
48 | static |
49 | void hold_module_trace_bprintk_format(const char **start, const char **end) | |
1427cdf0 | 50 | { |
1ba28e02 | 51 | const char **iter; |
0588fa30 | 52 | char *fmt; |
769b0441 FW |
53 | |
54 | mutex_lock(&btrace_mutex); | |
1ba28e02 LJ |
55 | for (iter = start; iter < end; iter++) { |
56 | struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter); | |
57 | if (tb_fmt) { | |
58 | *iter = tb_fmt->fmt; | |
59 | continue; | |
60 | } | |
61 | ||
0588fa30 SR |
62 | tb_fmt = kmalloc(sizeof(*tb_fmt), GFP_KERNEL); |
63 | if (tb_fmt) | |
64 | fmt = kmalloc(strlen(*iter) + 1, GFP_KERNEL); | |
65 | if (tb_fmt && fmt) { | |
1ba28e02 | 66 | list_add_tail(&tb_fmt->list, &trace_bprintk_fmt_list); |
0588fa30 SR |
67 | strcpy(fmt, *iter); |
68 | tb_fmt->fmt = fmt; | |
1ba28e02 | 69 | *iter = tb_fmt->fmt; |
0588fa30 SR |
70 | } else { |
71 | kfree(tb_fmt); | |
1ba28e02 | 72 | *iter = NULL; |
0588fa30 | 73 | } |
1ba28e02 | 74 | } |
769b0441 | 75 | mutex_unlock(&btrace_mutex); |
1427cdf0 LJ |
76 | } |
77 | ||
1ba28e02 LJ |
78 | static int module_trace_bprintk_format_notify(struct notifier_block *self, |
79 | unsigned long val, void *data) | |
80 | { | |
81 | struct module *mod = data; | |
82 | if (mod->num_trace_bprintk_fmt) { | |
83 | const char **start = mod->trace_bprintk_fmt_start; | |
84 | const char **end = start + mod->num_trace_bprintk_fmt; | |
85 | ||
86 | if (val == MODULE_STATE_COMING) | |
87 | hold_module_trace_bprintk_format(start, end); | |
88 | } | |
89 | return 0; | |
90 | } | |
91 | ||
92 | #else /* !CONFIG_MODULES */ | |
93 | __init static int | |
94 | module_trace_bprintk_format_notify(struct notifier_block *self, | |
95 | unsigned long val, void *data) | |
96 | { | |
97 | return 0; | |
98 | } | |
99 | #endif /* CONFIG_MODULES */ | |
100 | ||
101 | ||
102 | __initdata_or_module static | |
103 | struct notifier_block module_trace_bprintk_format_nb = { | |
104 | .notifier_call = module_trace_bprintk_format_notify, | |
105 | }; | |
106 | ||
48ead020 | 107 | int __trace_bprintk(unsigned long ip, const char *fmt, ...) |
769b0441 FW |
108 | { |
109 | int ret; | |
110 | va_list ap; | |
1427cdf0 | 111 | |
769b0441 FW |
112 | if (unlikely(!fmt)) |
113 | return 0; | |
1427cdf0 | 114 | |
769b0441 FW |
115 | if (!(trace_flags & TRACE_ITER_PRINTK)) |
116 | return 0; | |
117 | ||
118 | va_start(ap, fmt); | |
40ce74f1 | 119 | ret = trace_vbprintk(ip, fmt, ap); |
769b0441 FW |
120 | va_end(ap); |
121 | return ret; | |
1427cdf0 | 122 | } |
48ead020 | 123 | EXPORT_SYMBOL_GPL(__trace_bprintk); |
1427cdf0 | 124 | |
48ead020 | 125 | int __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap) |
769b0441 FW |
126 | { |
127 | if (unlikely(!fmt)) | |
128 | return 0; | |
129 | ||
130 | if (!(trace_flags & TRACE_ITER_PRINTK)) | |
131 | return 0; | |
132 | ||
40ce74f1 | 133 | return trace_vbprintk(ip, fmt, ap); |
48ead020 FW |
134 | } |
135 | EXPORT_SYMBOL_GPL(__ftrace_vbprintk); | |
136 | ||
137 | int __trace_printk(unsigned long ip, const char *fmt, ...) | |
138 | { | |
139 | int ret; | |
140 | va_list ap; | |
141 | ||
142 | if (!(trace_flags & TRACE_ITER_PRINTK)) | |
143 | return 0; | |
144 | ||
145 | va_start(ap, fmt); | |
40ce74f1 | 146 | ret = trace_vprintk(ip, fmt, ap); |
48ead020 FW |
147 | va_end(ap); |
148 | return ret; | |
149 | } | |
150 | EXPORT_SYMBOL_GPL(__trace_printk); | |
151 | ||
152 | int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap) | |
153 | { | |
154 | if (!(trace_flags & TRACE_ITER_PRINTK)) | |
155 | return 0; | |
156 | ||
40ce74f1 | 157 | return trace_vprintk(ip, fmt, ap); |
1427cdf0 | 158 | } |
769b0441 | 159 | EXPORT_SYMBOL_GPL(__ftrace_vprintk); |
1427cdf0 | 160 | |
7975a2be | 161 | static void * |
c8961ec6 | 162 | t_start(struct seq_file *m, loff_t *pos) |
7975a2be | 163 | { |
c8961ec6 | 164 | const char **fmt = __start___trace_bprintk_fmt + *pos; |
7975a2be SR |
165 | |
166 | if ((unsigned long)fmt >= (unsigned long)__stop___trace_bprintk_fmt) | |
167 | return NULL; | |
7975a2be SR |
168 | return fmt; |
169 | } | |
170 | ||
c8961ec6 | 171 | static void *t_next(struct seq_file *m, void * v, loff_t *pos) |
7975a2be | 172 | { |
c8961ec6 LZ |
173 | (*pos)++; |
174 | return t_start(m, pos); | |
7975a2be SR |
175 | } |
176 | ||
177 | static int t_show(struct seq_file *m, void *v) | |
178 | { | |
179 | const char **fmt = v; | |
180 | const char *str = *fmt; | |
181 | int i; | |
182 | ||
4c739ff0 | 183 | seq_printf(m, "0x%lx : \"", *(unsigned long *)fmt); |
7975a2be SR |
184 | |
185 | /* | |
186 | * Tabs and new lines need to be converted. | |
187 | */ | |
188 | for (i = 0; str[i]; i++) { | |
189 | switch (str[i]) { | |
190 | case '\n': | |
191 | seq_puts(m, "\\n"); | |
192 | break; | |
193 | case '\t': | |
194 | seq_puts(m, "\\t"); | |
195 | break; | |
196 | case '\\': | |
197 | seq_puts(m, "\\"); | |
198 | break; | |
199 | case '"': | |
200 | seq_puts(m, "\\\""); | |
201 | break; | |
202 | default: | |
203 | seq_putc(m, str[i]); | |
204 | } | |
205 | } | |
206 | seq_puts(m, "\"\n"); | |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
211 | static void t_stop(struct seq_file *m, void *p) | |
212 | { | |
213 | } | |
214 | ||
215 | static const struct seq_operations show_format_seq_ops = { | |
216 | .start = t_start, | |
217 | .next = t_next, | |
218 | .show = t_show, | |
219 | .stop = t_stop, | |
220 | }; | |
221 | ||
222 | static int | |
223 | ftrace_formats_open(struct inode *inode, struct file *file) | |
224 | { | |
c8961ec6 | 225 | return seq_open(file, &show_format_seq_ops); |
7975a2be SR |
226 | } |
227 | ||
228 | static const struct file_operations ftrace_formats_fops = { | |
229 | .open = ftrace_formats_open, | |
230 | .read = seq_read, | |
231 | .llseek = seq_lseek, | |
232 | .release = seq_release, | |
233 | }; | |
234 | ||
235 | static __init int init_trace_printk_function_export(void) | |
236 | { | |
237 | struct dentry *d_tracer; | |
7975a2be SR |
238 | |
239 | d_tracer = tracing_init_dentry(); | |
240 | if (!d_tracer) | |
241 | return 0; | |
242 | ||
5452af66 | 243 | trace_create_file("printk_formats", 0444, d_tracer, |
7975a2be | 244 | NULL, &ftrace_formats_fops); |
7975a2be SR |
245 | |
246 | return 0; | |
247 | } | |
248 | ||
249 | fs_initcall(init_trace_printk_function_export); | |
250 | ||
769b0441 | 251 | static __init int init_trace_printk(void) |
1427cdf0 | 252 | { |
769b0441 | 253 | return register_module_notifier(&module_trace_bprintk_format_nb); |
1427cdf0 LJ |
254 | } |
255 | ||
769b0441 | 256 | early_initcall(init_trace_printk); |