| 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/color.h" |
| 12 | #include "util/evlist.h" |
| 13 | #include "util/evsel.h" |
| 14 | #include "util/session.h" |
| 15 | #include "util/tool.h" |
| 16 | #include "util/debug.h" |
| 17 | |
| 18 | #include "util/parse-options.h" |
| 19 | |
| 20 | #include <linux/list.h> |
| 21 | |
| 22 | struct perf_inject { |
| 23 | struct perf_tool tool; |
| 24 | bool build_ids; |
| 25 | bool sched_stat; |
| 26 | const char *input_name; |
| 27 | int pipe_output, |
| 28 | output; |
| 29 | u64 bytes_written; |
| 30 | struct list_head samples; |
| 31 | }; |
| 32 | |
| 33 | struct event_entry { |
| 34 | struct list_head node; |
| 35 | u32 tid; |
| 36 | union perf_event event[0]; |
| 37 | }; |
| 38 | |
| 39 | static int perf_event__repipe_synth(struct perf_tool *tool, |
| 40 | union perf_event *event, |
| 41 | struct machine *machine __maybe_unused) |
| 42 | { |
| 43 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 44 | uint32_t size; |
| 45 | void *buf = event; |
| 46 | |
| 47 | size = event->header.size; |
| 48 | |
| 49 | while (size) { |
| 50 | int ret = write(inject->output, buf, size); |
| 51 | if (ret < 0) |
| 52 | return -errno; |
| 53 | |
| 54 | size -= ret; |
| 55 | buf += ret; |
| 56 | inject->bytes_written += ret; |
| 57 | } |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int perf_event__repipe_op2_synth(struct perf_tool *tool, |
| 63 | union perf_event *event, |
| 64 | struct perf_session *session |
| 65 | __maybe_unused) |
| 66 | { |
| 67 | return perf_event__repipe_synth(tool, event, NULL); |
| 68 | } |
| 69 | |
| 70 | static int perf_event__repipe_event_type_synth(struct perf_tool *tool, |
| 71 | union perf_event *event) |
| 72 | { |
| 73 | return perf_event__repipe_synth(tool, event, NULL); |
| 74 | } |
| 75 | |
| 76 | static int perf_event__repipe_tracing_data_synth(union perf_event *event, |
| 77 | struct perf_session *session |
| 78 | __maybe_unused) |
| 79 | { |
| 80 | return perf_event__repipe_synth(NULL, event, NULL); |
| 81 | } |
| 82 | |
| 83 | static int perf_event__repipe_attr(union perf_event *event, |
| 84 | struct perf_evlist **pevlist __maybe_unused) |
| 85 | { |
| 86 | int ret; |
| 87 | ret = perf_event__process_attr(event, pevlist); |
| 88 | if (ret) |
| 89 | return ret; |
| 90 | |
| 91 | return perf_event__repipe_synth(NULL, event, NULL); |
| 92 | } |
| 93 | |
| 94 | static int perf_event__repipe(struct perf_tool *tool, |
| 95 | union perf_event *event, |
| 96 | struct perf_sample *sample __maybe_unused, |
| 97 | struct machine *machine) |
| 98 | { |
| 99 | return perf_event__repipe_synth(tool, event, machine); |
| 100 | } |
| 101 | |
| 102 | typedef int (*inject_handler)(struct perf_tool *tool, |
| 103 | union perf_event *event, |
| 104 | struct perf_sample *sample, |
| 105 | struct perf_evsel *evsel, |
| 106 | struct machine *machine); |
| 107 | |
| 108 | static int perf_event__repipe_sample(struct perf_tool *tool, |
| 109 | union perf_event *event, |
| 110 | struct perf_sample *sample, |
| 111 | struct perf_evsel *evsel, |
| 112 | struct machine *machine) |
| 113 | { |
| 114 | if (evsel->handler.func) { |
| 115 | inject_handler f = evsel->handler.func; |
| 116 | return f(tool, event, sample, evsel, machine); |
| 117 | } |
| 118 | |
| 119 | return perf_event__repipe_synth(tool, event, machine); |
| 120 | } |
| 121 | |
| 122 | static int perf_event__repipe_mmap(struct perf_tool *tool, |
| 123 | union perf_event *event, |
| 124 | struct perf_sample *sample, |
| 125 | struct machine *machine) |
| 126 | { |
| 127 | int err; |
| 128 | |
| 129 | err = perf_event__process_mmap(tool, event, sample, machine); |
| 130 | perf_event__repipe(tool, event, sample, machine); |
| 131 | |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | static int perf_event__repipe_fork(struct perf_tool *tool, |
| 136 | union perf_event *event, |
| 137 | struct perf_sample *sample, |
| 138 | struct machine *machine) |
| 139 | { |
| 140 | int err; |
| 141 | |
| 142 | err = perf_event__process_fork(tool, event, sample, machine); |
| 143 | perf_event__repipe(tool, event, sample, machine); |
| 144 | |
| 145 | return err; |
| 146 | } |
| 147 | |
| 148 | static int perf_event__repipe_tracing_data(union perf_event *event, |
| 149 | struct perf_session *session) |
| 150 | { |
| 151 | int err; |
| 152 | |
| 153 | perf_event__repipe_synth(NULL, event, NULL); |
| 154 | err = perf_event__process_tracing_data(event, session); |
| 155 | |
| 156 | return err; |
| 157 | } |
| 158 | |
| 159 | static int dso__read_build_id(struct dso *self) |
| 160 | { |
| 161 | if (self->has_build_id) |
| 162 | return 0; |
| 163 | |
| 164 | if (filename__read_build_id(self->long_name, self->build_id, |
| 165 | sizeof(self->build_id)) > 0) { |
| 166 | self->has_build_id = true; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | static int dso__inject_build_id(struct dso *self, struct perf_tool *tool, |
| 174 | struct machine *machine) |
| 175 | { |
| 176 | u16 misc = PERF_RECORD_MISC_USER; |
| 177 | int err; |
| 178 | |
| 179 | if (dso__read_build_id(self) < 0) { |
| 180 | pr_debug("no build_id found for %s\n", self->long_name); |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | if (self->kernel) |
| 185 | misc = PERF_RECORD_MISC_KERNEL; |
| 186 | |
| 187 | err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe, |
| 188 | machine); |
| 189 | if (err) { |
| 190 | pr_err("Can't synthesize build_id event for %s\n", self->long_name); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int perf_event__inject_buildid(struct perf_tool *tool, |
| 198 | union perf_event *event, |
| 199 | struct perf_sample *sample, |
| 200 | struct perf_evsel *evsel __maybe_unused, |
| 201 | struct machine *machine) |
| 202 | { |
| 203 | struct addr_location al; |
| 204 | struct thread *thread; |
| 205 | u8 cpumode; |
| 206 | |
| 207 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 208 | |
| 209 | thread = machine__findnew_thread(machine, event->ip.pid); |
| 210 | if (thread == NULL) { |
| 211 | pr_err("problem processing %d event, skipping it.\n", |
| 212 | event->header.type); |
| 213 | goto repipe; |
| 214 | } |
| 215 | |
| 216 | thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION, |
| 217 | event->ip.ip, &al); |
| 218 | |
| 219 | if (al.map != NULL) { |
| 220 | if (!al.map->dso->hit) { |
| 221 | al.map->dso->hit = 1; |
| 222 | if (map__load(al.map, NULL) >= 0) { |
| 223 | dso__inject_build_id(al.map->dso, tool, machine); |
| 224 | /* |
| 225 | * If this fails, too bad, let the other side |
| 226 | * account this as unresolved. |
| 227 | */ |
| 228 | } else { |
| 229 | #ifdef LIBELF_SUPPORT |
| 230 | pr_warning("no symbols found in %s, maybe " |
| 231 | "install a debug package?\n", |
| 232 | al.map->dso->long_name); |
| 233 | #endif |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | repipe: |
| 239 | perf_event__repipe(tool, event, sample, machine); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int perf_inject__sched_process_exit(struct perf_tool *tool, |
| 244 | union perf_event *event __maybe_unused, |
| 245 | struct perf_sample *sample, |
| 246 | struct perf_evsel *evsel __maybe_unused, |
| 247 | struct machine *machine __maybe_unused) |
| 248 | { |
| 249 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 250 | struct event_entry *ent; |
| 251 | |
| 252 | list_for_each_entry(ent, &inject->samples, node) { |
| 253 | if (sample->tid == ent->tid) { |
| 254 | list_del_init(&ent->node); |
| 255 | free(ent); |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int perf_inject__sched_switch(struct perf_tool *tool, |
| 264 | union perf_event *event, |
| 265 | struct perf_sample *sample, |
| 266 | struct perf_evsel *evsel, |
| 267 | struct machine *machine) |
| 268 | { |
| 269 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 270 | struct event_entry *ent; |
| 271 | |
| 272 | perf_inject__sched_process_exit(tool, event, sample, evsel, machine); |
| 273 | |
| 274 | ent = malloc(event->header.size + sizeof(struct event_entry)); |
| 275 | if (ent == NULL) { |
| 276 | color_fprintf(stderr, PERF_COLOR_RED, |
| 277 | "Not enough memory to process sched switch event!"); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | ent->tid = sample->tid; |
| 282 | memcpy(&ent->event, event, event->header.size); |
| 283 | list_add(&ent->node, &inject->samples); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int perf_inject__sched_stat(struct perf_tool *tool, |
| 288 | union perf_event *event __maybe_unused, |
| 289 | struct perf_sample *sample, |
| 290 | struct perf_evsel *evsel, |
| 291 | struct machine *machine) |
| 292 | { |
| 293 | struct event_entry *ent; |
| 294 | union perf_event *event_sw; |
| 295 | struct perf_sample sample_sw; |
| 296 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 297 | u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
| 298 | |
| 299 | list_for_each_entry(ent, &inject->samples, node) { |
| 300 | if (pid == ent->tid) |
| 301 | goto found; |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | found: |
| 306 | event_sw = &ent->event[0]; |
| 307 | perf_evsel__parse_sample(evsel, event_sw, &sample_sw); |
| 308 | |
| 309 | sample_sw.period = sample->period; |
| 310 | sample_sw.time = sample->time; |
| 311 | perf_event__synthesize_sample(event_sw, evsel->attr.sample_type, |
| 312 | &sample_sw, false); |
| 313 | return perf_event__repipe(tool, event_sw, &sample_sw, machine); |
| 314 | } |
| 315 | |
| 316 | extern volatile int session_done; |
| 317 | |
| 318 | static void sig_handler(int sig __maybe_unused) |
| 319 | { |
| 320 | session_done = 1; |
| 321 | } |
| 322 | |
| 323 | static int perf_evsel__check_stype(struct perf_evsel *evsel, |
| 324 | u64 sample_type, const char *sample_msg) |
| 325 | { |
| 326 | struct perf_event_attr *attr = &evsel->attr; |
| 327 | const char *name = perf_evsel__name(evsel); |
| 328 | |
| 329 | if (!(attr->sample_type & sample_type)) { |
| 330 | pr_err("Samples for %s event do not have %s attribute set.", |
| 331 | name, sample_msg); |
| 332 | return -EINVAL; |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int __cmd_inject(struct perf_inject *inject) |
| 339 | { |
| 340 | struct perf_session *session; |
| 341 | int ret = -EINVAL; |
| 342 | |
| 343 | signal(SIGINT, sig_handler); |
| 344 | |
| 345 | if (inject->build_ids) { |
| 346 | inject->tool.sample = perf_event__inject_buildid; |
| 347 | inject->tool.mmap = perf_event__repipe_mmap; |
| 348 | inject->tool.fork = perf_event__repipe_fork; |
| 349 | inject->tool.tracing_data = perf_event__repipe_tracing_data; |
| 350 | } |
| 351 | |
| 352 | session = perf_session__new(inject->input_name, O_RDONLY, false, true, &inject->tool); |
| 353 | if (session == NULL) |
| 354 | return -ENOMEM; |
| 355 | |
| 356 | if (inject->sched_stat) { |
| 357 | struct perf_evsel *evsel; |
| 358 | |
| 359 | inject->tool.ordered_samples = true; |
| 360 | |
| 361 | list_for_each_entry(evsel, &session->evlist->entries, node) { |
| 362 | const char *name = perf_evsel__name(evsel); |
| 363 | |
| 364 | if (!strcmp(name, "sched:sched_switch")) { |
| 365 | if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID")) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | evsel->handler.func = perf_inject__sched_switch; |
| 369 | } else if (!strcmp(name, "sched:sched_process_exit")) |
| 370 | evsel->handler.func = perf_inject__sched_process_exit; |
| 371 | else if (!strncmp(name, "sched:sched_stat_", 17)) |
| 372 | evsel->handler.func = perf_inject__sched_stat; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (!inject->pipe_output) |
| 377 | lseek(inject->output, session->header.data_offset, SEEK_SET); |
| 378 | |
| 379 | ret = perf_session__process_events(session, &inject->tool); |
| 380 | |
| 381 | if (!inject->pipe_output) { |
| 382 | session->header.data_size = inject->bytes_written; |
| 383 | perf_session__write_header(session, session->evlist, inject->output, true); |
| 384 | } |
| 385 | |
| 386 | perf_session__delete(session); |
| 387 | |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused) |
| 392 | { |
| 393 | struct perf_inject inject = { |
| 394 | .tool = { |
| 395 | .sample = perf_event__repipe_sample, |
| 396 | .mmap = perf_event__repipe, |
| 397 | .comm = perf_event__repipe, |
| 398 | .fork = perf_event__repipe, |
| 399 | .exit = perf_event__repipe, |
| 400 | .lost = perf_event__repipe, |
| 401 | .read = perf_event__repipe_sample, |
| 402 | .throttle = perf_event__repipe, |
| 403 | .unthrottle = perf_event__repipe, |
| 404 | .attr = perf_event__repipe_attr, |
| 405 | .event_type = perf_event__repipe_event_type_synth, |
| 406 | .tracing_data = perf_event__repipe_tracing_data_synth, |
| 407 | .build_id = perf_event__repipe_op2_synth, |
| 408 | }, |
| 409 | .input_name = "-", |
| 410 | .samples = LIST_HEAD_INIT(inject.samples), |
| 411 | }; |
| 412 | const char *output_name = "-"; |
| 413 | const struct option options[] = { |
| 414 | OPT_BOOLEAN('b', "build-ids", &inject.build_ids, |
| 415 | "Inject build-ids into the output stream"), |
| 416 | OPT_STRING('i', "input", &inject.input_name, "file", |
| 417 | "input file name"), |
| 418 | OPT_STRING('o', "output", &output_name, "file", |
| 419 | "output file name"), |
| 420 | OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat, |
| 421 | "Merge sched-stat and sched-switch for getting events " |
| 422 | "where and how long tasks slept"), |
| 423 | OPT_INCR('v', "verbose", &verbose, |
| 424 | "be more verbose (show build ids, etc)"), |
| 425 | OPT_END() |
| 426 | }; |
| 427 | const char * const inject_usage[] = { |
| 428 | "perf inject [<options>]", |
| 429 | NULL |
| 430 | }; |
| 431 | |
| 432 | argc = parse_options(argc, argv, options, inject_usage, 0); |
| 433 | |
| 434 | /* |
| 435 | * Any (unrecognized) arguments left? |
| 436 | */ |
| 437 | if (argc) |
| 438 | usage_with_options(inject_usage, options); |
| 439 | |
| 440 | if (!strcmp(output_name, "-")) { |
| 441 | inject.pipe_output = 1; |
| 442 | inject.output = STDOUT_FILENO; |
| 443 | } else { |
| 444 | inject.output = open(output_name, O_CREAT | O_WRONLY | O_TRUNC, |
| 445 | S_IRUSR | S_IWUSR); |
| 446 | if (inject.output < 0) { |
| 447 | perror("failed to create output file"); |
| 448 | return -1; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | if (symbol__init() < 0) |
| 453 | return -1; |
| 454 | |
| 455 | return __cmd_inject(&inject); |
| 456 | } |