builtin-inject.c 23 KB

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