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