trace_printk.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_start(struct seq_file *m, loff_t *pos)
  132. {
  133. const char **fmt = __start___trace_bprintk_fmt + *pos;
  134. if ((unsigned long)fmt >= (unsigned long)__stop___trace_bprintk_fmt)
  135. return NULL;
  136. return fmt;
  137. }
  138. static void *t_next(struct seq_file *m, void * v, loff_t *pos)
  139. {
  140. (*pos)++;
  141. return t_start(m, pos);
  142. }
  143. static int t_show(struct seq_file *m, void *v)
  144. {
  145. const char **fmt = v;
  146. const char *str = *fmt;
  147. int i;
  148. seq_printf(m, "0x%lx : \"", (unsigned long)fmt);
  149. /*
  150. * Tabs and new lines need to be converted.
  151. */
  152. for (i = 0; str[i]; i++) {
  153. switch (str[i]) {
  154. case '\n':
  155. seq_puts(m, "\\n");
  156. break;
  157. case '\t':
  158. seq_puts(m, "\\t");
  159. break;
  160. case '\\':
  161. seq_puts(m, "\\");
  162. break;
  163. case '"':
  164. seq_puts(m, "\\\"");
  165. break;
  166. default:
  167. seq_putc(m, str[i]);
  168. }
  169. }
  170. seq_puts(m, "\"\n");
  171. return 0;
  172. }
  173. static void t_stop(struct seq_file *m, void *p)
  174. {
  175. }
  176. static const struct seq_operations show_format_seq_ops = {
  177. .start = t_start,
  178. .next = t_next,
  179. .show = t_show,
  180. .stop = t_stop,
  181. };
  182. static int
  183. ftrace_formats_open(struct inode *inode, struct file *file)
  184. {
  185. return seq_open(file, &show_format_seq_ops);
  186. }
  187. static const struct file_operations ftrace_formats_fops = {
  188. .open = ftrace_formats_open,
  189. .read = seq_read,
  190. .llseek = seq_lseek,
  191. .release = seq_release,
  192. };
  193. static __init int init_trace_printk_function_export(void)
  194. {
  195. struct dentry *d_tracer;
  196. d_tracer = tracing_init_dentry();
  197. if (!d_tracer)
  198. return 0;
  199. trace_create_file("printk_formats", 0444, d_tracer,
  200. NULL, &ftrace_formats_fops);
  201. return 0;
  202. }
  203. fs_initcall(init_trace_printk_function_export);
  204. static __init int init_trace_printk(void)
  205. {
  206. return register_module_notifier(&module_trace_bprintk_format_nb);
  207. }
  208. early_initcall(init_trace_printk);