trace_printk.c 7.6 KB

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