trace_printk.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * trace binary printk
  3. *
  4. * Copyright (C) 2008 Lai Jiangshan <laijs@cn.fujitsu.com>
  5. *
  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/marker.h>
  15. #include <linux/mutex.h>
  16. #include <linux/ctype.h>
  17. #include <linux/list.h>
  18. #include <linux/slab.h>
  19. #include <linux/fs.h>
  20. #include "trace.h"
  21. #ifdef CONFIG_MODULES
  22. /*
  23. * modules trace_printk()'s formats are autosaved in struct trace_bprintk_fmt
  24. * which are queued on trace_bprintk_fmt_list.
  25. */
  26. static LIST_HEAD(trace_bprintk_fmt_list);
  27. /* serialize accesses to trace_bprintk_fmt_list */
  28. static DEFINE_MUTEX(btrace_mutex);
  29. struct trace_bprintk_fmt {
  30. struct list_head list;
  31. char fmt[0];
  32. };
  33. static inline struct trace_bprintk_fmt *lookup_format(const char *fmt)
  34. {
  35. struct trace_bprintk_fmt *pos;
  36. list_for_each_entry(pos, &trace_bprintk_fmt_list, list) {
  37. if (!strcmp(pos->fmt, fmt))
  38. return pos;
  39. }
  40. return NULL;
  41. }
  42. static
  43. void hold_module_trace_bprintk_format(const char **start, const char **end)
  44. {
  45. const char **iter;
  46. mutex_lock(&btrace_mutex);
  47. for (iter = start; iter < end; iter++) {
  48. struct trace_bprintk_fmt *tb_fmt = lookup_format(*iter);
  49. if (tb_fmt) {
  50. *iter = tb_fmt->fmt;
  51. continue;
  52. }
  53. tb_fmt = kmalloc(offsetof(struct trace_bprintk_fmt, fmt)
  54. + strlen(*iter) + 1, GFP_KERNEL);
  55. if (tb_fmt) {
  56. list_add_tail(&tb_fmt->list, &trace_bprintk_fmt_list);
  57. strcpy(tb_fmt->fmt, *iter);
  58. *iter = tb_fmt->fmt;
  59. } else
  60. *iter = NULL;
  61. }
  62. mutex_unlock(&btrace_mutex);
  63. }
  64. static int module_trace_bprintk_format_notify(struct notifier_block *self,
  65. unsigned long val, void *data)
  66. {
  67. struct module *mod = data;
  68. if (mod->num_trace_bprintk_fmt) {
  69. const char **start = mod->trace_bprintk_fmt_start;
  70. const char **end = start + mod->num_trace_bprintk_fmt;
  71. if (val == MODULE_STATE_COMING)
  72. hold_module_trace_bprintk_format(start, end);
  73. }
  74. return 0;
  75. }
  76. #else /* !CONFIG_MODULES */
  77. __init static int
  78. module_trace_bprintk_format_notify(struct notifier_block *self,
  79. unsigned long val, void *data)
  80. {
  81. return 0;
  82. }
  83. #endif /* CONFIG_MODULES */
  84. __initdata_or_module static
  85. struct notifier_block module_trace_bprintk_format_nb = {
  86. .notifier_call = module_trace_bprintk_format_notify,
  87. };
  88. int __trace_bprintk(unsigned long ip, const char *fmt, ...)
  89. {
  90. int ret;
  91. va_list ap;
  92. if (unlikely(!fmt))
  93. return 0;
  94. if (!(trace_flags & TRACE_ITER_PRINTK))
  95. return 0;
  96. va_start(ap, fmt);
  97. ret = trace_vbprintk(ip, fmt, ap);
  98. va_end(ap);
  99. return ret;
  100. }
  101. EXPORT_SYMBOL_GPL(__trace_bprintk);
  102. int __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap)
  103. {
  104. if (unlikely(!fmt))
  105. return 0;
  106. if (!(trace_flags & TRACE_ITER_PRINTK))
  107. return 0;
  108. return trace_vbprintk(ip, fmt, ap);
  109. }
  110. EXPORT_SYMBOL_GPL(__ftrace_vbprintk);
  111. int __trace_printk(unsigned long ip, const char *fmt, ...)
  112. {
  113. int ret;
  114. va_list ap;
  115. if (!(trace_flags & TRACE_ITER_PRINTK))
  116. return 0;
  117. va_start(ap, fmt);
  118. ret = trace_vprintk(ip, fmt, ap);
  119. va_end(ap);
  120. return ret;
  121. }
  122. EXPORT_SYMBOL_GPL(__trace_printk);
  123. int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
  124. {
  125. if (!(trace_flags & TRACE_ITER_PRINTK))
  126. return 0;
  127. return trace_vprintk(ip, fmt, ap);
  128. }
  129. EXPORT_SYMBOL_GPL(__ftrace_vprintk);
  130. static void *
  131. t_next(struct seq_file *m, void *v, loff_t *pos)
  132. {
  133. const char **fmt = m->private;
  134. const char **next = fmt;
  135. (*pos)++;
  136. if ((unsigned long)fmt >= (unsigned long)__stop___trace_bprintk_fmt)
  137. return NULL;
  138. next = fmt;
  139. m->private = ++next;
  140. return fmt;
  141. }
  142. static void *t_start(struct seq_file *m, loff_t *pos)
  143. {
  144. return t_next(m, NULL, pos);
  145. }
  146. static int t_show(struct seq_file *m, void *v)
  147. {
  148. const char **fmt = v;
  149. const char *str = *fmt;
  150. int i;
  151. seq_printf(m, "0x%lx : \"", (unsigned long)fmt);
  152. /*
  153. * Tabs and new lines need to be converted.
  154. */
  155. for (i = 0; str[i]; i++) {
  156. switch (str[i]) {
  157. case '\n':
  158. seq_puts(m, "\\n");
  159. break;
  160. case '\t':
  161. seq_puts(m, "\\t");
  162. break;
  163. case '\\':
  164. seq_puts(m, "\\");
  165. break;
  166. case '"':
  167. seq_puts(m, "\\\"");
  168. break;
  169. default:
  170. seq_putc(m, str[i]);
  171. }
  172. }
  173. seq_puts(m, "\"\n");
  174. return 0;
  175. }
  176. static void t_stop(struct seq_file *m, void *p)
  177. {
  178. }
  179. static const struct seq_operations show_format_seq_ops = {
  180. .start = t_start,
  181. .next = t_next,
  182. .show = t_show,
  183. .stop = t_stop,
  184. };
  185. static int
  186. ftrace_formats_open(struct inode *inode, struct file *file)
  187. {
  188. int ret;
  189. ret = seq_open(file, &show_format_seq_ops);
  190. if (!ret) {
  191. struct seq_file *m = file->private_data;
  192. m->private = __start___trace_bprintk_fmt;
  193. }
  194. return ret;
  195. }
  196. static const struct file_operations ftrace_formats_fops = {
  197. .open = ftrace_formats_open,
  198. .read = seq_read,
  199. .llseek = seq_lseek,
  200. .release = seq_release,
  201. };
  202. static __init int init_trace_printk_function_export(void)
  203. {
  204. struct dentry *d_tracer;
  205. struct dentry *entry;
  206. d_tracer = tracing_init_dentry();
  207. if (!d_tracer)
  208. return 0;
  209. entry = debugfs_create_file("printk_formats", 0444, d_tracer,
  210. NULL, &ftrace_formats_fops);
  211. if (!entry)
  212. pr_warning("Could not create debugfs "
  213. "'printk_formats' entry\n");
  214. return 0;
  215. }
  216. fs_initcall(init_trace_printk_function_export);
  217. static __init int init_trace_printk(void)
  218. {
  219. return register_module_notifier(&module_trace_bprintk_format_nb);
  220. }
  221. early_initcall(init_trace_printk);