trace_bts.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * BTS tracer
  3. *
  4. * Copyright (C) 2008 Markus Metzger <markus.t.metzger@gmail.com>
  5. *
  6. */
  7. #include <linux/module.h>
  8. #include <linux/fs.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/ftrace.h>
  11. #include <linux/kallsyms.h>
  12. #include <asm/ds.h>
  13. #include "trace.h"
  14. #define SIZEOF_BTS (1 << 13)
  15. static DEFINE_PER_CPU(struct bts_tracer *, tracer);
  16. static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer);
  17. #define this_tracer per_cpu(tracer, smp_processor_id())
  18. #define this_buffer per_cpu(buffer, smp_processor_id())
  19. /*
  20. * Information to interpret a BTS record.
  21. * This will go into an in-kernel BTS interface.
  22. */
  23. static unsigned char sizeof_field;
  24. static unsigned long debugctl_mask;
  25. #define sizeof_bts (3 * sizeof_field)
  26. static void bts_trace_cpuinit(struct cpuinfo_x86 *c)
  27. {
  28. switch (c->x86) {
  29. case 0x6:
  30. switch (c->x86_model) {
  31. case 0x0 ... 0xC:
  32. break;
  33. case 0xD:
  34. case 0xE: /* Pentium M */
  35. sizeof_field = sizeof(long);
  36. debugctl_mask = (1<<6)|(1<<7);
  37. break;
  38. default:
  39. sizeof_field = 8;
  40. debugctl_mask = (1<<6)|(1<<7);
  41. break;
  42. }
  43. break;
  44. case 0xF:
  45. switch (c->x86_model) {
  46. case 0x0:
  47. case 0x1:
  48. case 0x2: /* Netburst */
  49. sizeof_field = sizeof(long);
  50. debugctl_mask = (1<<2)|(1<<3);
  51. break;
  52. default:
  53. /* sorry, don't know about them */
  54. break;
  55. }
  56. break;
  57. default:
  58. /* sorry, don't know about them */
  59. break;
  60. }
  61. }
  62. static inline void bts_enable(void)
  63. {
  64. unsigned long debugctl;
  65. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  66. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl | debugctl_mask);
  67. }
  68. static inline void bts_disable(void)
  69. {
  70. unsigned long debugctl;
  71. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  72. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl & ~debugctl_mask);
  73. }
  74. static void bts_trace_reset(struct trace_array *tr)
  75. {
  76. int cpu;
  77. tr->time_start = ftrace_now(tr->cpu);
  78. for_each_online_cpu(cpu)
  79. tracing_reset(tr, cpu);
  80. }
  81. static void bts_trace_start_cpu(void *arg)
  82. {
  83. this_tracer =
  84. ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS,
  85. /* ovfl = */ NULL, /* th = */ (size_t)-1);
  86. if (IS_ERR(this_tracer)) {
  87. this_tracer = NULL;
  88. return;
  89. }
  90. bts_enable();
  91. }
  92. static void bts_trace_start(struct trace_array *tr)
  93. {
  94. int cpu;
  95. bts_trace_reset(tr);
  96. for_each_cpu_mask(cpu, cpu_possible_map)
  97. smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
  98. }
  99. static void bts_trace_stop_cpu(void *arg)
  100. {
  101. if (this_tracer) {
  102. bts_disable();
  103. ds_release_bts(this_tracer);
  104. this_tracer = NULL;
  105. }
  106. }
  107. static void bts_trace_stop(struct trace_array *tr)
  108. {
  109. int cpu;
  110. for_each_cpu_mask(cpu, cpu_possible_map)
  111. smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
  112. }
  113. static int bts_trace_init(struct trace_array *tr)
  114. {
  115. bts_trace_cpuinit(&boot_cpu_data);
  116. bts_trace_reset(tr);
  117. bts_trace_start(tr);
  118. return 0;
  119. }
  120. static void bts_trace_print_header(struct seq_file *m)
  121. {
  122. #ifdef __i386__
  123. seq_puts(m, "# CPU# FROM TO FUNCTION\n");
  124. seq_puts(m, "# | | | |\n");
  125. #else
  126. seq_puts(m,
  127. "# CPU# FROM TO FUNCTION\n");
  128. seq_puts(m,
  129. "# | | | |\n");
  130. #endif
  131. }
  132. static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
  133. {
  134. struct trace_entry *entry = iter->ent;
  135. struct trace_seq *seq = &iter->seq;
  136. struct bts_entry *it;
  137. trace_assign_type(it, entry);
  138. if (entry->type == TRACE_BTS) {
  139. int ret;
  140. #ifdef CONFIG_KALLSYMS
  141. char function[KSYM_SYMBOL_LEN];
  142. sprint_symbol(function, it->from);
  143. #else
  144. char *function = "<unknown>";
  145. #endif
  146. ret = trace_seq_printf(seq, "%4d 0x%lx -> 0x%lx [%s]\n",
  147. entry->cpu, it->from, it->to, function);
  148. if (!ret)
  149. return TRACE_TYPE_PARTIAL_LINE;;
  150. return TRACE_TYPE_HANDLED;
  151. }
  152. return TRACE_TYPE_UNHANDLED;
  153. }
  154. void trace_bts(struct trace_array *tr, unsigned long from, unsigned long to)
  155. {
  156. struct ring_buffer_event *event;
  157. struct bts_entry *entry;
  158. unsigned long irq;
  159. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), &irq);
  160. if (!event)
  161. return;
  162. entry = ring_buffer_event_data(event);
  163. tracing_generic_entry_update(&entry->ent, 0, from);
  164. entry->ent.type = TRACE_BTS;
  165. entry->ent.cpu = smp_processor_id();
  166. entry->from = from;
  167. entry->to = to;
  168. ring_buffer_unlock_commit(tr->buffer, event, irq);
  169. }
  170. static void trace_bts_at(struct trace_array *tr, size_t index)
  171. {
  172. const void *raw = NULL;
  173. unsigned long from, to;
  174. int err;
  175. err = ds_access_bts(this_tracer, index, &raw);
  176. if (err < 0)
  177. return;
  178. from = *(const unsigned long *)raw;
  179. to = *(const unsigned long *)((const char *)raw + sizeof_field);
  180. trace_bts(tr, from, to);
  181. }
  182. static void trace_bts_cpu(void *arg)
  183. {
  184. struct trace_array *tr = (struct trace_array *) arg;
  185. size_t index = 0, end = 0, i;
  186. int err;
  187. if (!this_tracer)
  188. return;
  189. bts_disable();
  190. err = ds_get_bts_index(this_tracer, &index);
  191. if (err < 0)
  192. goto out;
  193. err = ds_get_bts_end(this_tracer, &end);
  194. if (err < 0)
  195. goto out;
  196. for (i = index; i < end; i++)
  197. trace_bts_at(tr, i);
  198. for (i = 0; i < index; i++)
  199. trace_bts_at(tr, i);
  200. out:
  201. bts_enable();
  202. }
  203. static void trace_bts_prepare(struct trace_iterator *iter)
  204. {
  205. int cpu;
  206. for_each_cpu_mask(cpu, cpu_possible_map)
  207. smp_call_function_single(cpu, trace_bts_cpu, iter->tr, 1);
  208. }
  209. struct tracer bts_tracer __read_mostly =
  210. {
  211. .name = "bts",
  212. .init = bts_trace_init,
  213. .reset = bts_trace_stop,
  214. .print_header = bts_trace_print_header,
  215. .print_line = bts_trace_print_line,
  216. .start = bts_trace_start,
  217. .stop = bts_trace_stop,
  218. .open = trace_bts_prepare
  219. };
  220. __init static int init_bts_trace(void)
  221. {
  222. return register_tracer(&bts_tracer);
  223. }
  224. device_initcall(init_bts_trace);