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