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