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