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/debug.h"
14 #include "util/parse-options.h"
16 static char const *input_name = "-";
17 static bool inject_build_ids;
19 static int perf_event__repipe_synth(union perf_event *event,
20 struct perf_session *session __used)
25 size = event->header.size;
28 int ret = write(STDOUT_FILENO, buf, size);
39 static int perf_event__repipe_attr(union perf_event *event,
40 struct perf_evlist **pevlist __used)
42 return perf_event__repipe_synth(event, NULL);
45 static int perf_event__repipe(union perf_event *event,
46 struct perf_sample *sample __used,
47 struct perf_session *session)
49 return perf_event__repipe_synth(event, session);
52 static int perf_event__repipe_sample(union perf_event *event,
53 struct perf_sample *sample __used,
54 struct perf_evsel *evsel __used,
55 struct perf_session *session)
57 return perf_event__repipe_synth(event, session);
60 static int perf_event__repipe_mmap(union perf_event *event,
61 struct perf_sample *sample,
62 struct perf_session *session)
66 err = perf_event__process_mmap(event, sample, session);
67 perf_event__repipe(event, sample, session);
72 static int perf_event__repipe_task(union perf_event *event,
73 struct perf_sample *sample,
74 struct perf_session *session)
78 err = perf_event__process_task(event, sample, session);
79 perf_event__repipe(event, sample, session);
84 static int perf_event__repipe_tracing_data(union perf_event *event,
85 struct perf_session *session)
89 perf_event__repipe_synth(event, session);
90 err = perf_event__process_tracing_data(event, session);
95 static int dso__read_build_id(struct dso *self)
97 if (self->has_build_id)
100 if (filename__read_build_id(self->long_name, self->build_id,
101 sizeof(self->build_id)) > 0) {
102 self->has_build_id = true;
109 static int dso__inject_build_id(struct dso *self, struct perf_session *session)
111 u16 misc = PERF_RECORD_MISC_USER;
112 struct machine *machine;
115 if (dso__read_build_id(self) < 0) {
116 pr_debug("no build_id found for %s\n", self->long_name);
120 machine = perf_session__find_host_machine(session);
121 if (machine == NULL) {
122 pr_err("Can't find machine for session\n");
127 misc = PERF_RECORD_MISC_KERNEL;
129 err = perf_event__synthesize_build_id(self, misc, perf_event__repipe,
132 pr_err("Can't synthesize build_id event for %s\n", self->long_name);
139 static int perf_event__inject_buildid(union perf_event *event,
140 struct perf_sample *sample,
141 struct perf_evsel *evsel __used,
142 struct perf_session *session)
144 struct addr_location al;
145 struct thread *thread;
148 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
150 thread = perf_session__findnew(session, event->ip.pid);
151 if (thread == NULL) {
152 pr_err("problem processing %d event, skipping it.\n",
157 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
158 event->ip.pid, event->ip.ip, &al);
160 if (al.map != NULL) {
161 if (!al.map->dso->hit) {
162 al.map->dso->hit = 1;
163 if (map__load(al.map, NULL) >= 0) {
164 dso__inject_build_id(al.map->dso, session);
166 * If this fails, too bad, let the other side
167 * account this as unresolved.
170 pr_warning("no symbols found in %s, maybe "
171 "install a debug package?\n",
172 al.map->dso->long_name);
177 perf_event__repipe(event, sample, session);
181 struct perf_event_ops inject_ops = {
182 .sample = perf_event__repipe_sample,
183 .mmap = perf_event__repipe,
184 .comm = perf_event__repipe,
185 .fork = perf_event__repipe,
186 .exit = perf_event__repipe,
187 .lost = perf_event__repipe,
188 .read = perf_event__repipe,
189 .throttle = perf_event__repipe,
190 .unthrottle = perf_event__repipe,
191 .attr = perf_event__repipe_attr,
192 .event_type = perf_event__repipe_synth,
193 .tracing_data = perf_event__repipe_synth,
194 .build_id = perf_event__repipe_synth,
197 extern volatile int session_done;
199 static void sig_handler(int sig __attribute__((__unused__)))
204 static int __cmd_inject(void)
206 struct perf_session *session;
209 signal(SIGINT, sig_handler);
211 if (inject_build_ids) {
212 inject_ops.sample = perf_event__inject_buildid;
213 inject_ops.mmap = perf_event__repipe_mmap;
214 inject_ops.fork = perf_event__repipe_task;
215 inject_ops.tracing_data = perf_event__repipe_tracing_data;
218 session = perf_session__new(input_name, O_RDONLY, false, true, &inject_ops);
222 ret = perf_session__process_events(session, &inject_ops);
224 perf_session__delete(session);
229 static const char * const report_usage[] = {
230 "perf inject [<options>]",
234 static const struct option options[] = {
235 OPT_BOOLEAN('b', "build-ids", &inject_build_ids,
236 "Inject build-ids into the output stream"),
237 OPT_INCR('v', "verbose", &verbose,
238 "be more verbose (show build ids, etc)"),
242 int cmd_inject(int argc, const char **argv, const char *prefix __used)
244 argc = parse_options(argc, argv, options, report_usage, 0);
247 * Any (unrecognized) arguments left?
250 usage_with_options(report_usage, options);
252 if (symbol__init() < 0)
255 return __cmd_inject();