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