Commit | Line | Data |
---|---|---|
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" | |
45694aa7 | 12 | #include "util/tool.h" |
454c407e TZ |
13 | #include "util/debug.h" |
14 | ||
15 | #include "util/parse-options.h" | |
16 | ||
17 | static char const *input_name = "-"; | |
18 | static bool inject_build_ids; | |
19 | ||
1d037ca1 | 20 | static int perf_event__repipe_synth(struct perf_tool *tool __maybe_unused, |
d20deb64 | 21 | union perf_event *event, |
1d037ca1 | 22 | struct machine *machine __maybe_unused) |
454c407e TZ |
23 | { |
24 | uint32_t size; | |
25 | void *buf = event; | |
26 | ||
27 | size = event->header.size; | |
28 | ||
29 | while (size) { | |
30 | int ret = write(STDOUT_FILENO, buf, size); | |
31 | if (ret < 0) | |
32 | return -errno; | |
33 | ||
34 | size -= ret; | |
35 | buf += ret; | |
36 | } | |
37 | ||
38 | return 0; | |
39 | } | |
40 | ||
45694aa7 | 41 | static int perf_event__repipe_op2_synth(struct perf_tool *tool, |
743eb868 | 42 | union perf_event *event, |
1d037ca1 IT |
43 | struct perf_session *session |
44 | __maybe_unused) | |
743eb868 | 45 | { |
45694aa7 | 46 | return perf_event__repipe_synth(tool, event, NULL); |
743eb868 ACM |
47 | } |
48 | ||
45694aa7 | 49 | static int perf_event__repipe_event_type_synth(struct perf_tool *tool, |
743eb868 ACM |
50 | union perf_event *event) |
51 | { | |
45694aa7 | 52 | return perf_event__repipe_synth(tool, event, NULL); |
743eb868 ACM |
53 | } |
54 | ||
d20deb64 | 55 | static int perf_event__repipe_tracing_data_synth(union perf_event *event, |
1d037ca1 IT |
56 | struct perf_session *session |
57 | __maybe_unused) | |
d20deb64 | 58 | { |
743eb868 | 59 | return perf_event__repipe_synth(NULL, event, NULL); |
d20deb64 ACM |
60 | } |
61 | ||
10d0f086 | 62 | static int perf_event__repipe_attr(union perf_event *event, |
1d037ca1 | 63 | struct perf_evlist **pevlist __maybe_unused) |
10d0f086 | 64 | { |
1a1ed1ba SE |
65 | int ret; |
66 | ret = perf_event__process_attr(event, pevlist); | |
67 | if (ret) | |
68 | return ret; | |
69 | ||
d20deb64 | 70 | return perf_event__repipe_synth(NULL, event, NULL); |
10d0f086 ACM |
71 | } |
72 | ||
45694aa7 | 73 | static int perf_event__repipe(struct perf_tool *tool, |
d20deb64 | 74 | union perf_event *event, |
1d037ca1 | 75 | struct perf_sample *sample __maybe_unused, |
743eb868 | 76 | struct machine *machine) |
640c03ce | 77 | { |
45694aa7 | 78 | return perf_event__repipe_synth(tool, event, machine); |
640c03ce ACM |
79 | } |
80 | ||
45694aa7 | 81 | static int perf_event__repipe_sample(struct perf_tool *tool, |
d20deb64 | 82 | union perf_event *event, |
1d037ca1 IT |
83 | struct perf_sample *sample __maybe_unused, |
84 | struct perf_evsel *evsel __maybe_unused, | |
743eb868 | 85 | struct machine *machine) |
9e69c210 | 86 | { |
45694aa7 | 87 | return perf_event__repipe_synth(tool, event, machine); |
9e69c210 ACM |
88 | } |
89 | ||
45694aa7 | 90 | static int perf_event__repipe_mmap(struct perf_tool *tool, |
d20deb64 | 91 | union perf_event *event, |
8115d60c | 92 | struct perf_sample *sample, |
743eb868 | 93 | struct machine *machine) |
454c407e TZ |
94 | { |
95 | int err; | |
96 | ||
45694aa7 ACM |
97 | err = perf_event__process_mmap(tool, event, sample, machine); |
98 | perf_event__repipe(tool, event, sample, machine); | |
454c407e TZ |
99 | |
100 | return err; | |
101 | } | |
102 | ||
45694aa7 | 103 | static int perf_event__repipe_task(struct perf_tool *tool, |
d20deb64 | 104 | union perf_event *event, |
8115d60c | 105 | struct perf_sample *sample, |
743eb868 | 106 | struct machine *machine) |
454c407e TZ |
107 | { |
108 | int err; | |
109 | ||
45694aa7 ACM |
110 | err = perf_event__process_task(tool, event, sample, machine); |
111 | perf_event__repipe(tool, event, sample, machine); | |
454c407e TZ |
112 | |
113 | return err; | |
114 | } | |
115 | ||
8115d60c ACM |
116 | static int perf_event__repipe_tracing_data(union perf_event *event, |
117 | struct perf_session *session) | |
454c407e TZ |
118 | { |
119 | int err; | |
120 | ||
743eb868 | 121 | perf_event__repipe_synth(NULL, event, NULL); |
8115d60c | 122 | err = perf_event__process_tracing_data(event, session); |
454c407e TZ |
123 | |
124 | return err; | |
125 | } | |
126 | ||
090f7204 | 127 | static int dso__read_build_id(struct dso *self) |
454c407e | 128 | { |
090f7204 ACM |
129 | if (self->has_build_id) |
130 | return 0; | |
454c407e | 131 | |
090f7204 ACM |
132 | if (filename__read_build_id(self->long_name, self->build_id, |
133 | sizeof(self->build_id)) > 0) { | |
134 | self->has_build_id = true; | |
135 | return 0; | |
136 | } | |
454c407e | 137 | |
090f7204 ACM |
138 | return -1; |
139 | } | |
454c407e | 140 | |
45694aa7 | 141 | static int dso__inject_build_id(struct dso *self, struct perf_tool *tool, |
743eb868 | 142 | struct machine *machine) |
090f7204 ACM |
143 | { |
144 | u16 misc = PERF_RECORD_MISC_USER; | |
090f7204 | 145 | int err; |
454c407e | 146 | |
090f7204 ACM |
147 | if (dso__read_build_id(self) < 0) { |
148 | pr_debug("no build_id found for %s\n", self->long_name); | |
149 | return -1; | |
150 | } | |
454c407e | 151 | |
090f7204 ACM |
152 | if (self->kernel) |
153 | misc = PERF_RECORD_MISC_KERNEL; | |
454c407e | 154 | |
45694aa7 | 155 | err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe, |
743eb868 | 156 | machine); |
090f7204 ACM |
157 | if (err) { |
158 | pr_err("Can't synthesize build_id event for %s\n", self->long_name); | |
454c407e TZ |
159 | return -1; |
160 | } | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
45694aa7 | 165 | static int perf_event__inject_buildid(struct perf_tool *tool, |
d20deb64 | 166 | union perf_event *event, |
8115d60c | 167 | struct perf_sample *sample, |
1d037ca1 | 168 | struct perf_evsel *evsel __maybe_unused, |
743eb868 | 169 | struct machine *machine) |
454c407e TZ |
170 | { |
171 | struct addr_location al; | |
172 | struct thread *thread; | |
173 | u8 cpumode; | |
454c407e TZ |
174 | |
175 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; | |
176 | ||
743eb868 | 177 | thread = machine__findnew_thread(machine, event->ip.pid); |
454c407e TZ |
178 | if (thread == NULL) { |
179 | pr_err("problem processing %d event, skipping it.\n", | |
180 | event->header.type); | |
454c407e TZ |
181 | goto repipe; |
182 | } | |
183 | ||
743eb868 ACM |
184 | thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION, |
185 | event->ip.ip, &al); | |
454c407e TZ |
186 | |
187 | if (al.map != NULL) { | |
188 | if (!al.map->dso->hit) { | |
189 | al.map->dso->hit = 1; | |
090f7204 | 190 | if (map__load(al.map, NULL) >= 0) { |
45694aa7 | 191 | dso__inject_build_id(al.map->dso, tool, machine); |
090f7204 ACM |
192 | /* |
193 | * If this fails, too bad, let the other side | |
194 | * account this as unresolved. | |
195 | */ | |
393be2e3 NK |
196 | } else { |
197 | #ifndef NO_LIBELF_SUPPORT | |
454c407e TZ |
198 | pr_warning("no symbols found in %s, maybe " |
199 | "install a debug package?\n", | |
200 | al.map->dso->long_name); | |
393be2e3 NK |
201 | #endif |
202 | } | |
454c407e TZ |
203 | } |
204 | } | |
205 | ||
206 | repipe: | |
45694aa7 | 207 | perf_event__repipe(tool, event, sample, machine); |
090f7204 | 208 | return 0; |
454c407e TZ |
209 | } |
210 | ||
45694aa7 | 211 | struct perf_tool perf_inject = { |
9e69c210 | 212 | .sample = perf_event__repipe_sample, |
8115d60c ACM |
213 | .mmap = perf_event__repipe, |
214 | .comm = perf_event__repipe, | |
215 | .fork = perf_event__repipe, | |
216 | .exit = perf_event__repipe, | |
217 | .lost = perf_event__repipe, | |
743eb868 | 218 | .read = perf_event__repipe_sample, |
8115d60c ACM |
219 | .throttle = perf_event__repipe, |
220 | .unthrottle = perf_event__repipe, | |
10d0f086 | 221 | .attr = perf_event__repipe_attr, |
743eb868 | 222 | .event_type = perf_event__repipe_event_type_synth, |
d20deb64 | 223 | .tracing_data = perf_event__repipe_tracing_data_synth, |
743eb868 | 224 | .build_id = perf_event__repipe_op2_synth, |
454c407e TZ |
225 | }; |
226 | ||
227 | extern volatile int session_done; | |
228 | ||
1d037ca1 | 229 | static void sig_handler(int sig __maybe_unused) |
454c407e TZ |
230 | { |
231 | session_done = 1; | |
232 | } | |
233 | ||
234 | static int __cmd_inject(void) | |
235 | { | |
236 | struct perf_session *session; | |
237 | int ret = -EINVAL; | |
238 | ||
239 | signal(SIGINT, sig_handler); | |
240 | ||
241 | if (inject_build_ids) { | |
45694aa7 ACM |
242 | perf_inject.sample = perf_event__inject_buildid; |
243 | perf_inject.mmap = perf_event__repipe_mmap; | |
244 | perf_inject.fork = perf_event__repipe_task; | |
245 | perf_inject.tracing_data = perf_event__repipe_tracing_data; | |
454c407e TZ |
246 | } |
247 | ||
45694aa7 | 248 | session = perf_session__new(input_name, O_RDONLY, false, true, &perf_inject); |
454c407e TZ |
249 | if (session == NULL) |
250 | return -ENOMEM; | |
251 | ||
45694aa7 | 252 | ret = perf_session__process_events(session, &perf_inject); |
454c407e TZ |
253 | |
254 | perf_session__delete(session); | |
255 | ||
256 | return ret; | |
257 | } | |
258 | ||
259 | static const char * const report_usage[] = { | |
260 | "perf inject [<options>]", | |
261 | NULL | |
262 | }; | |
263 | ||
264 | static const struct option options[] = { | |
11d232ec | 265 | OPT_BOOLEAN('b', "build-ids", &inject_build_ids, |
454c407e TZ |
266 | "Inject build-ids into the output stream"), |
267 | OPT_INCR('v', "verbose", &verbose, | |
268 | "be more verbose (show build ids, etc)"), | |
269 | OPT_END() | |
270 | }; | |
271 | ||
1d037ca1 | 272 | int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused) |
454c407e TZ |
273 | { |
274 | argc = parse_options(argc, argv, options, report_usage, 0); | |
275 | ||
276 | /* | |
277 | * Any (unrecognized) arguments left? | |
278 | */ | |
279 | if (argc) | |
280 | usage_with_options(report_usage, options); | |
281 | ||
282 | if (symbol__init() < 0) | |
283 | return -1; | |
284 | ||
285 | return __cmd_inject(); | |
286 | } |