trace_hw_branches.c 4.2 KB

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