builtin-trace.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "builtin.h"
  2. #include "util/util.h"
  3. #include "util/cache.h"
  4. #include "util/symbol.h"
  5. #include "util/thread.h"
  6. #include "util/header.h"
  7. #include "util/parse-options.h"
  8. #include "perf.h"
  9. #include "util/debug.h"
  10. #include "util/trace-event.h"
  11. #include "util/data_map.h"
  12. static char const *input_name = "perf.data";
  13. static struct perf_header *header;
  14. static u64 sample_type;
  15. static int process_sample_event(event_t *event)
  16. {
  17. u64 ip = event->ip.ip;
  18. u64 timestamp = -1;
  19. u32 cpu = -1;
  20. u64 period = 1;
  21. void *more_data = event->ip.__more_data;
  22. struct thread *thread = threads__findnew(event->ip.pid);
  23. if (sample_type & PERF_SAMPLE_TIME) {
  24. timestamp = *(u64 *)more_data;
  25. more_data += sizeof(u64);
  26. }
  27. if (sample_type & PERF_SAMPLE_CPU) {
  28. cpu = *(u32 *)more_data;
  29. more_data += sizeof(u32);
  30. more_data += sizeof(u32); /* reserved */
  31. }
  32. if (sample_type & PERF_SAMPLE_PERIOD) {
  33. period = *(u64 *)more_data;
  34. more_data += sizeof(u64);
  35. }
  36. dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
  37. event->header.misc,
  38. event->ip.pid, event->ip.tid,
  39. (void *)(long)ip,
  40. (long long)period);
  41. if (thread == NULL) {
  42. pr_debug("problem processing %d event, skipping it.\n",
  43. event->header.type);
  44. return -1;
  45. }
  46. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  47. if (sample_type & PERF_SAMPLE_RAW) {
  48. struct {
  49. u32 size;
  50. char data[0];
  51. } *raw = more_data;
  52. /*
  53. * FIXME: better resolve from pid from the struct trace_entry
  54. * field, although it should be the same than this perf
  55. * event pid
  56. */
  57. print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
  58. }
  59. event__stats.total += period;
  60. return 0;
  61. }
  62. static int sample_type_check(u64 type)
  63. {
  64. sample_type = type;
  65. if (!(sample_type & PERF_SAMPLE_RAW)) {
  66. fprintf(stderr,
  67. "No trace sample to read. Did you call perf record "
  68. "without -R?");
  69. return -1;
  70. }
  71. return 0;
  72. }
  73. static struct perf_file_handler file_handler = {
  74. .process_sample_event = process_sample_event,
  75. .process_comm_event = event__process_comm,
  76. .sample_type_check = sample_type_check,
  77. };
  78. static int __cmd_trace(void)
  79. {
  80. register_idle_thread();
  81. register_perf_file_handler(&file_handler);
  82. return mmap_dispatch_perf_file(&header, input_name,
  83. 0, 0, &event__cwdlen, &event__cwd);
  84. }
  85. static const char * const annotate_usage[] = {
  86. "perf trace [<options>] <command>",
  87. NULL
  88. };
  89. static const struct option options[] = {
  90. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  91. "dump raw trace in ASCII"),
  92. OPT_BOOLEAN('v', "verbose", &verbose,
  93. "be more verbose (show symbol address, etc)"),
  94. OPT_BOOLEAN('l', "latency", &latency_format,
  95. "show latency attributes (irqs/preemption disabled, etc)"),
  96. OPT_END()
  97. };
  98. int cmd_trace(int argc, const char **argv, const char *prefix __used)
  99. {
  100. symbol__init(0);
  101. argc = parse_options(argc, argv, options, annotate_usage, 0);
  102. if (argc) {
  103. /*
  104. * Special case: if there's an argument left then assume tha
  105. * it's a symbol filter:
  106. */
  107. if (argc > 1)
  108. usage_with_options(annotate_usage, options);
  109. }
  110. setup_pager();
  111. return __cmd_trace();
  112. }