builtin-inject.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/tool.h"
  12. #include "util/debug.h"
  13. #include "util/parse-options.h"
  14. struct perf_inject {
  15. struct perf_tool tool;
  16. bool build_ids;
  17. const char *input_name;
  18. int pipe_output,
  19. output;
  20. u64 bytes_written;
  21. };
  22. static int perf_event__repipe_synth(struct perf_tool *tool,
  23. union perf_event *event,
  24. struct machine *machine __maybe_unused)
  25. {
  26. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  27. uint32_t size;
  28. void *buf = event;
  29. size = event->header.size;
  30. while (size) {
  31. int ret = write(inject->output, buf, size);
  32. if (ret < 0)
  33. return -errno;
  34. size -= ret;
  35. buf += ret;
  36. inject->bytes_written += ret;
  37. }
  38. return 0;
  39. }
  40. static int perf_event__repipe_op2_synth(struct perf_tool *tool,
  41. union perf_event *event,
  42. struct perf_session *session
  43. __maybe_unused)
  44. {
  45. return perf_event__repipe_synth(tool, event, NULL);
  46. }
  47. static int perf_event__repipe_event_type_synth(struct perf_tool *tool,
  48. union perf_event *event)
  49. {
  50. return perf_event__repipe_synth(tool, event, NULL);
  51. }
  52. static int perf_event__repipe_tracing_data_synth(union perf_event *event,
  53. struct perf_session *session
  54. __maybe_unused)
  55. {
  56. return perf_event__repipe_synth(NULL, event, NULL);
  57. }
  58. static int perf_event__repipe_attr(union perf_event *event,
  59. struct perf_evlist **pevlist __maybe_unused)
  60. {
  61. int ret;
  62. ret = perf_event__process_attr(event, pevlist);
  63. if (ret)
  64. return ret;
  65. return perf_event__repipe_synth(NULL, event, NULL);
  66. }
  67. static int perf_event__repipe(struct perf_tool *tool,
  68. union perf_event *event,
  69. struct perf_sample *sample __maybe_unused,
  70. struct machine *machine)
  71. {
  72. return perf_event__repipe_synth(tool, event, machine);
  73. }
  74. static int perf_event__repipe_sample(struct perf_tool *tool,
  75. union perf_event *event,
  76. struct perf_sample *sample __maybe_unused,
  77. struct perf_evsel *evsel __maybe_unused,
  78. struct machine *machine)
  79. {
  80. return perf_event__repipe_synth(tool, event, machine);
  81. }
  82. static int perf_event__repipe_mmap(struct perf_tool *tool,
  83. union perf_event *event,
  84. struct perf_sample *sample,
  85. struct machine *machine)
  86. {
  87. int err;
  88. err = perf_event__process_mmap(tool, event, sample, machine);
  89. perf_event__repipe(tool, event, sample, machine);
  90. return err;
  91. }
  92. static int perf_event__repipe_fork(struct perf_tool *tool,
  93. union perf_event *event,
  94. struct perf_sample *sample,
  95. struct machine *machine)
  96. {
  97. int err;
  98. err = perf_event__process_fork(tool, event, sample, machine);
  99. perf_event__repipe(tool, event, sample, machine);
  100. return err;
  101. }
  102. static int perf_event__repipe_tracing_data(union perf_event *event,
  103. struct perf_session *session)
  104. {
  105. int err;
  106. perf_event__repipe_synth(NULL, event, NULL);
  107. err = perf_event__process_tracing_data(event, session);
  108. return err;
  109. }
  110. static int dso__read_build_id(struct dso *self)
  111. {
  112. if (self->has_build_id)
  113. return 0;
  114. if (filename__read_build_id(self->long_name, self->build_id,
  115. sizeof(self->build_id)) > 0) {
  116. self->has_build_id = true;
  117. return 0;
  118. }
  119. return -1;
  120. }
  121. static int dso__inject_build_id(struct dso *self, struct perf_tool *tool,
  122. struct machine *machine)
  123. {
  124. u16 misc = PERF_RECORD_MISC_USER;
  125. int err;
  126. if (dso__read_build_id(self) < 0) {
  127. pr_debug("no build_id found for %s\n", self->long_name);
  128. return -1;
  129. }
  130. if (self->kernel)
  131. misc = PERF_RECORD_MISC_KERNEL;
  132. err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe,
  133. machine);
  134. if (err) {
  135. pr_err("Can't synthesize build_id event for %s\n", self->long_name);
  136. return -1;
  137. }
  138. return 0;
  139. }
  140. static int perf_event__inject_buildid(struct perf_tool *tool,
  141. union perf_event *event,
  142. struct perf_sample *sample,
  143. struct perf_evsel *evsel __maybe_unused,
  144. struct machine *machine)
  145. {
  146. struct addr_location al;
  147. struct thread *thread;
  148. u8 cpumode;
  149. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  150. thread = machine__findnew_thread(machine, event->ip.pid);
  151. if (thread == NULL) {
  152. pr_err("problem processing %d event, skipping it.\n",
  153. event->header.type);
  154. goto repipe;
  155. }
  156. thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
  157. event->ip.ip, &al);
  158. if (al.map != NULL) {
  159. if (!al.map->dso->hit) {
  160. al.map->dso->hit = 1;
  161. if (map__load(al.map, NULL) >= 0) {
  162. dso__inject_build_id(al.map->dso, tool, machine);
  163. /*
  164. * If this fails, too bad, let the other side
  165. * account this as unresolved.
  166. */
  167. } else {
  168. #ifdef LIBELF_SUPPORT
  169. pr_warning("no symbols found in %s, maybe "
  170. "install a debug package?\n",
  171. al.map->dso->long_name);
  172. #endif
  173. }
  174. }
  175. }
  176. repipe:
  177. perf_event__repipe(tool, event, sample, machine);
  178. return 0;
  179. }
  180. extern volatile int session_done;
  181. static void sig_handler(int sig __maybe_unused)
  182. {
  183. session_done = 1;
  184. }
  185. static int __cmd_inject(struct perf_inject *inject)
  186. {
  187. struct perf_session *session;
  188. int ret = -EINVAL;
  189. signal(SIGINT, sig_handler);
  190. if (inject->build_ids) {
  191. inject->tool.sample = perf_event__inject_buildid;
  192. inject->tool.mmap = perf_event__repipe_mmap;
  193. inject->tool.fork = perf_event__repipe_fork;
  194. inject->tool.tracing_data = perf_event__repipe_tracing_data;
  195. }
  196. session = perf_session__new(inject->input_name, O_RDONLY, false, true, &inject->tool);
  197. if (session == NULL)
  198. return -ENOMEM;
  199. if (!inject->pipe_output)
  200. lseek(inject->output, session->header.data_offset, SEEK_SET);
  201. ret = perf_session__process_events(session, &inject->tool);
  202. if (!inject->pipe_output) {
  203. session->header.data_size = inject->bytes_written;
  204. perf_session__write_header(session, session->evlist, inject->output, true);
  205. }
  206. perf_session__delete(session);
  207. return ret;
  208. }
  209. int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
  210. {
  211. struct perf_inject inject = {
  212. .tool = {
  213. .sample = perf_event__repipe_sample,
  214. .mmap = perf_event__repipe,
  215. .comm = perf_event__repipe,
  216. .fork = perf_event__repipe,
  217. .exit = perf_event__repipe,
  218. .lost = perf_event__repipe,
  219. .read = perf_event__repipe_sample,
  220. .throttle = perf_event__repipe,
  221. .unthrottle = perf_event__repipe,
  222. .attr = perf_event__repipe_attr,
  223. .event_type = perf_event__repipe_event_type_synth,
  224. .tracing_data = perf_event__repipe_tracing_data_synth,
  225. .build_id = perf_event__repipe_op2_synth,
  226. },
  227. .input_name = "-",
  228. };
  229. const char *output_name = "-";
  230. const struct option options[] = {
  231. OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
  232. "Inject build-ids into the output stream"),
  233. OPT_STRING('i', "input", &inject.input_name, "file",
  234. "input file name"),
  235. OPT_STRING('o', "output", &output_name, "file",
  236. "output file name"),
  237. OPT_INCR('v', "verbose", &verbose,
  238. "be more verbose (show build ids, etc)"),
  239. OPT_END()
  240. };
  241. const char * const inject_usage[] = {
  242. "perf inject [<options>]",
  243. NULL
  244. };
  245. argc = parse_options(argc, argv, options, inject_usage, 0);
  246. /*
  247. * Any (unrecognized) arguments left?
  248. */
  249. if (argc)
  250. usage_with_options(inject_usage, options);
  251. if (!strcmp(output_name, "-")) {
  252. inject.pipe_output = 1;
  253. inject.output = STDOUT_FILENO;
  254. } else {
  255. inject.output = open(output_name, O_CREAT | O_WRONLY | O_TRUNC,
  256. S_IRUSR | S_IWUSR);
  257. if (inject.output < 0) {
  258. perror("failed to create output file");
  259. return -1;
  260. }
  261. }
  262. if (symbol__init() < 0)
  263. return -1;
  264. return __cmd_inject(&inject);
  265. }