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