Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | |
Andrea Gelmini | b7eead8 | 2010-08-05 15:51:38 +0200 | [diff] [blame] | 3 | #include "perf.h" |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 4 | #include "util/cache.h" |
Andrea Gelmini | b7eead8 | 2010-08-05 15:51:38 +0200 | [diff] [blame] | 5 | #include "util/debug.h" |
| 6 | #include "util/exec_cmd.h" |
| 7 | #include "util/header.h" |
| 8 | #include "util/parse-options.h" |
| 9 | #include "util/session.h" |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 10 | #include "util/symbol.h" |
| 11 | #include "util/thread.h" |
Ingo Molnar | cf72344 | 2009-11-28 10:11:00 +0100 | [diff] [blame] | 12 | #include "util/trace-event.h" |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 13 | #include "util/parse-options.h" |
Andrea Gelmini | b7eead8 | 2010-08-05 15:51:38 +0200 | [diff] [blame] | 14 | #include "util/util.h" |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 15 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 16 | static char const *script_name; |
| 17 | static char const *generate_script_lang; |
Frederic Weisbecker | ffabd99 | 2010-05-27 16:27:47 +0200 | [diff] [blame] | 18 | static bool debug_mode; |
Frederic Weisbecker | e1889d7 | 2010-04-24 01:55:09 +0200 | [diff] [blame] | 19 | static u64 last_timestamp; |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 20 | static u64 nr_unordered; |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 21 | extern const struct option record_options[]; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 22 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 23 | enum perf_output_field { |
| 24 | PERF_OUTPUT_COMM = 1U << 0, |
| 25 | PERF_OUTPUT_TID = 1U << 1, |
| 26 | PERF_OUTPUT_PID = 1U << 2, |
| 27 | PERF_OUTPUT_TIME = 1U << 3, |
| 28 | PERF_OUTPUT_CPU = 1U << 4, |
| 29 | PERF_OUTPUT_EVNAME = 1U << 5, |
| 30 | PERF_OUTPUT_TRACE = 1U << 6, |
| 31 | }; |
| 32 | |
| 33 | struct output_option { |
| 34 | const char *str; |
| 35 | enum perf_output_field field; |
| 36 | } all_output_options[] = { |
| 37 | {.str = "comm", .field = PERF_OUTPUT_COMM}, |
| 38 | {.str = "tid", .field = PERF_OUTPUT_TID}, |
| 39 | {.str = "pid", .field = PERF_OUTPUT_PID}, |
| 40 | {.str = "time", .field = PERF_OUTPUT_TIME}, |
| 41 | {.str = "cpu", .field = PERF_OUTPUT_CPU}, |
| 42 | {.str = "event", .field = PERF_OUTPUT_EVNAME}, |
| 43 | {.str = "trace", .field = PERF_OUTPUT_TRACE}, |
| 44 | }; |
| 45 | |
| 46 | /* default set to maintain compatibility with current format */ |
| 47 | static u64 output_fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | \ |
| 48 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | \ |
| 49 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE; |
| 50 | |
| 51 | static bool output_set_by_user; |
| 52 | |
| 53 | #define PRINT_FIELD(x) (output_fields & PERF_OUTPUT_##x) |
| 54 | |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 55 | static void print_sample_start(struct perf_sample *sample, |
| 56 | struct thread *thread) |
| 57 | { |
| 58 | int type; |
| 59 | struct event *event; |
| 60 | const char *evname = NULL; |
| 61 | unsigned long secs; |
| 62 | unsigned long usecs; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 63 | unsigned long long nsecs; |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 64 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 65 | if (PRINT_FIELD(COMM)) { |
| 66 | if (latency_format) |
| 67 | printf("%8.8s ", thread->comm); |
| 68 | else |
| 69 | printf("%16s ", thread->comm); |
| 70 | } |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 71 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 72 | if (PRINT_FIELD(PID) && PRINT_FIELD(TID)) |
| 73 | printf("%5d/%-5d ", sample->pid, sample->tid); |
| 74 | else if (PRINT_FIELD(PID)) |
| 75 | printf("%5d ", sample->pid); |
| 76 | else if (PRINT_FIELD(TID)) |
| 77 | printf("%5d ", sample->tid); |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 78 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 79 | if (PRINT_FIELD(CPU)) { |
| 80 | if (latency_format) |
| 81 | printf("%3d ", sample->cpu); |
| 82 | else |
| 83 | printf("[%03d] ", sample->cpu); |
| 84 | } |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 85 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 86 | if (PRINT_FIELD(TIME)) { |
| 87 | nsecs = sample->time; |
| 88 | secs = nsecs / NSECS_PER_SEC; |
| 89 | nsecs -= secs * NSECS_PER_SEC; |
| 90 | usecs = nsecs / NSECS_PER_USEC; |
| 91 | printf("%5lu.%06lu: ", secs, usecs); |
| 92 | } |
| 93 | |
| 94 | if (PRINT_FIELD(EVNAME)) { |
| 95 | type = trace_parse_common_type(sample->raw_data); |
| 96 | event = trace_find_event(type); |
| 97 | if (event) |
| 98 | evname = event->name; |
| 99 | |
| 100 | printf("%s: ", evname ? evname : "(unknown)"); |
| 101 | } |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 102 | } |
| 103 | |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 104 | static void process_event(union perf_event *event __unused, |
| 105 | struct perf_sample *sample, |
| 106 | struct perf_session *session __unused, |
| 107 | struct thread *thread) |
| 108 | { |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 109 | print_sample_start(sample, thread); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 110 | |
| 111 | if (PRINT_FIELD(TRACE)) |
| 112 | print_trace_event(sample->cpu, sample->raw_data, |
| 113 | sample->raw_size); |
| 114 | |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 115 | printf("\n"); |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Tom Zanussi | 586bc5c | 2009-12-15 02:53:35 -0600 | [diff] [blame] | 118 | static int default_start_script(const char *script __unused, |
| 119 | int argc __unused, |
| 120 | const char **argv __unused) |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 121 | { |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int default_stop_script(void) |
| 126 | { |
| 127 | return 0; |
| 128 | } |
| 129 | |
Tom Zanussi | 586bc5c | 2009-12-15 02:53:35 -0600 | [diff] [blame] | 130 | static int default_generate_script(const char *outfile __unused) |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 131 | { |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static struct scripting_ops default_scripting_ops = { |
| 136 | .start_script = default_start_script, |
| 137 | .stop_script = default_stop_script, |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 138 | .process_event = process_event, |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 139 | .generate_script = default_generate_script, |
| 140 | }; |
| 141 | |
| 142 | static struct scripting_ops *scripting_ops; |
| 143 | |
| 144 | static void setup_scripting(void) |
| 145 | { |
Tom Zanussi | 16c632d | 2009-11-25 01:15:48 -0600 | [diff] [blame] | 146 | setup_perl_scripting(); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 147 | setup_python_scripting(); |
Tom Zanussi | 16c632d | 2009-11-25 01:15:48 -0600 | [diff] [blame] | 148 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 149 | scripting_ops = &default_scripting_ops; |
| 150 | } |
| 151 | |
| 152 | static int cleanup_scripting(void) |
| 153 | { |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 154 | pr_debug("\nperf script stopped\n"); |
Tom Zanussi | 3824a4e | 2010-05-09 23:46:57 -0500 | [diff] [blame] | 155 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 156 | return scripting_ops->stop_script(); |
| 157 | } |
| 158 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 159 | static char const *input_name = "perf.data"; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 160 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 161 | static int process_sample_event(union perf_event *event, |
| 162 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 163 | struct perf_session *session) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 164 | { |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 165 | struct thread *thread = perf_session__findnew(session, event->ip.pid); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 166 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 167 | if (thread == NULL) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 168 | pr_debug("problem processing %d event, skipping it.\n", |
| 169 | event->header.type); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 170 | return -1; |
| 171 | } |
| 172 | |
Arnaldo Carvalho de Melo | c019879 | 2009-12-14 14:23:00 -0200 | [diff] [blame] | 173 | if (session->sample_type & PERF_SAMPLE_RAW) { |
Frederic Weisbecker | ffabd99 | 2010-05-27 16:27:47 +0200 | [diff] [blame] | 174 | if (debug_mode) { |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 175 | if (sample->time < last_timestamp) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 176 | pr_err("Samples misordered, previous: %" PRIu64 |
| 177 | " this: %" PRIu64 "\n", last_timestamp, |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 178 | sample->time); |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 179 | nr_unordered++; |
Frederic Weisbecker | e1889d7 | 2010-04-24 01:55:09 +0200 | [diff] [blame] | 180 | } |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 181 | last_timestamp = sample->time; |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 182 | return 0; |
Frederic Weisbecker | e1889d7 | 2010-04-24 01:55:09 +0200 | [diff] [blame] | 183 | } |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 184 | scripting_ops->process_event(event, sample, session, thread); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 185 | } |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 186 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 187 | session->hists.stats.total_period += sample->period; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 191 | static struct perf_event_ops event_ops = { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 192 | .sample = process_sample_event, |
| 193 | .comm = perf_event__process_comm, |
| 194 | .attr = perf_event__process_attr, |
| 195 | .event_type = perf_event__process_event_type, |
| 196 | .tracing_data = perf_event__process_tracing_data, |
| 197 | .build_id = perf_event__process_build_id, |
Frederic Weisbecker | e0a808c | 2010-04-24 00:38:33 +0200 | [diff] [blame] | 198 | .ordered_samples = true, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 199 | .ordering_requires_timestamps = true, |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame] | 200 | }; |
| 201 | |
Tom Zanussi | c239da3 | 2010-04-01 23:59:18 -0500 | [diff] [blame] | 202 | extern volatile int session_done; |
| 203 | |
| 204 | static void sig_handler(int sig __unused) |
| 205 | { |
| 206 | session_done = 1; |
| 207 | } |
| 208 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 209 | static int __cmd_script(struct perf_session *session) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 210 | { |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 211 | int ret; |
| 212 | |
Tom Zanussi | c239da3 | 2010-04-01 23:59:18 -0500 | [diff] [blame] | 213 | signal(SIGINT, sig_handler); |
| 214 | |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 215 | ret = perf_session__process_events(session, &event_ops); |
| 216 | |
Arnaldo Carvalho de Melo | 6d8afb5 | 2011-01-04 16:27:30 -0200 | [diff] [blame] | 217 | if (debug_mode) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 218 | pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 219 | |
| 220 | return ret; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 223 | struct script_spec { |
| 224 | struct list_head node; |
| 225 | struct scripting_ops *ops; |
| 226 | char spec[0]; |
| 227 | }; |
| 228 | |
Arnaldo Carvalho de Melo | eccdfe2 | 2011-01-04 16:32:52 -0200 | [diff] [blame] | 229 | static LIST_HEAD(script_specs); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 230 | |
| 231 | static struct script_spec *script_spec__new(const char *spec, |
| 232 | struct scripting_ops *ops) |
| 233 | { |
| 234 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); |
| 235 | |
| 236 | if (s != NULL) { |
| 237 | strcpy(s->spec, spec); |
| 238 | s->ops = ops; |
| 239 | } |
| 240 | |
| 241 | return s; |
| 242 | } |
| 243 | |
| 244 | static void script_spec__delete(struct script_spec *s) |
| 245 | { |
| 246 | free(s->spec); |
| 247 | free(s); |
| 248 | } |
| 249 | |
| 250 | static void script_spec__add(struct script_spec *s) |
| 251 | { |
| 252 | list_add_tail(&s->node, &script_specs); |
| 253 | } |
| 254 | |
| 255 | static struct script_spec *script_spec__find(const char *spec) |
| 256 | { |
| 257 | struct script_spec *s; |
| 258 | |
| 259 | list_for_each_entry(s, &script_specs, node) |
| 260 | if (strcasecmp(s->spec, spec) == 0) |
| 261 | return s; |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | static struct script_spec *script_spec__findnew(const char *spec, |
| 266 | struct scripting_ops *ops) |
| 267 | { |
| 268 | struct script_spec *s = script_spec__find(spec); |
| 269 | |
| 270 | if (s) |
| 271 | return s; |
| 272 | |
| 273 | s = script_spec__new(spec, ops); |
| 274 | if (!s) |
| 275 | goto out_delete_spec; |
| 276 | |
| 277 | script_spec__add(s); |
| 278 | |
| 279 | return s; |
| 280 | |
| 281 | out_delete_spec: |
| 282 | script_spec__delete(s); |
| 283 | |
| 284 | return NULL; |
| 285 | } |
| 286 | |
| 287 | int script_spec_register(const char *spec, struct scripting_ops *ops) |
| 288 | { |
| 289 | struct script_spec *s; |
| 290 | |
| 291 | s = script_spec__find(spec); |
| 292 | if (s) |
| 293 | return -1; |
| 294 | |
| 295 | s = script_spec__findnew(spec, ops); |
| 296 | if (!s) |
| 297 | return -1; |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static struct scripting_ops *script_spec__lookup(const char *spec) |
| 303 | { |
| 304 | struct script_spec *s = script_spec__find(spec); |
| 305 | if (!s) |
| 306 | return NULL; |
| 307 | |
| 308 | return s->ops; |
| 309 | } |
| 310 | |
| 311 | static void list_available_languages(void) |
| 312 | { |
| 313 | struct script_spec *s; |
| 314 | |
| 315 | fprintf(stderr, "\n"); |
| 316 | fprintf(stderr, "Scripting language extensions (used in " |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 317 | "perf script -s [spec:]script.[spec]):\n\n"); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 318 | |
| 319 | list_for_each_entry(s, &script_specs, node) |
| 320 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); |
| 321 | |
| 322 | fprintf(stderr, "\n"); |
| 323 | } |
| 324 | |
| 325 | static int parse_scriptname(const struct option *opt __used, |
| 326 | const char *str, int unset __used) |
| 327 | { |
| 328 | char spec[PATH_MAX]; |
| 329 | const char *script, *ext; |
| 330 | int len; |
| 331 | |
Tom Zanussi | f526d68 | 2010-01-27 02:27:52 -0600 | [diff] [blame] | 332 | if (strcmp(str, "lang") == 0) { |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 333 | list_available_languages(); |
Tom Zanussi | f526d68 | 2010-01-27 02:27:52 -0600 | [diff] [blame] | 334 | exit(0); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | script = strchr(str, ':'); |
| 338 | if (script) { |
| 339 | len = script - str; |
| 340 | if (len >= PATH_MAX) { |
| 341 | fprintf(stderr, "invalid language specifier"); |
| 342 | return -1; |
| 343 | } |
| 344 | strncpy(spec, str, len); |
| 345 | spec[len] = '\0'; |
| 346 | scripting_ops = script_spec__lookup(spec); |
| 347 | if (!scripting_ops) { |
| 348 | fprintf(stderr, "invalid language specifier"); |
| 349 | return -1; |
| 350 | } |
| 351 | script++; |
| 352 | } else { |
| 353 | script = str; |
Ben Hutchings | d1e95bb | 2010-10-10 16:11:02 +0100 | [diff] [blame] | 354 | ext = strrchr(script, '.'); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 355 | if (!ext) { |
| 356 | fprintf(stderr, "invalid script extension"); |
| 357 | return -1; |
| 358 | } |
| 359 | scripting_ops = script_spec__lookup(++ext); |
| 360 | if (!scripting_ops) { |
| 361 | fprintf(stderr, "invalid script extension"); |
| 362 | return -1; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | script_name = strdup(script); |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 371 | static int parse_output_fields(const struct option *opt __used, |
| 372 | const char *arg, int unset __used) |
| 373 | { |
| 374 | char *tok; |
| 375 | int i, imax = sizeof(all_output_options) / sizeof(struct output_option); |
| 376 | int rc = 0; |
| 377 | char *str = strdup(arg); |
| 378 | |
| 379 | if (!str) |
| 380 | return -ENOMEM; |
| 381 | |
| 382 | tok = strtok(str, ","); |
| 383 | if (!tok) { |
| 384 | fprintf(stderr, "Invalid field string."); |
| 385 | return -EINVAL; |
| 386 | } |
| 387 | |
| 388 | output_fields = 0; |
| 389 | while (1) { |
| 390 | for (i = 0; i < imax; ++i) { |
| 391 | if (strcmp(tok, all_output_options[i].str) == 0) { |
| 392 | output_fields |= all_output_options[i].field; |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | if (i == imax) { |
| 397 | fprintf(stderr, "Invalid field requested."); |
| 398 | rc = -EINVAL; |
| 399 | break; |
| 400 | } |
| 401 | |
| 402 | tok = strtok(NULL, ","); |
| 403 | if (!tok) |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | output_set_by_user = true; |
| 408 | |
| 409 | free(str); |
| 410 | return rc; |
| 411 | } |
| 412 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 413 | /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */ |
| 414 | static int is_directory(const char *base_path, const struct dirent *dent) |
| 415 | { |
| 416 | char path[PATH_MAX]; |
| 417 | struct stat st; |
| 418 | |
| 419 | sprintf(path, "%s/%s", base_path, dent->d_name); |
| 420 | if (stat(path, &st)) |
| 421 | return 0; |
| 422 | |
| 423 | return S_ISDIR(st.st_mode); |
| 424 | } |
| 425 | |
| 426 | #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\ |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 427 | while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \ |
| 428 | lang_next) \ |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 429 | if ((lang_dirent.d_type == DT_DIR || \ |
| 430 | (lang_dirent.d_type == DT_UNKNOWN && \ |
| 431 | is_directory(scripts_path, &lang_dirent))) && \ |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 432 | (strcmp(lang_dirent.d_name, ".")) && \ |
| 433 | (strcmp(lang_dirent.d_name, ".."))) |
| 434 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 435 | #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\ |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 436 | while (!readdir_r(lang_dir, &script_dirent, &script_next) && \ |
| 437 | script_next) \ |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 438 | if (script_dirent.d_type != DT_DIR && \ |
| 439 | (script_dirent.d_type != DT_UNKNOWN || \ |
| 440 | !is_directory(lang_path, &script_dirent))) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 441 | |
| 442 | |
| 443 | #define RECORD_SUFFIX "-record" |
| 444 | #define REPORT_SUFFIX "-report" |
| 445 | |
| 446 | struct script_desc { |
| 447 | struct list_head node; |
| 448 | char *name; |
| 449 | char *half_liner; |
| 450 | char *args; |
| 451 | }; |
| 452 | |
Arnaldo Carvalho de Melo | eccdfe2 | 2011-01-04 16:32:52 -0200 | [diff] [blame] | 453 | static LIST_HEAD(script_descs); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 454 | |
| 455 | static struct script_desc *script_desc__new(const char *name) |
| 456 | { |
| 457 | struct script_desc *s = zalloc(sizeof(*s)); |
| 458 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 459 | if (s != NULL && name) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 460 | s->name = strdup(name); |
| 461 | |
| 462 | return s; |
| 463 | } |
| 464 | |
| 465 | static void script_desc__delete(struct script_desc *s) |
| 466 | { |
| 467 | free(s->name); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 468 | free(s->half_liner); |
| 469 | free(s->args); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 470 | free(s); |
| 471 | } |
| 472 | |
| 473 | static void script_desc__add(struct script_desc *s) |
| 474 | { |
| 475 | list_add_tail(&s->node, &script_descs); |
| 476 | } |
| 477 | |
| 478 | static struct script_desc *script_desc__find(const char *name) |
| 479 | { |
| 480 | struct script_desc *s; |
| 481 | |
| 482 | list_for_each_entry(s, &script_descs, node) |
| 483 | if (strcasecmp(s->name, name) == 0) |
| 484 | return s; |
| 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | static struct script_desc *script_desc__findnew(const char *name) |
| 489 | { |
| 490 | struct script_desc *s = script_desc__find(name); |
| 491 | |
| 492 | if (s) |
| 493 | return s; |
| 494 | |
| 495 | s = script_desc__new(name); |
| 496 | if (!s) |
| 497 | goto out_delete_desc; |
| 498 | |
| 499 | script_desc__add(s); |
| 500 | |
| 501 | return s; |
| 502 | |
| 503 | out_delete_desc: |
| 504 | script_desc__delete(s); |
| 505 | |
| 506 | return NULL; |
| 507 | } |
| 508 | |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 509 | static const char *ends_with(const char *str, const char *suffix) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 510 | { |
| 511 | size_t suffix_len = strlen(suffix); |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 512 | const char *p = str; |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 513 | |
| 514 | if (strlen(str) > suffix_len) { |
| 515 | p = str + strlen(str) - suffix_len; |
| 516 | if (!strncmp(p, suffix, suffix_len)) |
| 517 | return p; |
| 518 | } |
| 519 | |
| 520 | return NULL; |
| 521 | } |
| 522 | |
| 523 | static char *ltrim(char *str) |
| 524 | { |
| 525 | int len = strlen(str); |
| 526 | |
| 527 | while (len && isspace(*str)) { |
| 528 | len--; |
| 529 | str++; |
| 530 | } |
| 531 | |
| 532 | return str; |
| 533 | } |
| 534 | |
| 535 | static int read_script_info(struct script_desc *desc, const char *filename) |
| 536 | { |
| 537 | char line[BUFSIZ], *p; |
| 538 | FILE *fp; |
| 539 | |
| 540 | fp = fopen(filename, "r"); |
| 541 | if (!fp) |
| 542 | return -1; |
| 543 | |
| 544 | while (fgets(line, sizeof(line), fp)) { |
| 545 | p = ltrim(line); |
| 546 | if (strlen(p) == 0) |
| 547 | continue; |
| 548 | if (*p != '#') |
| 549 | continue; |
| 550 | p++; |
| 551 | if (strlen(p) && *p == '!') |
| 552 | continue; |
| 553 | |
| 554 | p = ltrim(p); |
| 555 | if (strlen(p) && p[strlen(p) - 1] == '\n') |
| 556 | p[strlen(p) - 1] = '\0'; |
| 557 | |
| 558 | if (!strncmp(p, "description:", strlen("description:"))) { |
| 559 | p += strlen("description:"); |
| 560 | desc->half_liner = strdup(ltrim(p)); |
| 561 | continue; |
| 562 | } |
| 563 | |
| 564 | if (!strncmp(p, "args:", strlen("args:"))) { |
| 565 | p += strlen("args:"); |
| 566 | desc->args = strdup(ltrim(p)); |
| 567 | continue; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | fclose(fp); |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int list_available_scripts(const struct option *opt __used, |
| 577 | const char *s __used, int unset __used) |
| 578 | { |
| 579 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; |
| 580 | char scripts_path[MAXPATHLEN]; |
| 581 | DIR *scripts_dir, *lang_dir; |
| 582 | char script_path[MAXPATHLEN]; |
| 583 | char lang_path[MAXPATHLEN]; |
| 584 | struct script_desc *desc; |
| 585 | char first_half[BUFSIZ]; |
| 586 | char *script_root; |
| 587 | char *str; |
| 588 | |
| 589 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); |
| 590 | |
| 591 | scripts_dir = opendir(scripts_path); |
| 592 | if (!scripts_dir) |
| 593 | return -1; |
| 594 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 595 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 596 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
| 597 | lang_dirent.d_name); |
| 598 | lang_dir = opendir(lang_path); |
| 599 | if (!lang_dir) |
| 600 | continue; |
| 601 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 602 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 603 | script_root = strdup(script_dirent.d_name); |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 604 | str = (char *)ends_with(script_root, REPORT_SUFFIX); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 605 | if (str) { |
| 606 | *str = '\0'; |
| 607 | desc = script_desc__findnew(script_root); |
| 608 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
| 609 | lang_path, script_dirent.d_name); |
| 610 | read_script_info(desc, script_path); |
| 611 | } |
| 612 | free(script_root); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | fprintf(stdout, "List of available trace scripts:\n"); |
| 617 | list_for_each_entry(desc, &script_descs, node) { |
| 618 | sprintf(first_half, "%s %s", desc->name, |
| 619 | desc->args ? desc->args : ""); |
| 620 | fprintf(stdout, " %-36s %s\n", first_half, |
| 621 | desc->half_liner ? desc->half_liner : ""); |
| 622 | } |
| 623 | |
| 624 | exit(0); |
| 625 | } |
| 626 | |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 627 | static char *get_script_path(const char *script_root, const char *suffix) |
| 628 | { |
| 629 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; |
| 630 | char scripts_path[MAXPATHLEN]; |
| 631 | char script_path[MAXPATHLEN]; |
| 632 | DIR *scripts_dir, *lang_dir; |
| 633 | char lang_path[MAXPATHLEN]; |
| 634 | char *str, *__script_root; |
| 635 | char *path = NULL; |
| 636 | |
| 637 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); |
| 638 | |
| 639 | scripts_dir = opendir(scripts_path); |
| 640 | if (!scripts_dir) |
| 641 | return NULL; |
| 642 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 643 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 644 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
| 645 | lang_dirent.d_name); |
| 646 | lang_dir = opendir(lang_path); |
| 647 | if (!lang_dir) |
| 648 | continue; |
| 649 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 650 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 651 | __script_root = strdup(script_dirent.d_name); |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 652 | str = (char *)ends_with(__script_root, suffix); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 653 | if (str) { |
| 654 | *str = '\0'; |
| 655 | if (strcmp(__script_root, script_root)) |
| 656 | continue; |
| 657 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
| 658 | lang_path, script_dirent.d_name); |
| 659 | path = strdup(script_path); |
| 660 | free(__script_root); |
| 661 | break; |
| 662 | } |
| 663 | free(__script_root); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | return path; |
| 668 | } |
| 669 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 670 | static bool is_top_script(const char *script_path) |
| 671 | { |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 672 | return ends_with(script_path, "top") == NULL ? false : true; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | static int has_required_arg(char *script_path) |
| 676 | { |
| 677 | struct script_desc *desc; |
| 678 | int n_args = 0; |
| 679 | char *p; |
| 680 | |
| 681 | desc = script_desc__new(NULL); |
| 682 | |
| 683 | if (read_script_info(desc, script_path)) |
| 684 | goto out; |
| 685 | |
| 686 | if (!desc->args) |
| 687 | goto out; |
| 688 | |
| 689 | for (p = desc->args; *p; p++) |
| 690 | if (*p == '<') |
| 691 | n_args++; |
| 692 | out: |
| 693 | script_desc__delete(desc); |
| 694 | |
| 695 | return n_args; |
| 696 | } |
| 697 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 698 | static const char * const script_usage[] = { |
| 699 | "perf script [<options>]", |
| 700 | "perf script [<options>] record <script> [<record-options>] <command>", |
| 701 | "perf script [<options>] report <script> [script-args]", |
| 702 | "perf script [<options>] <script> [<record-options>] <command>", |
| 703 | "perf script [<options>] <top-script> [script-args]", |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 704 | NULL |
| 705 | }; |
| 706 | |
| 707 | static const struct option options[] = { |
| 708 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 709 | "dump raw trace in ASCII"), |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 710 | OPT_INCR('v', "verbose", &verbose, |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 711 | "be more verbose (show symbol address, etc)"), |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 712 | OPT_BOOLEAN('L', "Latency", &latency_format, |
Steven Rostedt | cda4846 | 2009-10-14 15:43:42 -0400 | [diff] [blame] | 713 | "show latency attributes (irqs/preemption disabled, etc)"), |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 714 | OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", |
| 715 | list_available_scripts), |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 716 | OPT_CALLBACK('s', "script", NULL, "name", |
| 717 | "script file name (lang:script name, script name, or *)", |
| 718 | parse_scriptname), |
| 719 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 720 | "generate perf-script.xx script in specified language"), |
Hitoshi Mitake | 408f0d1 | 2010-01-22 22:45:29 +0900 | [diff] [blame] | 721 | OPT_STRING('i', "input", &input_name, "file", |
| 722 | "input file name"), |
Frederic Weisbecker | ffabd99 | 2010-05-27 16:27:47 +0200 | [diff] [blame] | 723 | OPT_BOOLEAN('d', "debug-mode", &debug_mode, |
| 724 | "do various checks like samples ordering and lost events"), |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 725 | OPT_CALLBACK('f', "fields", NULL, "str", |
| 726 | "comma separated output fields. Options: comm,tid,pid,time,cpu,event,trace", |
| 727 | parse_output_fields), |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 728 | |
Masami Hiramatsu | 1909629 | 2009-08-21 14:56:03 -0400 | [diff] [blame] | 729 | OPT_END() |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 730 | }; |
| 731 | |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 732 | static bool have_cmd(int argc, const char **argv) |
| 733 | { |
| 734 | char **__argv = malloc(sizeof(const char *) * argc); |
| 735 | |
| 736 | if (!__argv) |
| 737 | die("malloc"); |
| 738 | memcpy(__argv, argv, sizeof(const char *) * argc); |
| 739 | argc = parse_options(argc, (const char **)__argv, record_options, |
| 740 | NULL, PARSE_OPT_STOP_AT_NON_OPTION); |
| 741 | free(__argv); |
| 742 | |
| 743 | return argc != 0; |
| 744 | } |
| 745 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 746 | int cmd_script(int argc, const char **argv, const char *prefix __used) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 747 | { |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 748 | char *rec_script_path = NULL; |
| 749 | char *rep_script_path = NULL; |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 750 | struct perf_session *session; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 751 | char *script_path = NULL; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 752 | const char **__argv; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 753 | bool system_wide; |
| 754 | int i, j, err; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 755 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 756 | setup_scripting(); |
| 757 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 758 | argc = parse_options(argc, argv, options, script_usage, |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 759 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 760 | |
| 761 | if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { |
| 762 | rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); |
| 763 | if (!rec_script_path) |
| 764 | return cmd_record(argc, argv, NULL); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 765 | } |
| 766 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 767 | if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { |
| 768 | rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); |
| 769 | if (!rep_script_path) { |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 770 | fprintf(stderr, |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 771 | "Please specify a valid report script" |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 772 | "(see 'perf script -l' for listing)\n"); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 773 | return -1; |
| 774 | } |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 775 | } |
| 776 | |
Ben Hutchings | 44e668c | 2010-10-10 16:10:03 +0100 | [diff] [blame] | 777 | /* make sure PERF_EXEC_PATH is set for scripts */ |
| 778 | perf_set_argv_exec_path(perf_exec_path()); |
| 779 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 780 | if (argc && !script_name && !rec_script_path && !rep_script_path) { |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 781 | int live_pipe[2]; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 782 | int rep_args; |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 783 | pid_t pid; |
| 784 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 785 | rec_script_path = get_script_path(argv[0], RECORD_SUFFIX); |
| 786 | rep_script_path = get_script_path(argv[0], REPORT_SUFFIX); |
| 787 | |
| 788 | if (!rec_script_path && !rep_script_path) { |
| 789 | fprintf(stderr, " Couldn't find script %s\n\n See perf" |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 790 | " script -l for available scripts.\n", argv[0]); |
| 791 | usage_with_options(script_usage, options); |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 792 | } |
| 793 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 794 | if (is_top_script(argv[0])) { |
| 795 | rep_args = argc - 1; |
| 796 | } else { |
| 797 | int rec_args; |
| 798 | |
| 799 | rep_args = has_required_arg(rep_script_path); |
| 800 | rec_args = (argc - 1) - rep_args; |
| 801 | if (rec_args < 0) { |
| 802 | fprintf(stderr, " %s script requires options." |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 803 | "\n\n See perf script -l for available " |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 804 | "scripts and options.\n", argv[0]); |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 805 | usage_with_options(script_usage, options); |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 806 | } |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | if (pipe(live_pipe) < 0) { |
| 810 | perror("failed to create pipe"); |
| 811 | exit(-1); |
| 812 | } |
| 813 | |
| 814 | pid = fork(); |
| 815 | if (pid < 0) { |
| 816 | perror("failed to fork"); |
| 817 | exit(-1); |
| 818 | } |
| 819 | |
| 820 | if (!pid) { |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 821 | system_wide = true; |
| 822 | j = 0; |
| 823 | |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 824 | dup2(live_pipe[1], 1); |
| 825 | close(live_pipe[0]); |
| 826 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 827 | if (!is_top_script(argv[0])) |
| 828 | system_wide = !have_cmd(argc - rep_args, |
| 829 | &argv[rep_args]); |
| 830 | |
| 831 | __argv = malloc((argc + 6) * sizeof(const char *)); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 832 | if (!__argv) |
| 833 | die("malloc"); |
| 834 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 835 | __argv[j++] = "/bin/sh"; |
| 836 | __argv[j++] = rec_script_path; |
| 837 | if (system_wide) |
| 838 | __argv[j++] = "-a"; |
| 839 | __argv[j++] = "-q"; |
| 840 | __argv[j++] = "-o"; |
| 841 | __argv[j++] = "-"; |
| 842 | for (i = rep_args + 1; i < argc; i++) |
| 843 | __argv[j++] = argv[i]; |
| 844 | __argv[j++] = NULL; |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 845 | |
| 846 | execvp("/bin/sh", (char **)__argv); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 847 | free(__argv); |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 848 | exit(-1); |
| 849 | } |
| 850 | |
| 851 | dup2(live_pipe[0], 0); |
| 852 | close(live_pipe[1]); |
| 853 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 854 | __argv = malloc((argc + 4) * sizeof(const char *)); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 855 | if (!__argv) |
| 856 | die("malloc"); |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 857 | j = 0; |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 858 | __argv[j++] = "/bin/sh"; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 859 | __argv[j++] = rep_script_path; |
| 860 | for (i = 1; i < rep_args + 1; i++) |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 861 | __argv[j++] = argv[i]; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 862 | __argv[j++] = "-i"; |
| 863 | __argv[j++] = "-"; |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 864 | __argv[j++] = NULL; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 865 | |
| 866 | execvp("/bin/sh", (char **)__argv); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 867 | free(__argv); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 868 | exit(-1); |
| 869 | } |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 870 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 871 | if (rec_script_path) |
| 872 | script_path = rec_script_path; |
| 873 | if (rep_script_path) |
| 874 | script_path = rep_script_path; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 875 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 876 | if (script_path) { |
| 877 | system_wide = false; |
| 878 | j = 0; |
| 879 | |
| 880 | if (rec_script_path) |
| 881 | system_wide = !have_cmd(argc - 1, &argv[1]); |
| 882 | |
| 883 | __argv = malloc((argc + 2) * sizeof(const char *)); |
| 884 | if (!__argv) |
| 885 | die("malloc"); |
| 886 | __argv[j++] = "/bin/sh"; |
| 887 | __argv[j++] = script_path; |
| 888 | if (system_wide) |
| 889 | __argv[j++] = "-a"; |
| 890 | for (i = 2; i < argc; i++) |
| 891 | __argv[j++] = argv[i]; |
| 892 | __argv[j++] = NULL; |
| 893 | |
| 894 | execvp("/bin/sh", (char **)__argv); |
| 895 | free(__argv); |
| 896 | exit(-1); |
| 897 | } |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 898 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 899 | if (symbol__init() < 0) |
| 900 | return -1; |
Tom Zanussi | cf4fee5 | 2010-03-03 01:04:33 -0600 | [diff] [blame] | 901 | if (!script_name) |
| 902 | setup_pager(); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 903 | |
Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 904 | session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops); |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 905 | if (session == NULL) |
| 906 | return -ENOMEM; |
| 907 | |
Tom Zanussi | c239da3 | 2010-04-01 23:59:18 -0500 | [diff] [blame] | 908 | if (strcmp(input_name, "-") && |
| 909 | !perf_session__has_traces(session, "record -R")) |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 910 | return -EINVAL; |
| 911 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 912 | if (generate_script_lang) { |
| 913 | struct stat perf_stat; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 914 | int input; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 915 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame^] | 916 | if (output_set_by_user) { |
| 917 | fprintf(stderr, |
| 918 | "custom fields not supported for generated scripts"); |
| 919 | return -1; |
| 920 | } |
| 921 | |
| 922 | input = open(input_name, O_RDONLY); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 923 | if (input < 0) { |
| 924 | perror("failed to open file"); |
| 925 | exit(-1); |
| 926 | } |
| 927 | |
| 928 | err = fstat(input, &perf_stat); |
| 929 | if (err < 0) { |
| 930 | perror("failed to stat file"); |
| 931 | exit(-1); |
| 932 | } |
| 933 | |
| 934 | if (!perf_stat.st_size) { |
| 935 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 936 | exit(0); |
| 937 | } |
| 938 | |
| 939 | scripting_ops = script_spec__lookup(generate_script_lang); |
| 940 | if (!scripting_ops) { |
| 941 | fprintf(stderr, "invalid language specifier"); |
| 942 | return -1; |
| 943 | } |
| 944 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 945 | err = scripting_ops->generate_script("perf-script"); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 946 | goto out; |
| 947 | } |
| 948 | |
| 949 | if (script_name) { |
Tom Zanussi | 586bc5c | 2009-12-15 02:53:35 -0600 | [diff] [blame] | 950 | err = scripting_ops->start_script(script_name, argc, argv); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 951 | if (err) |
| 952 | goto out; |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 953 | pr_debug("perf script started with script %s\n\n", script_name); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 954 | } |
| 955 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 956 | err = __cmd_script(session); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 957 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 958 | perf_session__delete(session); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 959 | cleanup_scripting(); |
| 960 | out: |
| 961 | return err; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 962 | } |