trace_printk.c 8.9 KB

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