builtin-inject.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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/jit.h"
  20. #include <subcmd/parse-options.h>
  21. #include <linux/list.h>
  22. struct perf_inject {
  23. struct perf_tool tool;
  24. struct perf_session *session;
  25. bool build_ids;
  26. bool sched_stat;
  27. bool have_auxtrace;
  28. bool strip;
  29. bool jit_mode;
  30. const char *input_name;
  31. struct perf_data_file output;
  32. u64 bytes_written;
  33. u64 aux_id;
  34. struct list_head samples;
  35. struct itrace_synth_opts itrace_synth_opts;
  36. };
  37. struct event_entry {
  38. struct list_head node;
  39. u32 tid;
  40. union perf_event event[0];
  41. };
  42. static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
  43. {
  44. ssize_t size;
  45. size = perf_data_file__write(&inject->output, buf, sz);
  46. if (size < 0)
  47. return -errno;
  48. inject->bytes_written += size;
  49. return 0;
  50. }
  51. static int perf_event__repipe_synth(struct perf_tool *tool,
  52. union perf_event *event)
  53. {
  54. struct perf_inject *inject = container_of(tool, struct perf_inject,
  55. tool);
  56. return output_bytes(inject, event, event->header.size);
  57. }
  58. static int perf_event__repipe_oe_synth(struct perf_tool *tool,
  59. union perf_event *event,
  60. struct ordered_events *oe __maybe_unused)
  61. {
  62. return perf_event__repipe_synth(tool, event);
  63. }
  64. #ifdef HAVE_JITDUMP
  65. static int perf_event__drop_oe(struct perf_tool *tool __maybe_unused,
  66. union perf_event *event __maybe_unused,
  67. struct ordered_events *oe __maybe_unused)
  68. {
  69. return 0;
  70. }
  71. #endif
  72. static int perf_event__repipe_op2_synth(struct perf_tool *tool,
  73. union perf_event *event,
  74. struct perf_session *session
  75. __maybe_unused)
  76. {
  77. return perf_event__repipe_synth(tool, event);
  78. }
  79. static int perf_event__repipe_attr(struct perf_tool *tool,
  80. union perf_event *event,
  81. struct perf_evlist **pevlist)
  82. {
  83. struct perf_inject *inject = container_of(tool, struct perf_inject,
  84. tool);
  85. int ret;
  86. ret = perf_event__process_attr(tool, event, pevlist);
  87. if (ret)
  88. return ret;
  89. if (!inject->output.is_pipe)
  90. return 0;
  91. return perf_event__repipe_synth(tool, event);
  92. }
  93. #ifdef HAVE_AUXTRACE_SUPPORT
  94. static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
  95. {
  96. char buf[4096];
  97. ssize_t ssz;
  98. int ret;
  99. while (size > 0) {
  100. ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
  101. if (ssz < 0)
  102. return -errno;
  103. ret = output_bytes(inject, buf, ssz);
  104. if (ret)
  105. return ret;
  106. size -= ssz;
  107. }
  108. return 0;
  109. }
  110. static s64 perf_event__repipe_auxtrace(struct perf_tool *tool,
  111. union perf_event *event,
  112. struct perf_session *session)
  113. {
  114. struct perf_inject *inject = container_of(tool, struct perf_inject,
  115. tool);
  116. int ret;
  117. inject->have_auxtrace = true;
  118. if (!inject->output.is_pipe) {
  119. off_t offset;
  120. offset = lseek(inject->output.fd, 0, SEEK_CUR);
  121. if (offset == -1)
  122. return -errno;
  123. ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
  124. event, offset);
  125. if (ret < 0)
  126. return ret;
  127. }
  128. if (perf_data_file__is_pipe(session->file) || !session->one_mmap) {
  129. ret = output_bytes(inject, event, event->header.size);
  130. if (ret < 0)
  131. return ret;
  132. ret = copy_bytes(inject, perf_data_file__fd(session->file),
  133. event->auxtrace.size);
  134. } else {
  135. ret = output_bytes(inject, event,
  136. event->header.size + event->auxtrace.size);
  137. }
  138. if (ret < 0)
  139. return ret;
  140. return event->auxtrace.size;
  141. }
  142. #else
  143. static s64
  144. perf_event__repipe_auxtrace(struct perf_tool *tool __maybe_unused,
  145. union perf_event *event __maybe_unused,
  146. struct perf_session *session __maybe_unused)
  147. {
  148. pr_err("AUX area tracing not supported\n");
  149. return -EINVAL;
  150. }
  151. #endif
  152. static int perf_event__repipe(struct perf_tool *tool,
  153. union perf_event *event,
  154. struct perf_sample *sample __maybe_unused,
  155. struct machine *machine __maybe_unused)
  156. {
  157. return perf_event__repipe_synth(tool, event);
  158. }
  159. static int perf_event__drop(struct perf_tool *tool __maybe_unused,
  160. union perf_event *event __maybe_unused,
  161. struct perf_sample *sample __maybe_unused,
  162. struct machine *machine __maybe_unused)
  163. {
  164. return 0;
  165. }
  166. static int perf_event__drop_aux(struct perf_tool *tool,
  167. union perf_event *event __maybe_unused,
  168. struct perf_sample *sample,
  169. struct machine *machine __maybe_unused)
  170. {
  171. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  172. if (!inject->aux_id)
  173. inject->aux_id = sample->id;
  174. return 0;
  175. }
  176. typedef int (*inject_handler)(struct perf_tool *tool,
  177. union perf_event *event,
  178. struct perf_sample *sample,
  179. struct perf_evsel *evsel,
  180. struct machine *machine);
  181. static int perf_event__repipe_sample(struct perf_tool *tool,
  182. union perf_event *event,
  183. struct perf_sample *sample,
  184. struct perf_evsel *evsel,
  185. struct machine *machine)
  186. {
  187. if (evsel->handler) {
  188. inject_handler f = evsel->handler;
  189. return f(tool, event, sample, evsel, machine);
  190. }
  191. build_id__mark_dso_hit(tool, event, sample, evsel, machine);
  192. return perf_event__repipe_synth(tool, event);
  193. }
  194. static int perf_event__repipe_mmap(struct perf_tool *tool,
  195. union perf_event *event,
  196. struct perf_sample *sample,
  197. struct machine *machine)
  198. {
  199. int err;
  200. err = perf_event__process_mmap(tool, event, sample, machine);
  201. perf_event__repipe(tool, event, sample, machine);
  202. return err;
  203. }
  204. #ifdef HAVE_JITDUMP
  205. static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
  206. union perf_event *event,
  207. struct perf_sample *sample,
  208. struct machine *machine)
  209. {
  210. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  211. u64 n = 0;
  212. int ret;
  213. /*
  214. * if jit marker, then inject jit mmaps and generate ELF images
  215. */
  216. ret = jit_process(inject->session, &inject->output, machine,
  217. event->mmap.filename, sample->pid, &n);
  218. if (ret < 0)
  219. return ret;
  220. if (ret) {
  221. inject->bytes_written += n;
  222. return 0;
  223. }
  224. return perf_event__repipe_mmap(tool, event, sample, machine);
  225. }
  226. #endif
  227. static int perf_event__repipe_mmap2(struct perf_tool *tool,
  228. union perf_event *event,
  229. struct perf_sample *sample,
  230. struct machine *machine)
  231. {
  232. int err;
  233. err = perf_event__process_mmap2(tool, event, sample, machine);
  234. perf_event__repipe(tool, event, sample, machine);
  235. return err;
  236. }
  237. #ifdef HAVE_JITDUMP
  238. static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
  239. union perf_event *event,
  240. struct perf_sample *sample,
  241. struct machine *machine)
  242. {
  243. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  244. u64 n = 0;
  245. int ret;
  246. /*
  247. * if jit marker, then inject jit mmaps and generate ELF images
  248. */
  249. ret = jit_process(inject->session, &inject->output, machine,
  250. event->mmap2.filename, sample->pid, &n);
  251. if (ret < 0)
  252. return ret;
  253. if (ret) {
  254. inject->bytes_written += n;
  255. return 0;
  256. }
  257. return perf_event__repipe_mmap2(tool, event, sample, machine);
  258. }
  259. #endif
  260. static int perf_event__repipe_fork(struct perf_tool *tool,
  261. union perf_event *event,
  262. struct perf_sample *sample,
  263. struct machine *machine)
  264. {
  265. int err;
  266. err = perf_event__process_fork(tool, event, sample, machine);
  267. perf_event__repipe(tool, event, sample, machine);
  268. return err;
  269. }
  270. static int perf_event__repipe_comm(struct perf_tool *tool,
  271. union perf_event *event,
  272. struct perf_sample *sample,
  273. struct machine *machine)
  274. {
  275. int err;
  276. err = perf_event__process_comm(tool, event, sample, machine);
  277. perf_event__repipe(tool, event, sample, machine);
  278. return err;
  279. }
  280. static int perf_event__repipe_exit(struct perf_tool *tool,
  281. union perf_event *event,
  282. struct perf_sample *sample,
  283. struct machine *machine)
  284. {
  285. int err;
  286. err = perf_event__process_exit(tool, event, sample, machine);
  287. perf_event__repipe(tool, event, sample, machine);
  288. return err;
  289. }
  290. static int perf_event__repipe_tracing_data(struct perf_tool *tool,
  291. union perf_event *event,
  292. struct perf_session *session)
  293. {
  294. int err;
  295. perf_event__repipe_synth(tool, event);
  296. err = perf_event__process_tracing_data(tool, event, session);
  297. return err;
  298. }
  299. static int perf_event__repipe_id_index(struct perf_tool *tool,
  300. union perf_event *event,
  301. struct perf_session *session)
  302. {
  303. int err;
  304. perf_event__repipe_synth(tool, event);
  305. err = perf_event__process_id_index(tool, event, session);
  306. return err;
  307. }
  308. static int dso__read_build_id(struct dso *dso)
  309. {
  310. if (dso->has_build_id)
  311. return 0;
  312. if (filename__read_build_id(dso->long_name, dso->build_id,
  313. sizeof(dso->build_id)) > 0) {
  314. dso->has_build_id = true;
  315. return 0;
  316. }
  317. return -1;
  318. }
  319. static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
  320. struct machine *machine)
  321. {
  322. u16 misc = PERF_RECORD_MISC_USER;
  323. int err;
  324. if (dso__read_build_id(dso) < 0) {
  325. pr_debug("no build_id found for %s\n", dso->long_name);
  326. return -1;
  327. }
  328. if (dso->kernel)
  329. misc = PERF_RECORD_MISC_KERNEL;
  330. err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
  331. machine);
  332. if (err) {
  333. pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
  334. return -1;
  335. }
  336. return 0;
  337. }
  338. static int perf_event__inject_buildid(struct perf_tool *tool,
  339. union perf_event *event,
  340. struct perf_sample *sample,
  341. struct perf_evsel *evsel __maybe_unused,
  342. struct machine *machine)
  343. {
  344. struct addr_location al;
  345. struct thread *thread;
  346. thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  347. if (thread == NULL) {
  348. pr_err("problem processing %d event, skipping it.\n",
  349. event->header.type);
  350. goto repipe;
  351. }
  352. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, &al);
  353. if (al.map != NULL) {
  354. if (!al.map->dso->hit) {
  355. al.map->dso->hit = 1;
  356. if (map__load(al.map) >= 0) {
  357. dso__inject_build_id(al.map->dso, tool, machine);
  358. /*
  359. * If this fails, too bad, let the other side
  360. * account this as unresolved.
  361. */
  362. } else {
  363. #ifdef HAVE_LIBELF_SUPPORT
  364. pr_warning("no symbols found in %s, maybe "
  365. "install a debug package?\n",
  366. al.map->dso->long_name);
  367. #endif
  368. }
  369. }
  370. }
  371. thread__put(thread);
  372. repipe:
  373. perf_event__repipe(tool, event, sample, machine);
  374. return 0;
  375. }
  376. static int perf_inject__sched_process_exit(struct perf_tool *tool,
  377. union perf_event *event __maybe_unused,
  378. struct perf_sample *sample,
  379. struct perf_evsel *evsel __maybe_unused,
  380. struct machine *machine __maybe_unused)
  381. {
  382. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  383. struct event_entry *ent;
  384. list_for_each_entry(ent, &inject->samples, node) {
  385. if (sample->tid == ent->tid) {
  386. list_del_init(&ent->node);
  387. free(ent);
  388. break;
  389. }
  390. }
  391. return 0;
  392. }
  393. static int perf_inject__sched_switch(struct perf_tool *tool,
  394. union perf_event *event,
  395. struct perf_sample *sample,
  396. struct perf_evsel *evsel,
  397. struct machine *machine)
  398. {
  399. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  400. struct event_entry *ent;
  401. perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
  402. ent = malloc(event->header.size + sizeof(struct event_entry));
  403. if (ent == NULL) {
  404. color_fprintf(stderr, PERF_COLOR_RED,
  405. "Not enough memory to process sched switch event!");
  406. return -1;
  407. }
  408. ent->tid = sample->tid;
  409. memcpy(&ent->event, event, event->header.size);
  410. list_add(&ent->node, &inject->samples);
  411. return 0;
  412. }
  413. static int perf_inject__sched_stat(struct perf_tool *tool,
  414. union perf_event *event __maybe_unused,
  415. struct perf_sample *sample,
  416. struct perf_evsel *evsel,
  417. struct machine *machine)
  418. {
  419. struct event_entry *ent;
  420. union perf_event *event_sw;
  421. struct perf_sample sample_sw;
  422. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  423. u32 pid = perf_evsel__intval(evsel, sample, "pid");
  424. list_for_each_entry(ent, &inject->samples, node) {
  425. if (pid == ent->tid)
  426. goto found;
  427. }
  428. return 0;
  429. found:
  430. event_sw = &ent->event[0];
  431. perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
  432. sample_sw.period = sample->period;
  433. sample_sw.time = sample->time;
  434. perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
  435. evsel->attr.read_format, &sample_sw,
  436. false);
  437. build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
  438. return perf_event__repipe(tool, event_sw, &sample_sw, machine);
  439. }
  440. static void sig_handler(int sig __maybe_unused)
  441. {
  442. session_done = 1;
  443. }
  444. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  445. u64 sample_type, const char *sample_msg)
  446. {
  447. struct perf_event_attr *attr = &evsel->attr;
  448. const char *name = perf_evsel__name(evsel);
  449. if (!(attr->sample_type & sample_type)) {
  450. pr_err("Samples for %s event do not have %s attribute set.",
  451. name, sample_msg);
  452. return -EINVAL;
  453. }
  454. return 0;
  455. }
  456. static int drop_sample(struct perf_tool *tool __maybe_unused,
  457. union perf_event *event __maybe_unused,
  458. struct perf_sample *sample __maybe_unused,
  459. struct perf_evsel *evsel __maybe_unused,
  460. struct machine *machine __maybe_unused)
  461. {
  462. return 0;
  463. }
  464. static void strip_init(struct perf_inject *inject)
  465. {
  466. struct perf_evlist *evlist = inject->session->evlist;
  467. struct perf_evsel *evsel;
  468. inject->tool.context_switch = perf_event__drop;
  469. evlist__for_each_entry(evlist, evsel)
  470. evsel->handler = drop_sample;
  471. }
  472. static bool has_tracking(struct perf_evsel *evsel)
  473. {
  474. return evsel->attr.mmap || evsel->attr.mmap2 || evsel->attr.comm ||
  475. evsel->attr.task;
  476. }
  477. #define COMPAT_MASK (PERF_SAMPLE_ID | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \
  478. PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER)
  479. /*
  480. * In order that the perf.data file is parsable, tracking events like MMAP need
  481. * their selected event to exist, except if there is only 1 selected event left
  482. * and it has a compatible sample type.
  483. */
  484. static bool ok_to_remove(struct perf_evlist *evlist,
  485. struct perf_evsel *evsel_to_remove)
  486. {
  487. struct perf_evsel *evsel;
  488. int cnt = 0;
  489. bool ok = false;
  490. if (!has_tracking(evsel_to_remove))
  491. return true;
  492. evlist__for_each_entry(evlist, evsel) {
  493. if (evsel->handler != drop_sample) {
  494. cnt += 1;
  495. if ((evsel->attr.sample_type & COMPAT_MASK) ==
  496. (evsel_to_remove->attr.sample_type & COMPAT_MASK))
  497. ok = true;
  498. }
  499. }
  500. return ok && cnt == 1;
  501. }
  502. static void strip_fini(struct perf_inject *inject)
  503. {
  504. struct perf_evlist *evlist = inject->session->evlist;
  505. struct perf_evsel *evsel, *tmp;
  506. /* Remove non-synthesized evsels if possible */
  507. evlist__for_each_entry_safe(evlist, tmp, evsel) {
  508. if (evsel->handler == drop_sample &&
  509. ok_to_remove(evlist, evsel)) {
  510. pr_debug("Deleting %s\n", perf_evsel__name(evsel));
  511. perf_evlist__remove(evlist, evsel);
  512. perf_evsel__delete(evsel);
  513. }
  514. }
  515. }
  516. static int __cmd_inject(struct perf_inject *inject)
  517. {
  518. int ret = -EINVAL;
  519. struct perf_session *session = inject->session;
  520. struct perf_data_file *file_out = &inject->output;
  521. int fd = perf_data_file__fd(file_out);
  522. u64 output_data_offset;
  523. signal(SIGINT, sig_handler);
  524. if (inject->build_ids || inject->sched_stat ||
  525. inject->itrace_synth_opts.set) {
  526. inject->tool.mmap = perf_event__repipe_mmap;
  527. inject->tool.mmap2 = perf_event__repipe_mmap2;
  528. inject->tool.fork = perf_event__repipe_fork;
  529. inject->tool.tracing_data = perf_event__repipe_tracing_data;
  530. }
  531. output_data_offset = session->header.data_offset;
  532. if (inject->build_ids) {
  533. inject->tool.sample = perf_event__inject_buildid;
  534. } else if (inject->sched_stat) {
  535. struct perf_evsel *evsel;
  536. evlist__for_each_entry(session->evlist, evsel) {
  537. const char *name = perf_evsel__name(evsel);
  538. if (!strcmp(name, "sched:sched_switch")) {
  539. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
  540. return -EINVAL;
  541. evsel->handler = perf_inject__sched_switch;
  542. } else if (!strcmp(name, "sched:sched_process_exit"))
  543. evsel->handler = perf_inject__sched_process_exit;
  544. else if (!strncmp(name, "sched:sched_stat_", 17))
  545. evsel->handler = perf_inject__sched_stat;
  546. }
  547. } else if (inject->itrace_synth_opts.set) {
  548. session->itrace_synth_opts = &inject->itrace_synth_opts;
  549. inject->itrace_synth_opts.inject = true;
  550. inject->tool.comm = perf_event__repipe_comm;
  551. inject->tool.exit = perf_event__repipe_exit;
  552. inject->tool.id_index = perf_event__repipe_id_index;
  553. inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
  554. inject->tool.auxtrace = perf_event__process_auxtrace;
  555. inject->tool.aux = perf_event__drop_aux;
  556. inject->tool.itrace_start = perf_event__drop_aux,
  557. inject->tool.ordered_events = true;
  558. inject->tool.ordering_requires_timestamps = true;
  559. /* Allow space in the header for new attributes */
  560. output_data_offset = 4096;
  561. if (inject->strip)
  562. strip_init(inject);
  563. }
  564. if (!inject->itrace_synth_opts.set)
  565. auxtrace_index__free(&session->auxtrace_index);
  566. if (!file_out->is_pipe)
  567. lseek(fd, output_data_offset, SEEK_SET);
  568. ret = perf_session__process_events(session);
  569. if (!file_out->is_pipe) {
  570. if (inject->build_ids)
  571. perf_header__set_feat(&session->header,
  572. HEADER_BUILD_ID);
  573. /*
  574. * Keep all buildids when there is unprocessed AUX data because
  575. * it is not known which ones the AUX trace hits.
  576. */
  577. if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) &&
  578. inject->have_auxtrace && !inject->itrace_synth_opts.set)
  579. dsos__hit_all(session);
  580. /*
  581. * The AUX areas have been removed and replaced with
  582. * synthesized hardware events, so clear the feature flag and
  583. * remove the evsel.
  584. */
  585. if (inject->itrace_synth_opts.set) {
  586. struct perf_evsel *evsel;
  587. perf_header__clear_feat(&session->header,
  588. HEADER_AUXTRACE);
  589. if (inject->itrace_synth_opts.last_branch)
  590. perf_header__set_feat(&session->header,
  591. HEADER_BRANCH_STACK);
  592. evsel = perf_evlist__id2evsel_strict(session->evlist,
  593. inject->aux_id);
  594. if (evsel) {
  595. pr_debug("Deleting %s\n",
  596. perf_evsel__name(evsel));
  597. perf_evlist__remove(session->evlist, evsel);
  598. perf_evsel__delete(evsel);
  599. }
  600. if (inject->strip)
  601. strip_fini(inject);
  602. }
  603. session->header.data_offset = output_data_offset;
  604. session->header.data_size = inject->bytes_written;
  605. perf_session__write_header(session, session->evlist, fd, true);
  606. }
  607. return ret;
  608. }
  609. int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
  610. {
  611. struct perf_inject inject = {
  612. .tool = {
  613. .sample = perf_event__repipe_sample,
  614. .mmap = perf_event__repipe,
  615. .mmap2 = perf_event__repipe,
  616. .comm = perf_event__repipe,
  617. .fork = perf_event__repipe,
  618. .exit = perf_event__repipe,
  619. .lost = perf_event__repipe,
  620. .lost_samples = perf_event__repipe,
  621. .aux = perf_event__repipe,
  622. .itrace_start = perf_event__repipe,
  623. .context_switch = perf_event__repipe,
  624. .read = perf_event__repipe_sample,
  625. .throttle = perf_event__repipe,
  626. .unthrottle = perf_event__repipe,
  627. .attr = perf_event__repipe_attr,
  628. .tracing_data = perf_event__repipe_op2_synth,
  629. .auxtrace_info = perf_event__repipe_op2_synth,
  630. .auxtrace = perf_event__repipe_auxtrace,
  631. .auxtrace_error = perf_event__repipe_op2_synth,
  632. .time_conv = perf_event__repipe_op2_synth,
  633. .finished_round = perf_event__repipe_oe_synth,
  634. .build_id = perf_event__repipe_op2_synth,
  635. .id_index = perf_event__repipe_op2_synth,
  636. },
  637. .input_name = "-",
  638. .samples = LIST_HEAD_INIT(inject.samples),
  639. .output = {
  640. .path = "-",
  641. .mode = PERF_DATA_MODE_WRITE,
  642. },
  643. };
  644. struct perf_data_file file = {
  645. .mode = PERF_DATA_MODE_READ,
  646. };
  647. int ret;
  648. struct option options[] = {
  649. OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
  650. "Inject build-ids into the output stream"),
  651. OPT_STRING('i', "input", &inject.input_name, "file",
  652. "input file name"),
  653. OPT_STRING('o', "output", &inject.output.path, "file",
  654. "output file name"),
  655. OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
  656. "Merge sched-stat and sched-switch for getting events "
  657. "where and how long tasks slept"),
  658. #ifdef HAVE_JITDUMP
  659. OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
  660. #endif
  661. OPT_INCR('v', "verbose", &verbose,
  662. "be more verbose (show build ids, etc)"),
  663. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
  664. "kallsyms pathname"),
  665. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  666. OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
  667. NULL, "opts", "Instruction Tracing options",
  668. itrace_parse_synth_opts),
  669. OPT_BOOLEAN(0, "strip", &inject.strip,
  670. "strip non-synthesized events (use with --itrace)"),
  671. OPT_END()
  672. };
  673. const char * const inject_usage[] = {
  674. "perf inject [<options>]",
  675. NULL
  676. };
  677. #ifndef HAVE_JITDUMP
  678. set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
  679. #endif
  680. argc = parse_options(argc, argv, options, inject_usage, 0);
  681. /*
  682. * Any (unrecognized) arguments left?
  683. */
  684. if (argc)
  685. usage_with_options(inject_usage, options);
  686. if (inject.strip && !inject.itrace_synth_opts.set) {
  687. pr_err("--strip option requires --itrace option\n");
  688. return -1;
  689. }
  690. if (perf_data_file__open(&inject.output)) {
  691. perror("failed to create output file");
  692. return -1;
  693. }
  694. inject.tool.ordered_events = inject.sched_stat;
  695. file.path = inject.input_name;
  696. inject.session = perf_session__new(&file, true, &inject.tool);
  697. if (inject.session == NULL)
  698. return -1;
  699. if (inject.build_ids) {
  700. /*
  701. * to make sure the mmap records are ordered correctly
  702. * and so that the correct especially due to jitted code
  703. * mmaps. We cannot generate the buildid hit list and
  704. * inject the jit mmaps at the same time for now.
  705. */
  706. inject.tool.ordered_events = true;
  707. inject.tool.ordering_requires_timestamps = true;
  708. }
  709. #ifdef HAVE_JITDUMP
  710. if (inject.jit_mode) {
  711. inject.tool.mmap2 = perf_event__jit_repipe_mmap2;
  712. inject.tool.mmap = perf_event__jit_repipe_mmap;
  713. inject.tool.ordered_events = true;
  714. inject.tool.ordering_requires_timestamps = true;
  715. /*
  716. * JIT MMAP injection injects all MMAP events in one go, so it
  717. * does not obey finished_round semantics.
  718. */
  719. inject.tool.finished_round = perf_event__drop_oe;
  720. }
  721. #endif
  722. ret = symbol__init(&inject.session->header.env);
  723. if (ret < 0)
  724. goto out_delete;
  725. ret = __cmd_inject(&inject);
  726. out_delete:
  727. perf_session__delete(inject.session);
  728. return ret;
  729. }