builtin-inject.c 4.7 KB

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