Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 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 | |
| 16 | static char const *input_name = "-"; |
| 17 | static bool inject_build_ids; |
| 18 | |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 19 | static int perf_event__repipe_synth(struct perf_event_ops *ops __used, |
| 20 | union perf_event *event, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 21 | struct machine *machine __used) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 40 | static 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 Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 43 | { |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 44 | return perf_event__repipe_synth(ops, event, NULL); |
| 45 | } |
| 46 | |
| 47 | static 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 | |
| 53 | static 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 Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 57 | } |
| 58 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 59 | static int perf_event__repipe_attr(union perf_event *event, |
| 60 | struct perf_evlist **pevlist __used) |
| 61 | { |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 62 | return perf_event__repipe_synth(NULL, event, NULL); |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 63 | } |
| 64 | |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 65 | static int perf_event__repipe(struct perf_event_ops *ops, |
| 66 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 67 | struct perf_sample *sample __used, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 68 | struct machine *machine) |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 69 | { |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 70 | return perf_event__repipe_synth(ops, event, machine); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 71 | } |
| 72 | |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 73 | static int perf_event__repipe_sample(struct perf_event_ops *ops, |
| 74 | union perf_event *event, |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 75 | struct perf_sample *sample __used, |
| 76 | struct perf_evsel *evsel __used, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 77 | struct machine *machine) |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 78 | { |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 79 | return perf_event__repipe_synth(ops, event, machine); |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 80 | } |
| 81 | |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 82 | static int perf_event__repipe_mmap(struct perf_event_ops *ops, |
| 83 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 84 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 85 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 86 | { |
| 87 | int err; |
| 88 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 89 | err = perf_event__process_mmap(ops, event, sample, machine); |
| 90 | perf_event__repipe(ops, event, sample, machine); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 91 | |
| 92 | return err; |
| 93 | } |
| 94 | |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 95 | static int perf_event__repipe_task(struct perf_event_ops *ops, |
| 96 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 97 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 98 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 99 | { |
| 100 | int err; |
| 101 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 102 | err = perf_event__process_task(ops, event, sample, machine); |
| 103 | perf_event__repipe(ops, event, sample, machine); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 104 | |
| 105 | return err; |
| 106 | } |
| 107 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 108 | static int perf_event__repipe_tracing_data(union perf_event *event, |
| 109 | struct perf_session *session) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 110 | { |
| 111 | int err; |
| 112 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 113 | perf_event__repipe_synth(NULL, event, NULL); |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 114 | err = perf_event__process_tracing_data(event, session); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 115 | |
| 116 | return err; |
| 117 | } |
| 118 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 119 | static int dso__read_build_id(struct dso *self) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 120 | { |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 121 | if (self->has_build_id) |
| 122 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 123 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 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; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 130 | return -1; |
| 131 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 132 | |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 133 | static int dso__inject_build_id(struct dso *self, struct perf_event_ops *ops, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 134 | struct machine *machine) |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 135 | { |
| 136 | u16 misc = PERF_RECORD_MISC_USER; |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 137 | int err; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 138 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 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 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 143 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 144 | if (self->kernel) |
| 145 | misc = PERF_RECORD_MISC_KERNEL; |
| 146 | |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 147 | err = perf_event__synthesize_build_id(ops, self, misc, perf_event__repipe, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 148 | machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 149 | if (err) { |
| 150 | pr_err("Can't synthesize build_id event for %s\n", self->long_name); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 157 | static int perf_event__inject_buildid(struct perf_event_ops *ops, |
| 158 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 159 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 160 | struct perf_evsel *evsel __used, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 161 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 162 | { |
| 163 | struct addr_location al; |
| 164 | struct thread *thread; |
| 165 | u8 cpumode; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 166 | |
| 167 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 168 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 169 | thread = machine__findnew_thread(machine, event->ip.pid); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 170 | if (thread == NULL) { |
| 171 | pr_err("problem processing %d event, skipping it.\n", |
| 172 | event->header.type); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 173 | goto repipe; |
| 174 | } |
| 175 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 176 | thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION, |
| 177 | event->ip.ip, &al); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 178 | |
| 179 | if (al.map != NULL) { |
| 180 | if (!al.map->dso->hit) { |
| 181 | al.map->dso->hit = 1; |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 182 | if (map__load(al.map, NULL) >= 0) { |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 183 | dso__inject_build_id(al.map->dso, ops, machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 184 | /* |
| 185 | * If this fails, too bad, let the other side |
| 186 | * account this as unresolved. |
| 187 | */ |
| 188 | } else |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 189 | pr_warning("no symbols found in %s, maybe " |
| 190 | "install a debug package?\n", |
| 191 | al.map->dso->long_name); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | repipe: |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 196 | perf_event__repipe(ops, event, sample, machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 197 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | struct perf_event_ops inject_ops = { |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 201 | .sample = perf_event__repipe_sample, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 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, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 207 | .read = perf_event__repipe_sample, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 208 | .throttle = perf_event__repipe, |
| 209 | .unthrottle = perf_event__repipe, |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 210 | .attr = perf_event__repipe_attr, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 211 | .event_type = perf_event__repipe_event_type_synth, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 212 | .tracing_data = perf_event__repipe_tracing_data_synth, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame^] | 213 | .build_id = perf_event__repipe_op2_synth, |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | extern volatile int session_done; |
| 217 | |
| 218 | static void sig_handler(int sig __attribute__((__unused__))) |
| 219 | { |
| 220 | session_done = 1; |
| 221 | } |
| 222 | |
| 223 | static 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 Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 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; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 237 | session = perf_session__new(input_name, O_RDONLY, false, true, &inject_ops); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 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 | |
| 248 | static const char * const report_usage[] = { |
| 249 | "perf inject [<options>]", |
| 250 | NULL |
| 251 | }; |
| 252 | |
| 253 | static const struct option options[] = { |
Arnaldo Carvalho de Melo | 11d232e | 2010-05-04 10:48:22 -0300 | [diff] [blame] | 254 | OPT_BOOLEAN('b', "build-ids", &inject_build_ids, |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 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 | |
| 261 | int 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 | } |