blob: 5b50a4abf95f1a9a595b204f6fe71503619ec182 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Tom Zanussi454c4072010-05-01 01:41:20 -05002/*
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
Andrew Vagin26a031e2012-08-07 16:56:04 +040011#include "util/color.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030012#include "util/dso.h"
Namhyung Kim27c9c342020-10-12 16:02:13 +090013#include "util/vdso.h"
Andrew Vagin26a031e2012-08-07 16:56:04 +040014#include "util/evlist.h"
15#include "util/evsel.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010016#include "util/map.h"
Tom Zanussi454c4072010-05-01 01:41:20 -050017#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020018#include "util/tool.h"
Tom Zanussi454c4072010-05-01 01:41:20 -050019#include "util/debug.h"
Andrew Vagin54a3cf52012-08-07 16:56:05 +040020#include "util/build-id.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020021#include "util/data.h"
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +030022#include "util/auxtrace.h"
Stephane Eranian9b07e272015-11-30 10:02:21 +010023#include "util/jit.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010024#include "util/symbol.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030025#include "util/synthetic-events.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030026#include "util/thread.h"
Namhyung Kim336c95b2020-10-12 16:02:11 +090027#include "util/namespaces.h"
Namhyung Kim75d48c52022-02-01 23:08:27 -080028#include "util/util.h"
Tom Zanussi454c4072010-05-01 01:41:20 -050029
Namhyung Kime7b60c52020-10-12 16:02:12 +090030#include <linux/err.h>
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060031#include <subcmd/parse-options.h>
Namhyung Kime7b60c52020-10-12 16:02:12 +090032#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
Tom Zanussi454c4072010-05-01 01:41:20 -050033
Andrew Vagin26a031e2012-08-07 16:56:04 +040034#include <linux/list.h>
Adrian Hunter83d7f5f2021-04-30 10:03:02 +030035#include <linux/string.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030036#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030037#include <signal.h>
Andrew Vagin26a031e2012-08-07 16:56:04 +040038
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -030039struct perf_inject {
Jiri Olsa34069122013-10-29 19:04:57 +010040 struct perf_tool tool;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +090041 struct perf_session *session;
Jiri Olsa34069122013-10-29 19:04:57 +010042 bool build_ids;
Namhyung Kim27c9c342020-10-12 16:02:13 +090043 bool build_id_all;
Jiri Olsa34069122013-10-29 19:04:57 +010044 bool sched_stat;
Adrian Huntercd10b282015-04-30 17:37:26 +030045 bool have_auxtrace;
Adrian Hunterf56fb982015-09-25 16:15:55 +030046 bool strip;
Stephane Eranian9b07e272015-11-30 10:02:21 +010047 bool jit_mode;
Adrian Hunter2a525f62021-04-30 10:03:01 +030048 bool in_place_update;
49 bool in_place_update_dry_run;
Namhyung Kimfea20d62021-07-19 15:31:51 -070050 bool is_pipe;
Jiri Olsa34069122013-10-29 19:04:57 +010051 const char *input_name;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010052 struct perf_data output;
Jiri Olsa34069122013-10-29 19:04:57 +010053 u64 bytes_written;
Adrian Hunter73117302015-09-25 16:15:54 +030054 u64 aux_id;
Jiri Olsa34069122013-10-29 19:04:57 +010055 struct list_head samples;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +030056 struct itrace_synth_opts itrace_synth_opts;
Adrian Hunterba2675b2019-11-15 14:42:18 +020057 char event_copy[PERF_SAMPLE_MAX_SIZE];
Andrew Vagin26a031e2012-08-07 16:56:04 +040058};
59
60struct event_entry {
61 struct list_head node;
62 u32 tid;
Gustavo A. R. Silva6549a8c2020-05-15 12:29:26 -050063 union perf_event event[];
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -030064};
Tom Zanussi454c4072010-05-01 01:41:20 -050065
Namhyung Kim27c9c342020-10-12 16:02:13 +090066static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
67 struct machine *machine, u8 cpumode, u32 flags);
68
Adrian Huntercd17a9b2015-04-21 12:21:54 +030069static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
Tom Zanussi454c4072010-05-01 01:41:20 -050070{
Jiri Olsa34069122013-10-29 19:04:57 +010071 ssize_t size;
Tom Zanussi454c4072010-05-01 01:41:20 -050072
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010073 size = perf_data__write(&inject->output, buf, sz);
Jiri Olsa34069122013-10-29 19:04:57 +010074 if (size < 0)
75 return -errno;
Tom Zanussi454c4072010-05-01 01:41:20 -050076
Jiri Olsa34069122013-10-29 19:04:57 +010077 inject->bytes_written += size;
Tom Zanussi454c4072010-05-01 01:41:20 -050078 return 0;
79}
80
Adrian Huntercd17a9b2015-04-21 12:21:54 +030081static int perf_event__repipe_synth(struct perf_tool *tool,
82 union perf_event *event)
83{
84 struct perf_inject *inject = container_of(tool, struct perf_inject,
85 tool);
86
87 return output_bytes(inject, event, event->header.size);
88}
89
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030090static int perf_event__repipe_oe_synth(struct perf_tool *tool,
91 union perf_event *event,
92 struct ordered_events *oe __maybe_unused)
93{
94 return perf_event__repipe_synth(tool, event);
95}
96
Jiri Olsae12b2022016-03-10 17:41:13 +010097#ifdef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +010098static int perf_event__drop_oe(struct perf_tool *tool __maybe_unused,
99 union perf_event *event __maybe_unused,
100 struct ordered_events *oe __maybe_unused)
101{
102 return 0;
103}
104#endif
105
Jiri Olsa89f16882018-09-13 14:54:03 +0200106static int perf_event__repipe_op2_synth(struct perf_session *session,
107 union perf_event *event)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200108{
Jiri Olsa89f16882018-09-13 14:54:03 +0200109 return perf_event__repipe_synth(session->tool, event);
Arnaldo Carvalho de Melo743eb8682011-11-28 07:56:39 -0200110}
111
Namhyung Kim2946ece2020-10-12 16:02:10 +0900112static int perf_event__repipe_op4_synth(struct perf_session *session,
113 union perf_event *event,
Alexey Bayduraev22920832022-01-17 21:34:36 +0300114 u64 data __maybe_unused,
115 const char *str __maybe_unused)
Namhyung Kim2946ece2020-10-12 16:02:10 +0900116{
117 return perf_event__repipe_synth(session->tool, event);
118}
119
Adrian Hunter47c3d102013-07-04 16:20:21 +0300120static int perf_event__repipe_attr(struct perf_tool *tool,
121 union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +0200122 struct evlist **pevlist)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200123{
Adrian Hunter89c97d92013-10-22 10:34:09 +0300124 struct perf_inject *inject = container_of(tool, struct perf_inject,
125 tool);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +0200126 int ret;
Adrian Hunter47c3d102013-07-04 16:20:21 +0300127
128 ret = perf_event__process_attr(tool, event, pevlist);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +0200129 if (ret)
130 return ret;
131
Namhyung Kimfea20d62021-07-19 15:31:51 -0700132 if (!inject->is_pipe)
Adrian Hunter89c97d92013-10-22 10:34:09 +0300133 return 0;
134
Adrian Hunter47c3d102013-07-04 16:20:21 +0300135 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200136}
137
Namhyung Kim2946ece2020-10-12 16:02:10 +0900138static int perf_event__repipe_event_update(struct perf_tool *tool,
139 union perf_event *event,
140 struct evlist **pevlist __maybe_unused)
141{
142 return perf_event__repipe_synth(tool, event);
143}
144
Adrian Huntere31f0d02015-04-30 17:37:27 +0300145#ifdef HAVE_AUXTRACE_SUPPORT
146
147static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
148{
149 char buf[4096];
150 ssize_t ssz;
151 int ret;
152
153 while (size > 0) {
154 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
155 if (ssz < 0)
156 return -errno;
157 ret = output_bytes(inject, buf, ssz);
158 if (ret)
159 return ret;
160 size -= ssz;
161 }
162
163 return 0;
164}
165
Jiri Olsa73365552018-09-13 14:54:04 +0200166static s64 perf_event__repipe_auxtrace(struct perf_session *session,
167 union perf_event *event)
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300168{
Jiri Olsa73365552018-09-13 14:54:04 +0200169 struct perf_tool *tool = session->tool;
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300170 struct perf_inject *inject = container_of(tool, struct perf_inject,
171 tool);
172 int ret;
173
Adrian Huntercd10b282015-04-30 17:37:26 +0300174 inject->have_auxtrace = true;
175
Adrian Hunter99fa2982015-04-30 17:37:25 +0300176 if (!inject->output.is_pipe) {
177 off_t offset;
178
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100179 offset = lseek(inject->output.file.fd, 0, SEEK_CUR);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300180 if (offset == -1)
181 return -errno;
182 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
183 event, offset);
184 if (ret < 0)
185 return ret;
186 }
187
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100188 if (perf_data__is_pipe(session->data) || !session->one_mmap) {
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300189 ret = output_bytes(inject, event, event->header.size);
190 if (ret < 0)
191 return ret;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100192 ret = copy_bytes(inject, perf_data__fd(session->data),
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300193 event->auxtrace.size);
194 } else {
195 ret = output_bytes(inject, event,
196 event->header.size + event->auxtrace.size);
197 }
198 if (ret < 0)
199 return ret;
200
201 return event->auxtrace.size;
202}
203
Adrian Huntere31f0d02015-04-30 17:37:27 +0300204#else
205
206static s64
Jiri Olsa73365552018-09-13 14:54:04 +0200207perf_event__repipe_auxtrace(struct perf_session *session __maybe_unused,
208 union perf_event *event __maybe_unused)
Adrian Huntere31f0d02015-04-30 17:37:27 +0300209{
210 pr_err("AUX area tracing not supported\n");
211 return -EINVAL;
212}
213
214#endif
215
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200216static int perf_event__repipe(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200217 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300218 struct perf_sample *sample __maybe_unused,
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300219 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200220{
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300221 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200222}
223
Adrian Hunterf56fb982015-09-25 16:15:55 +0300224static int perf_event__drop(struct perf_tool *tool __maybe_unused,
225 union perf_event *event __maybe_unused,
226 struct perf_sample *sample __maybe_unused,
227 struct machine *machine __maybe_unused)
228{
229 return 0;
230}
231
Adrian Hunter73117302015-09-25 16:15:54 +0300232static int perf_event__drop_aux(struct perf_tool *tool,
233 union perf_event *event __maybe_unused,
234 struct perf_sample *sample,
235 struct machine *machine __maybe_unused)
236{
237 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
238
239 if (!inject->aux_id)
240 inject->aux_id = sample->id;
241
242 return 0;
243}
244
Adrian Hunterba2675b2019-11-15 14:42:18 +0200245static union perf_event *
246perf_inject__cut_auxtrace_sample(struct perf_inject *inject,
247 union perf_event *event,
248 struct perf_sample *sample)
249{
250 size_t sz1 = sample->aux_sample.data - (void *)event;
251 size_t sz2 = event->header.size - sample->aux_sample.size - sz1;
252 union perf_event *ev = (union perf_event *)inject->event_copy;
253
254 if (sz1 > event->header.size || sz2 > event->header.size ||
255 sz1 + sz2 > event->header.size ||
256 sz1 < sizeof(struct perf_event_header) + sizeof(u64))
257 return event;
258
259 memcpy(ev, event, sz1);
260 memcpy((void *)ev + sz1, (void *)event + event->header.size - sz2, sz2);
261 ev->header.size = sz1 + sz2;
262 ((u64 *)((void *)ev + sz1))[-1] = 0;
263
264 return ev;
265}
266
Andrew Vagin26a031e2012-08-07 16:56:04 +0400267typedef int (*inject_handler)(struct perf_tool *tool,
268 union perf_event *event,
269 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200270 struct evsel *evsel,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400271 struct machine *machine);
272
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200273static int perf_event__repipe_sample(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200274 union perf_event *event,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400275 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200276 struct evsel *evsel,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400277 struct machine *machine)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300278{
Adrian Hunterba2675b2019-11-15 14:42:18 +0200279 struct perf_inject *inject = container_of(tool, struct perf_inject,
280 tool);
281
Arnaldo Carvalho de Melo40978e92019-07-03 16:02:09 -0300282 if (evsel && evsel->handler) {
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300283 inject_handler f = evsel->handler;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400284 return f(tool, event, sample, evsel, machine);
285 }
286
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400287 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
288
Adrian Hunterba2675b2019-11-15 14:42:18 +0200289 if (inject->itrace_synth_opts.set && sample->aux_sample.size)
290 event = perf_inject__cut_auxtrace_sample(inject, event, sample);
291
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300292 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300293}
294
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200295static int perf_event__repipe_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200296 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200297 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb8682011-11-28 07:56:39 -0200298 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500299{
300 int err;
301
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200302 err = perf_event__process_mmap(tool, event, sample, machine);
303 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500304
305 return err;
306}
307
Jiri Olsae12b2022016-03-10 17:41:13 +0100308#ifdef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +0100309static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
310 union perf_event *event,
311 struct perf_sample *sample,
312 struct machine *machine)
313{
314 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
315 u64 n = 0;
Adrian Hunter570735b2016-03-07 16:44:40 -0300316 int ret;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100317
318 /*
319 * if jit marker, then inject jit mmaps and generate ELF images
320 */
Adrian Hunter570735b2016-03-07 16:44:40 -0300321 ret = jit_process(inject->session, &inject->output, machine,
Yonatan Goldschmidt67dec922020-11-05 03:56:04 +0200322 event->mmap.filename, event->mmap.pid, event->mmap.tid, &n);
Adrian Hunter570735b2016-03-07 16:44:40 -0300323 if (ret < 0)
324 return ret;
325 if (ret) {
Stephane Eranian9b07e272015-11-30 10:02:21 +0100326 inject->bytes_written += n;
327 return 0;
328 }
329 return perf_event__repipe_mmap(tool, event, sample, machine);
330}
331#endif
332
Namhyung Kim27c9c342020-10-12 16:02:13 +0900333static struct dso *findnew_dso(int pid, int tid, const char *filename,
334 struct dso_id *id, struct machine *machine)
335{
336 struct thread *thread;
337 struct nsinfo *nsi = NULL;
338 struct nsinfo *nnsi;
339 struct dso *dso;
340 bool vdso;
341
342 thread = machine__findnew_thread(machine, pid, tid);
343 if (thread == NULL) {
344 pr_err("cannot find or create a task %d/%d.\n", tid, pid);
345 return NULL;
346 }
347
348 vdso = is_vdso_map(filename);
349 nsi = nsinfo__get(thread->nsinfo);
350
351 if (vdso) {
352 /* The vdso maps are always on the host and not the
353 * container. Ensure that we don't use setns to look
354 * them up.
355 */
356 nnsi = nsinfo__copy(nsi);
357 if (nnsi) {
358 nsinfo__put(nsi);
Ian Rogersbcaf0a92022-02-11 02:34:06 -0800359 nsinfo__clear_need_setns(nnsi);
Namhyung Kim27c9c342020-10-12 16:02:13 +0900360 nsi = nnsi;
361 }
362 dso = machine__findnew_vdso(machine, thread);
363 } else {
364 dso = machine__findnew_dso_id(machine, filename, id);
365 }
366
Riccardo Mancini0967ebf2021-07-15 18:07:06 +0200367 if (dso) {
368 nsinfo__put(dso->nsinfo);
Namhyung Kim27c9c342020-10-12 16:02:13 +0900369 dso->nsinfo = nsi;
Riccardo Mancini0967ebf2021-07-15 18:07:06 +0200370 } else
Namhyung Kim27c9c342020-10-12 16:02:13 +0900371 nsinfo__put(nsi);
372
373 thread__put(thread);
374 return dso;
375}
376
377static int perf_event__repipe_buildid_mmap(struct perf_tool *tool,
378 union perf_event *event,
379 struct perf_sample *sample,
380 struct machine *machine)
381{
382 struct dso *dso;
383
384 dso = findnew_dso(event->mmap.pid, event->mmap.tid,
385 event->mmap.filename, NULL, machine);
386
387 if (dso && !dso->hit) {
388 dso->hit = 1;
389 dso__inject_build_id(dso, tool, machine, sample->cpumode, 0);
Namhyung Kim27c9c342020-10-12 16:02:13 +0900390 }
Namhyung Kim0c3f7b32021-05-24 15:50:50 -0700391 dso__put(dso);
Namhyung Kim27c9c342020-10-12 16:02:13 +0900392
393 return perf_event__repipe(tool, event, sample, machine);
394}
395
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200396static int perf_event__repipe_mmap2(struct perf_tool *tool,
397 union perf_event *event,
398 struct perf_sample *sample,
399 struct machine *machine)
400{
401 int err;
402
403 err = perf_event__process_mmap2(tool, event, sample, machine);
404 perf_event__repipe(tool, event, sample, machine);
405
Namhyung Kimf7fc0d12021-05-24 15:50:51 -0700406 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
407 struct dso *dso;
408
409 dso = findnew_dso(event->mmap2.pid, event->mmap2.tid,
410 event->mmap2.filename, NULL, machine);
411 if (dso) {
412 /* mark it not to inject build-id */
413 dso->hit = 1;
414 }
415 dso__put(dso);
416 }
417
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200418 return err;
419}
420
Jiri Olsae12b2022016-03-10 17:41:13 +0100421#ifdef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +0100422static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
423 union perf_event *event,
424 struct perf_sample *sample,
425 struct machine *machine)
426{
427 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
428 u64 n = 0;
Adrian Hunter570735b2016-03-07 16:44:40 -0300429 int ret;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100430
431 /*
432 * if jit marker, then inject jit mmaps and generate ELF images
433 */
Adrian Hunter570735b2016-03-07 16:44:40 -0300434 ret = jit_process(inject->session, &inject->output, machine,
Yonatan Goldschmidt67dec922020-11-05 03:56:04 +0200435 event->mmap2.filename, event->mmap2.pid, event->mmap2.tid, &n);
Adrian Hunter570735b2016-03-07 16:44:40 -0300436 if (ret < 0)
437 return ret;
438 if (ret) {
Stephane Eranian9b07e272015-11-30 10:02:21 +0100439 inject->bytes_written += n;
440 return 0;
441 }
442 return perf_event__repipe_mmap2(tool, event, sample, machine);
443}
444#endif
445
Namhyung Kim27c9c342020-10-12 16:02:13 +0900446static int perf_event__repipe_buildid_mmap2(struct perf_tool *tool,
447 union perf_event *event,
448 struct perf_sample *sample,
449 struct machine *machine)
450{
451 struct dso_id dso_id = {
452 .maj = event->mmap2.maj,
453 .min = event->mmap2.min,
454 .ino = event->mmap2.ino,
455 .ino_generation = event->mmap2.ino_generation,
456 };
457 struct dso *dso;
458
Namhyung Kimf7fc0d12021-05-24 15:50:51 -0700459 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
460 /* cannot use dso_id since it'd have invalid info */
461 dso = findnew_dso(event->mmap2.pid, event->mmap2.tid,
462 event->mmap2.filename, NULL, machine);
463 if (dso) {
464 /* mark it not to inject build-id */
465 dso->hit = 1;
466 }
467 dso__put(dso);
468 return 0;
469 }
470
Namhyung Kim27c9c342020-10-12 16:02:13 +0900471 dso = findnew_dso(event->mmap2.pid, event->mmap2.tid,
472 event->mmap2.filename, &dso_id, machine);
473
474 if (dso && !dso->hit) {
475 dso->hit = 1;
476 dso__inject_build_id(dso, tool, machine, sample->cpumode,
477 event->mmap2.flags);
Namhyung Kim27c9c342020-10-12 16:02:13 +0900478 }
Namhyung Kim0c3f7b32021-05-24 15:50:50 -0700479 dso__put(dso);
Namhyung Kim27c9c342020-10-12 16:02:13 +0900480
481 perf_event__repipe(tool, event, sample, machine);
482
483 return 0;
484}
485
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300486static int perf_event__repipe_fork(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200487 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200488 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb8682011-11-28 07:56:39 -0200489 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500490{
491 int err;
492
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300493 err = perf_event__process_fork(tool, event, sample, machine);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200494 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500495
496 return err;
497}
498
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300499static int perf_event__repipe_comm(struct perf_tool *tool,
500 union perf_event *event,
501 struct perf_sample *sample,
502 struct machine *machine)
503{
504 int err;
505
506 err = perf_event__process_comm(tool, event, sample, machine);
507 perf_event__repipe(tool, event, sample, machine);
508
509 return err;
510}
511
Hari Bathinif3b36142017-03-08 02:11:43 +0530512static int perf_event__repipe_namespaces(struct perf_tool *tool,
513 union perf_event *event,
514 struct perf_sample *sample,
515 struct machine *machine)
516{
517 int err = perf_event__process_namespaces(tool, event, sample, machine);
518
519 perf_event__repipe(tool, event, sample, machine);
520
521 return err;
522}
523
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300524static int perf_event__repipe_exit(struct perf_tool *tool,
525 union perf_event *event,
526 struct perf_sample *sample,
527 struct machine *machine)
528{
529 int err;
530
531 err = perf_event__process_exit(tool, event, sample, machine);
532 perf_event__repipe(tool, event, sample, machine);
533
534 return err;
535}
536
Jiri Olsa89f16882018-09-13 14:54:03 +0200537static int perf_event__repipe_tracing_data(struct perf_session *session,
538 union perf_event *event)
Tom Zanussi454c4072010-05-01 01:41:20 -0500539{
Jiri Olsa89f16882018-09-13 14:54:03 +0200540 perf_event__repipe_synth(session->tool, event);
Tom Zanussi454c4072010-05-01 01:41:20 -0500541
Minghao Chif0ac5b852022-01-12 08:01:09 +0000542 return perf_event__process_tracing_data(session, event);
Tom Zanussi454c4072010-05-01 01:41:20 -0500543}
544
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300545static int dso__read_build_id(struct dso *dso)
Tom Zanussi454c4072010-05-01 01:41:20 -0500546{
Namhyung Kim336c95b2020-10-12 16:02:11 +0900547 struct nscookie nsc;
548
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300549 if (dso->has_build_id)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300550 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500551
Namhyung Kim336c95b2020-10-12 16:02:11 +0900552 nsinfo__mountns_enter(dso->nsinfo, &nsc);
Jiri Olsaf7668192020-10-13 21:24:34 +0200553 if (filename__read_build_id(dso->long_name, &dso->bid) > 0)
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300554 dso->has_build_id = true;
Namhyung Kim75d48c52022-02-01 23:08:27 -0800555 else if (dso->nsinfo) {
556 char *new_name;
557
558 new_name = filename_with_chroot(dso->nsinfo->pid,
559 dso->long_name);
560 if (new_name && filename__read_build_id(new_name, &dso->bid) > 0)
561 dso->has_build_id = true;
562 free(new_name);
563 }
Namhyung Kim336c95b2020-10-12 16:02:11 +0900564 nsinfo__mountns_exit(&nsc);
Tom Zanussi454c4072010-05-01 01:41:20 -0500565
Namhyung Kim336c95b2020-10-12 16:02:11 +0900566 return dso->has_build_id ? 0 : -1;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300567}
Tom Zanussi454c4072010-05-01 01:41:20 -0500568
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300569static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
Namhyung Kime7b60c52020-10-12 16:02:12 +0900570 struct machine *machine, u8 cpumode, u32 flags)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300571{
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300572 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500573
Namhyung Kime7b60c52020-10-12 16:02:12 +0900574 if (is_anon_memory(dso->long_name) || flags & MAP_HUGETLB)
575 return 0;
576 if (is_no_dso_memory(dso->long_name))
577 return 0;
578
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300579 if (dso__read_build_id(dso) < 0) {
580 pr_debug("no build_id found for %s\n", dso->long_name);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300581 return -1;
582 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500583
Namhyung Kime7b60c52020-10-12 16:02:12 +0900584 err = perf_event__synthesize_build_id(tool, dso, cpumode,
585 perf_event__repipe, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300586 if (err) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300587 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500588 return -1;
589 }
590
591 return 0;
592}
593
Namhyung Kim0bf02a02020-10-12 16:02:09 +0900594int perf_event__inject_buildid(struct perf_tool *tool, union perf_event *event,
595 struct perf_sample *sample,
596 struct evsel *evsel __maybe_unused,
597 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500598{
599 struct addr_location al;
600 struct thread *thread;
Tom Zanussi454c4072010-05-01 01:41:20 -0500601
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900602 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Tom Zanussi454c4072010-05-01 01:41:20 -0500603 if (thread == NULL) {
604 pr_err("problem processing %d event, skipping it.\n",
605 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500606 goto repipe;
607 }
608
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300609 if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) {
Tom Zanussi454c4072010-05-01 01:41:20 -0500610 if (!al.map->dso->hit) {
611 al.map->dso->hit = 1;
Namhyung Kime7b60c52020-10-12 16:02:12 +0900612 dso__inject_build_id(al.map->dso, tool, machine,
613 sample->cpumode, al.map->flags);
Tom Zanussi454c4072010-05-01 01:41:20 -0500614 }
615 }
616
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300617 thread__put(thread);
Tom Zanussi454c4072010-05-01 01:41:20 -0500618repipe:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200619 perf_event__repipe(tool, event, sample, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300620 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500621}
622
Andrew Vagin26a031e2012-08-07 16:56:04 +0400623static int perf_inject__sched_process_exit(struct perf_tool *tool,
624 union perf_event *event __maybe_unused,
625 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200626 struct evsel *evsel __maybe_unused,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400627 struct machine *machine __maybe_unused)
628{
629 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
630 struct event_entry *ent;
631
632 list_for_each_entry(ent, &inject->samples, node) {
633 if (sample->tid == ent->tid) {
634 list_del_init(&ent->node);
635 free(ent);
636 break;
637 }
638 }
639
640 return 0;
641}
642
643static int perf_inject__sched_switch(struct perf_tool *tool,
644 union perf_event *event,
645 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200646 struct evsel *evsel,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400647 struct machine *machine)
648{
649 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
650 struct event_entry *ent;
651
652 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
653
654 ent = malloc(event->header.size + sizeof(struct event_entry));
655 if (ent == NULL) {
656 color_fprintf(stderr, PERF_COLOR_RED,
657 "Not enough memory to process sched switch event!");
658 return -1;
659 }
660
661 ent->tid = sample->tid;
662 memcpy(&ent->event, event, event->header.size);
663 list_add(&ent->node, &inject->samples);
664 return 0;
665}
666
667static int perf_inject__sched_stat(struct perf_tool *tool,
668 union perf_event *event __maybe_unused,
669 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200670 struct evsel *evsel,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400671 struct machine *machine)
672{
673 struct event_entry *ent;
674 union perf_event *event_sw;
675 struct perf_sample sample_sw;
676 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -0300677 u32 pid = evsel__intval(evsel, sample, "pid");
Andrew Vagin26a031e2012-08-07 16:56:04 +0400678
679 list_for_each_entry(ent, &inject->samples, node) {
680 if (pid == ent->tid)
681 goto found;
682 }
683
684 return 0;
685found:
686 event_sw = &ent->event[0];
Arnaldo Carvalho de Melo6b6017a2020-04-30 11:03:49 -0300687 evsel__parse_sample(evsel, event_sw, &sample_sw);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400688
689 sample_sw.period = sample->period;
690 sample_sw.time = sample->time;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200691 perf_event__synthesize_sample(event_sw, evsel->core.attr.sample_type,
692 evsel->core.attr.read_format, &sample_sw);
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400693 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400694 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
695}
696
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300697static void sig_handler(int sig __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500698{
699 session_done = 1;
700}
701
Arnaldo Carvalho de Melob14b36d2020-05-04 13:57:44 -0300702static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg)
Andrew Vagin26a031e2012-08-07 16:56:04 +0400703{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200704 struct perf_event_attr *attr = &evsel->core.attr;
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300705 const char *name = evsel__name(evsel);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400706
707 if (!(attr->sample_type & sample_type)) {
708 pr_err("Samples for %s event do not have %s attribute set.",
709 name, sample_msg);
710 return -EINVAL;
711 }
712
713 return 0;
714}
715
Adrian Hunterf56fb982015-09-25 16:15:55 +0300716static int drop_sample(struct perf_tool *tool __maybe_unused,
717 union perf_event *event __maybe_unused,
718 struct perf_sample *sample __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200719 struct evsel *evsel __maybe_unused,
Adrian Hunterf56fb982015-09-25 16:15:55 +0300720 struct machine *machine __maybe_unused)
721{
722 return 0;
723}
724
725static void strip_init(struct perf_inject *inject)
726{
Jiri Olsa63503db2019-07-21 13:23:52 +0200727 struct evlist *evlist = inject->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200728 struct evsel *evsel;
Adrian Hunterf56fb982015-09-25 16:15:55 +0300729
730 inject->tool.context_switch = perf_event__drop;
731
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300732 evlist__for_each_entry(evlist, evsel)
Adrian Hunterf56fb982015-09-25 16:15:55 +0300733 evsel->handler = drop_sample;
734}
735
Adrian Hunter83d7f5f2021-04-30 10:03:02 +0300736static int parse_vm_time_correlation(const struct option *opt, const char *str, int unset)
737{
738 struct perf_inject *inject = opt->value;
739 const char *args;
740 char *dry_run;
741
742 if (unset)
743 return 0;
744
745 inject->itrace_synth_opts.set = true;
746 inject->itrace_synth_opts.vm_time_correlation = true;
747 inject->in_place_update = true;
748
749 if (!str)
750 return 0;
751
752 dry_run = skip_spaces(str);
753 if (!strncmp(dry_run, "dry-run", strlen("dry-run"))) {
754 inject->itrace_synth_opts.vm_tm_corr_dry_run = true;
755 inject->in_place_update_dry_run = true;
756 args = dry_run + strlen("dry-run");
757 } else {
758 args = str;
759 }
760
761 inject->itrace_synth_opts.vm_tm_corr_args = strdup(args);
762
763 return inject->itrace_synth_opts.vm_tm_corr_args ? 0 : -ENOMEM;
764}
765
Adrian Hunterc271a552021-12-13 10:48:29 +0200766static int output_fd(struct perf_inject *inject)
767{
768 return inject->in_place_update ? -1 : perf_data__fd(&inject->output);
769}
770
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300771static int __cmd_inject(struct perf_inject *inject)
Tom Zanussi454c4072010-05-01 01:41:20 -0500772{
Tom Zanussi454c4072010-05-01 01:41:20 -0500773 int ret = -EINVAL;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900774 struct perf_session *session = inject->session;
Adrian Hunterc271a552021-12-13 10:48:29 +0200775 int fd = output_fd(inject);
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300776 u64 output_data_offset;
Tom Zanussi454c4072010-05-01 01:41:20 -0500777
778 signal(SIGINT, sig_handler);
779
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300780 if (inject->build_ids || inject->sched_stat ||
Namhyung Kim27c9c342020-10-12 16:02:13 +0900781 inject->itrace_synth_opts.set || inject->build_id_all) {
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300782 inject->tool.mmap = perf_event__repipe_mmap;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200783 inject->tool.mmap2 = perf_event__repipe_mmap2;
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300784 inject->tool.fork = perf_event__repipe_fork;
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300785 inject->tool.tracing_data = perf_event__repipe_tracing_data;
Tom Zanussi454c4072010-05-01 01:41:20 -0500786 }
787
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300788 output_data_offset = session->header.data_offset;
789
Namhyung Kim27c9c342020-10-12 16:02:13 +0900790 if (inject->build_id_all) {
791 inject->tool.mmap = perf_event__repipe_buildid_mmap;
792 inject->tool.mmap2 = perf_event__repipe_buildid_mmap2;
793 } else if (inject->build_ids) {
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400794 inject->tool.sample = perf_event__inject_buildid;
795 } else if (inject->sched_stat) {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200796 struct evsel *evsel;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400797
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300798 evlist__for_each_entry(session->evlist, evsel) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300799 const char *name = evsel__name(evsel);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400800
801 if (!strcmp(name, "sched:sched_switch")) {
Arnaldo Carvalho de Melob14b36d2020-05-04 13:57:44 -0300802 if (evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
Andrew Vagin26a031e2012-08-07 16:56:04 +0400803 return -EINVAL;
804
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300805 evsel->handler = perf_inject__sched_switch;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400806 } else if (!strcmp(name, "sched:sched_process_exit"))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300807 evsel->handler = perf_inject__sched_process_exit;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400808 else if (!strncmp(name, "sched:sched_stat_", 17))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300809 evsel->handler = perf_inject__sched_stat;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400810 }
Adrian Hunter83d7f5f2021-04-30 10:03:02 +0300811 } else if (inject->itrace_synth_opts.vm_time_correlation) {
812 session->itrace_synth_opts = &inject->itrace_synth_opts;
813 memset(&inject->tool, 0, sizeof(inject->tool));
814 inject->tool.id_index = perf_event__process_id_index;
815 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
816 inject->tool.auxtrace = perf_event__process_auxtrace;
817 inject->tool.auxtrace_error = perf_event__process_auxtrace_error;
818 inject->tool.ordered_events = true;
819 inject->tool.ordering_requires_timestamps = true;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300820 } else if (inject->itrace_synth_opts.set) {
821 session->itrace_synth_opts = &inject->itrace_synth_opts;
822 inject->itrace_synth_opts.inject = true;
823 inject->tool.comm = perf_event__repipe_comm;
Hari Bathinif3b36142017-03-08 02:11:43 +0530824 inject->tool.namespaces = perf_event__repipe_namespaces;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300825 inject->tool.exit = perf_event__repipe_exit;
Adrian Hunter29f6eec2019-12-04 14:08:00 +0200826 inject->tool.id_index = perf_event__process_id_index;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300827 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
828 inject->tool.auxtrace = perf_event__process_auxtrace;
Adrian Hunter73117302015-09-25 16:15:54 +0300829 inject->tool.aux = perf_event__drop_aux;
Adrian Hunter61750472021-09-07 19:39:02 +0300830 inject->tool.itrace_start = perf_event__drop_aux;
831 inject->tool.aux_output_hw_id = perf_event__drop_aux;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300832 inject->tool.ordered_events = true;
833 inject->tool.ordering_requires_timestamps = true;
834 /* Allow space in the header for new attributes */
Adrian Hunterc29d9792021-11-25 09:14:57 +0200835 output_data_offset = roundup(8192 + session->header.data_offset, 4096);
Adrian Hunterf56fb982015-09-25 16:15:55 +0300836 if (inject->strip)
837 strip_init(inject);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400838 }
839
Adrian Hunter99fa2982015-04-30 17:37:25 +0300840 if (!inject->itrace_synth_opts.set)
841 auxtrace_index__free(&session->auxtrace_index);
842
Namhyung Kimfea20d62021-07-19 15:31:51 -0700843 if (!inject->is_pipe && !inject->in_place_update)
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300844 lseek(fd, output_data_offset, SEEK_SET);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400845
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300846 ret = perf_session__process_events(session);
David Carrillo-Cisnerosbb8d5212017-04-10 13:14:26 -0700847 if (ret)
848 return ret;
Tom Zanussi454c4072010-05-01 01:41:20 -0500849
Namhyung Kimfea20d62021-07-19 15:31:51 -0700850 if (!inject->is_pipe && !inject->in_place_update) {
Adrian Hunter640dad42016-03-07 16:44:38 -0300851 if (inject->build_ids)
Adrian Huntere38b43c2014-07-14 13:02:34 +0300852 perf_header__set_feat(&session->header,
853 HEADER_BUILD_ID);
Adrian Hunter640dad42016-03-07 16:44:38 -0300854 /*
855 * Keep all buildids when there is unprocessed AUX data because
856 * it is not known which ones the AUX trace hits.
857 */
858 if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) &&
859 inject->have_auxtrace && !inject->itrace_synth_opts.set)
860 dsos__hit_all(session);
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300861 /*
862 * The AUX areas have been removed and replaced with
Al Grant1c756cd2020-11-13 20:38:26 +0000863 * synthesized hardware events, so clear the feature flag.
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300864 */
Adrian Hunter051a01b2015-09-25 16:15:43 +0300865 if (inject->itrace_synth_opts.set) {
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300866 perf_header__clear_feat(&session->header,
867 HEADER_AUXTRACE);
Adrian Hunterec90e422020-04-29 18:07:46 +0300868 if (inject->itrace_synth_opts.last_branch ||
869 inject->itrace_synth_opts.add_last_branch)
Adrian Hunter051a01b2015-09-25 16:15:43 +0300870 perf_header__set_feat(&session->header,
871 HEADER_BRANCH_STACK);
872 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300873 session->header.data_offset = output_data_offset;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400874 session->header.data_size = inject->bytes_written;
Namhyung Kim42aa2762015-01-29 17:06:48 +0900875 perf_session__write_header(session, session->evlist, fd, true);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400876 }
877
Tom Zanussi454c4072010-05-01 01:41:20 -0500878 return ret;
879}
880
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300881int cmd_inject(int argc, const char **argv)
Tom Zanussi454c4072010-05-01 01:41:20 -0500882{
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300883 struct perf_inject inject = {
884 .tool = {
885 .sample = perf_event__repipe_sample,
Namhyung Kim2946ece2020-10-12 16:02:10 +0900886 .read = perf_event__repipe_sample,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300887 .mmap = perf_event__repipe,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200888 .mmap2 = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300889 .comm = perf_event__repipe,
Namhyung Kim2946ece2020-10-12 16:02:10 +0900890 .namespaces = perf_event__repipe,
891 .cgroup = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300892 .fork = perf_event__repipe,
893 .exit = perf_event__repipe,
894 .lost = perf_event__repipe,
Adrian Hunterd8145b32015-11-13 11:48:32 +0200895 .lost_samples = perf_event__repipe,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300896 .aux = perf_event__repipe,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300897 .itrace_start = perf_event__repipe,
Adrian Hunter61750472021-09-07 19:39:02 +0300898 .aux_output_hw_id = perf_event__repipe,
Adrian Hunter02860392015-07-21 12:44:03 +0300899 .context_switch = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300900 .throttle = perf_event__repipe,
901 .unthrottle = perf_event__repipe,
Namhyung Kim2946ece2020-10-12 16:02:10 +0900902 .ksymbol = perf_event__repipe,
903 .bpf = perf_event__repipe,
904 .text_poke = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300905 .attr = perf_event__repipe_attr,
Namhyung Kim2946ece2020-10-12 16:02:10 +0900906 .event_update = perf_event__repipe_event_update,
Adrian Hunter47c3d102013-07-04 16:20:21 +0300907 .tracing_data = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300908 .finished_round = perf_event__repipe_oe_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300909 .build_id = perf_event__repipe_op2_synth,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200910 .id_index = perf_event__repipe_op2_synth,
Namhyung Kim2946ece2020-10-12 16:02:10 +0900911 .auxtrace_info = perf_event__repipe_op2_synth,
912 .auxtrace_error = perf_event__repipe_op2_synth,
913 .time_conv = perf_event__repipe_op2_synth,
914 .thread_map = perf_event__repipe_op2_synth,
915 .cpu_map = perf_event__repipe_op2_synth,
916 .stat_config = perf_event__repipe_op2_synth,
917 .stat = perf_event__repipe_op2_synth,
918 .stat_round = perf_event__repipe_op2_synth,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -0700919 .feature = perf_event__repipe_op2_synth,
Namhyung Kim2946ece2020-10-12 16:02:10 +0900920 .compressed = perf_event__repipe_op4_synth,
921 .auxtrace = perf_event__repipe_auxtrace,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300922 },
Andrew Vagine558a5b2012-08-07 16:56:02 +0400923 .input_name = "-",
Andrew Vagin26a031e2012-08-07 16:56:04 +0400924 .samples = LIST_HEAD_INIT(inject.samples),
Jiri Olsa34069122013-10-29 19:04:57 +0100925 .output = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +0100926 .path = "-",
927 .mode = PERF_DATA_MODE_WRITE,
Namhyung Kim60136662020-10-30 14:47:42 +0900928 .use_stdio = true,
Jiri Olsa34069122013-10-29 19:04:57 +0100929 },
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300930 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100931 struct perf_data data = {
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900932 .mode = PERF_DATA_MODE_READ,
Namhyung Kim60136662020-10-30 14:47:42 +0900933 .use_stdio = true,
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900934 };
935 int ret;
Namhyung Kimc3a057d2021-07-19 15:31:52 -0700936 bool repipe = true;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900937
Stephane Eranian9b07e272015-11-30 10:02:21 +0100938 struct option options[] = {
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300939 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
940 "Inject build-ids into the output stream"),
Namhyung Kim27c9c342020-10-12 16:02:13 +0900941 OPT_BOOLEAN(0, "buildid-all", &inject.build_id_all,
942 "Inject build-ids of all DSOs into the output stream"),
Andrew Vagine558a5b2012-08-07 16:56:02 +0400943 OPT_STRING('i', "input", &inject.input_name, "file",
944 "input file name"),
Jiri Olsa2d4f2792019-02-21 10:41:30 +0100945 OPT_STRING('o', "output", &inject.output.path, "file",
Andrew Vagine558a5b2012-08-07 16:56:02 +0400946 "output file name"),
Andrew Vagin26a031e2012-08-07 16:56:04 +0400947 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
948 "Merge sched-stat and sched-switch for getting events "
949 "where and how long tasks slept"),
Jiri Olsae12b2022016-03-10 17:41:13 +0100950#ifdef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +0100951 OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
Jiri Olsae12b2022016-03-10 17:41:13 +0100952#endif
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300953 OPT_INCR('v', "verbose", &verbose,
954 "be more verbose (show build ids, etc)"),
James Clarkb3a018f2021-10-18 14:48:43 +0100955 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
956 "file", "vmlinux pathname"),
957 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
958 "don't load vmlinux even if found"),
Adrian Huntera7a2b8b2014-07-22 16:17:38 +0300959 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
960 "kallsyms pathname"),
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100961 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300962 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
Andi Kleenc12e0392018-09-13 20:10:31 -0700963 NULL, "opts", "Instruction Tracing options\n"
964 ITRACE_HELP,
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300965 itrace_parse_synth_opts),
Adrian Hunterf56fb982015-09-25 16:15:55 +0300966 OPT_BOOLEAN(0, "strip", &inject.strip,
967 "strip non-synthesized events (use with --itrace)"),
Adrian Hunter83d7f5f2021-04-30 10:03:02 +0300968 OPT_CALLBACK_OPTARG(0, "vm-time-correlation", &inject, NULL, "opts",
969 "correlate time between VM guests and the host",
970 parse_vm_time_correlation),
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300971 OPT_END()
972 };
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300973 const char * const inject_usage[] = {
974 "perf inject [<options>]",
975 NULL
976 };
Jiri Olsae12b2022016-03-10 17:41:13 +0100977#ifndef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +0100978 set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
979#endif
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300980 argc = parse_options(argc, argv, options, inject_usage, 0);
Tom Zanussi454c4072010-05-01 01:41:20 -0500981
982 /*
983 * Any (unrecognized) arguments left?
984 */
985 if (argc)
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300986 usage_with_options(inject_usage, options);
Tom Zanussi454c4072010-05-01 01:41:20 -0500987
Adrian Hunterf56fb982015-09-25 16:15:55 +0300988 if (inject.strip && !inject.itrace_synth_opts.set) {
989 pr_err("--strip option requires --itrace option\n");
990 return -1;
991 }
992
James Clarkb3a018f2021-10-18 14:48:43 +0100993 if (symbol__validate_sym_arguments())
994 return -1;
995
Adrian Hunter2a525f62021-04-30 10:03:01 +0300996 if (inject.in_place_update) {
997 if (!strcmp(inject.input_name, "-")) {
998 pr_err("Input file name required for in-place updating\n");
999 return -1;
1000 }
1001 if (strcmp(inject.output.path, "-")) {
1002 pr_err("Output file name must not be specified for in-place updating\n");
1003 return -1;
1004 }
1005 if (!data.force && !inject.in_place_update_dry_run) {
1006 pr_err("The input file would be updated in place, "
1007 "the --force option is required.\n");
1008 return -1;
1009 }
1010 if (!inject.in_place_update_dry_run)
1011 data.in_place_update = true;
1012 } else if (perf_data__open(&inject.output)) {
Jiri Olsa34069122013-10-29 19:04:57 +01001013 perror("failed to create output file");
1014 return -1;
Andrew Vagine558a5b2012-08-07 16:56:02 +04001015 }
1016
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001017 data.path = inject.input_name;
Namhyung Kimc3a057d2021-07-19 15:31:52 -07001018 if (!strcmp(inject.input_name, "-") || inject.output.is_pipe) {
Namhyung Kimfea20d62021-07-19 15:31:51 -07001019 inject.is_pipe = true;
Namhyung Kimc3a057d2021-07-19 15:31:52 -07001020 /*
1021 * Do not repipe header when input is a regular file
1022 * since either it can rewrite the header at the end
1023 * or write a new pipe header.
1024 */
1025 if (strcmp(inject.input_name, "-"))
1026 repipe = false;
1027 }
Namhyung Kimfea20d62021-07-19 15:31:51 -07001028
Namhyung Kimc3a057d2021-07-19 15:31:52 -07001029 inject.session = __perf_session__new(&data, repipe,
Adrian Hunterc271a552021-12-13 10:48:29 +02001030 output_fd(&inject),
Namhyung Kimfea20d62021-07-19 15:31:51 -07001031 &inject.tool);
Riccardo Mancini02e62462021-07-15 18:07:15 +02001032 if (IS_ERR(inject.session)) {
1033 ret = PTR_ERR(inject.session);
1034 goto out_close_output;
1035 }
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +09001036
Alexey Budankov371a3372019-03-18 20:45:44 +03001037 if (zstd_init(&(inject.session->zstd_data), 0) < 0)
1038 pr_warning("Decompression initialization failed.\n");
1039
Namhyung Kimc3a057d2021-07-19 15:31:52 -07001040 if (!data.is_pipe && inject.output.is_pipe) {
1041 ret = perf_header__write_pipe(perf_data__fd(&inject.output));
1042 if (ret < 0) {
1043 pr_err("Couldn't write a new pipe header.\n");
1044 goto out_delete;
1045 }
1046
1047 ret = perf_event__synthesize_for_pipe(&inject.tool,
1048 inject.session,
1049 &inject.output,
1050 perf_event__repipe);
1051 if (ret < 0)
1052 goto out_delete;
1053 }
1054
Namhyung Kim27c9c342020-10-12 16:02:13 +09001055 if (inject.build_ids && !inject.build_id_all) {
Arnaldo Carvalho de Melo921f3fa2016-01-22 18:41:00 -03001056 /*
1057 * to make sure the mmap records are ordered correctly
1058 * and so that the correct especially due to jitted code
1059 * mmaps. We cannot generate the buildid hit list and
1060 * inject the jit mmaps at the same time for now.
1061 */
1062 inject.tool.ordered_events = true;
1063 inject.tool.ordering_requires_timestamps = true;
1064 }
Namhyung Kim27c9c342020-10-12 16:02:13 +09001065
1066 if (inject.sched_stat) {
1067 inject.tool.ordered_events = true;
1068 }
1069
Jiri Olsae12b2022016-03-10 17:41:13 +01001070#ifdef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +01001071 if (inject.jit_mode) {
Stephane Eranian9b07e272015-11-30 10:02:21 +01001072 inject.tool.mmap2 = perf_event__jit_repipe_mmap2;
1073 inject.tool.mmap = perf_event__jit_repipe_mmap;
1074 inject.tool.ordered_events = true;
1075 inject.tool.ordering_requires_timestamps = true;
1076 /*
1077 * JIT MMAP injection injects all MMAP events in one go, so it
1078 * does not obey finished_round semantics.
1079 */
1080 inject.tool.finished_round = perf_event__drop_oe;
1081 }
1082#endif
Taeung Song9fedfb02015-06-30 17:15:20 +09001083 ret = symbol__init(&inject.session->header.env);
1084 if (ret < 0)
1085 goto out_delete;
Tom Zanussi454c4072010-05-01 01:41:20 -05001086
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +09001087 ret = __cmd_inject(&inject);
1088
Taeung Song9fedfb02015-06-30 17:15:20 +09001089out_delete:
Alexey Budankov371a3372019-03-18 20:45:44 +03001090 zstd_fini(&(inject.session->zstd_data));
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +09001091 perf_session__delete(inject.session);
Riccardo Mancini02e62462021-07-15 18:07:15 +02001092out_close_output:
Adrian Hunter0c8e32f2021-12-13 10:48:28 +02001093 if (!inject.in_place_update)
1094 perf_data__close(&inject.output);
Adrian Hunter83d7f5f2021-04-30 10:03:02 +03001095 free(inject.itrace_synth_opts.vm_tm_corr_args);
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +09001096 return ret;
Tom Zanussi454c4072010-05-01 01:41:20 -05001097}