builtin-inject.c 23 KB

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