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