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"
17 static char const *input_name = "-";
18 static bool inject_build_ids;
20 static int perf_event__repipe_synth(struct perf_tool *tool __used,
21 union perf_event *event,
22 struct machine *machine __used)
27 size = event->header.size;
30 int ret = write(STDOUT_FILENO, buf, size);
41 static int perf_event__repipe_op2_synth(struct perf_tool *tool,
42 union perf_event *event,
43 struct perf_session *session __used)
45 return perf_event__repipe_synth(tool, event, NULL);
48 static int perf_event__repipe_event_type_synth(struct perf_tool *tool,
49 union perf_event *event)
51 return perf_event__repipe_synth(tool, event, NULL);
54 static int perf_event__repipe_tracing_data_synth(union perf_event *event,
55 struct perf_session *session __used)
57 return perf_event__repipe_synth(NULL, event, NULL);
60 static int perf_event__repipe_attr(union perf_event *event,
61 struct perf_evlist **pevlist __used)
63 return perf_event__repipe_synth(NULL, event, NULL);
66 static int perf_event__repipe(struct perf_tool *tool,
67 union perf_event *event,
68 struct perf_sample *sample __used,
69 struct machine *machine)
71 return perf_event__repipe_synth(tool, event, machine);
74 static int perf_event__repipe_sample(struct perf_tool *tool,
75 union perf_event *event,
76 struct perf_sample *sample __used,
77 struct perf_evsel *evsel __used,
78 struct machine *machine)
80 return perf_event__repipe_synth(tool, event, machine);
83 static int perf_event__repipe_mmap(struct perf_tool *tool,
84 union perf_event *event,
85 struct perf_sample *sample,
86 struct machine *machine)
90 err = perf_event__process_mmap(tool, event, sample, machine);
91 perf_event__repipe(tool, event, sample, machine);
96 static int perf_event__repipe_task(struct perf_tool *tool,
97 union perf_event *event,
98 struct perf_sample *sample,
99 struct machine *machine)
103 err = perf_event__process_task(tool, event, sample, machine);
104 perf_event__repipe(tool, event, sample, machine);
109 static int perf_event__repipe_tracing_data(union perf_event *event,
110 struct perf_session *session)
114 perf_event__repipe_synth(NULL, event, NULL);
115 err = perf_event__process_tracing_data(event, session);
120 static int dso__read_build_id(struct dso *self)
122 if (self->has_build_id)
125 if (filename__read_build_id(self->long_name, self->build_id,
126 sizeof(self->build_id)) > 0) {
127 self->has_build_id = true;
134 static int dso__inject_build_id(struct dso *self, struct perf_tool *tool,
135 struct machine *machine)
137 u16 misc = PERF_RECORD_MISC_USER;
140 if (dso__read_build_id(self) < 0) {
141 pr_debug("no build_id found for %s\n", self->long_name);
146 misc = PERF_RECORD_MISC_KERNEL;
148 err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe,
151 pr_err("Can't synthesize build_id event for %s\n", self->long_name);
158 static int perf_event__inject_buildid(struct perf_tool *tool,
159 union perf_event *event,
160 struct perf_sample *sample,
161 struct perf_evsel *evsel __used,
162 struct machine *machine)
164 struct addr_location al;
165 struct thread *thread;
168 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
170 thread = machine__findnew_thread(machine, event->ip.pid);
171 if (thread == NULL) {
172 pr_err("problem processing %d event, skipping it.\n",
177 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
180 if (al.map != NULL) {
181 if (!al.map->dso->hit) {
182 al.map->dso->hit = 1;
183 if (map__load(al.map, NULL) >= 0) {
184 dso__inject_build_id(al.map->dso, tool, machine);
186 * If this fails, too bad, let the other side
187 * account this as unresolved.
190 pr_warning("no symbols found in %s, maybe "
191 "install a debug package?\n",
192 al.map->dso->long_name);
197 perf_event__repipe(tool, event, sample, machine);
201 struct perf_tool perf_inject = {
202 .sample = perf_event__repipe_sample,
203 .mmap = perf_event__repipe,
204 .comm = perf_event__repipe,
205 .fork = perf_event__repipe,
206 .exit = perf_event__repipe,
207 .lost = perf_event__repipe,
208 .read = perf_event__repipe_sample,
209 .throttle = perf_event__repipe,
210 .unthrottle = perf_event__repipe,
211 .attr = perf_event__repipe_attr,
212 .event_type = perf_event__repipe_event_type_synth,
213 .tracing_data = perf_event__repipe_tracing_data_synth,
214 .build_id = perf_event__repipe_op2_synth,
217 extern volatile int session_done;
219 static void sig_handler(int sig __attribute__((__unused__)))
224 static int __cmd_inject(void)
226 struct perf_session *session;
229 signal(SIGINT, sig_handler);
231 if (inject_build_ids) {
232 perf_inject.sample = perf_event__inject_buildid;
233 perf_inject.mmap = perf_event__repipe_mmap;
234 perf_inject.fork = perf_event__repipe_task;
235 perf_inject.tracing_data = perf_event__repipe_tracing_data;
238 session = perf_session__new(input_name, O_RDONLY, false, true, &perf_inject);
242 ret = perf_session__process_events(session, &perf_inject);
244 perf_session__delete(session);
249 static const char * const report_usage[] = {
250 "perf inject [<options>]",
254 static const struct option options[] = {
255 OPT_BOOLEAN('b', "build-ids", &inject_build_ids,
256 "Inject build-ids into the output stream"),
257 OPT_INCR('v', "verbose", &verbose,
258 "be more verbose (show build ids, etc)"),
262 int cmd_inject(int argc, const char **argv, const char *prefix __used)
264 argc = parse_options(argc, argv, options, report_usage, 0);
267 * Any (unrecognized) arguments left?
270 usage_with_options(report_usage, options);
272 if (symbol__init() < 0)
275 return __cmd_inject();