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 event__repipe(event_t *event __used,
20 struct perf_session *session __used)
25 size = event->header.size;
28 int ret = write(STDOUT_FILENO, buf, size);
39 static int event__repipe_mmap(event_t *self, struct perf_session *session)
43 err = event__process_mmap(self, session);
44 event__repipe(self, session);
49 static int event__repipe_task(event_t *self, struct perf_session *session)
53 err = event__process_task(self, session);
54 event__repipe(self, session);
59 static int event__repipe_tracing_data(event_t *self,
60 struct perf_session *session)
64 event__repipe(self, session);
65 err = event__process_tracing_data(self, session);
70 static int dso__read_build_id(struct dso *self)
72 if (self->has_build_id)
75 if (filename__read_build_id(self->long_name, self->build_id,
76 sizeof(self->build_id)) > 0) {
77 self->has_build_id = true;
84 static int dso__inject_build_id(struct dso *self, struct perf_session *session)
86 u16 misc = PERF_RECORD_MISC_USER;
87 struct machine *machine;
90 if (dso__read_build_id(self) < 0) {
91 pr_debug("no build_id found for %s\n", self->long_name);
95 machine = perf_session__find_host_machine(session);
96 if (machine == NULL) {
97 pr_err("Can't find machine for session\n");
102 misc = PERF_RECORD_MISC_KERNEL;
104 err = event__synthesize_build_id(self, misc, event__repipe,
107 pr_err("Can't synthesize build_id event for %s\n", self->long_name);
114 static int event__inject_buildid(event_t *event, struct perf_session *session)
116 struct addr_location al;
117 struct thread *thread;
120 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
122 thread = perf_session__findnew(session, event->ip.pid);
123 if (thread == NULL) {
124 pr_err("problem processing %d event, skipping it.\n",
129 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
130 event->ip.pid, event->ip.ip, &al);
132 if (al.map != NULL) {
133 if (!al.map->dso->hit) {
134 al.map->dso->hit = 1;
135 if (map__load(al.map, NULL) >= 0) {
136 dso__inject_build_id(al.map->dso, session);
138 * If this fails, too bad, let the other side
139 * account this as unresolved.
142 pr_warning("no symbols found in %s, maybe "
143 "install a debug package?\n",
144 al.map->dso->long_name);
149 event__repipe(event, session);
153 struct perf_event_ops inject_ops = {
154 .sample = event__repipe,
155 .mmap = event__repipe,
156 .comm = event__repipe,
157 .fork = event__repipe,
158 .exit = event__repipe,
159 .lost = event__repipe,
160 .read = event__repipe,
161 .throttle = event__repipe,
162 .unthrottle = event__repipe,
163 .attr = event__repipe,
164 .event_type = event__repipe,
165 .tracing_data = event__repipe,
166 .build_id = event__repipe,
169 extern volatile int session_done;
171 static void sig_handler(int sig __attribute__((__unused__)))
176 static int __cmd_inject(void)
178 struct perf_session *session;
181 signal(SIGINT, sig_handler);
183 if (inject_build_ids) {
184 inject_ops.sample = event__inject_buildid;
185 inject_ops.mmap = event__repipe_mmap;
186 inject_ops.fork = event__repipe_task;
187 inject_ops.tracing_data = event__repipe_tracing_data;
190 session = perf_session__new(input_name, O_RDONLY, false, true);
194 ret = perf_session__process_events(session, &inject_ops);
196 perf_session__delete(session);
201 static const char * const report_usage[] = {
202 "perf inject [<options>]",
206 static const struct option options[] = {
207 OPT_BOOLEAN('b', "build-ids", &inject_build_ids,
208 "Inject build-ids into the output stream"),
209 OPT_INCR('v', "verbose", &verbose,
210 "be more verbose (show build ids, etc)"),
214 int cmd_inject(int argc, const char **argv, const char *prefix __used)
216 argc = parse_options(argc, argv, options, report_usage, 0);
219 * Any (unrecognized) arguments left?
222 usage_with_options(report_usage, options);
224 if (symbol__init() < 0)
227 return __cmd_inject();