builtin-inject.c 5.0 KB

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