blob: a5bcf81776fcc92eb5790dbe11476df995756dcd [file] [log] [blame]
Tom Zanussi454c4072010-05-01 01:41:20 -05001/*
2 * builtin-inject.c
3 *
4 * Builtin inject command: Examine the live mode (stdin) event stream
5 * and repipe it to stdout while optionally injecting additional
6 * events into it.
7 */
8#include "builtin.h"
9
10#include "perf.h"
11#include "util/session.h"
12#include "util/debug.h"
13
14#include "util/parse-options.h"
15
16static char const *input_name = "-";
17static bool inject_build_ids;
18
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020019static int perf_event__repipe_synth(struct perf_event_ops *ops __used,
20 union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020021 struct machine *machine __used)
Tom Zanussi454c4072010-05-01 01:41:20 -050022{
23 uint32_t size;
24 void *buf = event;
25
26 size = event->header.size;
27
28 while (size) {
29 int ret = write(STDOUT_FILENO, buf, size);
30 if (ret < 0)
31 return -errno;
32
33 size -= ret;
34 buf += ret;
35 }
36
37 return 0;
38}
39
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020040static int perf_event__repipe_op2_synth(struct perf_event_ops *ops,
41 union perf_event *event,
42 struct perf_session *session __used)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020043{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020044 return perf_event__repipe_synth(ops, event, NULL);
45}
46
47static int perf_event__repipe_event_type_synth(struct perf_event_ops *ops,
48 union perf_event *event)
49{
50 return perf_event__repipe_synth(ops, event, NULL);
51}
52
53static int perf_event__repipe_tracing_data_synth(union perf_event *event,
54 struct perf_session *session __used)
55{
56 return perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020057}
58
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020059static int perf_event__repipe_attr(union perf_event *event,
60 struct perf_evlist **pevlist __used)
61{
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020062 return perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020063}
64
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020065static int perf_event__repipe(struct perf_event_ops *ops,
66 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020067 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020068 struct machine *machine)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020069{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020070 return perf_event__repipe_synth(ops, event, machine);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020071}
72
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020073static int perf_event__repipe_sample(struct perf_event_ops *ops,
74 union perf_event *event,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030075 struct perf_sample *sample __used,
76 struct perf_evsel *evsel __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020077 struct machine *machine)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030078{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020079 return perf_event__repipe_synth(ops, event, machine);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030080}
81
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020082static int perf_event__repipe_mmap(struct perf_event_ops *ops,
83 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020084 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020085 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -050086{
87 int err;
88
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020089 err = perf_event__process_mmap(ops, event, sample, machine);
90 perf_event__repipe(ops, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -050091
92 return err;
93}
94
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020095static int perf_event__repipe_task(struct perf_event_ops *ops,
96 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020097 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020098 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -050099{
100 int err;
101
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200102 err = perf_event__process_task(ops, event, sample, machine);
103 perf_event__repipe(ops, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500104
105 return err;
106}
107
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200108static int perf_event__repipe_tracing_data(union perf_event *event,
109 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -0500110{
111 int err;
112
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200113 perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200114 err = perf_event__process_tracing_data(event, session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500115
116 return err;
117}
118
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300119static int dso__read_build_id(struct dso *self)
Tom Zanussi454c4072010-05-01 01:41:20 -0500120{
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300121 if (self->has_build_id)
122 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500123
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300124 if (filename__read_build_id(self->long_name, self->build_id,
125 sizeof(self->build_id)) > 0) {
126 self->has_build_id = true;
127 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500128 }
129
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300130 return -1;
131}
Tom Zanussi454c4072010-05-01 01:41:20 -0500132
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200133static int dso__inject_build_id(struct dso *self, struct perf_event_ops *ops,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200134 struct machine *machine)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300135{
136 u16 misc = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300137 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500138
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300139 if (dso__read_build_id(self) < 0) {
140 pr_debug("no build_id found for %s\n", self->long_name);
141 return -1;
142 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500143
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300144 if (self->kernel)
145 misc = PERF_RECORD_MISC_KERNEL;
146
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200147 err = perf_event__synthesize_build_id(ops, self, misc, perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200148 machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300149 if (err) {
150 pr_err("Can't synthesize build_id event for %s\n", self->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500151 return -1;
152 }
153
154 return 0;
155}
156
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200157static int perf_event__inject_buildid(struct perf_event_ops *ops,
158 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200159 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300160 struct perf_evsel *evsel __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200161 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500162{
163 struct addr_location al;
164 struct thread *thread;
165 u8 cpumode;
Tom Zanussi454c4072010-05-01 01:41:20 -0500166
167 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
168
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200169 thread = machine__findnew_thread(machine, event->ip.pid);
Tom Zanussi454c4072010-05-01 01:41:20 -0500170 if (thread == NULL) {
171 pr_err("problem processing %d event, skipping it.\n",
172 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500173 goto repipe;
174 }
175
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200176 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
177 event->ip.ip, &al);
Tom Zanussi454c4072010-05-01 01:41:20 -0500178
179 if (al.map != NULL) {
180 if (!al.map->dso->hit) {
181 al.map->dso->hit = 1;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300182 if (map__load(al.map, NULL) >= 0) {
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200183 dso__inject_build_id(al.map->dso, ops, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300184 /*
185 * If this fails, too bad, let the other side
186 * account this as unresolved.
187 */
188 } else
Tom Zanussi454c4072010-05-01 01:41:20 -0500189 pr_warning("no symbols found in %s, maybe "
190 "install a debug package?\n",
191 al.map->dso->long_name);
192 }
193 }
194
195repipe:
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200196 perf_event__repipe(ops, event, sample, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300197 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500198}
199
200struct perf_event_ops inject_ops = {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300201 .sample = perf_event__repipe_sample,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200202 .mmap = perf_event__repipe,
203 .comm = perf_event__repipe,
204 .fork = perf_event__repipe,
205 .exit = perf_event__repipe,
206 .lost = perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200207 .read = perf_event__repipe_sample,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200208 .throttle = perf_event__repipe,
209 .unthrottle = perf_event__repipe,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200210 .attr = perf_event__repipe_attr,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200211 .event_type = perf_event__repipe_event_type_synth,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200212 .tracing_data = perf_event__repipe_tracing_data_synth,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200213 .build_id = perf_event__repipe_op2_synth,
Tom Zanussi454c4072010-05-01 01:41:20 -0500214};
215
216extern volatile int session_done;
217
218static void sig_handler(int sig __attribute__((__unused__)))
219{
220 session_done = 1;
221}
222
223static int __cmd_inject(void)
224{
225 struct perf_session *session;
226 int ret = -EINVAL;
227
228 signal(SIGINT, sig_handler);
229
230 if (inject_build_ids) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200231 inject_ops.sample = perf_event__inject_buildid;
232 inject_ops.mmap = perf_event__repipe_mmap;
233 inject_ops.fork = perf_event__repipe_task;
234 inject_ops.tracing_data = perf_event__repipe_tracing_data;
Tom Zanussi454c4072010-05-01 01:41:20 -0500235 }
236
Ian Munsie21ef97f2010-12-10 14:09:16 +1100237 session = perf_session__new(input_name, O_RDONLY, false, true, &inject_ops);
Tom Zanussi454c4072010-05-01 01:41:20 -0500238 if (session == NULL)
239 return -ENOMEM;
240
241 ret = perf_session__process_events(session, &inject_ops);
242
243 perf_session__delete(session);
244
245 return ret;
246}
247
248static const char * const report_usage[] = {
249 "perf inject [<options>]",
250 NULL
251};
252
253static const struct option options[] = {
Arnaldo Carvalho de Melo11d232e2010-05-04 10:48:22 -0300254 OPT_BOOLEAN('b', "build-ids", &inject_build_ids,
Tom Zanussi454c4072010-05-01 01:41:20 -0500255 "Inject build-ids into the output stream"),
256 OPT_INCR('v', "verbose", &verbose,
257 "be more verbose (show build ids, etc)"),
258 OPT_END()
259};
260
261int cmd_inject(int argc, const char **argv, const char *prefix __used)
262{
263 argc = parse_options(argc, argv, options, report_usage, 0);
264
265 /*
266 * Any (unrecognized) arguments left?
267 */
268 if (argc)
269 usage_with_options(report_usage, options);
270
271 if (symbol__init() < 0)
272 return -1;
273
274 return __cmd_inject();
275}