trace_printk.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/mutex.h>
  15. #include <linux/ctype.h>
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/fs.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. list_for_each_entry(pos, &trace_bprintk_fmt_list, list) {
  36. if (!strcmp(pos->fmt, fmt))
  37. return pos;
  38. }
  39. return NULL;
  40. }
  41. static
  42. void hold_module_trace_bprintk_format(const char **start, const char **end)
  43. {
  44. const char **iter;
  45. char *fmt;
  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(sizeof(*tb_fmt), GFP_KERNEL);
  54. if (tb_fmt)
  55. fmt = kmalloc(strlen(*iter) + 1, GFP_KERNEL);
  56. if (tb_fmt && fmt) {
  57. list_add_tail(&tb_fmt->list, &trace_bprintk_fmt_list);
  58. strcpy(fmt, *iter);
  59. tb_fmt->fmt = fmt;
  60. *iter = tb_fmt->fmt;
  61. } else {
  62. kfree(tb_fmt);
  63. *iter = NULL;
  64. }
  65. }
  66. mutex_unlock(&btrace_mutex);
  67. }
  68. static int module_trace_bprintk_format_notify(struct notifier_block *self,
  69. unsigned long val, void *data)
  70. {
  71. struct module *mod = data;
  72. if (mod->num_trace_bprintk_fmt) {
  73. const char **start = mod->trace_bprintk_fmt_start;
  74. const char **end = start + mod->num_trace_bprintk_fmt;
  75. if (val == MODULE_STATE_COMING)
  76. hold_module_trace_bprintk_format(start, end);
  77. }
  78. return 0;
  79. }
  80. #else /* !CONFIG_MODULES */
  81. __init static int
  82. module_trace_bprintk_format_notify(struct notifier_block *self,
  83. unsigned long val, void *data)
  84. {
  85. return 0;
  86. }
  87. #endif /* CONFIG_MODULES */
  88. __initdata_or_module static
  89. struct notifier_block module_trace_bprintk_format_nb = {
  90. .notifier_call = module_trace_bprintk_format_notify,
  91. };
  92. int __trace_bprintk(unsigned long ip, const char *fmt, ...)
  93. {
  94. int ret;
  95. va_list ap;
  96. if (unlikely(!fmt))
  97. return 0;
  98. if (!(trace_flags & TRACE_ITER_PRINTK))
  99. return 0;
  100. va_start(ap, fmt);
  101. ret = trace_vbprintk(ip, fmt, ap);
  102. va_end(ap);
  103. return ret;
  104. }
  105. EXPORT_SYMBOL_GPL(__trace_bprintk);
  106. int __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap)
  107. {
  108. if (unlikely(!fmt))
  109. return 0;
  110. if (!(trace_flags & TRACE_ITER_PRINTK))
  111. return 0;
  112. return trace_vbprintk(ip, fmt, ap);
  113. }
  114. EXPORT_SYMBOL_GPL(__ftrace_vbprintk);
  115. int __trace_printk(unsigned long ip, const char *fmt, ...)
  116. {
  117. int ret;
  118. va_list ap;
  119. if (!(trace_flags & TRACE_ITER_PRINTK))
  120. return 0;
  121. va_start(ap, fmt);
  122. ret = trace_vprintk(ip, fmt, ap);
  123. va_end(ap);
  124. return ret;
  125. }
  126. EXPORT_SYMBOL_GPL(__trace_printk);
  127. int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
  128. {
  129. if (!(trace_flags & TRACE_ITER_PRINTK))
  130. return 0;
  131. return trace_vprintk(ip, fmt, ap);
  132. }
  133. EXPORT_SYMBOL_GPL(__ftrace_vprintk);
  134. static void *
  135. t_start(struct seq_file *m, loff_t *pos)
  136. {
  137. const char **fmt = __start___trace_bprintk_fmt + *pos;
  138. if ((unsigned long)fmt >= (unsigned long)__stop___trace_bprintk_fmt)
  139. return NULL;
  140. return fmt;
  141. }
  142. static void *t_next(struct seq_file *m, void * v, loff_t *pos)
  143. {
  144. (*pos)++;
  145. return t_start(m, pos);
  146. }
  147. static int t_show(struct seq_file *m, void *v)
  148. {
  149. const char **fmt = v;
  150. const char *str = *fmt;
  151. int i;
  152. seq_printf(m, "0x%lx : \"", *(unsigned long *)fmt);
  153. /*
  154. * Tabs and new lines need to be converted.
  155. */
  156. for (i = 0; str[i]; i++) {
  157. switch (str[i]) {
  158. case '\n':
  159. seq_puts(m, "\\n");
  160. break;
  161. case '\t':
  162. seq_puts(m, "\\t");
  163. break;
  164. case '\\':
  165. seq_puts(m, "\\");
  166. break;
  167. case '"':
  168. seq_puts(m, "\\\"");
  169. break;
  170. default:
  171. seq_putc(m, str[i]);
  172. }
  173. }
  174. seq_puts(m, "\"\n");
  175. return 0;
  176. }
  177. static void t_stop(struct seq_file *m, void *p)
  178. {
  179. }
  180. static const struct seq_operations show_format_seq_ops = {
  181. .start = t_start,
  182. .next = t_next,
  183. .show = t_show,
  184. .stop = t_stop,
  185. };
  186. static int
  187. ftrace_formats_open(struct inode *inode, struct file *file)
  188. {
  189. return seq_open(file, &show_format_seq_ops);
  190. }
  191. static const struct file_operations ftrace_formats_fops = {
  192. .open = ftrace_formats_open,
  193. .read = seq_read,
  194. .llseek = seq_lseek,
  195. .release = seq_release,
  196. };
  197. static __init int init_trace_printk_function_export(void)
  198. {
  199. struct dentry *d_tracer;
  200. d_tracer = tracing_init_dentry();
  201. if (!d_tracer)
  202. return 0;
  203. trace_create_file("printk_formats", 0444, d_tracer,
  204. NULL, &ftrace_formats_fops);
  205. return 0;
  206. }
  207. fs_initcall(init_trace_printk_function_export);
  208. static __init int init_trace_printk(void)
  209. {
  210. return register_module_notifier(&module_trace_bprintk_format_nb);
  211. }
  212. early_initcall(init_trace_printk);