trace_hw_branches.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * h/w branch tracer for x86 based on bts
  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. static void bts_trace_start_cpu(void *arg)
  20. {
  21. if (this_tracer)
  22. ds_release_bts(this_tracer);
  23. this_tracer =
  24. ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS,
  25. /* ovfl = */ NULL, /* th = */ (size_t)-1,
  26. BTS_KERNEL);
  27. if (IS_ERR(this_tracer)) {
  28. this_tracer = NULL;
  29. return;
  30. }
  31. }
  32. static void bts_trace_start(struct trace_array *tr)
  33. {
  34. int cpu;
  35. tracing_reset_online_cpus(tr);
  36. for_each_cpu(cpu, cpu_possible_mask)
  37. smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
  38. }
  39. static void bts_trace_stop_cpu(void *arg)
  40. {
  41. if (this_tracer) {
  42. ds_release_bts(this_tracer);
  43. this_tracer = NULL;
  44. }
  45. }
  46. static void bts_trace_stop(struct trace_array *tr)
  47. {
  48. int cpu;
  49. for_each_cpu(cpu, cpu_possible_mask)
  50. smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
  51. }
  52. static int bts_trace_init(struct trace_array *tr)
  53. {
  54. tracing_reset_online_cpus(tr);
  55. bts_trace_start(tr);
  56. return 0;
  57. }
  58. static void bts_trace_print_header(struct seq_file *m)
  59. {
  60. seq_puts(m,
  61. "# CPU# FROM TO FUNCTION\n");
  62. seq_puts(m,
  63. "# | | | |\n");
  64. }
  65. static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
  66. {
  67. struct trace_entry *entry = iter->ent;
  68. struct trace_seq *seq = &iter->seq;
  69. struct hw_branch_entry *it;
  70. trace_assign_type(it, entry);
  71. if (entry->type == TRACE_HW_BRANCHES) {
  72. if (trace_seq_printf(seq, "%4d ", entry->cpu) &&
  73. trace_seq_printf(seq, "0x%016llx -> 0x%016llx ",
  74. it->from, it->to) &&
  75. (!it->from ||
  76. seq_print_ip_sym(seq, it->from, /* sym_flags = */ 0)) &&
  77. trace_seq_printf(seq, "\n"))
  78. return TRACE_TYPE_HANDLED;
  79. return TRACE_TYPE_PARTIAL_LINE;;
  80. }
  81. return TRACE_TYPE_UNHANDLED;
  82. }
  83. void trace_hw_branch(struct trace_array *tr, u64 from, u64 to)
  84. {
  85. struct ring_buffer_event *event;
  86. struct hw_branch_entry *entry;
  87. unsigned long irq;
  88. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), &irq);
  89. if (!event)
  90. return;
  91. entry = ring_buffer_event_data(event);
  92. tracing_generic_entry_update(&entry->ent, 0, from);
  93. entry->ent.type = TRACE_HW_BRANCHES;
  94. entry->ent.cpu = smp_processor_id();
  95. entry->from = from;
  96. entry->to = to;
  97. ring_buffer_unlock_commit(tr->buffer, event, irq);
  98. }
  99. static void trace_bts_at(struct trace_array *tr,
  100. const struct bts_trace *trace, void *at)
  101. {
  102. struct bts_struct bts;
  103. int err = 0;
  104. WARN_ON_ONCE(!trace->read);
  105. if (!trace->read)
  106. return;
  107. err = trace->read(this_tracer, at, &bts);
  108. if (err < 0)
  109. return;
  110. switch (bts.qualifier) {
  111. case BTS_BRANCH:
  112. trace_hw_branch(tr, bts.variant.lbr.from, bts.variant.lbr.to);
  113. break;
  114. }
  115. }
  116. static void trace_bts_cpu(void *arg)
  117. {
  118. struct trace_array *tr = (struct trace_array *) arg;
  119. const struct bts_trace *trace;
  120. unsigned char *at;
  121. if (!this_tracer)
  122. return;
  123. ds_suspend_bts(this_tracer);
  124. trace = ds_read_bts(this_tracer);
  125. if (!trace)
  126. goto out;
  127. for (at = trace->ds.top; (void *)at < trace->ds.end;
  128. at += trace->ds.size)
  129. trace_bts_at(tr, trace, at);
  130. for (at = trace->ds.begin; (void *)at < trace->ds.top;
  131. at += trace->ds.size)
  132. trace_bts_at(tr, trace, at);
  133. out:
  134. ds_resume_bts(this_tracer);
  135. }
  136. static void trace_bts_prepare(struct trace_iterator *iter)
  137. {
  138. int cpu;
  139. for_each_cpu(cpu, cpu_possible_mask)
  140. smp_call_function_single(cpu, trace_bts_cpu, iter->tr, 1);
  141. }
  142. struct tracer bts_tracer __read_mostly =
  143. {
  144. .name = "hw-branch-tracer",
  145. .init = bts_trace_init,
  146. .reset = bts_trace_stop,
  147. .print_header = bts_trace_print_header,
  148. .print_line = bts_trace_print_line,
  149. .start = bts_trace_start,
  150. .stop = bts_trace_stop,
  151. .open = trace_bts_prepare
  152. };
  153. __init static int init_bts_trace(void)
  154. {
  155. return register_tracer(&bts_tracer);
  156. }
  157. device_initcall(init_bts_trace);