4 * Builtin inject command: Examine the live mode (stdin) event stream
5 * and repipe it to stdout while optionally injecting additional
11 #include "util/session.h"
12 #include "util/tool.h"
13 #include "util/debug.h"
15 #include "util/parse-options.h"
18 struct perf_tool tool
;
20 const char *input_name
;
26 static int perf_event__repipe_synth(struct perf_tool
*tool
,
27 union perf_event
*event
,
28 struct machine
*machine __maybe_unused
)
30 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
, tool
);
34 size
= event
->header
.size
;
37 int ret
= write(inject
->output
, buf
, size
);
43 inject
->bytes_written
+= ret
;
49 static int perf_event__repipe_op2_synth(struct perf_tool
*tool
,
50 union perf_event
*event
,
51 struct perf_session
*session
54 return perf_event__repipe_synth(tool
, event
, NULL
);
57 static int perf_event__repipe_event_type_synth(struct perf_tool
*tool
,
58 union perf_event
*event
)
60 return perf_event__repipe_synth(tool
, event
, NULL
);
63 static int perf_event__repipe_tracing_data_synth(union perf_event
*event
,
64 struct perf_session
*session
67 return perf_event__repipe_synth(NULL
, event
, NULL
);
70 static int perf_event__repipe_attr(union perf_event
*event
,
71 struct perf_evlist
**pevlist __maybe_unused
)
74 ret
= perf_event__process_attr(event
, pevlist
);
78 return perf_event__repipe_synth(NULL
, event
, NULL
);
81 static int perf_event__repipe(struct perf_tool
*tool
,
82 union perf_event
*event
,
83 struct perf_sample
*sample __maybe_unused
,
84 struct machine
*machine
)
86 return perf_event__repipe_synth(tool
, event
, machine
);
89 static int perf_event__repipe_sample(struct perf_tool
*tool
,
90 union perf_event
*event
,
91 struct perf_sample
*sample __maybe_unused
,
92 struct perf_evsel
*evsel __maybe_unused
,
93 struct machine
*machine
)
95 return perf_event__repipe_synth(tool
, event
, machine
);
98 static int perf_event__repipe_mmap(struct perf_tool
*tool
,
99 union perf_event
*event
,
100 struct perf_sample
*sample
,
101 struct machine
*machine
)
105 err
= perf_event__process_mmap(tool
, event
, sample
, machine
);
106 perf_event__repipe(tool
, event
, sample
, machine
);
111 static int perf_event__repipe_fork(struct perf_tool
*tool
,
112 union perf_event
*event
,
113 struct perf_sample
*sample
,
114 struct machine
*machine
)
118 err
= perf_event__process_fork(tool
, event
, sample
, machine
);
119 perf_event__repipe(tool
, event
, sample
, machine
);
124 static int perf_event__repipe_tracing_data(union perf_event
*event
,
125 struct perf_session
*session
)
129 perf_event__repipe_synth(NULL
, event
, NULL
);
130 err
= perf_event__process_tracing_data(event
, session
);
135 static int dso__read_build_id(struct dso
*self
)
137 if (self
->has_build_id
)
140 if (filename__read_build_id(self
->long_name
, self
->build_id
,
141 sizeof(self
->build_id
)) > 0) {
142 self
->has_build_id
= true;
149 static int dso__inject_build_id(struct dso
*self
, struct perf_tool
*tool
,
150 struct machine
*machine
)
152 u16 misc
= PERF_RECORD_MISC_USER
;
155 if (dso__read_build_id(self
) < 0) {
156 pr_debug("no build_id found for %s\n", self
->long_name
);
161 misc
= PERF_RECORD_MISC_KERNEL
;
163 err
= perf_event__synthesize_build_id(tool
, self
, misc
, perf_event__repipe
,
166 pr_err("Can't synthesize build_id event for %s\n", self
->long_name
);
173 static int perf_event__inject_buildid(struct perf_tool
*tool
,
174 union perf_event
*event
,
175 struct perf_sample
*sample
,
176 struct perf_evsel
*evsel __maybe_unused
,
177 struct machine
*machine
)
179 struct addr_location al
;
180 struct thread
*thread
;
183 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
185 thread
= machine__findnew_thread(machine
, event
->ip
.pid
);
186 if (thread
== NULL
) {
187 pr_err("problem processing %d event, skipping it.\n",
192 thread__find_addr_map(thread
, machine
, cpumode
, MAP__FUNCTION
,
195 if (al
.map
!= NULL
) {
196 if (!al
.map
->dso
->hit
) {
197 al
.map
->dso
->hit
= 1;
198 if (map__load(al
.map
, NULL
) >= 0) {
199 dso__inject_build_id(al
.map
->dso
, tool
, machine
);
201 * If this fails, too bad, let the other side
202 * account this as unresolved.
205 #ifdef LIBELF_SUPPORT
206 pr_warning("no symbols found in %s, maybe "
207 "install a debug package?\n",
208 al
.map
->dso
->long_name
);
215 perf_event__repipe(tool
, event
, sample
, machine
);
219 extern volatile int session_done
;
221 static void sig_handler(int sig __maybe_unused
)
226 static int __cmd_inject(struct perf_inject
*inject
)
228 struct perf_session
*session
;
231 signal(SIGINT
, sig_handler
);
233 if (inject
->build_ids
) {
234 inject
->tool
.sample
= perf_event__inject_buildid
;
235 inject
->tool
.mmap
= perf_event__repipe_mmap
;
236 inject
->tool
.fork
= perf_event__repipe_fork
;
237 inject
->tool
.tracing_data
= perf_event__repipe_tracing_data
;
240 session
= perf_session__new(inject
->input_name
, O_RDONLY
, false, true, &inject
->tool
);
244 if (!inject
->pipe_output
)
245 lseek(inject
->output
, session
->header
.data_offset
, SEEK_SET
);
247 ret
= perf_session__process_events(session
, &inject
->tool
);
249 if (!inject
->pipe_output
) {
250 session
->header
.data_size
= inject
->bytes_written
;
251 perf_session__write_header(session
, session
->evlist
, inject
->output
, true);
254 perf_session__delete(session
);
259 int cmd_inject(int argc
, const char **argv
, const char *prefix __maybe_unused
)
261 struct perf_inject inject
= {
263 .sample
= perf_event__repipe_sample
,
264 .mmap
= perf_event__repipe
,
265 .comm
= perf_event__repipe
,
266 .fork
= perf_event__repipe
,
267 .exit
= perf_event__repipe
,
268 .lost
= perf_event__repipe
,
269 .read
= perf_event__repipe_sample
,
270 .throttle
= perf_event__repipe
,
271 .unthrottle
= perf_event__repipe
,
272 .attr
= perf_event__repipe_attr
,
273 .event_type
= perf_event__repipe_event_type_synth
,
274 .tracing_data
= perf_event__repipe_tracing_data_synth
,
275 .build_id
= perf_event__repipe_op2_synth
,
279 const char *output_name
= "-";
280 const struct option options
[] = {
281 OPT_BOOLEAN('b', "build-ids", &inject
.build_ids
,
282 "Inject build-ids into the output stream"),
283 OPT_STRING('i', "input", &inject
.input_name
, "file",
285 OPT_STRING('o', "output", &output_name
, "file",
287 OPT_INCR('v', "verbose", &verbose
,
288 "be more verbose (show build ids, etc)"),
291 const char * const inject_usage
[] = {
292 "perf inject [<options>]",
296 argc
= parse_options(argc
, argv
, options
, inject_usage
, 0);
299 * Any (unrecognized) arguments left?
302 usage_with_options(inject_usage
, options
);
304 if (!strcmp(output_name
, "-")) {
305 inject
.pipe_output
= 1;
306 inject
.output
= STDOUT_FILENO
;
308 inject
.output
= open(output_name
, O_CREAT
| O_WRONLY
| O_TRUNC
,
310 if (inject
.output
< 0) {
311 perror("failed to create output file");
316 if (symbol__init() < 0)
319 return __cmd_inject(&inject
);