trace_printk.c 8.3 KB

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