Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | * builtin-inject.c |
| 3 | * |
| 4 | * Builtin inject command: Examine the live mode (stdin) event stream |
| 5 | * and repipe it to stdout while optionally injecting additional |
| 6 | * events into it. |
| 7 | */ |
| 8 | #include "builtin.h" |
| 9 | |
| 10 | #include "perf.h" |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 11 | #include "util/color.h" |
| 12 | #include "util/evlist.h" |
| 13 | #include "util/evsel.h" |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 14 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 15 | #include "util/tool.h" |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 16 | #include "util/debug.h" |
Andrew Vagin | 54a3cf5 | 2012-08-07 16:56:05 +0400 | [diff] [blame] | 17 | #include "util/build-id.h" |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 18 | #include "util/data.h" |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 19 | #include "util/auxtrace.h" |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 20 | |
| 21 | #include "util/parse-options.h" |
| 22 | |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 23 | #include <linux/list.h> |
| 24 | |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 25 | struct perf_inject { |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 26 | struct perf_tool tool; |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 27 | struct perf_session *session; |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 28 | bool build_ids; |
| 29 | bool sched_stat; |
| 30 | const char *input_name; |
| 31 | struct perf_data_file output; |
| 32 | u64 bytes_written; |
| 33 | struct list_head samples; |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 34 | struct itrace_synth_opts itrace_synth_opts; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | struct event_entry { |
| 38 | struct list_head node; |
| 39 | u32 tid; |
| 40 | union perf_event event[0]; |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 41 | }; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 42 | |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 43 | 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] | 44 | { |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 45 | ssize_t size; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 46 | |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 47 | size = perf_data_file__write(&inject->output, buf, sz); |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 48 | if (size < 0) |
| 49 | return -errno; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 50 | |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 51 | inject->bytes_written += size; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 55 | static int copy_bytes(struct perf_inject *inject, int fd, off_t size) |
| 56 | { |
| 57 | char buf[4096]; |
| 58 | ssize_t ssz; |
| 59 | int ret; |
| 60 | |
| 61 | while (size > 0) { |
| 62 | ssz = read(fd, buf, min(size, (off_t)sizeof(buf))); |
| 63 | if (ssz < 0) |
| 64 | return -errno; |
| 65 | ret = output_bytes(inject, buf, ssz); |
| 66 | if (ret) |
| 67 | return ret; |
| 68 | size -= ssz; |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int perf_event__repipe_synth(struct perf_tool *tool, |
| 75 | union perf_event *event) |
| 76 | { |
| 77 | struct perf_inject *inject = container_of(tool, struct perf_inject, |
| 78 | tool); |
| 79 | |
| 80 | return output_bytes(inject, event, event->header.size); |
| 81 | } |
| 82 | |
Arnaldo Carvalho de Melo | d704ebd | 2015-03-03 12:37:54 -0300 | [diff] [blame] | 83 | static int perf_event__repipe_oe_synth(struct perf_tool *tool, |
| 84 | union perf_event *event, |
| 85 | struct ordered_events *oe __maybe_unused) |
| 86 | { |
| 87 | return perf_event__repipe_synth(tool, event); |
| 88 | } |
| 89 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 90 | static int perf_event__repipe_op2_synth(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 91 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 92 | struct perf_session *session |
| 93 | __maybe_unused) |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 94 | { |
Adrian Hunter | 63c2c9f | 2013-07-04 16:20:20 +0300 | [diff] [blame] | 95 | return perf_event__repipe_synth(tool, event); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 96 | } |
| 97 | |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 98 | static int perf_event__repipe_attr(struct perf_tool *tool, |
| 99 | union perf_event *event, |
| 100 | struct perf_evlist **pevlist) |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 101 | { |
Adrian Hunter | 89c97d9 | 2013-10-22 10:34:09 +0300 | [diff] [blame] | 102 | struct perf_inject *inject = container_of(tool, struct perf_inject, |
| 103 | tool); |
Stephane Eranian | 1a1ed1b | 2012-05-15 13:28:11 +0200 | [diff] [blame] | 104 | int ret; |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 105 | |
| 106 | ret = perf_event__process_attr(tool, event, pevlist); |
Stephane Eranian | 1a1ed1b | 2012-05-15 13:28:11 +0200 | [diff] [blame] | 107 | if (ret) |
| 108 | return ret; |
| 109 | |
Jiri Olsa | a261e4a | 2014-06-05 18:51:44 +0200 | [diff] [blame] | 110 | if (!inject->output.is_pipe) |
Adrian Hunter | 89c97d9 | 2013-10-22 10:34:09 +0300 | [diff] [blame] | 111 | return 0; |
| 112 | |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 113 | return perf_event__repipe_synth(tool, event); |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 114 | } |
| 115 | |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 116 | static s64 perf_event__repipe_auxtrace(struct perf_tool *tool, |
| 117 | union perf_event *event, |
| 118 | struct perf_session *session |
| 119 | __maybe_unused) |
| 120 | { |
| 121 | struct perf_inject *inject = container_of(tool, struct perf_inject, |
| 122 | tool); |
| 123 | int ret; |
| 124 | |
| 125 | if (perf_data_file__is_pipe(session->file) || !session->one_mmap) { |
| 126 | ret = output_bytes(inject, event, event->header.size); |
| 127 | if (ret < 0) |
| 128 | return ret; |
| 129 | ret = copy_bytes(inject, perf_data_file__fd(session->file), |
| 130 | event->auxtrace.size); |
| 131 | } else { |
| 132 | ret = output_bytes(inject, event, |
| 133 | event->header.size + event->auxtrace.size); |
| 134 | } |
| 135 | if (ret < 0) |
| 136 | return ret; |
| 137 | |
| 138 | return event->auxtrace.size; |
| 139 | } |
| 140 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 141 | static int perf_event__repipe(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 142 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 143 | struct perf_sample *sample __maybe_unused, |
Adrian Hunter | 63c2c9f | 2013-07-04 16:20:20 +0300 | [diff] [blame] | 144 | struct machine *machine __maybe_unused) |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 145 | { |
Adrian Hunter | 63c2c9f | 2013-07-04 16:20:20 +0300 | [diff] [blame] | 146 | return perf_event__repipe_synth(tool, event); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 147 | } |
| 148 | |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 149 | typedef int (*inject_handler)(struct perf_tool *tool, |
| 150 | union perf_event *event, |
| 151 | struct perf_sample *sample, |
| 152 | struct perf_evsel *evsel, |
| 153 | struct machine *machine); |
| 154 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 155 | static int perf_event__repipe_sample(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 156 | union perf_event *event, |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 157 | struct perf_sample *sample, |
| 158 | struct perf_evsel *evsel, |
| 159 | struct machine *machine) |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 160 | { |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 161 | if (evsel->handler) { |
| 162 | inject_handler f = evsel->handler; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 163 | return f(tool, event, sample, evsel, machine); |
| 164 | } |
| 165 | |
Andrew Vagin | 54a3cf5 | 2012-08-07 16:56:05 +0400 | [diff] [blame] | 166 | build_id__mark_dso_hit(tool, event, sample, evsel, machine); |
| 167 | |
Adrian Hunter | 63c2c9f | 2013-07-04 16:20:20 +0300 | [diff] [blame] | 168 | return perf_event__repipe_synth(tool, event); |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 169 | } |
| 170 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 171 | static int perf_event__repipe_mmap(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 172 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 173 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 174 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 175 | { |
| 176 | int err; |
| 177 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 178 | err = perf_event__process_mmap(tool, event, sample, machine); |
| 179 | perf_event__repipe(tool, event, sample, machine); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 180 | |
| 181 | return err; |
| 182 | } |
| 183 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 184 | static int perf_event__repipe_mmap2(struct perf_tool *tool, |
| 185 | union perf_event *event, |
| 186 | struct perf_sample *sample, |
| 187 | struct machine *machine) |
| 188 | { |
| 189 | int err; |
| 190 | |
| 191 | err = perf_event__process_mmap2(tool, event, sample, machine); |
| 192 | perf_event__repipe(tool, event, sample, machine); |
| 193 | |
| 194 | return err; |
| 195 | } |
| 196 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 197 | static int perf_event__repipe_fork(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 198 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 199 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 200 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 201 | { |
| 202 | int err; |
| 203 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 204 | err = perf_event__process_fork(tool, event, sample, machine); |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 205 | perf_event__repipe(tool, event, sample, machine); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 206 | |
| 207 | return err; |
| 208 | } |
| 209 | |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 210 | static int perf_event__repipe_comm(struct perf_tool *tool, |
| 211 | union perf_event *event, |
| 212 | struct perf_sample *sample, |
| 213 | struct machine *machine) |
| 214 | { |
| 215 | int err; |
| 216 | |
| 217 | err = perf_event__process_comm(tool, event, sample, machine); |
| 218 | perf_event__repipe(tool, event, sample, machine); |
| 219 | |
| 220 | return err; |
| 221 | } |
| 222 | |
| 223 | static int perf_event__repipe_exit(struct perf_tool *tool, |
| 224 | union perf_event *event, |
| 225 | struct perf_sample *sample, |
| 226 | struct machine *machine) |
| 227 | { |
| 228 | int err; |
| 229 | |
| 230 | err = perf_event__process_exit(tool, event, sample, machine); |
| 231 | perf_event__repipe(tool, event, sample, machine); |
| 232 | |
| 233 | return err; |
| 234 | } |
| 235 | |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 236 | static int perf_event__repipe_tracing_data(struct perf_tool *tool, |
| 237 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 238 | struct perf_session *session) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 239 | { |
| 240 | int err; |
| 241 | |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 242 | perf_event__repipe_synth(tool, event); |
| 243 | err = perf_event__process_tracing_data(tool, event, session); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 244 | |
| 245 | return err; |
| 246 | } |
| 247 | |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 248 | static int perf_event__repipe_id_index(struct perf_tool *tool, |
| 249 | union perf_event *event, |
| 250 | struct perf_session *session) |
| 251 | { |
| 252 | int err; |
| 253 | |
| 254 | perf_event__repipe_synth(tool, event); |
| 255 | err = perf_event__process_id_index(tool, event, session); |
| 256 | |
| 257 | return err; |
| 258 | } |
| 259 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 260 | static int dso__read_build_id(struct dso *dso) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 261 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 262 | if (dso->has_build_id) |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 263 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 264 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 265 | if (filename__read_build_id(dso->long_name, dso->build_id, |
| 266 | sizeof(dso->build_id)) > 0) { |
| 267 | dso->has_build_id = true; |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 268 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 271 | return -1; |
| 272 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 273 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 274 | 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] | 275 | struct machine *machine) |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 276 | { |
| 277 | u16 misc = PERF_RECORD_MISC_USER; |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 278 | int err; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 279 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 280 | if (dso__read_build_id(dso) < 0) { |
| 281 | 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] | 282 | return -1; |
| 283 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 284 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 285 | if (dso->kernel) |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 286 | misc = PERF_RECORD_MISC_KERNEL; |
| 287 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 288 | 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] | 289 | machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 290 | if (err) { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 291 | 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] | 292 | return -1; |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 298 | static int perf_event__inject_buildid(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 299 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 300 | struct perf_sample *sample, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 301 | struct perf_evsel *evsel __maybe_unused, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 302 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 303 | { |
| 304 | struct addr_location al; |
| 305 | struct thread *thread; |
| 306 | u8 cpumode; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 307 | |
| 308 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 309 | |
Namhyung Kim | 13ce34d | 2014-05-12 09:56:42 +0900 | [diff] [blame] | 310 | thread = machine__findnew_thread(machine, sample->pid, sample->tid); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 311 | if (thread == NULL) { |
| 312 | pr_err("problem processing %d event, skipping it.\n", |
| 313 | event->header.type); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 314 | goto repipe; |
| 315 | } |
| 316 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 317 | thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 318 | |
| 319 | if (al.map != NULL) { |
| 320 | if (!al.map->dso->hit) { |
| 321 | al.map->dso->hit = 1; |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 322 | if (map__load(al.map, NULL) >= 0) { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 323 | dso__inject_build_id(al.map->dso, tool, machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 324 | /* |
| 325 | * If this fails, too bad, let the other side |
| 326 | * account this as unresolved. |
| 327 | */ |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 328 | } else { |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 329 | #ifdef HAVE_LIBELF_SUPPORT |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 330 | pr_warning("no symbols found in %s, maybe " |
| 331 | "install a debug package?\n", |
| 332 | al.map->dso->long_name); |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 333 | #endif |
| 334 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | repipe: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 339 | perf_event__repipe(tool, event, sample, machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 340 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 341 | } |
| 342 | |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 343 | static int perf_inject__sched_process_exit(struct perf_tool *tool, |
| 344 | union perf_event *event __maybe_unused, |
| 345 | struct perf_sample *sample, |
| 346 | struct perf_evsel *evsel __maybe_unused, |
| 347 | struct machine *machine __maybe_unused) |
| 348 | { |
| 349 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 350 | struct event_entry *ent; |
| 351 | |
| 352 | list_for_each_entry(ent, &inject->samples, node) { |
| 353 | if (sample->tid == ent->tid) { |
| 354 | list_del_init(&ent->node); |
| 355 | free(ent); |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int perf_inject__sched_switch(struct perf_tool *tool, |
| 364 | union perf_event *event, |
| 365 | struct perf_sample *sample, |
| 366 | struct perf_evsel *evsel, |
| 367 | struct machine *machine) |
| 368 | { |
| 369 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 370 | struct event_entry *ent; |
| 371 | |
| 372 | perf_inject__sched_process_exit(tool, event, sample, evsel, machine); |
| 373 | |
| 374 | ent = malloc(event->header.size + sizeof(struct event_entry)); |
| 375 | if (ent == NULL) { |
| 376 | color_fprintf(stderr, PERF_COLOR_RED, |
| 377 | "Not enough memory to process sched switch event!"); |
| 378 | return -1; |
| 379 | } |
| 380 | |
| 381 | ent->tid = sample->tid; |
| 382 | memcpy(&ent->event, event, event->header.size); |
| 383 | list_add(&ent->node, &inject->samples); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int perf_inject__sched_stat(struct perf_tool *tool, |
| 388 | union perf_event *event __maybe_unused, |
| 389 | struct perf_sample *sample, |
| 390 | struct perf_evsel *evsel, |
| 391 | struct machine *machine) |
| 392 | { |
| 393 | struct event_entry *ent; |
| 394 | union perf_event *event_sw; |
| 395 | struct perf_sample sample_sw; |
| 396 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
| 397 | u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
| 398 | |
| 399 | list_for_each_entry(ent, &inject->samples, node) { |
| 400 | if (pid == ent->tid) |
| 401 | goto found; |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | found: |
| 406 | event_sw = &ent->event[0]; |
| 407 | perf_evsel__parse_sample(evsel, event_sw, &sample_sw); |
| 408 | |
| 409 | sample_sw.period = sample->period; |
| 410 | sample_sw.time = sample->time; |
| 411 | perf_event__synthesize_sample(event_sw, evsel->attr.sample_type, |
Adrian Hunter | d03f217 | 2013-08-27 11:23:11 +0300 | [diff] [blame] | 412 | evsel->attr.read_format, &sample_sw, |
| 413 | false); |
Andrew Vagin | 54a3cf5 | 2012-08-07 16:56:05 +0400 | [diff] [blame] | 414 | build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine); |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 415 | return perf_event__repipe(tool, event_sw, &sample_sw, machine); |
| 416 | } |
| 417 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 418 | static void sig_handler(int sig __maybe_unused) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 419 | { |
| 420 | session_done = 1; |
| 421 | } |
| 422 | |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 423 | static int perf_evsel__check_stype(struct perf_evsel *evsel, |
| 424 | u64 sample_type, const char *sample_msg) |
| 425 | { |
| 426 | struct perf_event_attr *attr = &evsel->attr; |
| 427 | const char *name = perf_evsel__name(evsel); |
| 428 | |
| 429 | if (!(attr->sample_type & sample_type)) { |
| 430 | pr_err("Samples for %s event do not have %s attribute set.", |
| 431 | name, sample_msg); |
| 432 | return -EINVAL; |
| 433 | } |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 438 | static int __cmd_inject(struct perf_inject *inject) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 439 | { |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 440 | int ret = -EINVAL; |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 441 | struct perf_session *session = inject->session; |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 442 | struct perf_data_file *file_out = &inject->output; |
Namhyung Kim | 42aa276 | 2015-01-29 17:06:48 +0900 | [diff] [blame] | 443 | int fd = perf_data_file__fd(file_out); |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 444 | u64 output_data_offset; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 445 | |
| 446 | signal(SIGINT, sig_handler); |
| 447 | |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 448 | if (inject->build_ids || inject->sched_stat || |
| 449 | inject->itrace_synth_opts.set) { |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 450 | inject->tool.mmap = perf_event__repipe_mmap; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 451 | inject->tool.mmap2 = perf_event__repipe_mmap2; |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 452 | inject->tool.fork = perf_event__repipe_fork; |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 453 | inject->tool.tracing_data = perf_event__repipe_tracing_data; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 454 | } |
| 455 | |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 456 | output_data_offset = session->header.data_offset; |
| 457 | |
Andrew Vagin | 54a3cf5 | 2012-08-07 16:56:05 +0400 | [diff] [blame] | 458 | if (inject->build_ids) { |
| 459 | inject->tool.sample = perf_event__inject_buildid; |
| 460 | } else if (inject->sched_stat) { |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 461 | struct perf_evsel *evsel; |
| 462 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 463 | evlist__for_each(session->evlist, evsel) { |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 464 | const char *name = perf_evsel__name(evsel); |
| 465 | |
| 466 | if (!strcmp(name, "sched:sched_switch")) { |
| 467 | if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID")) |
| 468 | return -EINVAL; |
| 469 | |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 470 | evsel->handler = perf_inject__sched_switch; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 471 | } else if (!strcmp(name, "sched:sched_process_exit")) |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 472 | evsel->handler = perf_inject__sched_process_exit; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 473 | else if (!strncmp(name, "sched:sched_stat_", 17)) |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 474 | evsel->handler = perf_inject__sched_stat; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 475 | } |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 476 | } else if (inject->itrace_synth_opts.set) { |
| 477 | session->itrace_synth_opts = &inject->itrace_synth_opts; |
| 478 | inject->itrace_synth_opts.inject = true; |
| 479 | inject->tool.comm = perf_event__repipe_comm; |
| 480 | inject->tool.exit = perf_event__repipe_exit; |
| 481 | inject->tool.id_index = perf_event__repipe_id_index; |
| 482 | inject->tool.auxtrace_info = perf_event__process_auxtrace_info; |
| 483 | inject->tool.auxtrace = perf_event__process_auxtrace; |
| 484 | inject->tool.ordered_events = true; |
| 485 | inject->tool.ordering_requires_timestamps = true; |
| 486 | /* Allow space in the header for new attributes */ |
| 487 | output_data_offset = 4096; |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 488 | } |
| 489 | |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 490 | if (!file_out->is_pipe) |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 491 | lseek(fd, output_data_offset, SEEK_SET); |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 492 | |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 493 | ret = perf_session__process_events(session); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 494 | |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 495 | if (!file_out->is_pipe) { |
Adrian Hunter | e38b43c | 2014-07-14 13:02:34 +0300 | [diff] [blame] | 496 | if (inject->build_ids) |
| 497 | perf_header__set_feat(&session->header, |
| 498 | HEADER_BUILD_ID); |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 499 | /* |
| 500 | * The AUX areas have been removed and replaced with |
| 501 | * synthesized hardware events, so clear the feature flag. |
| 502 | */ |
| 503 | if (inject->itrace_synth_opts.set) |
| 504 | perf_header__clear_feat(&session->header, |
| 505 | HEADER_AUXTRACE); |
| 506 | session->header.data_offset = output_data_offset; |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 507 | session->header.data_size = inject->bytes_written; |
Namhyung Kim | 42aa276 | 2015-01-29 17:06:48 +0900 | [diff] [blame] | 508 | perf_session__write_header(session, session->evlist, fd, true); |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 509 | } |
| 510 | |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 511 | return ret; |
| 512 | } |
| 513 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 514 | int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 515 | { |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 516 | struct perf_inject inject = { |
| 517 | .tool = { |
| 518 | .sample = perf_event__repipe_sample, |
| 519 | .mmap = perf_event__repipe, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 520 | .mmap2 = perf_event__repipe, |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 521 | .comm = perf_event__repipe, |
| 522 | .fork = perf_event__repipe, |
| 523 | .exit = perf_event__repipe, |
| 524 | .lost = perf_event__repipe, |
| 525 | .read = perf_event__repipe_sample, |
| 526 | .throttle = perf_event__repipe, |
| 527 | .unthrottle = perf_event__repipe, |
| 528 | .attr = perf_event__repipe_attr, |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 529 | .tracing_data = perf_event__repipe_op2_synth, |
Adrian Hunter | cd17a9b | 2015-04-21 12:21:54 +0300 | [diff] [blame] | 530 | .auxtrace_info = perf_event__repipe_op2_synth, |
| 531 | .auxtrace = perf_event__repipe_auxtrace, |
| 532 | .auxtrace_error = perf_event__repipe_op2_synth, |
Arnaldo Carvalho de Melo | d704ebd | 2015-03-03 12:37:54 -0300 | [diff] [blame] | 533 | .finished_round = perf_event__repipe_oe_synth, |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 534 | .build_id = perf_event__repipe_op2_synth, |
Adrian Hunter | 3c659ee | 2014-10-27 15:49:22 +0200 | [diff] [blame] | 535 | .id_index = perf_event__repipe_op2_synth, |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 536 | }, |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 537 | .input_name = "-", |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 538 | .samples = LIST_HEAD_INIT(inject.samples), |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 539 | .output = { |
| 540 | .path = "-", |
| 541 | .mode = PERF_DATA_MODE_WRITE, |
| 542 | }, |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 543 | }; |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 544 | struct perf_data_file file = { |
| 545 | .mode = PERF_DATA_MODE_READ, |
| 546 | }; |
| 547 | int ret; |
| 548 | |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 549 | const struct option options[] = { |
| 550 | OPT_BOOLEAN('b', "build-ids", &inject.build_ids, |
| 551 | "Inject build-ids into the output stream"), |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 552 | OPT_STRING('i', "input", &inject.input_name, "file", |
| 553 | "input file name"), |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 554 | OPT_STRING('o', "output", &inject.output.path, "file", |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 555 | "output file name"), |
Andrew Vagin | 26a031e | 2012-08-07 16:56:04 +0400 | [diff] [blame] | 556 | OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat, |
| 557 | "Merge sched-stat and sched-switch for getting events " |
| 558 | "where and how long tasks slept"), |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 559 | OPT_INCR('v', "verbose", &verbose, |
| 560 | "be more verbose (show build ids, etc)"), |
Adrian Hunter | a7a2b8b | 2014-07-22 16:17:38 +0300 | [diff] [blame] | 561 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file", |
| 562 | "kallsyms pathname"), |
Yunlong Song | ccaa474 | 2015-04-02 21:47:11 +0800 | [diff] [blame] | 563 | OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"), |
Adrian Hunter | 0f0aa5e | 2015-04-09 18:54:00 +0300 | [diff] [blame^] | 564 | OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts, |
| 565 | NULL, "opts", "Instruction Tracing options", |
| 566 | itrace_parse_synth_opts), |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 567 | OPT_END() |
| 568 | }; |
Arnaldo Carvalho de Melo | 002439e | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 569 | const char * const inject_usage[] = { |
| 570 | "perf inject [<options>]", |
| 571 | NULL |
| 572 | }; |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 573 | |
Arnaldo Carvalho de Melo | 002439e | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 574 | argc = parse_options(argc, argv, options, inject_usage, 0); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * Any (unrecognized) arguments left? |
| 578 | */ |
| 579 | if (argc) |
Arnaldo Carvalho de Melo | 002439e | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 580 | usage_with_options(inject_usage, options); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 581 | |
Jiri Olsa | 3406912 | 2013-10-29 19:04:57 +0100 | [diff] [blame] | 582 | if (perf_data_file__open(&inject.output)) { |
| 583 | perror("failed to create output file"); |
| 584 | return -1; |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame] | 585 | } |
| 586 | |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 587 | inject.tool.ordered_events = inject.sched_stat; |
| 588 | |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 589 | file.path = inject.input_name; |
| 590 | inject.session = perf_session__new(&file, true, &inject.tool); |
| 591 | if (inject.session == NULL) |
Taeung Song | 52e02834 | 2014-09-24 10:33:37 +0900 | [diff] [blame] | 592 | return -1; |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 593 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 594 | if (symbol__init(&inject.session->header.env) < 0) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 595 | return -1; |
| 596 | |
Namhyung Kim | 1cb8bdc | 2014-08-12 15:40:37 +0900 | [diff] [blame] | 597 | ret = __cmd_inject(&inject); |
| 598 | |
| 599 | perf_session__delete(inject.session); |
| 600 | |
| 601 | return ret; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 602 | } |