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" |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 11 | #include "util/color.h" |
| 12 | #include "util/evlist.h" |
| 13 | #include "util/evsel.h" |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 14 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 15 | #include "util/tool.h" |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 16 | #include "util/debug.h" |
Andrew Vagin | 54a3cf5 | 2012-08-07 16:56:05 +0400 | [diff] [blame] | 17 | #include "util/build-id.h" |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 18 | #include "util/data.h" |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 19 | #include "util/auxtrace.h" |
Stephane Eranian | 9b07e27 | 2015-11-30 10:02:21 +0100 | [diff] [blame] | 20 | #include "util/jit.h" |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 21 | |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 22 | #include <subcmd/parse-options.h> |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 23 | |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 24 | #include <linux/list.h> |
| 25 | |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 26 | struct perf_inject { |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 27 | struct perf_tool tool; |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 28 | struct perf_session *session; |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 29 | bool build_ids; |
| 30 | bool sched_stat; |
Adrian Hunter | cd10b28 | 2015-04-30 17:37:26 +0300 | [diff] [blame] | 31 | bool have_auxtrace; |
Adrian Hunter | f56fb98 | 2015-09-25 16:15:55 +0300 | [diff] [blame] | 32 | bool strip; |
Stephane Eranian | 9b07e27 | 2015-11-30 10:02:21 +0100 | [diff] [blame] | 33 | bool jit_mode; |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 34 | const char *input_name; |
| 35 | struct perf_data_file output; |
| 36 | u64 bytes_written; |
Adrian Hunter | 7311730 | 2015-09-25 16:15:54 +0300 | [diff] [blame] | 37 | u64 aux_id; |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 38 | struct list_head samples; |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 39 | struct itrace_synth_opts itrace_synth_opts; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | struct event_entry { |
| 43 | struct list_head node; |
| 44 | u32 tid; |
| 45 | union perf_event event[0]; |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 46 | }; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 47 | |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 48 | static int output_bytes(struct perf_inject *inject, void *buf, size_t sz) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 49 | { |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 50 | ssize_t size; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 51 | |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 52 | size = perf_data_file__write(&inject->output, buf, sz); |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 53 | if (size < 0) |
| 54 | return -errno; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 55 | |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 56 | inject->bytes_written += size; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 60 | static int perf_event__repipe_synth(struct perf_tool *tool, |
| 61 | union perf_event *event) |
| 62 | { |
| 63 | struct perf_inject *inject = container_of(tool, struct perf_inject, |
| 64 | tool); |
| 65 | |
| 66 | return output_bytes(inject, event, event->header.size); |
| 67 | } |
| 68 | |
Arnaldo Carvalho de Melo | d704ebd | 2015-03-03 12:37:54 -0300 | [diff] [blame] | 69 | static int perf_event__repipe_oe_synth(struct perf_tool *tool, |
| 70 | union perf_event *event, |
| 71 | struct ordered_events *oe __maybe_unused) |
| 72 | { |
| 73 | return perf_event__repipe_synth(tool, event); |
| 74 | } |
| 75 | |
Stephane Eranian | 9b07e27 | 2015-11-30 10:02:21 +0100 | [diff] [blame] | 76 | #ifdef HAVE_LIBELF_SUPPORT |
| 77 | static int perf_event__drop_oe(struct perf_tool *tool __maybe_unused, |
| 78 | union perf_event *event __maybe_unused, |
| 79 | struct ordered_events *oe __maybe_unused) |
| 80 | { |
| 81 | return 0; |
| 82 | } |
| 83 | #endif |
| 84 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 85 | static int perf_event__repipe_op2_synth(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 86 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 87 | struct perf_session *session |
| 88 | __maybe_unused) |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 89 | { |
Adrian Hunter | 63c2c9f | 2013-07-04 16:20:20 +0300 | [diff] [blame] | 90 | return perf_event__repipe_synth(tool, event); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 91 | } |
| 92 | |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 93 | static int perf_event__repipe_attr(struct perf_tool *tool, |
| 94 | union perf_event *event, |
| 95 | struct perf_evlist **pevlist) |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 96 | { |
Adrian Hunter | 89c97d9 | 2013-10-22 10:34:09 +0300 | [diff] [blame] | 97 | struct perf_inject *inject = container_of(tool, struct perf_inject, |
| 98 | tool); |
Stephane Eranian | 1a1ed1b | 2012-05-15 13:28:11 +0200 | [diff] [blame] | 99 | int ret; |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 100 | |
| 101 | ret = perf_event__process_attr(tool, event, pevlist); |
Stephane Eranian | 1a1ed1b | 2012-05-15 13:28:11 +0200 | [diff] [blame] | 102 | if (ret) |
| 103 | return ret; |
| 104 | |
Jiri Olsa | a261e4a | 2014-06-05 18:51:44 +0200 | [diff] [blame] | 105 | if (!inject->output.is_pipe) |
Adrian Hunter | 89c97d9 | 2013-10-22 10:34:09 +0300 | [diff] [blame] | 106 | return 0; |
| 107 | |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 108 | return perf_event__repipe_synth(tool, event); |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 109 | } |
| 110 | |
Adrian Hunter | e31f0d0 | 2015-04-30 17:37:27 +0300 | [diff] [blame] | 111 | #ifdef HAVE_AUXTRACE_SUPPORT |
| 112 | |
| 113 | static int copy_bytes(struct perf_inject *inject, int fd, off_t size) |
| 114 | { |
| 115 | char buf[4096]; |
| 116 | ssize_t ssz; |
| 117 | int ret; |
| 118 | |
| 119 | while (size > 0) { |
| 120 | ssz = read(fd, buf, min(size, (off_t)sizeof(buf))); |
| 121 | if (ssz < 0) |
| 122 | return -errno; |
| 123 | ret = output_bytes(inject, buf, ssz); |
| 124 | if (ret) |
| 125 | return ret; |
| 126 | size -= ssz; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 132 | static s64 perf_event__repipe_auxtrace(struct perf_tool *tool, |
| 133 | union perf_event *event, |
| 134 | struct perf_session *session |
| 135 | __maybe_unused) |
| 136 | { |
| 137 | struct perf_inject *inject = container_of(tool, struct perf_inject, |
| 138 | tool); |
| 139 | int ret; |
| 140 | |
Adrian Hunter | cd10b28 | 2015-04-30 17:37:26 +0300 | [diff] [blame] | 141 | inject->have_auxtrace = true; |
| 142 | |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 143 | if (!inject->output.is_pipe) { |
| 144 | off_t offset; |
| 145 | |
| 146 | offset = lseek(inject->output.fd, 0, SEEK_CUR); |
| 147 | if (offset == -1) |
| 148 | return -errno; |
| 149 | ret = auxtrace_index__auxtrace_event(&session->auxtrace_index, |
| 150 | event, offset); |
| 151 | if (ret < 0) |
| 152 | return ret; |
| 153 | } |
| 154 | |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 155 | if (perf_data_file__is_pipe(session->file) || !session->one_mmap) { |
| 156 | ret = output_bytes(inject, event, event->header.size); |
| 157 | if (ret < 0) |
| 158 | return ret; |
| 159 | ret = copy_bytes(inject, perf_data_file__fd(session->file), |
| 160 | event->auxtrace.size); |
| 161 | } else { |
| 162 | ret = output_bytes(inject, event, |
| 163 | event->header.size + event->auxtrace.size); |
| 164 | } |
| 165 | if (ret < 0) |
| 166 | return ret; |
| 167 | |
| 168 | return event->auxtrace.size; |
| 169 | } |
| 170 | |
Adrian Hunter | e31f0d0 | 2015-04-30 17:37:27 +0300 | [diff] [blame] | 171 | #else |
| 172 | |
| 173 | static s64 |
| 174 | perf_event__repipe_auxtrace(struct perf_tool *tool __maybe_unused, |
| 175 | union perf_event *event __maybe_unused, |
| 176 | struct perf_session *session __maybe_unused) |
| 177 | { |
| 178 | pr_err("AUX area tracing not supported\n"); |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | |
| 182 | #endif |
| 183 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 184 | static int perf_event__repipe(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 185 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 186 | struct perf_sample *sample __maybe_unused, |
Adrian Hunter | 63c2c9f | 2013-07-04 16:20:20 +0300 | [diff] [blame] | 187 | struct machine *machine __maybe_unused) |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 188 | { |
Adrian Hunter | 63c2c9f | 2013-07-04 16:20:20 +0300 | [diff] [blame] | 189 | return perf_event__repipe_synth(tool, event); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 190 | } |
| 191 | |
Adrian Hunter | f56fb98 | 2015-09-25 16:15:55 +0300 | [diff] [blame] | 192 | static int perf_event__drop(struct perf_tool *tool __maybe_unused, |
| 193 | union perf_event *event __maybe_unused, |
| 194 | struct perf_sample *sample __maybe_unused, |
| 195 | struct machine *machine __maybe_unused) |
| 196 | { |
| 197 | return 0; |
| 198 | } |
| 199 | |
Adrian Hunter | 7311730 | 2015-09-25 16:15:54 +0300 | [diff] [blame] | 200 | static int perf_event__drop_aux(struct perf_tool *tool, |
| 201 | union perf_event *event __maybe_unused, |
| 202 | struct perf_sample *sample, |
| 203 | struct machine *machine __maybe_unused) |
| 204 | { |
| 205 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 206 | |
| 207 | if (!inject->aux_id) |
| 208 | inject->aux_id = sample->id; |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 213 | typedef int (*inject_handler)(struct perf_tool *tool, |
| 214 | union perf_event *event, |
| 215 | struct perf_sample *sample, |
| 216 | struct perf_evsel *evsel, |
| 217 | struct machine *machine); |
| 218 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 219 | static int perf_event__repipe_sample(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 220 | union perf_event *event, |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 221 | struct perf_sample *sample, |
| 222 | struct perf_evsel *evsel, |
| 223 | struct machine *machine) |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 224 | { |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 225 | if (evsel->handler) { |
| 226 | inject_handler f = evsel->handler; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 227 | return f(tool, event, sample, evsel, machine); |
| 228 | } |
| 229 | |
Andrew Vagin | 54a3cf5 | 2012-08-07 16:56:05 +0400 | [diff] [blame] | 230 | build_id__mark_dso_hit(tool, event, sample, evsel, machine); |
| 231 | |
Adrian Hunter | 63c2c9f | 2013-07-04 16:20:20 +0300 | [diff] [blame] | 232 | return perf_event__repipe_synth(tool, event); |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 233 | } |
| 234 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 235 | static int perf_event__repipe_mmap(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 236 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 237 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 238 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 239 | { |
| 240 | int err; |
| 241 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 242 | err = perf_event__process_mmap(tool, event, sample, machine); |
| 243 | perf_event__repipe(tool, event, sample, machine); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 244 | |
| 245 | return err; |
| 246 | } |
| 247 | |
Stephane Eranian | 9b07e27 | 2015-11-30 10:02:21 +0100 | [diff] [blame] | 248 | #ifdef HAVE_LIBELF_SUPPORT |
| 249 | static int perf_event__jit_repipe_mmap(struct perf_tool *tool, |
| 250 | union perf_event *event, |
| 251 | struct perf_sample *sample, |
| 252 | struct machine *machine) |
| 253 | { |
| 254 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 255 | u64 n = 0; |
| 256 | |
| 257 | /* |
| 258 | * if jit marker, then inject jit mmaps and generate ELF images |
| 259 | */ |
| 260 | if (!jit_process(inject->session, &inject->output, machine, |
| 261 | event->mmap.filename, sample->pid, &n)) { |
| 262 | inject->bytes_written += n; |
| 263 | return 0; |
| 264 | } |
| 265 | return perf_event__repipe_mmap(tool, event, sample, machine); |
| 266 | } |
| 267 | #endif |
| 268 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 269 | static int perf_event__repipe_mmap2(struct perf_tool *tool, |
| 270 | union perf_event *event, |
| 271 | struct perf_sample *sample, |
| 272 | struct machine *machine) |
| 273 | { |
| 274 | int err; |
| 275 | |
| 276 | err = perf_event__process_mmap2(tool, event, sample, machine); |
| 277 | perf_event__repipe(tool, event, sample, machine); |
| 278 | |
| 279 | return err; |
| 280 | } |
| 281 | |
Stephane Eranian | 9b07e27 | 2015-11-30 10:02:21 +0100 | [diff] [blame] | 282 | #ifdef HAVE_LIBELF_SUPPORT |
| 283 | static int perf_event__jit_repipe_mmap2(struct perf_tool *tool, |
| 284 | union perf_event *event, |
| 285 | struct perf_sample *sample, |
| 286 | struct machine *machine) |
| 287 | { |
| 288 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 289 | u64 n = 0; |
| 290 | |
| 291 | /* |
| 292 | * if jit marker, then inject jit mmaps and generate ELF images |
| 293 | */ |
| 294 | if (!jit_process(inject->session, &inject->output, machine, |
| 295 | event->mmap2.filename, sample->pid, &n)) { |
| 296 | inject->bytes_written += n; |
| 297 | return 0; |
| 298 | } |
| 299 | return perf_event__repipe_mmap2(tool, event, sample, machine); |
| 300 | } |
| 301 | #endif |
| 302 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 303 | static int perf_event__repipe_fork(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 304 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 305 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 306 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 307 | { |
| 308 | int err; |
| 309 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 310 | err = perf_event__process_fork(tool, event, sample, machine); |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 311 | perf_event__repipe(tool, event, sample, machine); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 312 | |
| 313 | return err; |
| 314 | } |
| 315 | |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 316 | static int perf_event__repipe_comm(struct perf_tool *tool, |
| 317 | union perf_event *event, |
| 318 | struct perf_sample *sample, |
| 319 | struct machine *machine) |
| 320 | { |
| 321 | int err; |
| 322 | |
| 323 | err = perf_event__process_comm(tool, event, sample, machine); |
| 324 | perf_event__repipe(tool, event, sample, machine); |
| 325 | |
| 326 | return err; |
| 327 | } |
| 328 | |
| 329 | static int perf_event__repipe_exit(struct perf_tool *tool, |
| 330 | union perf_event *event, |
| 331 | struct perf_sample *sample, |
| 332 | struct machine *machine) |
| 333 | { |
| 334 | int err; |
| 335 | |
| 336 | err = perf_event__process_exit(tool, event, sample, machine); |
| 337 | perf_event__repipe(tool, event, sample, machine); |
| 338 | |
| 339 | return err; |
| 340 | } |
| 341 | |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 342 | static int perf_event__repipe_tracing_data(struct perf_tool *tool, |
| 343 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 344 | struct perf_session *session) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 345 | { |
| 346 | int err; |
| 347 | |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 348 | perf_event__repipe_synth(tool, event); |
| 349 | err = perf_event__process_tracing_data(tool, event, session); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 350 | |
| 351 | return err; |
| 352 | } |
| 353 | |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 354 | static int perf_event__repipe_id_index(struct perf_tool *tool, |
| 355 | union perf_event *event, |
| 356 | struct perf_session *session) |
| 357 | { |
| 358 | int err; |
| 359 | |
| 360 | perf_event__repipe_synth(tool, event); |
| 361 | err = perf_event__process_id_index(tool, event, session); |
| 362 | |
| 363 | return err; |
| 364 | } |
| 365 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 366 | static int dso__read_build_id(struct dso *dso) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 367 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 368 | if (dso->has_build_id) |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 369 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 370 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 371 | if (filename__read_build_id(dso->long_name, dso->build_id, |
| 372 | sizeof(dso->build_id)) > 0) { |
| 373 | dso->has_build_id = true; |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 374 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 375 | } |
| 376 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 377 | return -1; |
| 378 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 379 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 380 | static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 381 | struct machine *machine) |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 382 | { |
| 383 | u16 misc = PERF_RECORD_MISC_USER; |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 384 | int err; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 385 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 386 | if (dso__read_build_id(dso) < 0) { |
| 387 | pr_debug("no build_id found for %s\n", dso->long_name); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 388 | return -1; |
| 389 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 390 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 391 | if (dso->kernel) |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 392 | misc = PERF_RECORD_MISC_KERNEL; |
| 393 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 394 | err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 395 | machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 396 | if (err) { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 397 | pr_err("Can't synthesize build_id event for %s\n", dso->long_name); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 404 | static int perf_event__inject_buildid(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 405 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 406 | struct perf_sample *sample, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 407 | struct perf_evsel *evsel __maybe_unused, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 408 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 409 | { |
| 410 | struct addr_location al; |
| 411 | struct thread *thread; |
| 412 | u8 cpumode; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 413 | |
| 414 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 415 | |
Namhyung Kim | 13ce34d | 2014-05-12 09:56:42 +0900 | [diff] [blame] | 416 | thread = machine__findnew_thread(machine, sample->pid, sample->tid); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 417 | if (thread == NULL) { |
| 418 | pr_err("problem processing %d event, skipping it.\n", |
| 419 | event->header.type); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 420 | goto repipe; |
| 421 | } |
| 422 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 423 | thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 424 | |
| 425 | if (al.map != NULL) { |
| 426 | if (!al.map->dso->hit) { |
| 427 | al.map->dso->hit = 1; |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 428 | if (map__load(al.map, NULL) >= 0) { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 429 | dso__inject_build_id(al.map->dso, tool, machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 430 | /* |
| 431 | * If this fails, too bad, let the other side |
| 432 | * account this as unresolved. |
| 433 | */ |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 434 | } else { |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 435 | #ifdef HAVE_LIBELF_SUPPORT |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 436 | pr_warning("no symbols found in %s, maybe " |
| 437 | "install a debug package?\n", |
| 438 | al.map->dso->long_name); |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 439 | #endif |
| 440 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 444 | thread__put(thread); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 445 | repipe: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 446 | perf_event__repipe(tool, event, sample, machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 447 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 448 | } |
| 449 | |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 450 | static int perf_inject__sched_process_exit(struct perf_tool *tool, |
| 451 | union perf_event *event __maybe_unused, |
| 452 | struct perf_sample *sample, |
| 453 | struct perf_evsel *evsel __maybe_unused, |
| 454 | struct machine *machine __maybe_unused) |
| 455 | { |
| 456 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 457 | struct event_entry *ent; |
| 458 | |
| 459 | list_for_each_entry(ent, &inject->samples, node) { |
| 460 | if (sample->tid == ent->tid) { |
| 461 | list_del_init(&ent->node); |
| 462 | free(ent); |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static int perf_inject__sched_switch(struct perf_tool *tool, |
| 471 | union perf_event *event, |
| 472 | struct perf_sample *sample, |
| 473 | struct perf_evsel *evsel, |
| 474 | struct machine *machine) |
| 475 | { |
| 476 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 477 | struct event_entry *ent; |
| 478 | |
| 479 | perf_inject__sched_process_exit(tool, event, sample, evsel, machine); |
| 480 | |
| 481 | ent = malloc(event->header.size + sizeof(struct event_entry)); |
| 482 | if (ent == NULL) { |
| 483 | color_fprintf(stderr, PERF_COLOR_RED, |
| 484 | "Not enough memory to process sched switch event!"); |
| 485 | return -1; |
| 486 | } |
| 487 | |
| 488 | ent->tid = sample->tid; |
| 489 | memcpy(&ent->event, event, event->header.size); |
| 490 | list_add(&ent->node, &inject->samples); |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static int perf_inject__sched_stat(struct perf_tool *tool, |
| 495 | union perf_event *event __maybe_unused, |
| 496 | struct perf_sample *sample, |
| 497 | struct perf_evsel *evsel, |
| 498 | struct machine *machine) |
| 499 | { |
| 500 | struct event_entry *ent; |
| 501 | union perf_event *event_sw; |
| 502 | struct perf_sample sample_sw; |
| 503 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 504 | u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
| 505 | |
| 506 | list_for_each_entry(ent, &inject->samples, node) { |
| 507 | if (pid == ent->tid) |
| 508 | goto found; |
| 509 | } |
| 510 | |
| 511 | return 0; |
| 512 | found: |
| 513 | event_sw = &ent->event[0]; |
| 514 | perf_evsel__parse_sample(evsel, event_sw, &sample_sw); |
| 515 | |
| 516 | sample_sw.period = sample->period; |
| 517 | sample_sw.time = sample->time; |
| 518 | perf_event__synthesize_sample(event_sw, evsel->attr.sample_type, |
Adrian Hunter | d03f217 | 2013-08-27 11:23:11 +0300 | [diff] [blame] | 519 | evsel->attr.read_format, &sample_sw, |
| 520 | false); |
Andrew Vagin | 54a3cf5 | 2012-08-07 16:56:05 +0400 | [diff] [blame] | 521 | build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine); |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 522 | return perf_event__repipe(tool, event_sw, &sample_sw, machine); |
| 523 | } |
| 524 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 525 | static void sig_handler(int sig __maybe_unused) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 526 | { |
| 527 | session_done = 1; |
| 528 | } |
| 529 | |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 530 | static int perf_evsel__check_stype(struct perf_evsel *evsel, |
| 531 | u64 sample_type, const char *sample_msg) |
| 532 | { |
| 533 | struct perf_event_attr *attr = &evsel->attr; |
| 534 | const char *name = perf_evsel__name(evsel); |
| 535 | |
| 536 | if (!(attr->sample_type & sample_type)) { |
| 537 | pr_err("Samples for %s event do not have %s attribute set.", |
| 538 | name, sample_msg); |
| 539 | return -EINVAL; |
| 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
Adrian Hunter | f56fb98 | 2015-09-25 16:15:55 +0300 | [diff] [blame] | 545 | static int drop_sample(struct perf_tool *tool __maybe_unused, |
| 546 | union perf_event *event __maybe_unused, |
| 547 | struct perf_sample *sample __maybe_unused, |
| 548 | struct perf_evsel *evsel __maybe_unused, |
| 549 | struct machine *machine __maybe_unused) |
| 550 | { |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static void strip_init(struct perf_inject *inject) |
| 555 | { |
| 556 | struct perf_evlist *evlist = inject->session->evlist; |
| 557 | struct perf_evsel *evsel; |
| 558 | |
| 559 | inject->tool.context_switch = perf_event__drop; |
| 560 | |
| 561 | evlist__for_each(evlist, evsel) |
| 562 | evsel->handler = drop_sample; |
| 563 | } |
| 564 | |
| 565 | static bool has_tracking(struct perf_evsel *evsel) |
| 566 | { |
| 567 | return evsel->attr.mmap || evsel->attr.mmap2 || evsel->attr.comm || |
| 568 | evsel->attr.task; |
| 569 | } |
| 570 | |
| 571 | #define COMPAT_MASK (PERF_SAMPLE_ID | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \ |
| 572 | PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER) |
| 573 | |
| 574 | /* |
| 575 | * In order that the perf.data file is parsable, tracking events like MMAP need |
| 576 | * their selected event to exist, except if there is only 1 selected event left |
| 577 | * and it has a compatible sample type. |
| 578 | */ |
| 579 | static bool ok_to_remove(struct perf_evlist *evlist, |
| 580 | struct perf_evsel *evsel_to_remove) |
| 581 | { |
| 582 | struct perf_evsel *evsel; |
| 583 | int cnt = 0; |
| 584 | bool ok = false; |
| 585 | |
| 586 | if (!has_tracking(evsel_to_remove)) |
| 587 | return true; |
| 588 | |
| 589 | evlist__for_each(evlist, evsel) { |
| 590 | if (evsel->handler != drop_sample) { |
| 591 | cnt += 1; |
| 592 | if ((evsel->attr.sample_type & COMPAT_MASK) == |
| 593 | (evsel_to_remove->attr.sample_type & COMPAT_MASK)) |
| 594 | ok = true; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | return ok && cnt == 1; |
| 599 | } |
| 600 | |
| 601 | static void strip_fini(struct perf_inject *inject) |
| 602 | { |
| 603 | struct perf_evlist *evlist = inject->session->evlist; |
| 604 | struct perf_evsel *evsel, *tmp; |
| 605 | |
| 606 | /* Remove non-synthesized evsels if possible */ |
| 607 | evlist__for_each_safe(evlist, tmp, evsel) { |
| 608 | if (evsel->handler == drop_sample && |
| 609 | ok_to_remove(evlist, evsel)) { |
| 610 | pr_debug("Deleting %s\n", perf_evsel__name(evsel)); |
| 611 | perf_evlist__remove(evlist, evsel); |
| 612 | perf_evsel__delete(evsel); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 617 | static int __cmd_inject(struct perf_inject *inject) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 618 | { |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 619 | int ret = -EINVAL; |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 620 | struct perf_session *session = inject->session; |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 621 | struct perf_data_file *file_out = &inject->output; |
Namhyung Kim | 42aa276 | 2015-01-29 17:06:48 +0900 | [diff] [blame] | 622 | int fd = perf_data_file__fd(file_out); |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 623 | u64 output_data_offset; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 624 | |
| 625 | signal(SIGINT, sig_handler); |
| 626 | |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 627 | if (inject->build_ids || inject->sched_stat || |
| 628 | inject->itrace_synth_opts.set) { |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 629 | inject->tool.mmap = perf_event__repipe_mmap; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 630 | inject->tool.mmap2 = perf_event__repipe_mmap2; |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 631 | inject->tool.fork = perf_event__repipe_fork; |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 632 | inject->tool.tracing_data = perf_event__repipe_tracing_data; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 633 | } |
| 634 | |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 635 | output_data_offset = session->header.data_offset; |
| 636 | |
Andrew Vagin | 54a3cf5 | 2012-08-07 16:56:05 +0400 | [diff] [blame] | 637 | if (inject->build_ids) { |
| 638 | inject->tool.sample = perf_event__inject_buildid; |
| 639 | } else if (inject->sched_stat) { |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 640 | struct perf_evsel *evsel; |
| 641 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 642 | evlist__for_each(session->evlist, evsel) { |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 643 | const char *name = perf_evsel__name(evsel); |
| 644 | |
| 645 | if (!strcmp(name, "sched:sched_switch")) { |
| 646 | if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID")) |
| 647 | return -EINVAL; |
| 648 | |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 649 | evsel->handler = perf_inject__sched_switch; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 650 | } else if (!strcmp(name, "sched:sched_process_exit")) |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 651 | evsel->handler = perf_inject__sched_process_exit; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 652 | else if (!strncmp(name, "sched:sched_stat_", 17)) |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 653 | evsel->handler = perf_inject__sched_stat; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 654 | } |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 655 | } else if (inject->itrace_synth_opts.set) { |
| 656 | session->itrace_synth_opts = &inject->itrace_synth_opts; |
| 657 | inject->itrace_synth_opts.inject = true; |
| 658 | inject->tool.comm = perf_event__repipe_comm; |
| 659 | inject->tool.exit = perf_event__repipe_exit; |
| 660 | inject->tool.id_index = perf_event__repipe_id_index; |
| 661 | inject->tool.auxtrace_info = perf_event__process_auxtrace_info; |
| 662 | inject->tool.auxtrace = perf_event__process_auxtrace; |
Adrian Hunter | 7311730 | 2015-09-25 16:15:54 +0300 | [diff] [blame] | 663 | inject->tool.aux = perf_event__drop_aux; |
| 664 | inject->tool.itrace_start = perf_event__drop_aux, |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 665 | inject->tool.ordered_events = true; |
| 666 | inject->tool.ordering_requires_timestamps = true; |
| 667 | /* Allow space in the header for new attributes */ |
| 668 | output_data_offset = 4096; |
Adrian Hunter | f56fb98 | 2015-09-25 16:15:55 +0300 | [diff] [blame] | 669 | if (inject->strip) |
| 670 | strip_init(inject); |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 671 | } |
| 672 | |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 673 | if (!inject->itrace_synth_opts.set) |
| 674 | auxtrace_index__free(&session->auxtrace_index); |
| 675 | |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 676 | if (!file_out->is_pipe) |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 677 | lseek(fd, output_data_offset, SEEK_SET); |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 678 | |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 679 | ret = perf_session__process_events(session); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 680 | |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 681 | if (!file_out->is_pipe) { |
Adrian Hunter | 640dad4 | 2016-03-07 16:44:38 -0300 | [diff] [blame^] | 682 | if (inject->build_ids) |
Adrian Hunter | e38b43c | 2014-07-14 13:02:34 +0300 | [diff] [blame] | 683 | perf_header__set_feat(&session->header, |
| 684 | HEADER_BUILD_ID); |
Adrian Hunter | 640dad4 | 2016-03-07 16:44:38 -0300 | [diff] [blame^] | 685 | /* |
| 686 | * Keep all buildids when there is unprocessed AUX data because |
| 687 | * it is not known which ones the AUX trace hits. |
| 688 | */ |
| 689 | if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) && |
| 690 | inject->have_auxtrace && !inject->itrace_synth_opts.set) |
| 691 | dsos__hit_all(session); |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 692 | /* |
| 693 | * The AUX areas have been removed and replaced with |
Adrian Hunter | 7311730 | 2015-09-25 16:15:54 +0300 | [diff] [blame] | 694 | * synthesized hardware events, so clear the feature flag and |
| 695 | * remove the evsel. |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 696 | */ |
Adrian Hunter | 051a01b | 2015-09-25 16:15:43 +0300 | [diff] [blame] | 697 | if (inject->itrace_synth_opts.set) { |
Adrian Hunter | 7311730 | 2015-09-25 16:15:54 +0300 | [diff] [blame] | 698 | struct perf_evsel *evsel; |
| 699 | |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 700 | perf_header__clear_feat(&session->header, |
| 701 | HEADER_AUXTRACE); |
Adrian Hunter | 051a01b | 2015-09-25 16:15:43 +0300 | [diff] [blame] | 702 | if (inject->itrace_synth_opts.last_branch) |
| 703 | perf_header__set_feat(&session->header, |
| 704 | HEADER_BRANCH_STACK); |
Adrian Hunter | 7311730 | 2015-09-25 16:15:54 +0300 | [diff] [blame] | 705 | evsel = perf_evlist__id2evsel_strict(session->evlist, |
| 706 | inject->aux_id); |
| 707 | if (evsel) { |
| 708 | pr_debug("Deleting %s\n", |
| 709 | perf_evsel__name(evsel)); |
| 710 | perf_evlist__remove(session->evlist, evsel); |
| 711 | perf_evsel__delete(evsel); |
| 712 | } |
Adrian Hunter | f56fb98 | 2015-09-25 16:15:55 +0300 | [diff] [blame] | 713 | if (inject->strip) |
| 714 | strip_fini(inject); |
Adrian Hunter | 051a01b | 2015-09-25 16:15:43 +0300 | [diff] [blame] | 715 | } |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 716 | session->header.data_offset = output_data_offset; |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 717 | session->header.data_size = inject->bytes_written; |
Namhyung Kim | 42aa276 | 2015-01-29 17:06:48 +0900 | [diff] [blame] | 718 | perf_session__write_header(session, session->evlist, fd, true); |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 719 | } |
| 720 | |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 721 | return ret; |
| 722 | } |
| 723 | |
Stephane Eranian | 9b07e27 | 2015-11-30 10:02:21 +0100 | [diff] [blame] | 724 | #ifdef HAVE_LIBELF_SUPPORT |
| 725 | static int |
| 726 | jit_validate_events(struct perf_session *session) |
| 727 | { |
| 728 | struct perf_evsel *evsel; |
| 729 | |
| 730 | /* |
| 731 | * check that all events use CLOCK_MONOTONIC |
| 732 | */ |
| 733 | evlist__for_each(session->evlist, evsel) { |
| 734 | if (evsel->attr.use_clockid == 0 || evsel->attr.clockid != CLOCK_MONOTONIC) |
| 735 | return -1; |
| 736 | } |
| 737 | return 0; |
| 738 | } |
| 739 | #endif |
| 740 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 741 | int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 742 | { |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 743 | struct perf_inject inject = { |
| 744 | .tool = { |
| 745 | .sample = perf_event__repipe_sample, |
| 746 | .mmap = perf_event__repipe, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 747 | .mmap2 = perf_event__repipe, |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 748 | .comm = perf_event__repipe, |
| 749 | .fork = perf_event__repipe, |
| 750 | .exit = perf_event__repipe, |
| 751 | .lost = perf_event__repipe, |
Adrian Hunter | d8145b3 | 2015-11-13 11:48:32 +0200 | [diff] [blame] | 752 | .lost_samples = perf_event__repipe, |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 753 | .aux = perf_event__repipe, |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 754 | .itrace_start = perf_event__repipe, |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 755 | .context_switch = perf_event__repipe, |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 756 | .read = perf_event__repipe_sample, |
| 757 | .throttle = perf_event__repipe, |
| 758 | .unthrottle = perf_event__repipe, |
| 759 | .attr = perf_event__repipe_attr, |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 760 | .tracing_data = perf_event__repipe_op2_synth, |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 761 | .auxtrace_info = perf_event__repipe_op2_synth, |
| 762 | .auxtrace = perf_event__repipe_auxtrace, |
| 763 | .auxtrace_error = perf_event__repipe_op2_synth, |
Arnaldo Carvalho de Melo | d704ebd | 2015-03-03 12:37:54 -0300 | [diff] [blame] | 764 | .finished_round = perf_event__repipe_oe_synth, |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 765 | .build_id = perf_event__repipe_op2_synth, |
Adrian Hunter | 3c659ee | 2014-10-27 15:49:22 +0200 | [diff] [blame] | 766 | .id_index = perf_event__repipe_op2_synth, |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 767 | }, |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 768 | .input_name = "-", |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 769 | .samples = LIST_HEAD_INIT(inject.samples), |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 770 | .output = { |
| 771 | .path = "-", |
| 772 | .mode = PERF_DATA_MODE_WRITE, |
| 773 | }, |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 774 | }; |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 775 | struct perf_data_file file = { |
| 776 | .mode = PERF_DATA_MODE_READ, |
| 777 | }; |
| 778 | int ret; |
| 779 | |
Stephane Eranian | 9b07e27 | 2015-11-30 10:02:21 +0100 | [diff] [blame] | 780 | struct option options[] = { |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 781 | OPT_BOOLEAN('b', "build-ids", &inject.build_ids, |
| 782 | "Inject build-ids into the output stream"), |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 783 | OPT_STRING('i', "input", &inject.input_name, "file", |
| 784 | "input file name"), |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 785 | OPT_STRING('o', "output", &inject.output.path, "file", |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 786 | "output file name"), |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 787 | OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat, |
| 788 | "Merge sched-stat and sched-switch for getting events " |
| 789 | "where and how long tasks slept"), |
Stephane Eranian | 9b07e27 | 2015-11-30 10:02:21 +0100 | [diff] [blame] | 790 | OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"), |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 791 | OPT_INCR('v', "verbose", &verbose, |
| 792 | "be more verbose (show build ids, etc)"), |
Adrian Hunter | a7a2b8b | 2014-07-22 16:17:38 +0300 | [diff] [blame] | 793 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file", |
| 794 | "kallsyms pathname"), |
Yunlong Song | ccaa474 | 2015-04-02 21:47:11 +0800 | [diff] [blame] | 795 | OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"), |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame] | 796 | OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts, |
| 797 | NULL, "opts", "Instruction Tracing options", |
| 798 | itrace_parse_synth_opts), |
Adrian Hunter | f56fb98 | 2015-09-25 16:15:55 +0300 | [diff] [blame] | 799 | OPT_BOOLEAN(0, "strip", &inject.strip, |
| 800 | "strip non-synthesized events (use with --itrace)"), |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 801 | OPT_END() |
| 802 | }; |
Arnaldo Carvalho de Melo | 002439e | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 803 | const char * const inject_usage[] = { |
| 804 | "perf inject [<options>]", |
| 805 | NULL |
| 806 | }; |
Stephane Eranian | 9b07e27 | 2015-11-30 10:02:21 +0100 | [diff] [blame] | 807 | #ifndef HAVE_LIBELF_SUPPORT |
| 808 | set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true); |
| 809 | #endif |
Arnaldo Carvalho de Melo | 002439e | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 810 | argc = parse_options(argc, argv, options, inject_usage, 0); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 811 | |
| 812 | /* |
| 813 | * Any (unrecognized) arguments left? |
| 814 | */ |
| 815 | if (argc) |
Arnaldo Carvalho de Melo | 002439e | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 816 | usage_with_options(inject_usage, options); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 817 | |
Adrian Hunter | f56fb98 | 2015-09-25 16:15:55 +0300 | [diff] [blame] | 818 | if (inject.strip && !inject.itrace_synth_opts.set) { |
| 819 | pr_err("--strip option requires --itrace option\n"); |
| 820 | return -1; |
| 821 | } |
| 822 | |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 823 | if (perf_data_file__open(&inject.output)) { |
| 824 | perror("failed to create output file"); |
| 825 | return -1; |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 826 | } |
| 827 | |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 828 | inject.tool.ordered_events = inject.sched_stat; |
| 829 | |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 830 | file.path = inject.input_name; |
| 831 | inject.session = perf_session__new(&file, true, &inject.tool); |
| 832 | if (inject.session == NULL) |
Taeung Song | 52e02834 | 2014-09-24 10:33:37 +0900 | [diff] [blame] | 833 | return -1; |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 834 | |
Arnaldo Carvalho de Melo | 921f3fa | 2016-01-22 18:41:00 -0300 | [diff] [blame] | 835 | if (inject.build_ids) { |
| 836 | /* |
| 837 | * to make sure the mmap records are ordered correctly |
| 838 | * and so that the correct especially due to jitted code |
| 839 | * mmaps. We cannot generate the buildid hit list and |
| 840 | * inject the jit mmaps at the same time for now. |
| 841 | */ |
| 842 | inject.tool.ordered_events = true; |
| 843 | inject.tool.ordering_requires_timestamps = true; |
| 844 | } |
Stephane Eranian | 9b07e27 | 2015-11-30 10:02:21 +0100 | [diff] [blame] | 845 | #ifdef HAVE_LIBELF_SUPPORT |
| 846 | if (inject.jit_mode) { |
| 847 | /* |
| 848 | * validate event is using the correct clockid |
| 849 | */ |
| 850 | if (jit_validate_events(inject.session)) { |
| 851 | fprintf(stderr, "error, jitted code must be sampled with perf record -k 1\n"); |
| 852 | return -1; |
| 853 | } |
| 854 | inject.tool.mmap2 = perf_event__jit_repipe_mmap2; |
| 855 | inject.tool.mmap = perf_event__jit_repipe_mmap; |
| 856 | inject.tool.ordered_events = true; |
| 857 | inject.tool.ordering_requires_timestamps = true; |
| 858 | /* |
| 859 | * JIT MMAP injection injects all MMAP events in one go, so it |
| 860 | * does not obey finished_round semantics. |
| 861 | */ |
| 862 | inject.tool.finished_round = perf_event__drop_oe; |
| 863 | } |
| 864 | #endif |
Taeung Song | 9fedfb0 | 2015-06-30 17:15:20 +0900 | [diff] [blame] | 865 | ret = symbol__init(&inject.session->header.env); |
| 866 | if (ret < 0) |
| 867 | goto out_delete; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 868 | |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 869 | ret = __cmd_inject(&inject); |
| 870 | |
Taeung Song | 9fedfb0 | 2015-06-30 17:15:20 +0900 | [diff] [blame] | 871 | out_delete: |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 872 | perf_session__delete(inject.session); |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 873 | return ret; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 874 | } |