]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/builtin-inject.c
perf tools: Resolve machine earlier and pass it to perf_event_ops
[mirror_ubuntu-hirsute-kernel.git] / tools / perf / builtin-inject.c
CommitLineData
454c407e
TZ
1/*
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
d20deb64
ACM
19static int perf_event__repipe_synth(struct perf_event_ops *ops __used,
20 union perf_event *event,
743eb868 21 struct machine *machine __used)
454c407e
TZ
22{
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
743eb868
ACM
40static int perf_event__repipe_op2_synth(struct perf_event_ops *ops,
41 union perf_event *event,
42 struct perf_session *session __used)
43{
44 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
d20deb64 53static int perf_event__repipe_tracing_data_synth(union perf_event *event,
743eb868 54 struct perf_session *session __used)
d20deb64 55{
743eb868 56 return perf_event__repipe_synth(NULL, event, NULL);
d20deb64
ACM
57}
58
10d0f086
ACM
59static int perf_event__repipe_attr(union perf_event *event,
60 struct perf_evlist **pevlist __used)
61{
d20deb64 62 return perf_event__repipe_synth(NULL, event, NULL);
10d0f086
ACM
63}
64
d20deb64
ACM
65static int perf_event__repipe(struct perf_event_ops *ops,
66 union perf_event *event,
8115d60c 67 struct perf_sample *sample __used,
743eb868 68 struct machine *machine)
640c03ce 69{
743eb868 70 return perf_event__repipe_synth(ops, event, machine);
640c03ce
ACM
71}
72
d20deb64
ACM
73static int perf_event__repipe_sample(struct perf_event_ops *ops,
74 union perf_event *event,
9e69c210
ACM
75 struct perf_sample *sample __used,
76 struct perf_evsel *evsel __used,
743eb868 77 struct machine *machine)
9e69c210 78{
743eb868 79 return perf_event__repipe_synth(ops, event, machine);
9e69c210
ACM
80}
81
d20deb64
ACM
82static int perf_event__repipe_mmap(struct perf_event_ops *ops,
83 union perf_event *event,
8115d60c 84 struct perf_sample *sample,
743eb868 85 struct machine *machine)
454c407e
TZ
86{
87 int err;
88
743eb868
ACM
89 err = perf_event__process_mmap(ops, event, sample, machine);
90 perf_event__repipe(ops, event, sample, machine);
454c407e
TZ
91
92 return err;
93}
94
d20deb64
ACM
95static int perf_event__repipe_task(struct perf_event_ops *ops,
96 union perf_event *event,
8115d60c 97 struct perf_sample *sample,
743eb868 98 struct machine *machine)
454c407e
TZ
99{
100 int err;
101
743eb868
ACM
102 err = perf_event__process_task(ops, event, sample, machine);
103 perf_event__repipe(ops, event, sample, machine);
454c407e
TZ
104
105 return err;
106}
107
8115d60c
ACM
108static int perf_event__repipe_tracing_data(union perf_event *event,
109 struct perf_session *session)
454c407e
TZ
110{
111 int err;
112
743eb868 113 perf_event__repipe_synth(NULL, event, NULL);
8115d60c 114 err = perf_event__process_tracing_data(event, session);
454c407e
TZ
115
116 return err;
117}
118
090f7204 119static int dso__read_build_id(struct dso *self)
454c407e 120{
090f7204
ACM
121 if (self->has_build_id)
122 return 0;
454c407e 123
090f7204
ACM
124 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;
128 }
454c407e 129
090f7204
ACM
130 return -1;
131}
454c407e 132
d20deb64 133static int dso__inject_build_id(struct dso *self, struct perf_event_ops *ops,
743eb868 134 struct machine *machine)
090f7204
ACM
135{
136 u16 misc = PERF_RECORD_MISC_USER;
090f7204 137 int err;
454c407e 138
090f7204
ACM
139 if (dso__read_build_id(self) < 0) {
140 pr_debug("no build_id found for %s\n", self->long_name);
141 return -1;
142 }
454c407e 143
090f7204
ACM
144 if (self->kernel)
145 misc = PERF_RECORD_MISC_KERNEL;
454c407e 146
d20deb64 147 err = perf_event__synthesize_build_id(ops, self, misc, perf_event__repipe,
743eb868 148 machine);
090f7204
ACM
149 if (err) {
150 pr_err("Can't synthesize build_id event for %s\n", self->long_name);
454c407e
TZ
151 return -1;
152 }
153
154 return 0;
155}
156
d20deb64
ACM
157static int perf_event__inject_buildid(struct perf_event_ops *ops,
158 union perf_event *event,
8115d60c 159 struct perf_sample *sample,
9e69c210 160 struct perf_evsel *evsel __used,
743eb868 161 struct machine *machine)
454c407e
TZ
162{
163 struct addr_location al;
164 struct thread *thread;
165 u8 cpumode;
454c407e
TZ
166
167 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
168
743eb868 169 thread = machine__findnew_thread(machine, event->ip.pid);
454c407e
TZ
170 if (thread == NULL) {
171 pr_err("problem processing %d event, skipping it.\n",
172 event->header.type);
454c407e
TZ
173 goto repipe;
174 }
175
743eb868
ACM
176 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
177 event->ip.ip, &al);
454c407e
TZ
178
179 if (al.map != NULL) {
180 if (!al.map->dso->hit) {
181 al.map->dso->hit = 1;
090f7204 182 if (map__load(al.map, NULL) >= 0) {
743eb868 183 dso__inject_build_id(al.map->dso, ops, machine);
090f7204
ACM
184 /*
185 * If this fails, too bad, let the other side
186 * account this as unresolved.
187 */
188 } else
454c407e
TZ
189 pr_warning("no symbols found in %s, maybe "
190 "install a debug package?\n",
191 al.map->dso->long_name);
192 }
193 }
194
195repipe:
743eb868 196 perf_event__repipe(ops, event, sample, machine);
090f7204 197 return 0;
454c407e
TZ
198}
199
200struct perf_event_ops inject_ops = {
9e69c210 201 .sample = perf_event__repipe_sample,
8115d60c
ACM
202 .mmap = perf_event__repipe,
203 .comm = perf_event__repipe,
204 .fork = perf_event__repipe,
205 .exit = perf_event__repipe,
206 .lost = perf_event__repipe,
743eb868 207 .read = perf_event__repipe_sample,
8115d60c
ACM
208 .throttle = perf_event__repipe,
209 .unthrottle = perf_event__repipe,
10d0f086 210 .attr = perf_event__repipe_attr,
743eb868 211 .event_type = perf_event__repipe_event_type_synth,
d20deb64 212 .tracing_data = perf_event__repipe_tracing_data_synth,
743eb868 213 .build_id = perf_event__repipe_op2_synth,
454c407e
TZ
214};
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) {
8115d60c
ACM
231 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;
454c407e
TZ
235 }
236
21ef97f0 237 session = perf_session__new(input_name, O_RDONLY, false, true, &inject_ops);
454c407e
TZ
238 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[] = {
11d232ec 254 OPT_BOOLEAN('b', "build-ids", &inject_build_ids,
454c407e
TZ
255 "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}