blob: 978751ec64ce950f1b29312a92d2e60f1e7d0509 [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 Melo8115d602011-01-29 14:01:45 -020019static int perf_event__repipe_synth(union perf_event *event,
20 struct perf_session *session __used)
Tom Zanussi454c4072010-05-01 01:41:20 -050021{
22 uint32_t size;
23 void *buf = event;
24
25 size = event->header.size;
26
27 while (size) {
28 int ret = write(STDOUT_FILENO, buf, size);
29 if (ret < 0)
30 return -errno;
31
32 size -= ret;
33 buf += ret;
34 }
35
36 return 0;
37}
38
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020039static int perf_event__repipe_attr(union perf_event *event,
40 struct perf_evlist **pevlist __used)
41{
42 return perf_event__repipe_synth(event, NULL);
43}
44
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020045static int perf_event__repipe(union perf_event *event,
46 struct perf_sample *sample __used,
47 struct perf_session *session)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020048{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020049 return perf_event__repipe_synth(event, session);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020050}
51
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030052static 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)
56{
57 return perf_event__repipe_synth(event, session);
58}
59
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020060static int perf_event__repipe_mmap(union perf_event *event,
61 struct perf_sample *sample,
62 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -050063{
64 int err;
65
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020066 err = perf_event__process_mmap(event, sample, session);
67 perf_event__repipe(event, sample, session);
Tom Zanussi454c4072010-05-01 01:41:20 -050068
69 return err;
70}
71
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020072static int perf_event__repipe_task(union perf_event *event,
73 struct perf_sample *sample,
74 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -050075{
76 int err;
77
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020078 err = perf_event__process_task(event, sample, session);
79 perf_event__repipe(event, sample, session);
Tom Zanussi454c4072010-05-01 01:41:20 -050080
81 return err;
82}
83
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020084static int perf_event__repipe_tracing_data(union perf_event *event,
85 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -050086{
87 int err;
88
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020089 perf_event__repipe_synth(event, session);
90 err = perf_event__process_tracing_data(event, session);
Tom Zanussi454c4072010-05-01 01:41:20 -050091
92 return err;
93}
94
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -030095static int dso__read_build_id(struct dso *self)
Tom Zanussi454c4072010-05-01 01:41:20 -050096{
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -030097 if (self->has_build_id)
98 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -050099
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300100 if (filename__read_build_id(self->long_name, self->build_id,
101 sizeof(self->build_id)) > 0) {
102 self->has_build_id = true;
103 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500104 }
105
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300106 return -1;
107}
Tom Zanussi454c4072010-05-01 01:41:20 -0500108
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300109static int dso__inject_build_id(struct dso *self, struct perf_session *session)
110{
111 u16 misc = PERF_RECORD_MISC_USER;
112 struct machine *machine;
113 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500114
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300115 if (dso__read_build_id(self) < 0) {
116 pr_debug("no build_id found for %s\n", self->long_name);
117 return -1;
118 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500119
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300120 machine = perf_session__find_host_machine(session);
121 if (machine == NULL) {
122 pr_err("Can't find machine for session\n");
123 return -1;
124 }
125
126 if (self->kernel)
127 misc = PERF_RECORD_MISC_KERNEL;
128
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200129 err = perf_event__synthesize_build_id(self, misc, perf_event__repipe,
130 machine, session);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300131 if (err) {
132 pr_err("Can't synthesize build_id event for %s\n", self->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500133 return -1;
134 }
135
136 return 0;
137}
138
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200139static int perf_event__inject_buildid(union perf_event *event,
140 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300141 struct perf_evsel *evsel __used,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200142 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -0500143{
144 struct addr_location al;
145 struct thread *thread;
146 u8 cpumode;
Tom Zanussi454c4072010-05-01 01:41:20 -0500147
148 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
149
150 thread = perf_session__findnew(session, event->ip.pid);
151 if (thread == NULL) {
152 pr_err("problem processing %d event, skipping it.\n",
153 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500154 goto repipe;
155 }
156
157 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
158 event->ip.pid, event->ip.ip, &al);
159
160 if (al.map != NULL) {
161 if (!al.map->dso->hit) {
162 al.map->dso->hit = 1;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300163 if (map__load(al.map, NULL) >= 0) {
164 dso__inject_build_id(al.map->dso, session);
165 /*
166 * If this fails, too bad, let the other side
167 * account this as unresolved.
168 */
169 } else
Tom Zanussi454c4072010-05-01 01:41:20 -0500170 pr_warning("no symbols found in %s, maybe "
171 "install a debug package?\n",
172 al.map->dso->long_name);
173 }
174 }
175
176repipe:
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200177 perf_event__repipe(event, sample, session);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300178 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500179}
180
181struct perf_event_ops inject_ops = {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300182 .sample = perf_event__repipe_sample,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200183 .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,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200191 .attr = perf_event__repipe_attr,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200192 .event_type = perf_event__repipe_synth,
193 .tracing_data = perf_event__repipe_synth,
194 .build_id = perf_event__repipe_synth,
Tom Zanussi454c4072010-05-01 01:41:20 -0500195};
196
197extern volatile int session_done;
198
199static void sig_handler(int sig __attribute__((__unused__)))
200{
201 session_done = 1;
202}
203
204static int __cmd_inject(void)
205{
206 struct perf_session *session;
207 int ret = -EINVAL;
208
209 signal(SIGINT, sig_handler);
210
211 if (inject_build_ids) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200212 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;
Tom Zanussi454c4072010-05-01 01:41:20 -0500216 }
217
Ian Munsie21ef97f2010-12-10 14:09:16 +1100218 session = perf_session__new(input_name, O_RDONLY, false, true, &inject_ops);
Tom Zanussi454c4072010-05-01 01:41:20 -0500219 if (session == NULL)
220 return -ENOMEM;
221
222 ret = perf_session__process_events(session, &inject_ops);
223
224 perf_session__delete(session);
225
226 return ret;
227}
228
229static const char * const report_usage[] = {
230 "perf inject [<options>]",
231 NULL
232};
233
234static const struct option options[] = {
Arnaldo Carvalho de Melo11d232e2010-05-04 10:48:22 -0300235 OPT_BOOLEAN('b', "build-ids", &inject_build_ids,
Tom Zanussi454c4072010-05-01 01:41:20 -0500236 "Inject build-ids into the output stream"),
237 OPT_INCR('v', "verbose", &verbose,
238 "be more verbose (show build ids, etc)"),
239 OPT_END()
240};
241
242int cmd_inject(int argc, const char **argv, const char *prefix __used)
243{
244 argc = parse_options(argc, argv, options, report_usage, 0);
245
246 /*
247 * Any (unrecognized) arguments left?
248 */
249 if (argc)
250 usage_with_options(report_usage, options);
251
252 if (symbol__init() < 0)
253 return -1;
254
255 return __cmd_inject();
256}