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