builtin-inject.c 12 KB

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