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" |
| 11 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 12 | #include "util/tool.h" |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 13 | #include "util/debug.h" |
| 14 | |
| 15 | #include "util/parse-options.h" |
| 16 | |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 17 | struct perf_inject { |
| 18 | struct perf_tool tool; |
| 19 | bool build_ids; |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 20 | const char *input_name; |
| 21 | int pipe_output, |
| 22 | output; |
| 23 | u64 bytes_written; |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 24 | }; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 25 | |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 26 | static int perf_event__repipe_synth(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 27 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 28 | struct machine *machine __maybe_unused) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 29 | { |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 30 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 31 | uint32_t size; |
| 32 | void *buf = event; |
| 33 | |
| 34 | size = event->header.size; |
| 35 | |
| 36 | while (size) { |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 37 | int ret = write(inject->output, buf, size); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 38 | if (ret < 0) |
| 39 | return -errno; |
| 40 | |
| 41 | size -= ret; |
| 42 | buf += ret; |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 43 | inject->bytes_written += ret; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 49 | 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] | 50 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 51 | struct perf_session *session |
| 52 | __maybe_unused) |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 53 | { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 54 | return perf_event__repipe_synth(tool, event, NULL); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 55 | } |
| 56 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 57 | static int perf_event__repipe_event_type_synth(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 58 | union perf_event *event) |
| 59 | { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 60 | return perf_event__repipe_synth(tool, event, NULL); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static int perf_event__repipe_tracing_data_synth(union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 64 | struct perf_session *session |
| 65 | __maybe_unused) |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 66 | { |
| 67 | return perf_event__repipe_synth(NULL, event, NULL); |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 68 | } |
| 69 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 70 | static int perf_event__repipe_attr(union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 71 | struct perf_evlist **pevlist __maybe_unused) |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 72 | { |
Stephane Eranian | 1a1ed1b | 2012-05-15 13:28:11 +0200 | [diff] [blame] | 73 | int ret; |
| 74 | ret = perf_event__process_attr(event, pevlist); |
| 75 | if (ret) |
| 76 | return ret; |
| 77 | |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 78 | return perf_event__repipe_synth(NULL, event, NULL); |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 79 | } |
| 80 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 81 | static int perf_event__repipe(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 82 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 83 | struct perf_sample *sample __maybe_unused, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 84 | struct machine *machine) |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 85 | { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 86 | return perf_event__repipe_synth(tool, event, machine); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 87 | } |
| 88 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 89 | static int perf_event__repipe_sample(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 90 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 91 | struct perf_sample *sample __maybe_unused, |
| 92 | struct perf_evsel *evsel __maybe_unused, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 93 | struct machine *machine) |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 94 | { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 95 | return perf_event__repipe_synth(tool, event, machine); |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 96 | } |
| 97 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 98 | static int perf_event__repipe_mmap(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 99 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 100 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 101 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 102 | { |
| 103 | int err; |
| 104 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 105 | err = perf_event__process_mmap(tool, event, sample, machine); |
| 106 | perf_event__repipe(tool, event, sample, machine); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 107 | |
| 108 | return err; |
| 109 | } |
| 110 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 111 | static int perf_event__repipe_fork(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 112 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 113 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 114 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 115 | { |
| 116 | int err; |
| 117 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 118 | err = perf_event__process_fork(tool, event, sample, machine); |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 119 | perf_event__repipe(tool, event, sample, machine); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 120 | |
| 121 | return err; |
| 122 | } |
| 123 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 124 | static int perf_event__repipe_tracing_data(union perf_event *event, |
| 125 | struct perf_session *session) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 126 | { |
| 127 | int err; |
| 128 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 129 | perf_event__repipe_synth(NULL, event, NULL); |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 130 | err = perf_event__process_tracing_data(event, session); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 131 | |
| 132 | return err; |
| 133 | } |
| 134 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 135 | static int dso__read_build_id(struct dso *self) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 136 | { |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 137 | if (self->has_build_id) |
| 138 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 139 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 140 | if (filename__read_build_id(self->long_name, self->build_id, |
| 141 | sizeof(self->build_id)) > 0) { |
| 142 | self->has_build_id = true; |
| 143 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 146 | return -1; |
| 147 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 148 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 149 | static int dso__inject_build_id(struct dso *self, struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 150 | struct machine *machine) |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 151 | { |
| 152 | u16 misc = PERF_RECORD_MISC_USER; |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 153 | int err; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 154 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 155 | if (dso__read_build_id(self) < 0) { |
| 156 | pr_debug("no build_id found for %s\n", self->long_name); |
| 157 | return -1; |
| 158 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 159 | |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 160 | if (self->kernel) |
| 161 | misc = PERF_RECORD_MISC_KERNEL; |
| 162 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 163 | err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 164 | machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 165 | if (err) { |
| 166 | pr_err("Can't synthesize build_id event for %s\n", self->long_name); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 173 | static int perf_event__inject_buildid(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 174 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 175 | struct perf_sample *sample, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 176 | struct perf_evsel *evsel __maybe_unused, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 177 | struct machine *machine) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 178 | { |
| 179 | struct addr_location al; |
| 180 | struct thread *thread; |
| 181 | u8 cpumode; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 182 | |
| 183 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 184 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 185 | thread = machine__findnew_thread(machine, event->ip.pid); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 186 | if (thread == NULL) { |
| 187 | pr_err("problem processing %d event, skipping it.\n", |
| 188 | event->header.type); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 189 | goto repipe; |
| 190 | } |
| 191 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 192 | thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION, |
| 193 | event->ip.ip, &al); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 194 | |
| 195 | if (al.map != NULL) { |
| 196 | if (!al.map->dso->hit) { |
| 197 | al.map->dso->hit = 1; |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 198 | if (map__load(al.map, NULL) >= 0) { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 199 | dso__inject_build_id(al.map->dso, tool, machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 200 | /* |
| 201 | * If this fails, too bad, let the other side |
| 202 | * account this as unresolved. |
| 203 | */ |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 204 | } else { |
Namhyung Kim | 29a0fc9 | 2012-09-28 18:31:59 +0900 | [diff] [blame] | 205 | #ifdef LIBELF_SUPPORT |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 206 | pr_warning("no symbols found in %s, maybe " |
| 207 | "install a debug package?\n", |
| 208 | al.map->dso->long_name); |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 209 | #endif |
| 210 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | repipe: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 215 | perf_event__repipe(tool, event, sample, machine); |
Arnaldo Carvalho de Melo | 090f720 | 2010-05-02 19:46:36 -0300 | [diff] [blame] | 216 | return 0; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 219 | extern volatile int session_done; |
| 220 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 221 | static void sig_handler(int sig __maybe_unused) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 222 | { |
| 223 | session_done = 1; |
| 224 | } |
| 225 | |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 226 | static int __cmd_inject(struct perf_inject *inject) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 227 | { |
| 228 | struct perf_session *session; |
| 229 | int ret = -EINVAL; |
| 230 | |
| 231 | signal(SIGINT, sig_handler); |
| 232 | |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 233 | if (inject->build_ids) { |
| 234 | inject->tool.sample = perf_event__inject_buildid; |
| 235 | inject->tool.mmap = perf_event__repipe_mmap; |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 236 | inject->tool.fork = perf_event__repipe_fork; |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 237 | inject->tool.tracing_data = perf_event__repipe_tracing_data; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 240 | session = perf_session__new(inject->input_name, O_RDONLY, false, true, &inject->tool); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 241 | if (session == NULL) |
| 242 | return -ENOMEM; |
| 243 | |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 244 | if (!inject->pipe_output) |
| 245 | lseek(inject->output, session->header.data_offset, SEEK_SET); |
| 246 | |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 247 | ret = perf_session__process_events(session, &inject->tool); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 248 | |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 249 | if (!inject->pipe_output) { |
| 250 | session->header.data_size = inject->bytes_written; |
| 251 | perf_session__write_header(session, session->evlist, inject->output, true); |
| 252 | } |
| 253 | |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 254 | perf_session__delete(session); |
| 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 259 | 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] | 260 | { |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 261 | struct perf_inject inject = { |
| 262 | .tool = { |
| 263 | .sample = perf_event__repipe_sample, |
| 264 | .mmap = perf_event__repipe, |
| 265 | .comm = perf_event__repipe, |
| 266 | .fork = perf_event__repipe, |
| 267 | .exit = perf_event__repipe, |
| 268 | .lost = perf_event__repipe, |
| 269 | .read = perf_event__repipe_sample, |
| 270 | .throttle = perf_event__repipe, |
| 271 | .unthrottle = perf_event__repipe, |
| 272 | .attr = perf_event__repipe_attr, |
| 273 | .event_type = perf_event__repipe_event_type_synth, |
| 274 | .tracing_data = perf_event__repipe_tracing_data_synth, |
| 275 | .build_id = perf_event__repipe_op2_synth, |
| 276 | }, |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 277 | .input_name = "-", |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 278 | }; |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 279 | const char *output_name = "-"; |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 280 | const struct option options[] = { |
| 281 | OPT_BOOLEAN('b', "build-ids", &inject.build_ids, |
| 282 | "Inject build-ids into the output stream"), |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 283 | OPT_STRING('i', "input", &inject.input_name, "file", |
| 284 | "input file name"), |
| 285 | OPT_STRING('o', "output", &output_name, "file", |
| 286 | "output file name"), |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 287 | OPT_INCR('v', "verbose", &verbose, |
| 288 | "be more verbose (show build ids, etc)"), |
| 289 | OPT_END() |
| 290 | }; |
Arnaldo Carvalho de Melo | 002439e | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 291 | const char * const inject_usage[] = { |
| 292 | "perf inject [<options>]", |
| 293 | NULL |
| 294 | }; |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 295 | |
Arnaldo Carvalho de Melo | 002439e | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 296 | argc = parse_options(argc, argv, options, inject_usage, 0); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * Any (unrecognized) arguments left? |
| 300 | */ |
| 301 | if (argc) |
Arnaldo Carvalho de Melo | 002439e | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 302 | usage_with_options(inject_usage, options); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 303 | |
Andrew Vagin | e558a5b | 2012-08-07 16:56:02 +0400 | [diff] [blame^] | 304 | if (!strcmp(output_name, "-")) { |
| 305 | inject.pipe_output = 1; |
| 306 | inject.output = STDOUT_FILENO; |
| 307 | } else { |
| 308 | inject.output = open(output_name, O_CREAT | O_WRONLY | O_TRUNC, |
| 309 | S_IRUSR | S_IWUSR); |
| 310 | if (inject.output < 0) { |
| 311 | perror("failed to create output file"); |
| 312 | return -1; |
| 313 | } |
| 314 | } |
| 315 | |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 316 | if (symbol__init() < 0) |
| 317 | return -1; |
| 318 | |
Arnaldo Carvalho de Melo | 5ded57a | 2012-09-30 19:54:10 -0300 | [diff] [blame] | 319 | return __cmd_inject(&inject); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 320 | } |