trace_printk.c 8.5 KB

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