4 * Copyright (C) 2008 Lai Jiangshan <laijs@cn.fujitsu.com>
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>
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
;
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
))
49 void hold_module_trace_bprintk_format(const char **start
, const char **end
)
53 mutex_lock(&btrace_mutex
);
54 for (iter
= start
; iter
< end
; iter
++) {
55 struct trace_bprintk_fmt
*tb_fmt
= lookup_format(*iter
);
61 tb_fmt
= kmalloc(offsetof(struct trace_bprintk_fmt
, fmt
)
62 + strlen(*iter
) + 1, GFP_KERNEL
);
64 list_add_tail(&tb_fmt
->list
, &trace_bprintk_fmt_list
);
65 strcpy(tb_fmt
->fmt
, *iter
);
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
);
87 #else /* !CONFIG_MODULES */
89 module_trace_bprintk_format_notify(struct notifier_block
*self
,
90 unsigned long val
, void *data
)
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
, ...)
110 if (!(trace_flags
& TRACE_ITER_PRINTK
))
114 ret
= trace_vbprintk(ip
, fmt
, ap
);
118 EXPORT_SYMBOL_GPL(__trace_bprintk
);
120 int __ftrace_vbprintk(unsigned long ip
, const char *fmt
, va_list ap
)
125 if (!(trace_flags
& TRACE_ITER_PRINTK
))
128 return trace_vbprintk(ip
, fmt
, ap
);
130 EXPORT_SYMBOL_GPL(__ftrace_vbprintk
);
132 int __trace_printk(unsigned long ip
, const char *fmt
, ...)
137 if (!(trace_flags
& TRACE_ITER_PRINTK
))
141 ret
= trace_vprintk(ip
, fmt
, ap
);
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
))
152 return trace_vprintk(ip
, fmt
, ap
);
154 EXPORT_SYMBOL_GPL(__ftrace_vprintk
);
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
)
166 static void *t_next(struct seq_file
*m
, void * v
, loff_t
*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
;
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
++) {
206 static void t_stop(struct seq_file
*m
, void *p
)
210 static const struct seq_operations show_format_seq_ops
= {
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
,
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();
238 trace_create_file("printk_formats", 0444, d_tracer
,
239 NULL
, &ftrace_formats_fops
);
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
);