builtin-inject.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * builtin-inject.c
  3. *
  4. * Builtin inject command: Examine the live mode (stdin) event stream
  5. * and repipe it to stdout while optionally injecting additional
  6. * events into it.
  7. */
  8. #include "builtin.h"
  9. #include "perf.h"
  10. #include "util/session.h"
  11. #include "util/debug.h"
  12. #include "util/parse-options.h"
  13. static char const *input_name = "-";
  14. static bool inject_build_ids;
  15. static int perf_event__repipe_synth(union perf_event *event,
  16. struct perf_session *session __used)
  17. {
  18. uint32_t size;
  19. void *buf = event;
  20. size = event->header.size;
  21. while (size) {
  22. int ret = write(STDOUT_FILENO, buf, size);
  23. if (ret < 0)
  24. return -errno;
  25. size -= ret;
  26. buf += ret;
  27. }
  28. return 0;
  29. }
  30. static int perf_event__repipe(union perf_event *event,
  31. struct perf_sample *sample __used,
  32. struct perf_session *session)
  33. {
  34. return perf_event__repipe_synth(event, session);
  35. }
  36. static int perf_event__repipe_mmap(union perf_event *event,
  37. struct perf_sample *sample,
  38. struct perf_session *session)
  39. {
  40. int err;
  41. err = perf_event__process_mmap(event, sample, session);
  42. perf_event__repipe(event, sample, session);
  43. return err;
  44. }
  45. static int perf_event__repipe_task(union perf_event *event,
  46. struct perf_sample *sample,
  47. struct perf_session *session)
  48. {
  49. int err;
  50. err = perf_event__process_task(event, sample, session);
  51. perf_event__repipe(event, sample, session);
  52. return err;
  53. }
  54. static int perf_event__repipe_tracing_data(union perf_event *event,
  55. struct perf_session *session)
  56. {
  57. int err;
  58. perf_event__repipe_synth(event, session);
  59. err = perf_event__process_tracing_data(event, session);
  60. return err;
  61. }
  62. static int dso__read_build_id(struct dso *self)
  63. {
  64. if (self->has_build_id)
  65. return 0;
  66. if (filename__read_build_id(self->long_name, self->build_id,
  67. sizeof(self->build_id)) > 0) {
  68. self->has_build_id = true;
  69. return 0;
  70. }
  71. return -1;
  72. }
  73. static int dso__inject_build_id(struct dso *self, struct perf_session *session)
  74. {
  75. u16 misc = PERF_RECORD_MISC_USER;
  76. struct machine *machine;
  77. int err;
  78. if (dso__read_build_id(self) < 0) {
  79. pr_debug("no build_id found for %s\n", self->long_name);
  80. return -1;
  81. }
  82. machine = perf_session__find_host_machine(session);
  83. if (machine == NULL) {
  84. pr_err("Can't find machine for session\n");
  85. return -1;
  86. }
  87. if (self->kernel)
  88. misc = PERF_RECORD_MISC_KERNEL;
  89. err = perf_event__synthesize_build_id(self, misc, perf_event__repipe,
  90. machine, session);
  91. if (err) {
  92. pr_err("Can't synthesize build_id event for %s\n", self->long_name);
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. static int perf_event__inject_buildid(union perf_event *event,
  98. struct perf_sample *sample,
  99. struct perf_session *session)
  100. {
  101. struct addr_location al;
  102. struct thread *thread;
  103. u8 cpumode;
  104. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  105. thread = perf_session__findnew(session, event->ip.pid);
  106. if (thread == NULL) {
  107. pr_err("problem processing %d event, skipping it.\n",
  108. event->header.type);
  109. goto repipe;
  110. }
  111. thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
  112. event->ip.pid, event->ip.ip, &al);
  113. if (al.map != NULL) {
  114. if (!al.map->dso->hit) {
  115. al.map->dso->hit = 1;
  116. if (map__load(al.map, NULL) >= 0) {
  117. dso__inject_build_id(al.map->dso, session);
  118. /*
  119. * If this fails, too bad, let the other side
  120. * account this as unresolved.
  121. */
  122. } else
  123. pr_warning("no symbols found in %s, maybe "
  124. "install a debug package?\n",
  125. al.map->dso->long_name);
  126. }
  127. }
  128. repipe:
  129. perf_event__repipe(event, sample, session);
  130. return 0;
  131. }
  132. struct perf_event_ops inject_ops = {
  133. .sample = perf_event__repipe,
  134. .mmap = perf_event__repipe,
  135. .comm = perf_event__repipe,
  136. .fork = perf_event__repipe,
  137. .exit = perf_event__repipe,
  138. .lost = perf_event__repipe,
  139. .read = perf_event__repipe,
  140. .throttle = perf_event__repipe,
  141. .unthrottle = perf_event__repipe,
  142. .attr = perf_event__repipe_synth,
  143. .event_type = perf_event__repipe_synth,
  144. .tracing_data = perf_event__repipe_synth,
  145. .build_id = perf_event__repipe_synth,
  146. };
  147. extern volatile int session_done;
  148. static void sig_handler(int sig __attribute__((__unused__)))
  149. {
  150. session_done = 1;
  151. }
  152. static int __cmd_inject(void)
  153. {
  154. struct perf_session *session;
  155. int ret = -EINVAL;
  156. signal(SIGINT, sig_handler);
  157. if (inject_build_ids) {
  158. inject_ops.sample = perf_event__inject_buildid;
  159. inject_ops.mmap = perf_event__repipe_mmap;
  160. inject_ops.fork = perf_event__repipe_task;
  161. inject_ops.tracing_data = perf_event__repipe_tracing_data;
  162. }
  163. session = perf_session__new(input_name, O_RDONLY, false, true, &inject_ops);
  164. if (session == NULL)
  165. return -ENOMEM;
  166. ret = perf_session__process_events(session, &inject_ops);
  167. perf_session__delete(session);
  168. return ret;
  169. }
  170. static const char * const report_usage[] = {
  171. "perf inject [<options>]",
  172. NULL
  173. };
  174. static const struct option options[] = {
  175. OPT_BOOLEAN('b', "build-ids", &inject_build_ids,
  176. "Inject build-ids into the output stream"),
  177. OPT_INCR('v', "verbose", &verbose,
  178. "be more verbose (show build ids, etc)"),
  179. OPT_END()
  180. };
  181. int cmd_inject(int argc, const char **argv, const char *prefix __used)
  182. {
  183. argc = parse_options(argc, argv, options, report_usage, 0);
  184. /*
  185. * Any (unrecognized) arguments left?
  186. */
  187. if (argc)
  188. usage_with_options(report_usage, options);
  189. if (symbol__init() < 0)
  190. return -1;
  191. return __cmd_inject();
  192. }