Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * trace binary printk
0004  *
0005  * Copyright (C) 2008 Lai Jiangshan <laijs@cn.fujitsu.com>
0006  *
0007  */
0008 #include <linux/seq_file.h>
0009 #include <linux/security.h>
0010 #include <linux/uaccess.h>
0011 #include <linux/kernel.h>
0012 #include <linux/ftrace.h>
0013 #include <linux/string.h>
0014 #include <linux/module.h>
0015 #include <linux/mutex.h>
0016 #include <linux/ctype.h>
0017 #include <linux/list.h>
0018 #include <linux/slab.h>
0019 
0020 #include "trace.h"
0021 
0022 #ifdef CONFIG_MODULES
0023 
0024 /*
0025  * modules trace_printk()'s formats are autosaved in struct trace_bprintk_fmt
0026  * which are queued on trace_bprintk_fmt_list.
0027  */
0028 static LIST_HEAD(trace_bprintk_fmt_list);
0029 
0030 /* serialize accesses to trace_bprintk_fmt_list */
0031 static DEFINE_MUTEX(btrace_mutex);
0032 
0033 struct trace_bprintk_fmt {
0034     struct list_head list;
0035     const char *fmt;
0036 };
0037 
0038 static inline struct trace_bprintk_fmt *lookup_format(const char *fmt)
0039 {
0040     struct trace_bprintk_fmt *pos;
0041 
0042     if (!fmt)
0043         return ERR_PTR(-EINVAL);
0044 
0045     list_for_each_entry(pos, &trace_bprintk_fmt_list, list) {
0046         if (!strcmp(pos->fmt, fmt))
0047             return pos;
0048     }
0049     return NULL;
0050 }
0051 
0052 static
0053 void hold_module_trace_bprintk_format(const char **start, const char **end)
0054 {
0055     const char **iter;
0056     char *fmt;
0057 
0058     /* allocate the trace_printk per cpu buffers */
0059     if (start != end)
0060         trace_printk_init_buffers();
0061 
0062     mutex_lock(&btrace_mutex);
0063     for (iter = start; iter < end; iter++) {
0064         struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);
0065         if (tb_fmt) {
0066             if (!IS_ERR(tb_fmt))
0067                 *iter = tb_fmt->fmt;
0068             continue;
0069         }
0070 
0071         fmt = NULL;
0072         tb_fmt = kmalloc(sizeof(*tb_fmt), GFP_KERNEL);
0073         if (tb_fmt) {
0074             fmt = kmalloc(strlen(*iter) + 1, GFP_KERNEL);
0075             if (fmt) {
0076                 list_add_tail(&tb_fmt->list, &trace_bprintk_fmt_list);
0077                 strcpy(fmt, *iter);
0078                 tb_fmt->fmt = fmt;
0079             } else
0080                 kfree(tb_fmt);
0081         }
0082         *iter = fmt;
0083 
0084     }
0085     mutex_unlock(&btrace_mutex);
0086 }
0087 
0088 static int module_trace_bprintk_format_notify(struct notifier_block *self,
0089         unsigned long val, void *data)
0090 {
0091     struct module *mod = data;
0092     if (mod->num_trace_bprintk_fmt) {
0093         const char **start = mod->trace_bprintk_fmt_start;
0094         const char **end = start + mod->num_trace_bprintk_fmt;
0095 
0096         if (val == MODULE_STATE_COMING)
0097             hold_module_trace_bprintk_format(start, end);
0098     }
0099     return NOTIFY_OK;
0100 }
0101 
0102 /*
0103  * The debugfs/tracing/printk_formats file maps the addresses with
0104  * the ASCII formats that are used in the bprintk events in the
0105  * buffer. For userspace tools to be able to decode the events from
0106  * the buffer, they need to be able to map the address with the format.
0107  *
0108  * The addresses of the bprintk formats are in their own section
0109  * __trace_printk_fmt. But for modules we copy them into a link list.
0110  * The code to print the formats and their addresses passes around the
0111  * address of the fmt string. If the fmt address passed into the seq
0112  * functions is within the kernel core __trace_printk_fmt section, then
0113  * it simply uses the next pointer in the list.
0114  *
0115  * When the fmt pointer is outside the kernel core __trace_printk_fmt
0116  * section, then we need to read the link list pointers. The trick is
0117  * we pass the address of the string to the seq function just like
0118  * we do for the kernel core formats. To get back the structure that
0119  * holds the format, we simply use container_of() and then go to the
0120  * next format in the list.
0121  */
0122 static const char **
0123 find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos)
0124 {
0125     struct trace_bprintk_fmt *mod_fmt;
0126 
0127     if (list_empty(&trace_bprintk_fmt_list))
0128         return NULL;
0129 
0130     /*
0131      * v will point to the address of the fmt record from t_next
0132      * v will be NULL from t_start.
0133      * If this is the first pointer or called from start
0134      * then we need to walk the list.
0135      */
0136     if (!v || start_index == *pos) {
0137         struct trace_bprintk_fmt *p;
0138 
0139         /* search the module list */
0140         list_for_each_entry(p, &trace_bprintk_fmt_list, list) {
0141             if (start_index == *pos)
0142                 return &p->fmt;
0143             start_index++;
0144         }
0145         /* pos > index */
0146         return NULL;
0147     }
0148 
0149     /*
0150      * v points to the address of the fmt field in the mod list
0151      * structure that holds the module print format.
0152      */
0153     mod_fmt = container_of(v, typeof(*mod_fmt), fmt);
0154     if (mod_fmt->list.next == &trace_bprintk_fmt_list)
0155         return NULL;
0156 
0157     mod_fmt = container_of(mod_fmt->list.next, typeof(*mod_fmt), list);
0158 
0159     return &mod_fmt->fmt;
0160 }
0161 
0162 static void format_mod_start(void)
0163 {
0164     mutex_lock(&btrace_mutex);
0165 }
0166 
0167 static void format_mod_stop(void)
0168 {
0169     mutex_unlock(&btrace_mutex);
0170 }
0171 
0172 #else /* !CONFIG_MODULES */
0173 __init static int
0174 module_trace_bprintk_format_notify(struct notifier_block *self,
0175         unsigned long val, void *data)
0176 {
0177     return NOTIFY_OK;
0178 }
0179 static inline const char **
0180 find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos)
0181 {
0182     return NULL;
0183 }
0184 static inline void format_mod_start(void) { }
0185 static inline void format_mod_stop(void) { }
0186 #endif /* CONFIG_MODULES */
0187 
0188 static bool __read_mostly trace_printk_enabled = true;
0189 
0190 void trace_printk_control(bool enabled)
0191 {
0192     trace_printk_enabled = enabled;
0193 }
0194 
0195 __initdata_or_module static
0196 struct notifier_block module_trace_bprintk_format_nb = {
0197     .notifier_call = module_trace_bprintk_format_notify,
0198 };
0199 
0200 int __trace_bprintk(unsigned long ip, const char *fmt, ...)
0201 {
0202     int ret;
0203     va_list ap;
0204 
0205     if (unlikely(!fmt))
0206         return 0;
0207 
0208     if (!trace_printk_enabled)
0209         return 0;
0210 
0211     va_start(ap, fmt);
0212     ret = trace_vbprintk(ip, fmt, ap);
0213     va_end(ap);
0214     return ret;
0215 }
0216 EXPORT_SYMBOL_GPL(__trace_bprintk);
0217 
0218 int __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap)
0219 {
0220     if (unlikely(!fmt))
0221         return 0;
0222 
0223     if (!trace_printk_enabled)
0224         return 0;
0225 
0226     return trace_vbprintk(ip, fmt, ap);
0227 }
0228 EXPORT_SYMBOL_GPL(__ftrace_vbprintk);
0229 
0230 int __trace_printk(unsigned long ip, const char *fmt, ...)
0231 {
0232     int ret;
0233     va_list ap;
0234 
0235     if (!trace_printk_enabled)
0236         return 0;
0237 
0238     va_start(ap, fmt);
0239     ret = trace_vprintk(ip, fmt, ap);
0240     va_end(ap);
0241     return ret;
0242 }
0243 EXPORT_SYMBOL_GPL(__trace_printk);
0244 
0245 int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
0246 {
0247     if (!trace_printk_enabled)
0248         return 0;
0249 
0250     return trace_vprintk(ip, fmt, ap);
0251 }
0252 EXPORT_SYMBOL_GPL(__ftrace_vprintk);
0253 
0254 bool trace_is_tracepoint_string(const char *str)
0255 {
0256     const char **ptr = __start___tracepoint_str;
0257 
0258     for (ptr = __start___tracepoint_str; ptr < __stop___tracepoint_str; ptr++) {
0259         if (str == *ptr)
0260             return true;
0261     }
0262     return false;
0263 }
0264 
0265 static const char **find_next(void *v, loff_t *pos)
0266 {
0267     const char **fmt = v;
0268     int start_index;
0269     int last_index;
0270 
0271     start_index = __stop___trace_bprintk_fmt - __start___trace_bprintk_fmt;
0272 
0273     if (*pos < start_index)
0274         return __start___trace_bprintk_fmt + *pos;
0275 
0276     /*
0277      * The __tracepoint_str section is treated the same as the
0278      * __trace_printk_fmt section. The difference is that the
0279      * __trace_printk_fmt section should only be used by trace_printk()
0280      * in a debugging environment, as if anything exists in that section
0281      * the trace_prink() helper buffers are allocated, which would just
0282      * waste space in a production environment.
0283      *
0284      * The __tracepoint_str sections on the other hand are used by
0285      * tracepoints which need to map pointers to their strings to
0286      * the ASCII text for userspace.
0287      */
0288     last_index = start_index;
0289     start_index = __stop___tracepoint_str - __start___tracepoint_str;
0290 
0291     if (*pos < last_index + start_index)
0292         return __start___tracepoint_str + (*pos - last_index);
0293 
0294     start_index += last_index;
0295     return find_next_mod_format(start_index, v, fmt, pos);
0296 }
0297 
0298 static void *
0299 t_start(struct seq_file *m, loff_t *pos)
0300 {
0301     format_mod_start();
0302     return find_next(NULL, pos);
0303 }
0304 
0305 static void *t_next(struct seq_file *m, void * v, loff_t *pos)
0306 {
0307     (*pos)++;
0308     return find_next(v, pos);
0309 }
0310 
0311 static int t_show(struct seq_file *m, void *v)
0312 {
0313     const char **fmt = v;
0314     const char *str = *fmt;
0315     int i;
0316 
0317     if (!*fmt)
0318         return 0;
0319 
0320     seq_printf(m, "0x%lx : \"", *(unsigned long *)fmt);
0321 
0322     /*
0323      * Tabs and new lines need to be converted.
0324      */
0325     for (i = 0; str[i]; i++) {
0326         switch (str[i]) {
0327         case '\n':
0328             seq_puts(m, "\\n");
0329             break;
0330         case '\t':
0331             seq_puts(m, "\\t");
0332             break;
0333         case '\\':
0334             seq_putc(m, '\\');
0335             break;
0336         case '"':
0337             seq_puts(m, "\\\"");
0338             break;
0339         default:
0340             seq_putc(m, str[i]);
0341         }
0342     }
0343     seq_puts(m, "\"\n");
0344 
0345     return 0;
0346 }
0347 
0348 static void t_stop(struct seq_file *m, void *p)
0349 {
0350     format_mod_stop();
0351 }
0352 
0353 static const struct seq_operations show_format_seq_ops = {
0354     .start = t_start,
0355     .next = t_next,
0356     .show = t_show,
0357     .stop = t_stop,
0358 };
0359 
0360 static int
0361 ftrace_formats_open(struct inode *inode, struct file *file)
0362 {
0363     int ret;
0364 
0365     ret = security_locked_down(LOCKDOWN_TRACEFS);
0366     if (ret)
0367         return ret;
0368 
0369     return seq_open(file, &show_format_seq_ops);
0370 }
0371 
0372 static const struct file_operations ftrace_formats_fops = {
0373     .open = ftrace_formats_open,
0374     .read = seq_read,
0375     .llseek = seq_lseek,
0376     .release = seq_release,
0377 };
0378 
0379 static __init int init_trace_printk_function_export(void)
0380 {
0381     int ret;
0382 
0383     ret = tracing_init_dentry();
0384     if (ret)
0385         return 0;
0386 
0387     trace_create_file("printk_formats", TRACE_MODE_READ, NULL,
0388                     NULL, &ftrace_formats_fops);
0389 
0390     return 0;
0391 }
0392 
0393 fs_initcall(init_trace_printk_function_export);
0394 
0395 static __init int init_trace_printk(void)
0396 {
0397     return register_module_notifier(&module_trace_bprintk_format_nb);
0398 }
0399 
0400 early_initcall(init_trace_printk);