^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * trace binary printk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2008 Lai Jiangshan <laijs@cn.fujitsu.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #ifdef CONFIG_MODULES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * modules trace_printk()'s formats are autosaved in struct trace_bprintk_fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * which are queued on trace_bprintk_fmt_list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static LIST_HEAD(trace_bprintk_fmt_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* serialize accesses to trace_bprintk_fmt_list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static DEFINE_MUTEX(btrace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct trace_bprintk_fmt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const char *fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static inline struct trace_bprintk_fmt *lookup_format(const char *fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct trace_bprintk_fmt *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) list_for_each_entry(pos, &trace_bprintk_fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (!strcmp(pos->fmt, fmt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) void hold_module_trace_bprintk_format(const char **start, const char **end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) const char **iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) char *fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* allocate the trace_printk per cpu buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (start != end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) trace_printk_init_buffers();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) mutex_lock(&btrace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) for (iter = start; iter < end; iter++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (tb_fmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!IS_ERR(tb_fmt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *iter = tb_fmt->fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) fmt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) tb_fmt = kmalloc(sizeof(*tb_fmt), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (tb_fmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fmt = kmalloc(strlen(*iter) + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (fmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) list_add_tail(&tb_fmt->list, &trace_bprintk_fmt_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) strcpy(fmt, *iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) tb_fmt->fmt = fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) kfree(tb_fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *iter = fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) mutex_unlock(&btrace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static int module_trace_bprintk_format_notify(struct notifier_block *self,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned long val, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct module *mod = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (mod->num_trace_bprintk_fmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) const char **start = mod->trace_bprintk_fmt_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) const char **end = start + mod->num_trace_bprintk_fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (val == MODULE_STATE_COMING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) hold_module_trace_bprintk_format(start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * The debugfs/tracing/printk_formats file maps the addresses with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * the ASCII formats that are used in the bprintk events in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * buffer. For userspace tools to be able to decode the events from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * the buffer, they need to be able to map the address with the format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * The addresses of the bprintk formats are in their own section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * __trace_printk_fmt. But for modules we copy them into a link list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * The code to print the formats and their addresses passes around the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * address of the fmt string. If the fmt address passed into the seq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * functions is within the kernel core __trace_printk_fmt section, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * it simply uses the next pointer in the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * When the fmt pointer is outside the kernel core __trace_printk_fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * section, then we need to read the link list pointers. The trick is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * we pass the address of the string to the seq function just like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * we do for the kernel core formats. To get back the structure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * holds the format, we simply use container_of() and then go to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * next format in the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static const char **
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct trace_bprintk_fmt *mod_fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (list_empty(&trace_bprintk_fmt_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * v will point to the address of the fmt record from t_next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * v will be NULL from t_start.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * If this is the first pointer or called from start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * then we need to walk the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (!v || start_index == *pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct trace_bprintk_fmt *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* search the module list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) list_for_each_entry(p, &trace_bprintk_fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (start_index == *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return &p->fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) start_index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* pos > index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * v points to the address of the fmt field in the mod list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * structure that holds the module print format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) mod_fmt = container_of(v, typeof(*mod_fmt), fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (mod_fmt->list.next == &trace_bprintk_fmt_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) mod_fmt = container_of(mod_fmt->list.next, typeof(*mod_fmt), list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return &mod_fmt->fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static void format_mod_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) mutex_lock(&btrace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static void format_mod_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) mutex_unlock(&btrace_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #else /* !CONFIG_MODULES */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) __init static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) module_trace_bprintk_format_notify(struct notifier_block *self,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) unsigned long val, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static inline const char **
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static inline void format_mod_start(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static inline void format_mod_stop(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #endif /* CONFIG_MODULES */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static bool __read_mostly trace_printk_enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) void trace_printk_control(bool enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) trace_printk_enabled = enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) __initdata_or_module static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct notifier_block module_trace_bprintk_format_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .notifier_call = module_trace_bprintk_format_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) int __trace_bprintk(unsigned long ip, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (unlikely(!fmt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!trace_printk_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) va_start(ap, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ret = trace_vbprintk(ip, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) EXPORT_SYMBOL_GPL(__trace_bprintk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (unlikely(!fmt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (!trace_printk_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return trace_vbprintk(ip, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) EXPORT_SYMBOL_GPL(__ftrace_vbprintk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int __trace_printk(unsigned long ip, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!trace_printk_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) va_start(ap, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) ret = trace_vprintk(ip, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) EXPORT_SYMBOL_GPL(__trace_printk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (!trace_printk_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return trace_vprintk(ip, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) EXPORT_SYMBOL_GPL(__ftrace_vprintk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static const char **find_next(void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) const char **fmt = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int start_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int last_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) start_index = __stop___trace_bprintk_fmt - __start___trace_bprintk_fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (*pos < start_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return __start___trace_bprintk_fmt + *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * The __tracepoint_str section is treated the same as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * __trace_printk_fmt section. The difference is that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * __trace_printk_fmt section should only be used by trace_printk()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * in a debugging environment, as if anything exists in that section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * the trace_prink() helper buffers are allocated, which would just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * waste space in a production environment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * The __tracepoint_str sections on the other hand are used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * tracepoints which need to map pointers to their strings to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * the ASCII text for userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) last_index = start_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) start_index = __stop___tracepoint_str - __start___tracepoint_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (*pos < last_index + start_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return __start___tracepoint_str + (*pos - last_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) start_index += last_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return find_next_mod_format(start_index, v, fmt, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static void *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) t_start(struct seq_file *m, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) format_mod_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return find_next(NULL, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static void *t_next(struct seq_file *m, void * v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) (*pos)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return find_next(v, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static int t_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) const char **fmt = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) const char *str = *fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (!*fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) seq_printf(m, "0x%lx : \"", *(unsigned long *)fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * Tabs and new lines need to be converted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) for (i = 0; str[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) switch (str[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) case '\n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) seq_puts(m, "\\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) case '\t':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) seq_puts(m, "\\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) case '\\':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) seq_putc(m, '\\');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) case '"':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) seq_puts(m, "\\\"");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) seq_putc(m, str[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) seq_puts(m, "\"\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static void t_stop(struct seq_file *m, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) format_mod_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static const struct seq_operations show_format_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) .start = t_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) .next = t_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) .show = t_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) .stop = t_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) ftrace_formats_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ret = security_locked_down(LOCKDOWN_TRACEFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return seq_open(file, &show_format_seq_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static const struct file_operations ftrace_formats_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) .open = ftrace_formats_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) .read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) .llseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) .release = seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static __init int init_trace_printk_function_export(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ret = tracing_init_dentry();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) trace_create_file("printk_formats", 0444, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) NULL, &ftrace_formats_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) fs_initcall(init_trace_printk_function_export);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) static __init int init_trace_printk(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return register_module_notifier(&module_trace_bprintk_format_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) early_initcall(init_trace_printk);