builtin-inject.c 5.6 KB

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