builtin-inject.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/color.h"
  11. #include "util/evlist.h"
  12. #include "util/evsel.h"
  13. #include "util/session.h"
  14. #include "util/tool.h"
  15. #include "util/debug.h"
  16. #include "util/build-id.h"
  17. #include "util/data.h"
  18. #include "util/parse-options.h"
  19. #include <linux/list.h>
  20. struct perf_inject {
  21. struct perf_tool tool;
  22. bool build_ids;
  23. bool sched_stat;
  24. const char *input_name;
  25. int pipe_output,
  26. output;
  27. u64 bytes_written;
  28. struct list_head samples;
  29. };
  30. struct event_entry {
  31. struct list_head node;
  32. u32 tid;
  33. union perf_event event[0];
  34. };
  35. static int perf_event__repipe_synth(struct perf_tool *tool,
  36. union perf_event *event)
  37. {
  38. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  39. uint32_t size;
  40. void *buf = event;
  41. size = event->header.size;
  42. while (size) {
  43. int ret = write(inject->output, buf, size);
  44. if (ret < 0)
  45. return -errno;
  46. size -= ret;
  47. buf += ret;
  48. inject->bytes_written += ret;
  49. }
  50. return 0;
  51. }
  52. static int perf_event__repipe_op2_synth(struct perf_tool *tool,
  53. union perf_event *event,
  54. struct perf_session *session
  55. __maybe_unused)
  56. {
  57. return perf_event__repipe_synth(tool, event);
  58. }
  59. static int perf_event__repipe_attr(struct perf_tool *tool,
  60. union perf_event *event,
  61. struct perf_evlist **pevlist)
  62. {
  63. int ret;
  64. ret = perf_event__process_attr(tool, event, pevlist);
  65. if (ret)
  66. return ret;
  67. return perf_event__repipe_synth(tool, event);
  68. }
  69. static int perf_event__repipe(struct perf_tool *tool,
  70. union perf_event *event,
  71. struct perf_sample *sample __maybe_unused,
  72. struct machine *machine __maybe_unused)
  73. {
  74. return perf_event__repipe_synth(tool, event);
  75. }
  76. typedef int (*inject_handler)(struct perf_tool *tool,
  77. union perf_event *event,
  78. struct perf_sample *sample,
  79. struct perf_evsel *evsel,
  80. struct machine *machine);
  81. static int perf_event__repipe_sample(struct perf_tool *tool,
  82. union perf_event *event,
  83. struct perf_sample *sample,
  84. struct perf_evsel *evsel,
  85. struct machine *machine)
  86. {
  87. if (evsel->handler.func) {
  88. inject_handler f = evsel->handler.func;
  89. return f(tool, event, sample, evsel, machine);
  90. }
  91. build_id__mark_dso_hit(tool, event, sample, evsel, machine);
  92. return perf_event__repipe_synth(tool, event);
  93. }
  94. static int perf_event__repipe_mmap(struct perf_tool *tool,
  95. union perf_event *event,
  96. struct perf_sample *sample,
  97. struct machine *machine)
  98. {
  99. int err;
  100. err = perf_event__process_mmap(tool, event, sample, machine);
  101. perf_event__repipe(tool, event, sample, machine);
  102. return err;
  103. }
  104. static int perf_event__repipe_mmap2(struct perf_tool *tool,
  105. union perf_event *event,
  106. struct perf_sample *sample,
  107. struct machine *machine)
  108. {
  109. int err;
  110. err = perf_event__process_mmap2(tool, event, sample, machine);
  111. perf_event__repipe(tool, event, sample, machine);
  112. return err;
  113. }
  114. static int perf_event__repipe_fork(struct perf_tool *tool,
  115. union perf_event *event,
  116. struct perf_sample *sample,
  117. struct machine *machine)
  118. {
  119. int err;
  120. err = perf_event__process_fork(tool, event, sample, machine);
  121. perf_event__repipe(tool, event, sample, machine);
  122. return err;
  123. }
  124. static int perf_event__repipe_tracing_data(struct perf_tool *tool,
  125. union perf_event *event,
  126. struct perf_session *session)
  127. {
  128. int err;
  129. perf_event__repipe_synth(tool, event);
  130. err = perf_event__process_tracing_data(tool, event, session);
  131. return err;
  132. }
  133. static int dso__read_build_id(struct dso *self)
  134. {
  135. if (self->has_build_id)
  136. return 0;
  137. if (filename__read_build_id(self->long_name, self->build_id,
  138. sizeof(self->build_id)) > 0) {
  139. self->has_build_id = true;
  140. return 0;
  141. }
  142. return -1;
  143. }
  144. static int dso__inject_build_id(struct dso *self, struct perf_tool *tool,
  145. struct machine *machine)
  146. {
  147. u16 misc = PERF_RECORD_MISC_USER;
  148. int err;
  149. if (dso__read_build_id(self) < 0) {
  150. pr_debug("no build_id found for %s\n", self->long_name);
  151. return -1;
  152. }
  153. if (self->kernel)
  154. misc = PERF_RECORD_MISC_KERNEL;
  155. err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe,
  156. machine);
  157. if (err) {
  158. pr_err("Can't synthesize build_id event for %s\n", self->long_name);
  159. return -1;
  160. }
  161. return 0;
  162. }
  163. static int perf_event__inject_buildid(struct perf_tool *tool,
  164. union perf_event *event,
  165. struct perf_sample *sample,
  166. struct perf_evsel *evsel __maybe_unused,
  167. struct machine *machine)
  168. {
  169. struct addr_location al;
  170. struct thread *thread;
  171. u8 cpumode;
  172. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  173. thread = machine__findnew_thread(machine, sample->pid, sample->pid);
  174. if (thread == NULL) {
  175. pr_err("problem processing %d event, skipping it.\n",
  176. event->header.type);
  177. goto repipe;
  178. }
  179. thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
  180. sample->ip, &al);
  181. if (al.map != NULL) {
  182. if (!al.map->dso->hit) {
  183. al.map->dso->hit = 1;
  184. if (map__load(al.map, NULL) >= 0) {
  185. dso__inject_build_id(al.map->dso, tool, machine);
  186. /*
  187. * If this fails, too bad, let the other side
  188. * account this as unresolved.
  189. */
  190. } else {
  191. #ifdef HAVE_LIBELF_SUPPORT
  192. pr_warning("no symbols found in %s, maybe "
  193. "install a debug package?\n",
  194. al.map->dso->long_name);
  195. #endif
  196. }
  197. }
  198. }
  199. repipe:
  200. perf_event__repipe(tool, event, sample, machine);
  201. return 0;
  202. }
  203. static int perf_inject__sched_process_exit(struct perf_tool *tool,
  204. union perf_event *event __maybe_unused,
  205. struct perf_sample *sample,
  206. struct perf_evsel *evsel __maybe_unused,
  207. struct machine *machine __maybe_unused)
  208. {
  209. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  210. struct event_entry *ent;
  211. list_for_each_entry(ent, &inject->samples, node) {
  212. if (sample->tid == ent->tid) {
  213. list_del_init(&ent->node);
  214. free(ent);
  215. break;
  216. }
  217. }
  218. return 0;
  219. }
  220. static int perf_inject__sched_switch(struct perf_tool *tool,
  221. union perf_event *event,
  222. struct perf_sample *sample,
  223. struct perf_evsel *evsel,
  224. struct machine *machine)
  225. {
  226. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  227. struct event_entry *ent;
  228. perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
  229. ent = malloc(event->header.size + sizeof(struct event_entry));
  230. if (ent == NULL) {
  231. color_fprintf(stderr, PERF_COLOR_RED,
  232. "Not enough memory to process sched switch event!");
  233. return -1;
  234. }
  235. ent->tid = sample->tid;
  236. memcpy(&ent->event, event, event->header.size);
  237. list_add(&ent->node, &inject->samples);
  238. return 0;
  239. }
  240. static int perf_inject__sched_stat(struct perf_tool *tool,
  241. union perf_event *event __maybe_unused,
  242. struct perf_sample *sample,
  243. struct perf_evsel *evsel,
  244. struct machine *machine)
  245. {
  246. struct event_entry *ent;
  247. union perf_event *event_sw;
  248. struct perf_sample sample_sw;
  249. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  250. u32 pid = perf_evsel__intval(evsel, sample, "pid");
  251. list_for_each_entry(ent, &inject->samples, node) {
  252. if (pid == ent->tid)
  253. goto found;
  254. }
  255. return 0;
  256. found:
  257. event_sw = &ent->event[0];
  258. perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
  259. sample_sw.period = sample->period;
  260. sample_sw.time = sample->time;
  261. perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
  262. evsel->attr.sample_regs_user,
  263. evsel->attr.read_format, &sample_sw,
  264. false);
  265. build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
  266. return perf_event__repipe(tool, event_sw, &sample_sw, machine);
  267. }
  268. static void sig_handler(int sig __maybe_unused)
  269. {
  270. session_done = 1;
  271. }
  272. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  273. u64 sample_type, const char *sample_msg)
  274. {
  275. struct perf_event_attr *attr = &evsel->attr;
  276. const char *name = perf_evsel__name(evsel);
  277. if (!(attr->sample_type & sample_type)) {
  278. pr_err("Samples for %s event do not have %s attribute set.",
  279. name, sample_msg);
  280. return -EINVAL;
  281. }
  282. return 0;
  283. }
  284. static int __cmd_inject(struct perf_inject *inject)
  285. {
  286. struct perf_session *session;
  287. int ret = -EINVAL;
  288. struct perf_data_file file = {
  289. .path = inject->input_name,
  290. .mode = PERF_DATA_MODE_READ,
  291. };
  292. signal(SIGINT, sig_handler);
  293. if (inject->build_ids || inject->sched_stat) {
  294. inject->tool.mmap = perf_event__repipe_mmap;
  295. inject->tool.mmap2 = perf_event__repipe_mmap2;
  296. inject->tool.fork = perf_event__repipe_fork;
  297. inject->tool.tracing_data = perf_event__repipe_tracing_data;
  298. }
  299. session = perf_session__new(&file, true, &inject->tool);
  300. if (session == NULL)
  301. return -ENOMEM;
  302. if (inject->build_ids) {
  303. inject->tool.sample = perf_event__inject_buildid;
  304. } else if (inject->sched_stat) {
  305. struct perf_evsel *evsel;
  306. inject->tool.ordered_samples = true;
  307. list_for_each_entry(evsel, &session->evlist->entries, node) {
  308. const char *name = perf_evsel__name(evsel);
  309. if (!strcmp(name, "sched:sched_switch")) {
  310. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
  311. return -EINVAL;
  312. evsel->handler.func = perf_inject__sched_switch;
  313. } else if (!strcmp(name, "sched:sched_process_exit"))
  314. evsel->handler.func = perf_inject__sched_process_exit;
  315. else if (!strncmp(name, "sched:sched_stat_", 17))
  316. evsel->handler.func = perf_inject__sched_stat;
  317. }
  318. }
  319. if (!inject->pipe_output)
  320. lseek(inject->output, session->header.data_offset, SEEK_SET);
  321. ret = perf_session__process_events(session, &inject->tool);
  322. if (!inject->pipe_output) {
  323. session->header.data_size = inject->bytes_written;
  324. perf_session__write_header(session, session->evlist, inject->output, true);
  325. }
  326. perf_session__delete(session);
  327. return ret;
  328. }
  329. int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
  330. {
  331. struct perf_inject inject = {
  332. .tool = {
  333. .sample = perf_event__repipe_sample,
  334. .mmap = perf_event__repipe,
  335. .mmap2 = perf_event__repipe,
  336. .comm = perf_event__repipe,
  337. .fork = perf_event__repipe,
  338. .exit = perf_event__repipe,
  339. .lost = perf_event__repipe,
  340. .read = perf_event__repipe_sample,
  341. .throttle = perf_event__repipe,
  342. .unthrottle = perf_event__repipe,
  343. .attr = perf_event__repipe_attr,
  344. .tracing_data = perf_event__repipe_op2_synth,
  345. .finished_round = perf_event__repipe_op2_synth,
  346. .build_id = perf_event__repipe_op2_synth,
  347. },
  348. .input_name = "-",
  349. .samples = LIST_HEAD_INIT(inject.samples),
  350. };
  351. const char *output_name = "-";
  352. const struct option options[] = {
  353. OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
  354. "Inject build-ids into the output stream"),
  355. OPT_STRING('i', "input", &inject.input_name, "file",
  356. "input file name"),
  357. OPT_STRING('o', "output", &output_name, "file",
  358. "output file name"),
  359. OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
  360. "Merge sched-stat and sched-switch for getting events "
  361. "where and how long tasks slept"),
  362. OPT_INCR('v', "verbose", &verbose,
  363. "be more verbose (show build ids, etc)"),
  364. OPT_END()
  365. };
  366. const char * const inject_usage[] = {
  367. "perf inject [<options>]",
  368. NULL
  369. };
  370. argc = parse_options(argc, argv, options, inject_usage, 0);
  371. /*
  372. * Any (unrecognized) arguments left?
  373. */
  374. if (argc)
  375. usage_with_options(inject_usage, options);
  376. if (!strcmp(output_name, "-")) {
  377. inject.pipe_output = 1;
  378. inject.output = STDOUT_FILENO;
  379. } else {
  380. inject.output = open(output_name, O_CREAT | O_WRONLY | O_TRUNC,
  381. S_IRUSR | S_IWUSR);
  382. if (inject.output < 0) {
  383. perror("failed to create output file");
  384. return -1;
  385. }
  386. }
  387. if (symbol__init() < 0)
  388. return -1;
  389. return __cmd_inject(&inject);
  390. }