builtin-inject.c 6.2 KB

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