builtin-inject.c 5.7 KB

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