trace_printk.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. /*
  81. * The debugfs/tracing/printk_formats file maps the addresses with
  82. * the ASCII formats that are used in the bprintk events in the
  83. * buffer. For userspace tools to be able to decode the events from
  84. * the buffer, they need to be able to map the address with the format.
  85. *
  86. * The addresses of the bprintk formats are in their own section
  87. * __trace_printk_fmt. But for modules we copy them into a link list.
  88. * The code to print the formats and their addresses passes around the
  89. * address of the fmt string. If the fmt address passed into the seq
  90. * functions is within the kernel core __trace_printk_fmt section, then
  91. * it simply uses the next pointer in the list.
  92. *
  93. * When the fmt pointer is outside the kernel core __trace_printk_fmt
  94. * section, then we need to read the link list pointers. The trick is
  95. * we pass the address of the string to the seq function just like
  96. * we do for the kernel core formats. To get back the structure that
  97. * holds the format, we simply use containerof() and then go to the
  98. * next format in the list.
  99. */
  100. static const char **
  101. find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos)
  102. {
  103. struct trace_bprintk_fmt *mod_fmt;
  104. if (list_empty(&trace_bprintk_fmt_list))
  105. return NULL;
  106. /*
  107. * v will point to the address of the fmt record from t_next
  108. * v will be NULL from t_start.
  109. * If this is the first pointer or called from start
  110. * then we need to walk the list.
  111. */
  112. if (!v || start_index == *pos) {
  113. struct trace_bprintk_fmt *p;
  114. /* search the module list */
  115. list_for_each_entry(p, &trace_bprintk_fmt_list, list) {
  116. if (start_index == *pos)
  117. return &p->fmt;
  118. start_index++;
  119. }
  120. /* pos > index */
  121. return NULL;
  122. }
  123. /*
  124. * v points to the address of the fmt field in the mod list
  125. * structure that holds the module print format.
  126. */
  127. mod_fmt = container_of(v, typeof(*mod_fmt), fmt);
  128. if (mod_fmt->list.next == &trace_bprintk_fmt_list)
  129. return NULL;
  130. mod_fmt = container_of(mod_fmt->list.next, typeof(*mod_fmt), list);
  131. return &mod_fmt->fmt;
  132. }
  133. static void format_mod_start(void)
  134. {
  135. mutex_lock(&btrace_mutex);
  136. }
  137. static void format_mod_stop(void)
  138. {
  139. mutex_unlock(&btrace_mutex);
  140. }
  141. #else /* !CONFIG_MODULES */
  142. __init static int
  143. module_trace_bprintk_format_notify(struct notifier_block *self,
  144. unsigned long val, void *data)
  145. {
  146. return 0;
  147. }
  148. static inline const char **
  149. find_next_mod_format(int start_index, void *v, const char **fmt, loff_t *pos)
  150. {
  151. return NULL;
  152. }
  153. static inline void format_mod_start(void) { }
  154. static inline void format_mod_stop(void) { }
  155. #endif /* CONFIG_MODULES */
  156. __initdata_or_module static
  157. struct notifier_block module_trace_bprintk_format_nb = {
  158. .notifier_call = module_trace_bprintk_format_notify,
  159. };
  160. int __trace_bprintk(unsigned long ip, const char *fmt, ...)
  161. {
  162. int ret;
  163. va_list ap;
  164. if (unlikely(!fmt))
  165. return 0;
  166. if (!(trace_flags & TRACE_ITER_PRINTK))
  167. return 0;
  168. va_start(ap, fmt);
  169. ret = trace_vbprintk(ip, fmt, ap);
  170. va_end(ap);
  171. return ret;
  172. }
  173. EXPORT_SYMBOL_GPL(__trace_bprintk);
  174. int __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap)
  175. {
  176. if (unlikely(!fmt))
  177. return 0;
  178. if (!(trace_flags & TRACE_ITER_PRINTK))
  179. return 0;
  180. return trace_vbprintk(ip, fmt, ap);
  181. }
  182. EXPORT_SYMBOL_GPL(__ftrace_vbprintk);
  183. int __trace_printk(unsigned long ip, const char *fmt, ...)
  184. {
  185. int ret;
  186. va_list ap;
  187. if (!(trace_flags & TRACE_ITER_PRINTK))
  188. return 0;
  189. va_start(ap, fmt);
  190. ret = trace_vprintk(ip, fmt, ap);
  191. va_end(ap);
  192. return ret;
  193. }
  194. EXPORT_SYMBOL_GPL(__trace_printk);
  195. int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
  196. {
  197. if (!(trace_flags & TRACE_ITER_PRINTK))
  198. return 0;
  199. return trace_vprintk(ip, fmt, ap);
  200. }
  201. EXPORT_SYMBOL_GPL(__ftrace_vprintk);
  202. static const char **find_next(void *v, loff_t *pos)
  203. {
  204. const char **fmt = v;
  205. int start_index;
  206. start_index = __stop___trace_bprintk_fmt - __start___trace_bprintk_fmt;
  207. if (*pos < start_index)
  208. return __start___trace_bprintk_fmt + *pos;
  209. return find_next_mod_format(start_index, v, fmt, pos);
  210. }
  211. static void *
  212. t_start(struct seq_file *m, loff_t *pos)
  213. {
  214. format_mod_start();
  215. return find_next(NULL, pos);
  216. }
  217. static void *t_next(struct seq_file *m, void * v, loff_t *pos)
  218. {
  219. (*pos)++;
  220. return find_next(v, pos);
  221. }
  222. static int t_show(struct seq_file *m, void *v)
  223. {
  224. const char **fmt = v;
  225. const char *str = *fmt;
  226. int i;
  227. seq_printf(m, "0x%lx : \"", *(unsigned long *)fmt);
  228. /*
  229. * Tabs and new lines need to be converted.
  230. */
  231. for (i = 0; str[i]; i++) {
  232. switch (str[i]) {
  233. case '\n':
  234. seq_puts(m, "\\n");
  235. break;
  236. case '\t':
  237. seq_puts(m, "\\t");
  238. break;
  239. case '\\':
  240. seq_puts(m, "\\");
  241. break;
  242. case '"':
  243. seq_puts(m, "\\\"");
  244. break;
  245. default:
  246. seq_putc(m, str[i]);
  247. }
  248. }
  249. seq_puts(m, "\"\n");
  250. return 0;
  251. }
  252. static void t_stop(struct seq_file *m, void *p)
  253. {
  254. format_mod_stop();
  255. }
  256. static const struct seq_operations show_format_seq_ops = {
  257. .start = t_start,
  258. .next = t_next,
  259. .show = t_show,
  260. .stop = t_stop,
  261. };
  262. static int
  263. ftrace_formats_open(struct inode *inode, struct file *file)
  264. {
  265. return seq_open(file, &show_format_seq_ops);
  266. }
  267. static const struct file_operations ftrace_formats_fops = {
  268. .open = ftrace_formats_open,
  269. .read = seq_read,
  270. .llseek = seq_lseek,
  271. .release = seq_release,
  272. };
  273. static __init int init_trace_printk_function_export(void)
  274. {
  275. struct dentry *d_tracer;
  276. d_tracer = tracing_init_dentry();
  277. if (!d_tracer)
  278. return 0;
  279. trace_create_file("printk_formats", 0444, d_tracer,
  280. NULL, &ftrace_formats_fops);
  281. return 0;
  282. }
  283. fs_initcall(init_trace_printk_function_export);
  284. static __init int init_trace_printk(void)
  285. {
  286. return register_module_notifier(&module_trace_bprintk_format_nb);
  287. }
  288. early_initcall(init_trace_printk);