net: CONFIG_COMPAT redux
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / trace_printk.c
blob2547d8813cf0535048162db1d986263a638f188a
1 /*
2 * trace binary printk
4 * Copyright (C) 2008 Lai Jiangshan <laijs@cn.fujitsu.com>
6 */
7 #include <linux/seq_file.h>
8 #include <linux/debugfs.h>
9 #include <linux/uaccess.h>
10 #include <linux/kernel.h>
11 #include <linux/ftrace.h>
12 #include <linux/string.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/ctype.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
18 #include <linux/fs.h>
20 #include "trace.h"
22 #ifdef CONFIG_MODULES
25 * modules trace_printk()'s formats are autosaved in struct trace_bprintk_fmt
26 * which are queued on trace_bprintk_fmt_list.
28 static LIST_HEAD(trace_bprintk_fmt_list);
30 /* serialize accesses to trace_bprintk_fmt_list */
31 static DEFINE_MUTEX(btrace_mutex);
33 struct trace_bprintk_fmt {
34 struct list_head list;
35 char fmt[0];
38 static inline struct trace_bprintk_fmt *lookup_format(const char *fmt)
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;
45 return NULL;
48 static
49 void hold_module_trace_bprintk_format(const char **start, const char **end)
51 const char **iter;
53 mutex_lock(&btrace_mutex);
54 for (iter = start; iter < end; iter++) {
55 struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);
56 if (tb_fmt) {
57 *iter = tb_fmt->fmt;
58 continue;
61 tb_fmt = kmalloc(offsetof(struct trace_bprintk_fmt, fmt)
62 + strlen(*iter) + 1, GFP_KERNEL);
63 if (tb_fmt) {
64 list_add_tail(&tb_fmt->list, &trace_bprintk_fmt_list);
65 strcpy(tb_fmt->fmt, *iter);
66 *iter = tb_fmt->fmt;
67 } else
68 *iter = NULL;
70 mutex_unlock(&btrace_mutex);
73 static int module_trace_bprintk_format_notify(struct notifier_block *self,
74 unsigned long val, void *data)
76 struct module *mod = data;
77 if (mod->num_trace_bprintk_fmt) {
78 const char **start = mod->trace_bprintk_fmt_start;
79 const char **end = start + mod->num_trace_bprintk_fmt;
81 if (val == MODULE_STATE_COMING)
82 hold_module_trace_bprintk_format(start, end);
84 return 0;
87 #else /* !CONFIG_MODULES */
88 __init static int
89 module_trace_bprintk_format_notify(struct notifier_block *self,
90 unsigned long val, void *data)
92 return 0;
94 #endif /* CONFIG_MODULES */
97 __initdata_or_module static
98 struct notifier_block module_trace_bprintk_format_nb = {
99 .notifier_call = module_trace_bprintk_format_notify,
102 int __trace_bprintk(unsigned long ip, const char *fmt, ...)
104 int ret;
105 va_list ap;
107 if (unlikely(!fmt))
108 return 0;
110 if (!(trace_flags & TRACE_ITER_PRINTK))
111 return 0;
113 va_start(ap, fmt);
114 ret = trace_vbprintk(ip, fmt, ap);
115 va_end(ap);
116 return ret;
118 EXPORT_SYMBOL_GPL(__trace_bprintk);
120 int __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap)
122 if (unlikely(!fmt))
123 return 0;
125 if (!(trace_flags & TRACE_ITER_PRINTK))
126 return 0;
128 return trace_vbprintk(ip, fmt, ap);
130 EXPORT_SYMBOL_GPL(__ftrace_vbprintk);
132 int __trace_printk(unsigned long ip, const char *fmt, ...)
134 int ret;
135 va_list ap;
137 if (!(trace_flags & TRACE_ITER_PRINTK))
138 return 0;
140 va_start(ap, fmt);
141 ret = trace_vprintk(ip, fmt, ap);
142 va_end(ap);
143 return ret;
145 EXPORT_SYMBOL_GPL(__trace_printk);
147 int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
149 if (!(trace_flags & TRACE_ITER_PRINTK))
150 return 0;
152 return trace_vprintk(ip, fmt, ap);
154 EXPORT_SYMBOL_GPL(__ftrace_vprintk);
156 static void *
157 t_start(struct seq_file *m, loff_t *pos)
159 const char **fmt = __start___trace_bprintk_fmt + *pos;
161 if ((unsigned long)fmt >= (unsigned long)__stop___trace_bprintk_fmt)
162 return NULL;
163 return fmt;
166 static void *t_next(struct seq_file *m, void * v, loff_t *pos)
168 (*pos)++;
169 return t_start(m, pos);
172 static int t_show(struct seq_file *m, void *v)
174 const char **fmt = v;
175 const char *str = *fmt;
176 int i;
178 seq_printf(m, "0x%lx : \"", *(unsigned long *)fmt);
181 * Tabs and new lines need to be converted.
183 for (i = 0; str[i]; i++) {
184 switch (str[i]) {
185 case '\n':
186 seq_puts(m, "\\n");
187 break;
188 case '\t':
189 seq_puts(m, "\\t");
190 break;
191 case '\\':
192 seq_puts(m, "\\");
193 break;
194 case '"':
195 seq_puts(m, "\\\"");
196 break;
197 default:
198 seq_putc(m, str[i]);
201 seq_puts(m, "\"\n");
203 return 0;
206 static void t_stop(struct seq_file *m, void *p)
210 static const struct seq_operations show_format_seq_ops = {
211 .start = t_start,
212 .next = t_next,
213 .show = t_show,
214 .stop = t_stop,
217 static int
218 ftrace_formats_open(struct inode *inode, struct file *file)
220 return seq_open(file, &show_format_seq_ops);
223 static const struct file_operations ftrace_formats_fops = {
224 .open = ftrace_formats_open,
225 .read = seq_read,
226 .llseek = seq_lseek,
227 .release = seq_release,
230 static __init int init_trace_printk_function_export(void)
232 struct dentry *d_tracer;
234 d_tracer = tracing_init_dentry();
235 if (!d_tracer)
236 return 0;
238 trace_create_file("printk_formats", 0444, d_tracer,
239 NULL, &ftrace_formats_fops);
241 return 0;
244 fs_initcall(init_trace_printk_function_export);
246 static __init int init_trace_printk(void)
248 return register_module_notifier(&module_trace_bprintk_format_nb);
251 early_initcall(init_trace_printk);