builtin-inject.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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/auxtrace.h"
  19. #include "util/parse-options.h"
  20. #include <linux/list.h>
  21. struct perf_inject {
  22. struct perf_tool tool;
  23. struct perf_session *session;
  24. bool build_ids;
  25. bool sched_stat;
  26. bool have_auxtrace;
  27. const char *input_name;
  28. struct perf_data_file output;
  29. u64 bytes_written;
  30. struct list_head samples;
  31. struct itrace_synth_opts itrace_synth_opts;
  32. };
  33. struct event_entry {
  34. struct list_head node;
  35. u32 tid;
  36. union perf_event event[0];
  37. };
  38. static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
  39. {
  40. ssize_t size;
  41. size = perf_data_file__write(&inject->output, buf, sz);
  42. if (size < 0)
  43. return -errno;
  44. inject->bytes_written += size;
  45. return 0;
  46. }
  47. static int perf_event__repipe_synth(struct perf_tool *tool,
  48. union perf_event *event)
  49. {
  50. struct perf_inject *inject = container_of(tool, struct perf_inject,
  51. tool);
  52. return output_bytes(inject, event, event->header.size);
  53. }
  54. static int perf_event__repipe_oe_synth(struct perf_tool *tool,
  55. union perf_event *event,
  56. struct ordered_events *oe __maybe_unused)
  57. {
  58. return perf_event__repipe_synth(tool, event);
  59. }
  60. static int perf_event__repipe_op2_synth(struct perf_tool *tool,
  61. union perf_event *event,
  62. struct perf_session *session
  63. __maybe_unused)
  64. {
  65. return perf_event__repipe_synth(tool, event);
  66. }
  67. static int perf_event__repipe_attr(struct perf_tool *tool,
  68. union perf_event *event,
  69. struct perf_evlist **pevlist)
  70. {
  71. struct perf_inject *inject = container_of(tool, struct perf_inject,
  72. tool);
  73. int ret;
  74. ret = perf_event__process_attr(tool, event, pevlist);
  75. if (ret)
  76. return ret;
  77. if (!inject->output.is_pipe)
  78. return 0;
  79. return perf_event__repipe_synth(tool, event);
  80. }
  81. #ifdef HAVE_AUXTRACE_SUPPORT
  82. static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
  83. {
  84. char buf[4096];
  85. ssize_t ssz;
  86. int ret;
  87. while (size > 0) {
  88. ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
  89. if (ssz < 0)
  90. return -errno;
  91. ret = output_bytes(inject, buf, ssz);
  92. if (ret)
  93. return ret;
  94. size -= ssz;
  95. }
  96. return 0;
  97. }
  98. static s64 perf_event__repipe_auxtrace(struct perf_tool *tool,
  99. union perf_event *event,
  100. struct perf_session *session
  101. __maybe_unused)
  102. {
  103. struct perf_inject *inject = container_of(tool, struct perf_inject,
  104. tool);
  105. int ret;
  106. inject->have_auxtrace = true;
  107. if (!inject->output.is_pipe) {
  108. off_t offset;
  109. offset = lseek(inject->output.fd, 0, SEEK_CUR);
  110. if (offset == -1)
  111. return -errno;
  112. ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
  113. event, offset);
  114. if (ret < 0)
  115. return ret;
  116. }
  117. if (perf_data_file__is_pipe(session->file) || !session->one_mmap) {
  118. ret = output_bytes(inject, event, event->header.size);
  119. if (ret < 0)
  120. return ret;
  121. ret = copy_bytes(inject, perf_data_file__fd(session->file),
  122. event->auxtrace.size);
  123. } else {
  124. ret = output_bytes(inject, event,
  125. event->header.size + event->auxtrace.size);
  126. }
  127. if (ret < 0)
  128. return ret;
  129. return event->auxtrace.size;
  130. }
  131. #else
  132. static s64
  133. perf_event__repipe_auxtrace(struct perf_tool *tool __maybe_unused,
  134. union perf_event *event __maybe_unused,
  135. struct perf_session *session __maybe_unused)
  136. {
  137. pr_err("AUX area tracing not supported\n");
  138. return -EINVAL;
  139. }
  140. #endif
  141. static int perf_event__repipe(struct perf_tool *tool,
  142. union perf_event *event,
  143. struct perf_sample *sample __maybe_unused,
  144. struct machine *machine __maybe_unused)
  145. {
  146. return perf_event__repipe_synth(tool, event);
  147. }
  148. typedef int (*inject_handler)(struct perf_tool *tool,
  149. union perf_event *event,
  150. struct perf_sample *sample,
  151. struct perf_evsel *evsel,
  152. struct machine *machine);
  153. static int perf_event__repipe_sample(struct perf_tool *tool,
  154. union perf_event *event,
  155. struct perf_sample *sample,
  156. struct perf_evsel *evsel,
  157. struct machine *machine)
  158. {
  159. if (evsel->handler) {
  160. inject_handler f = evsel->handler;
  161. return f(tool, event, sample, evsel, machine);
  162. }
  163. build_id__mark_dso_hit(tool, event, sample, evsel, machine);
  164. return perf_event__repipe_synth(tool, event);
  165. }
  166. static int perf_event__repipe_mmap(struct perf_tool *tool,
  167. union perf_event *event,
  168. struct perf_sample *sample,
  169. struct machine *machine)
  170. {
  171. int err;
  172. err = perf_event__process_mmap(tool, event, sample, machine);
  173. perf_event__repipe(tool, event, sample, machine);
  174. return err;
  175. }
  176. static int perf_event__repipe_mmap2(struct perf_tool *tool,
  177. union perf_event *event,
  178. struct perf_sample *sample,
  179. struct machine *machine)
  180. {
  181. int err;
  182. err = perf_event__process_mmap2(tool, event, sample, machine);
  183. perf_event__repipe(tool, event, sample, machine);
  184. return err;
  185. }
  186. static int perf_event__repipe_fork(struct perf_tool *tool,
  187. union perf_event *event,
  188. struct perf_sample *sample,
  189. struct machine *machine)
  190. {
  191. int err;
  192. err = perf_event__process_fork(tool, event, sample, machine);
  193. perf_event__repipe(tool, event, sample, machine);
  194. return err;
  195. }
  196. static int perf_event__repipe_comm(struct perf_tool *tool,
  197. union perf_event *event,
  198. struct perf_sample *sample,
  199. struct machine *machine)
  200. {
  201. int err;
  202. err = perf_event__process_comm(tool, event, sample, machine);
  203. perf_event__repipe(tool, event, sample, machine);
  204. return err;
  205. }
  206. static int perf_event__repipe_exit(struct perf_tool *tool,
  207. union perf_event *event,
  208. struct perf_sample *sample,
  209. struct machine *machine)
  210. {
  211. int err;
  212. err = perf_event__process_exit(tool, event, sample, machine);
  213. perf_event__repipe(tool, event, sample, machine);
  214. return err;
  215. }
  216. static int perf_event__repipe_tracing_data(struct perf_tool *tool,
  217. union perf_event *event,
  218. struct perf_session *session)
  219. {
  220. int err;
  221. perf_event__repipe_synth(tool, event);
  222. err = perf_event__process_tracing_data(tool, event, session);
  223. return err;
  224. }
  225. static int perf_event__repipe_id_index(struct perf_tool *tool,
  226. union perf_event *event,
  227. struct perf_session *session)
  228. {
  229. int err;
  230. perf_event__repipe_synth(tool, event);
  231. err = perf_event__process_id_index(tool, event, session);
  232. return err;
  233. }
  234. static int dso__read_build_id(struct dso *dso)
  235. {
  236. if (dso->has_build_id)
  237. return 0;
  238. if (filename__read_build_id(dso->long_name, dso->build_id,
  239. sizeof(dso->build_id)) > 0) {
  240. dso->has_build_id = true;
  241. return 0;
  242. }
  243. return -1;
  244. }
  245. static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
  246. struct machine *machine)
  247. {
  248. u16 misc = PERF_RECORD_MISC_USER;
  249. int err;
  250. if (dso__read_build_id(dso) < 0) {
  251. pr_debug("no build_id found for %s\n", dso->long_name);
  252. return -1;
  253. }
  254. if (dso->kernel)
  255. misc = PERF_RECORD_MISC_KERNEL;
  256. err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
  257. machine);
  258. if (err) {
  259. pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
  260. return -1;
  261. }
  262. return 0;
  263. }
  264. static int perf_event__inject_buildid(struct perf_tool *tool,
  265. union perf_event *event,
  266. struct perf_sample *sample,
  267. struct perf_evsel *evsel __maybe_unused,
  268. struct machine *machine)
  269. {
  270. struct addr_location al;
  271. struct thread *thread;
  272. u8 cpumode;
  273. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  274. thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  275. if (thread == NULL) {
  276. pr_err("problem processing %d event, skipping it.\n",
  277. event->header.type);
  278. goto repipe;
  279. }
  280. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al);
  281. if (al.map != NULL) {
  282. if (!al.map->dso->hit) {
  283. al.map->dso->hit = 1;
  284. if (map__load(al.map, NULL) >= 0) {
  285. dso__inject_build_id(al.map->dso, tool, machine);
  286. /*
  287. * If this fails, too bad, let the other side
  288. * account this as unresolved.
  289. */
  290. } else {
  291. #ifdef HAVE_LIBELF_SUPPORT
  292. pr_warning("no symbols found in %s, maybe "
  293. "install a debug package?\n",
  294. al.map->dso->long_name);
  295. #endif
  296. }
  297. }
  298. }
  299. thread__put(thread);
  300. repipe:
  301. perf_event__repipe(tool, event, sample, machine);
  302. return 0;
  303. }
  304. static int perf_inject__sched_process_exit(struct perf_tool *tool,
  305. union perf_event *event __maybe_unused,
  306. struct perf_sample *sample,
  307. struct perf_evsel *evsel __maybe_unused,
  308. struct machine *machine __maybe_unused)
  309. {
  310. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  311. struct event_entry *ent;
  312. list_for_each_entry(ent, &inject->samples, node) {
  313. if (sample->tid == ent->tid) {
  314. list_del_init(&ent->node);
  315. free(ent);
  316. break;
  317. }
  318. }
  319. return 0;
  320. }
  321. static int perf_inject__sched_switch(struct perf_tool *tool,
  322. union perf_event *event,
  323. struct perf_sample *sample,
  324. struct perf_evsel *evsel,
  325. struct machine *machine)
  326. {
  327. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  328. struct event_entry *ent;
  329. perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
  330. ent = malloc(event->header.size + sizeof(struct event_entry));
  331. if (ent == NULL) {
  332. color_fprintf(stderr, PERF_COLOR_RED,
  333. "Not enough memory to process sched switch event!");
  334. return -1;
  335. }
  336. ent->tid = sample->tid;
  337. memcpy(&ent->event, event, event->header.size);
  338. list_add(&ent->node, &inject->samples);
  339. return 0;
  340. }
  341. static int perf_inject__sched_stat(struct perf_tool *tool,
  342. union perf_event *event __maybe_unused,
  343. struct perf_sample *sample,
  344. struct perf_evsel *evsel,
  345. struct machine *machine)
  346. {
  347. struct event_entry *ent;
  348. union perf_event *event_sw;
  349. struct perf_sample sample_sw;
  350. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  351. u32 pid = perf_evsel__intval(evsel, sample, "pid");
  352. list_for_each_entry(ent, &inject->samples, node) {
  353. if (pid == ent->tid)
  354. goto found;
  355. }
  356. return 0;
  357. found:
  358. event_sw = &ent->event[0];
  359. perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
  360. sample_sw.period = sample->period;
  361. sample_sw.time = sample->time;
  362. perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
  363. evsel->attr.read_format, &sample_sw,
  364. false);
  365. build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
  366. return perf_event__repipe(tool, event_sw, &sample_sw, machine);
  367. }
  368. static void sig_handler(int sig __maybe_unused)
  369. {
  370. session_done = 1;
  371. }
  372. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  373. u64 sample_type, const char *sample_msg)
  374. {
  375. struct perf_event_attr *attr = &evsel->attr;
  376. const char *name = perf_evsel__name(evsel);
  377. if (!(attr->sample_type & sample_type)) {
  378. pr_err("Samples for %s event do not have %s attribute set.",
  379. name, sample_msg);
  380. return -EINVAL;
  381. }
  382. return 0;
  383. }
  384. static int __cmd_inject(struct perf_inject *inject)
  385. {
  386. int ret = -EINVAL;
  387. struct perf_session *session = inject->session;
  388. struct perf_data_file *file_out = &inject->output;
  389. int fd = perf_data_file__fd(file_out);
  390. u64 output_data_offset;
  391. signal(SIGINT, sig_handler);
  392. if (inject->build_ids || inject->sched_stat ||
  393. inject->itrace_synth_opts.set) {
  394. inject->tool.mmap = perf_event__repipe_mmap;
  395. inject->tool.mmap2 = perf_event__repipe_mmap2;
  396. inject->tool.fork = perf_event__repipe_fork;
  397. inject->tool.tracing_data = perf_event__repipe_tracing_data;
  398. }
  399. output_data_offset = session->header.data_offset;
  400. if (inject->build_ids) {
  401. inject->tool.sample = perf_event__inject_buildid;
  402. } else if (inject->sched_stat) {
  403. struct perf_evsel *evsel;
  404. evlist__for_each(session->evlist, evsel) {
  405. const char *name = perf_evsel__name(evsel);
  406. if (!strcmp(name, "sched:sched_switch")) {
  407. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
  408. return -EINVAL;
  409. evsel->handler = perf_inject__sched_switch;
  410. } else if (!strcmp(name, "sched:sched_process_exit"))
  411. evsel->handler = perf_inject__sched_process_exit;
  412. else if (!strncmp(name, "sched:sched_stat_", 17))
  413. evsel->handler = perf_inject__sched_stat;
  414. }
  415. } else if (inject->itrace_synth_opts.set) {
  416. session->itrace_synth_opts = &inject->itrace_synth_opts;
  417. inject->itrace_synth_opts.inject = true;
  418. inject->tool.comm = perf_event__repipe_comm;
  419. inject->tool.exit = perf_event__repipe_exit;
  420. inject->tool.id_index = perf_event__repipe_id_index;
  421. inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
  422. inject->tool.auxtrace = perf_event__process_auxtrace;
  423. inject->tool.ordered_events = true;
  424. inject->tool.ordering_requires_timestamps = true;
  425. /* Allow space in the header for new attributes */
  426. output_data_offset = 4096;
  427. }
  428. if (!inject->itrace_synth_opts.set)
  429. auxtrace_index__free(&session->auxtrace_index);
  430. if (!file_out->is_pipe)
  431. lseek(fd, output_data_offset, SEEK_SET);
  432. ret = perf_session__process_events(session);
  433. if (!file_out->is_pipe) {
  434. if (inject->build_ids) {
  435. perf_header__set_feat(&session->header,
  436. HEADER_BUILD_ID);
  437. if (inject->have_auxtrace)
  438. dsos__hit_all(session);
  439. }
  440. /*
  441. * The AUX areas have been removed and replaced with
  442. * synthesized hardware events, so clear the feature flag.
  443. */
  444. if (inject->itrace_synth_opts.set)
  445. perf_header__clear_feat(&session->header,
  446. HEADER_AUXTRACE);
  447. session->header.data_offset = output_data_offset;
  448. session->header.data_size = inject->bytes_written;
  449. perf_session__write_header(session, session->evlist, fd, true);
  450. }
  451. return ret;
  452. }
  453. int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
  454. {
  455. struct perf_inject inject = {
  456. .tool = {
  457. .sample = perf_event__repipe_sample,
  458. .mmap = perf_event__repipe,
  459. .mmap2 = perf_event__repipe,
  460. .comm = perf_event__repipe,
  461. .fork = perf_event__repipe,
  462. .exit = perf_event__repipe,
  463. .lost = perf_event__repipe,
  464. .aux = perf_event__repipe,
  465. .itrace_start = perf_event__repipe,
  466. .read = perf_event__repipe_sample,
  467. .throttle = perf_event__repipe,
  468. .unthrottle = perf_event__repipe,
  469. .attr = perf_event__repipe_attr,
  470. .tracing_data = perf_event__repipe_op2_synth,
  471. .auxtrace_info = perf_event__repipe_op2_synth,
  472. .auxtrace = perf_event__repipe_auxtrace,
  473. .auxtrace_error = perf_event__repipe_op2_synth,
  474. .finished_round = perf_event__repipe_oe_synth,
  475. .build_id = perf_event__repipe_op2_synth,
  476. .id_index = perf_event__repipe_op2_synth,
  477. },
  478. .input_name = "-",
  479. .samples = LIST_HEAD_INIT(inject.samples),
  480. .output = {
  481. .path = "-",
  482. .mode = PERF_DATA_MODE_WRITE,
  483. },
  484. };
  485. struct perf_data_file file = {
  486. .mode = PERF_DATA_MODE_READ,
  487. };
  488. int ret;
  489. const struct option options[] = {
  490. OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
  491. "Inject build-ids into the output stream"),
  492. OPT_STRING('i', "input", &inject.input_name, "file",
  493. "input file name"),
  494. OPT_STRING('o', "output", &inject.output.path, "file",
  495. "output file name"),
  496. OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
  497. "Merge sched-stat and sched-switch for getting events "
  498. "where and how long tasks slept"),
  499. OPT_INCR('v', "verbose", &verbose,
  500. "be more verbose (show build ids, etc)"),
  501. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
  502. "kallsyms pathname"),
  503. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  504. OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
  505. NULL, "opts", "Instruction Tracing options",
  506. itrace_parse_synth_opts),
  507. OPT_END()
  508. };
  509. const char * const inject_usage[] = {
  510. "perf inject [<options>]",
  511. NULL
  512. };
  513. argc = parse_options(argc, argv, options, inject_usage, 0);
  514. /*
  515. * Any (unrecognized) arguments left?
  516. */
  517. if (argc)
  518. usage_with_options(inject_usage, options);
  519. if (perf_data_file__open(&inject.output)) {
  520. perror("failed to create output file");
  521. return -1;
  522. }
  523. inject.tool.ordered_events = inject.sched_stat;
  524. file.path = inject.input_name;
  525. inject.session = perf_session__new(&file, true, &inject.tool);
  526. if (inject.session == NULL)
  527. return -1;
  528. ret = symbol__init(&inject.session->header.env);
  529. if (ret < 0)
  530. goto out_delete;
  531. ret = __cmd_inject(&inject);
  532. out_delete:
  533. perf_session__delete(inject.session);
  534. return ret;
  535. }