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" |
Andrea Gelmini | b7eead8 | 2010-08-05 15:51:38 +0200 | [diff] [blame] | 13 | #include "util/util.h" |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 14 | #include "util/evlist.h" |
| 15 | #include "util/evsel.h" |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 16 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 17 | static char const *script_name; |
| 18 | static char const *generate_script_lang; |
Frederic Weisbecker | ffabd99 | 2010-05-27 16:27:47 +0200 | [diff] [blame] | 19 | static bool debug_mode; |
Frederic Weisbecker | e1889d7 | 2010-04-24 01:55:09 +0200 | [diff] [blame] | 20 | static u64 last_timestamp; |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 21 | static u64 nr_unordered; |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 22 | extern const struct option record_options[]; |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 23 | static bool no_callchain; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 24 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 25 | enum perf_output_field { |
| 26 | PERF_OUTPUT_COMM = 1U << 0, |
| 27 | PERF_OUTPUT_TID = 1U << 1, |
| 28 | PERF_OUTPUT_PID = 1U << 2, |
| 29 | PERF_OUTPUT_TIME = 1U << 3, |
| 30 | PERF_OUTPUT_CPU = 1U << 4, |
| 31 | PERF_OUTPUT_EVNAME = 1U << 5, |
| 32 | PERF_OUTPUT_TRACE = 1U << 6, |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 33 | PERF_OUTPUT_IP = 1U << 7, |
| 34 | PERF_OUTPUT_SYM = 1U << 8, |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame^] | 35 | PERF_OUTPUT_DSO = 1U << 9, |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct output_option { |
| 39 | const char *str; |
| 40 | enum perf_output_field field; |
| 41 | } all_output_options[] = { |
| 42 | {.str = "comm", .field = PERF_OUTPUT_COMM}, |
| 43 | {.str = "tid", .field = PERF_OUTPUT_TID}, |
| 44 | {.str = "pid", .field = PERF_OUTPUT_PID}, |
| 45 | {.str = "time", .field = PERF_OUTPUT_TIME}, |
| 46 | {.str = "cpu", .field = PERF_OUTPUT_CPU}, |
| 47 | {.str = "event", .field = PERF_OUTPUT_EVNAME}, |
| 48 | {.str = "trace", .field = PERF_OUTPUT_TRACE}, |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 49 | {.str = "ip", .field = PERF_OUTPUT_IP}, |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 50 | {.str = "sym", .field = PERF_OUTPUT_SYM}, |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame^] | 51 | {.str = "dso", .field = PERF_OUTPUT_DSO}, |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | /* default set to maintain compatibility with current format */ |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 55 | static struct { |
| 56 | bool user_set; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 57 | bool wildcard_set; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 58 | u64 fields; |
| 59 | u64 invalid_fields; |
| 60 | } output[PERF_TYPE_MAX] = { |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 61 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 62 | [PERF_TYPE_HARDWARE] = { |
| 63 | .user_set = false, |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 64 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 65 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | |
| 66 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 67 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame^] | 68 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO, |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 69 | |
| 70 | .invalid_fields = PERF_OUTPUT_TRACE, |
| 71 | }, |
| 72 | |
| 73 | [PERF_TYPE_SOFTWARE] = { |
| 74 | .user_set = false, |
| 75 | |
| 76 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | |
| 77 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 78 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame^] | 79 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO, |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 80 | |
| 81 | .invalid_fields = PERF_OUTPUT_TRACE, |
| 82 | }, |
| 83 | |
| 84 | [PERF_TYPE_TRACEPOINT] = { |
| 85 | .user_set = false, |
| 86 | |
| 87 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | |
| 88 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | |
| 89 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE, |
| 90 | }, |
Arun Sharma | 0817a6a | 2011-04-14 10:38:18 -0700 | [diff] [blame] | 91 | |
| 92 | [PERF_TYPE_RAW] = { |
| 93 | .user_set = false, |
| 94 | |
| 95 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | |
| 96 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 97 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame^] | 98 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO, |
Arun Sharma | 0817a6a | 2011-04-14 10:38:18 -0700 | [diff] [blame] | 99 | |
| 100 | .invalid_fields = PERF_OUTPUT_TRACE, |
| 101 | }, |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 102 | }; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 103 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 104 | static bool output_set_by_user(void) |
| 105 | { |
| 106 | int j; |
| 107 | for (j = 0; j < PERF_TYPE_MAX; ++j) { |
| 108 | if (output[j].user_set) |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 113 | |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 114 | static const char *output_field2str(enum perf_output_field field) |
| 115 | { |
| 116 | int i, imax = ARRAY_SIZE(all_output_options); |
| 117 | const char *str = ""; |
| 118 | |
| 119 | for (i = 0; i < imax; ++i) { |
| 120 | if (all_output_options[i].field == field) { |
| 121 | str = all_output_options[i].str; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | return str; |
| 126 | } |
| 127 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 128 | #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 129 | |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 130 | static int perf_event_attr__check_stype(struct perf_event_attr *attr, |
| 131 | u64 sample_type, const char *sample_msg, |
| 132 | enum perf_output_field field) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 133 | { |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 134 | int type = attr->type; |
| 135 | const char *evname; |
| 136 | |
| 137 | if (attr->sample_type & sample_type) |
| 138 | return 0; |
| 139 | |
| 140 | if (output[type].user_set) { |
| 141 | evname = __event_name(attr->type, attr->config); |
| 142 | pr_err("Samples for '%s' event do not have %s attribute set. " |
| 143 | "Cannot print '%s' field.\n", |
| 144 | evname, sample_msg, output_field2str(field)); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | /* user did not ask for it explicitly so remove from the default list */ |
| 149 | output[type].fields &= ~field; |
| 150 | evname = __event_name(attr->type, attr->config); |
| 151 | pr_debug("Samples for '%s' event do not have %s attribute set. " |
| 152 | "Skipping '%s' field.\n", |
| 153 | evname, sample_msg, output_field2str(field)); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int perf_evsel__check_attr(struct perf_evsel *evsel, |
| 159 | struct perf_session *session) |
| 160 | { |
| 161 | struct perf_event_attr *attr = &evsel->attr; |
| 162 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 163 | if (PRINT_FIELD(TRACE) && |
| 164 | !perf_session__has_traces(session, "record -R")) |
| 165 | return -EINVAL; |
| 166 | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 167 | if (PRINT_FIELD(IP)) { |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 168 | if (perf_event_attr__check_stype(attr, PERF_SAMPLE_IP, "IP", |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 169 | PERF_OUTPUT_IP)) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 170 | return -EINVAL; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 171 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 172 | if (!no_callchain && |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 173 | !(attr->sample_type & PERF_SAMPLE_CALLCHAIN)) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 174 | symbol_conf.use_callchain = false; |
| 175 | } |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 176 | if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP)) { |
| 177 | pr_err("Display of symbols requested but IP is not selected.\n" |
| 178 | "No addresses to convert to symbols.\n"); |
| 179 | return -EINVAL; |
| 180 | } |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame^] | 181 | if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP)) { |
| 182 | pr_err("Display of DSO requested but IP is not selected.\n" |
| 183 | "No addresses to convert to dso.\n"); |
| 184 | return -EINVAL; |
| 185 | } |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 186 | |
| 187 | if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) && |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 188 | perf_event_attr__check_stype(attr, PERF_SAMPLE_TID, "TID", |
| 189 | PERF_OUTPUT_TID|PERF_OUTPUT_PID)) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 190 | return -EINVAL; |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 191 | |
| 192 | if (PRINT_FIELD(TIME) && |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 193 | perf_event_attr__check_stype(attr, PERF_SAMPLE_TIME, "TIME", |
| 194 | PERF_OUTPUT_TIME)) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 195 | return -EINVAL; |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 196 | |
| 197 | if (PRINT_FIELD(CPU) && |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 198 | perf_event_attr__check_stype(attr, PERF_SAMPLE_CPU, "CPU", |
| 199 | PERF_OUTPUT_CPU)) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 200 | return -EINVAL; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * verify all user requested events exist and the samples |
| 207 | * have the expected data |
| 208 | */ |
| 209 | static int perf_session__check_output_opt(struct perf_session *session) |
| 210 | { |
| 211 | int j; |
| 212 | struct perf_evsel *evsel; |
| 213 | |
| 214 | for (j = 0; j < PERF_TYPE_MAX; ++j) { |
| 215 | evsel = perf_session__find_first_evtype(session, j); |
| 216 | |
| 217 | /* |
| 218 | * even if fields is set to 0 (ie., show nothing) event must |
| 219 | * exist if user explicitly includes it on the command line |
| 220 | */ |
| 221 | if (!evsel && output[j].user_set && !output[j].wildcard_set) { |
| 222 | pr_err("%s events do not exist. " |
| 223 | "Remove corresponding -f option to proceed.\n", |
| 224 | event_type(j)); |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | if (evsel && output[j].fields && |
| 229 | perf_evsel__check_attr(evsel, session)) |
| 230 | return -1; |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 235 | |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 236 | static void print_sample_start(struct perf_sample *sample, |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 237 | struct thread *thread, |
| 238 | struct perf_event_attr *attr) |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 239 | { |
| 240 | int type; |
| 241 | struct event *event; |
| 242 | const char *evname = NULL; |
| 243 | unsigned long secs; |
| 244 | unsigned long usecs; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 245 | unsigned long long nsecs; |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 246 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 247 | if (PRINT_FIELD(COMM)) { |
| 248 | if (latency_format) |
| 249 | printf("%8.8s ", thread->comm); |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 250 | else if (PRINT_FIELD(IP) && symbol_conf.use_callchain) |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 251 | printf("%s ", thread->comm); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 252 | else |
| 253 | printf("%16s ", thread->comm); |
| 254 | } |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 255 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 256 | if (PRINT_FIELD(PID) && PRINT_FIELD(TID)) |
| 257 | printf("%5d/%-5d ", sample->pid, sample->tid); |
| 258 | else if (PRINT_FIELD(PID)) |
| 259 | printf("%5d ", sample->pid); |
| 260 | else if (PRINT_FIELD(TID)) |
| 261 | printf("%5d ", sample->tid); |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 262 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 263 | if (PRINT_FIELD(CPU)) { |
| 264 | if (latency_format) |
| 265 | printf("%3d ", sample->cpu); |
| 266 | else |
| 267 | printf("[%03d] ", sample->cpu); |
| 268 | } |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 269 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 270 | if (PRINT_FIELD(TIME)) { |
| 271 | nsecs = sample->time; |
| 272 | secs = nsecs / NSECS_PER_SEC; |
| 273 | nsecs -= secs * NSECS_PER_SEC; |
| 274 | usecs = nsecs / NSECS_PER_USEC; |
| 275 | printf("%5lu.%06lu: ", secs, usecs); |
| 276 | } |
| 277 | |
| 278 | if (PRINT_FIELD(EVNAME)) { |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 279 | if (attr->type == PERF_TYPE_TRACEPOINT) { |
| 280 | type = trace_parse_common_type(sample->raw_data); |
| 281 | event = trace_find_event(type); |
| 282 | if (event) |
| 283 | evname = event->name; |
| 284 | } else |
| 285 | evname = __event_name(attr->type, attr->config); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 286 | |
| 287 | printf("%s: ", evname ? evname : "(unknown)"); |
| 288 | } |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 289 | } |
| 290 | |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 291 | static void process_event(union perf_event *event __unused, |
| 292 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 293 | struct perf_evsel *evsel, |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 294 | struct perf_session *session, |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 295 | struct thread *thread) |
| 296 | { |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 297 | struct perf_event_attr *attr = &evsel->attr; |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 298 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 299 | if (output[attr->type].fields == 0) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 300 | return; |
| 301 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 302 | print_sample_start(sample, thread, attr); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 303 | |
| 304 | if (PRINT_FIELD(TRACE)) |
| 305 | print_trace_event(sample->cpu, sample->raw_data, |
| 306 | sample->raw_size); |
| 307 | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 308 | if (PRINT_FIELD(IP)) { |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 309 | if (!symbol_conf.use_callchain) |
| 310 | printf(" "); |
| 311 | else |
| 312 | printf("\n"); |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 313 | perf_session__print_ip(event, sample, session, |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame^] | 314 | PRINT_FIELD(SYM), PRINT_FIELD(DSO)); |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 315 | } |
| 316 | |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 317 | printf("\n"); |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Tom Zanussi | 586bc5c | 2009-12-15 02:53:35 -0600 | [diff] [blame] | 320 | static int default_start_script(const char *script __unused, |
| 321 | int argc __unused, |
| 322 | const char **argv __unused) |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 323 | { |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static int default_stop_script(void) |
| 328 | { |
| 329 | return 0; |
| 330 | } |
| 331 | |
Tom Zanussi | 586bc5c | 2009-12-15 02:53:35 -0600 | [diff] [blame] | 332 | static int default_generate_script(const char *outfile __unused) |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 333 | { |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static struct scripting_ops default_scripting_ops = { |
| 338 | .start_script = default_start_script, |
| 339 | .stop_script = default_stop_script, |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 340 | .process_event = process_event, |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 341 | .generate_script = default_generate_script, |
| 342 | }; |
| 343 | |
| 344 | static struct scripting_ops *scripting_ops; |
| 345 | |
| 346 | static void setup_scripting(void) |
| 347 | { |
Tom Zanussi | 16c632d | 2009-11-25 01:15:48 -0600 | [diff] [blame] | 348 | setup_perl_scripting(); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 349 | setup_python_scripting(); |
Tom Zanussi | 16c632d | 2009-11-25 01:15:48 -0600 | [diff] [blame] | 350 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 351 | scripting_ops = &default_scripting_ops; |
| 352 | } |
| 353 | |
| 354 | static int cleanup_scripting(void) |
| 355 | { |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 356 | pr_debug("\nperf script stopped\n"); |
Tom Zanussi | 3824a4e | 2010-05-09 23:46:57 -0500 | [diff] [blame] | 357 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 358 | return scripting_ops->stop_script(); |
| 359 | } |
| 360 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 361 | static char const *input_name = "perf.data"; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 362 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 363 | static int process_sample_event(union perf_event *event, |
| 364 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 365 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 366 | struct perf_session *session) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 367 | { |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 368 | struct thread *thread = perf_session__findnew(session, event->ip.pid); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 369 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 370 | if (thread == NULL) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 371 | pr_debug("problem processing %d event, skipping it.\n", |
| 372 | event->header.type); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 373 | return -1; |
| 374 | } |
| 375 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 376 | if (debug_mode) { |
| 377 | if (sample->time < last_timestamp) { |
| 378 | pr_err("Samples misordered, previous: %" PRIu64 |
| 379 | " this: %" PRIu64 "\n", last_timestamp, |
| 380 | sample->time); |
| 381 | nr_unordered++; |
Frederic Weisbecker | e1889d7 | 2010-04-24 01:55:09 +0200 | [diff] [blame] | 382 | } |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 383 | last_timestamp = sample->time; |
| 384 | return 0; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 385 | } |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 386 | scripting_ops->process_event(event, sample, evsel, session, thread); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 387 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 388 | session->hists.stats.total_period += sample->period; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 392 | static struct perf_event_ops event_ops = { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 393 | .sample = process_sample_event, |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 394 | .mmap = perf_event__process_mmap, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 395 | .comm = perf_event__process_comm, |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 396 | .exit = perf_event__process_task, |
| 397 | .fork = perf_event__process_task, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 398 | .attr = perf_event__process_attr, |
| 399 | .event_type = perf_event__process_event_type, |
| 400 | .tracing_data = perf_event__process_tracing_data, |
| 401 | .build_id = perf_event__process_build_id, |
Frederic Weisbecker | e0a808c | 2010-04-24 00:38:33 +0200 | [diff] [blame] | 402 | .ordered_samples = true, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 403 | .ordering_requires_timestamps = true, |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame] | 404 | }; |
| 405 | |
Tom Zanussi | c239da3 | 2010-04-01 23:59:18 -0500 | [diff] [blame] | 406 | extern volatile int session_done; |
| 407 | |
| 408 | static void sig_handler(int sig __unused) |
| 409 | { |
| 410 | session_done = 1; |
| 411 | } |
| 412 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 413 | static int __cmd_script(struct perf_session *session) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 414 | { |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 415 | int ret; |
| 416 | |
Tom Zanussi | c239da3 | 2010-04-01 23:59:18 -0500 | [diff] [blame] | 417 | signal(SIGINT, sig_handler); |
| 418 | |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 419 | ret = perf_session__process_events(session, &event_ops); |
| 420 | |
Arnaldo Carvalho de Melo | 6d8afb5 | 2011-01-04 16:27:30 -0200 | [diff] [blame] | 421 | if (debug_mode) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 422 | pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 423 | |
| 424 | return ret; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 427 | struct script_spec { |
| 428 | struct list_head node; |
| 429 | struct scripting_ops *ops; |
| 430 | char spec[0]; |
| 431 | }; |
| 432 | |
Arnaldo Carvalho de Melo | eccdfe2 | 2011-01-04 16:32:52 -0200 | [diff] [blame] | 433 | static LIST_HEAD(script_specs); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 434 | |
| 435 | static struct script_spec *script_spec__new(const char *spec, |
| 436 | struct scripting_ops *ops) |
| 437 | { |
| 438 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); |
| 439 | |
| 440 | if (s != NULL) { |
| 441 | strcpy(s->spec, spec); |
| 442 | s->ops = ops; |
| 443 | } |
| 444 | |
| 445 | return s; |
| 446 | } |
| 447 | |
| 448 | static void script_spec__delete(struct script_spec *s) |
| 449 | { |
| 450 | free(s->spec); |
| 451 | free(s); |
| 452 | } |
| 453 | |
| 454 | static void script_spec__add(struct script_spec *s) |
| 455 | { |
| 456 | list_add_tail(&s->node, &script_specs); |
| 457 | } |
| 458 | |
| 459 | static struct script_spec *script_spec__find(const char *spec) |
| 460 | { |
| 461 | struct script_spec *s; |
| 462 | |
| 463 | list_for_each_entry(s, &script_specs, node) |
| 464 | if (strcasecmp(s->spec, spec) == 0) |
| 465 | return s; |
| 466 | return NULL; |
| 467 | } |
| 468 | |
| 469 | static struct script_spec *script_spec__findnew(const char *spec, |
| 470 | struct scripting_ops *ops) |
| 471 | { |
| 472 | struct script_spec *s = script_spec__find(spec); |
| 473 | |
| 474 | if (s) |
| 475 | return s; |
| 476 | |
| 477 | s = script_spec__new(spec, ops); |
| 478 | if (!s) |
| 479 | goto out_delete_spec; |
| 480 | |
| 481 | script_spec__add(s); |
| 482 | |
| 483 | return s; |
| 484 | |
| 485 | out_delete_spec: |
| 486 | script_spec__delete(s); |
| 487 | |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | int script_spec_register(const char *spec, struct scripting_ops *ops) |
| 492 | { |
| 493 | struct script_spec *s; |
| 494 | |
| 495 | s = script_spec__find(spec); |
| 496 | if (s) |
| 497 | return -1; |
| 498 | |
| 499 | s = script_spec__findnew(spec, ops); |
| 500 | if (!s) |
| 501 | return -1; |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static struct scripting_ops *script_spec__lookup(const char *spec) |
| 507 | { |
| 508 | struct script_spec *s = script_spec__find(spec); |
| 509 | if (!s) |
| 510 | return NULL; |
| 511 | |
| 512 | return s->ops; |
| 513 | } |
| 514 | |
| 515 | static void list_available_languages(void) |
| 516 | { |
| 517 | struct script_spec *s; |
| 518 | |
| 519 | fprintf(stderr, "\n"); |
| 520 | fprintf(stderr, "Scripting language extensions (used in " |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 521 | "perf script -s [spec:]script.[spec]):\n\n"); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 522 | |
| 523 | list_for_each_entry(s, &script_specs, node) |
| 524 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); |
| 525 | |
| 526 | fprintf(stderr, "\n"); |
| 527 | } |
| 528 | |
| 529 | static int parse_scriptname(const struct option *opt __used, |
| 530 | const char *str, int unset __used) |
| 531 | { |
| 532 | char spec[PATH_MAX]; |
| 533 | const char *script, *ext; |
| 534 | int len; |
| 535 | |
Tom Zanussi | f526d68 | 2010-01-27 02:27:52 -0600 | [diff] [blame] | 536 | if (strcmp(str, "lang") == 0) { |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 537 | list_available_languages(); |
Tom Zanussi | f526d68 | 2010-01-27 02:27:52 -0600 | [diff] [blame] | 538 | exit(0); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | script = strchr(str, ':'); |
| 542 | if (script) { |
| 543 | len = script - str; |
| 544 | if (len >= PATH_MAX) { |
| 545 | fprintf(stderr, "invalid language specifier"); |
| 546 | return -1; |
| 547 | } |
| 548 | strncpy(spec, str, len); |
| 549 | spec[len] = '\0'; |
| 550 | scripting_ops = script_spec__lookup(spec); |
| 551 | if (!scripting_ops) { |
| 552 | fprintf(stderr, "invalid language specifier"); |
| 553 | return -1; |
| 554 | } |
| 555 | script++; |
| 556 | } else { |
| 557 | script = str; |
Ben Hutchings | d1e95bb | 2010-10-10 16:11:02 +0100 | [diff] [blame] | 558 | ext = strrchr(script, '.'); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 559 | if (!ext) { |
| 560 | fprintf(stderr, "invalid script extension"); |
| 561 | return -1; |
| 562 | } |
| 563 | scripting_ops = script_spec__lookup(++ext); |
| 564 | if (!scripting_ops) { |
| 565 | fprintf(stderr, "invalid script extension"); |
| 566 | return -1; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | script_name = strdup(script); |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 575 | static int parse_output_fields(const struct option *opt __used, |
| 576 | const char *arg, int unset __used) |
| 577 | { |
| 578 | char *tok; |
| 579 | int i, imax = sizeof(all_output_options) / sizeof(struct output_option); |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 580 | int j; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 581 | int rc = 0; |
| 582 | char *str = strdup(arg); |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 583 | int type = -1; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 584 | |
| 585 | if (!str) |
| 586 | return -ENOMEM; |
| 587 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 588 | /* first word can state for which event type the user is specifying |
| 589 | * the fields. If no type exists, the specified fields apply to all |
| 590 | * event types found in the file minus the invalid fields for a type. |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 591 | */ |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 592 | tok = strchr(str, ':'); |
| 593 | if (tok) { |
| 594 | *tok = '\0'; |
| 595 | tok++; |
| 596 | if (!strcmp(str, "hw")) |
| 597 | type = PERF_TYPE_HARDWARE; |
| 598 | else if (!strcmp(str, "sw")) |
| 599 | type = PERF_TYPE_SOFTWARE; |
| 600 | else if (!strcmp(str, "trace")) |
| 601 | type = PERF_TYPE_TRACEPOINT; |
Arun Sharma | 0817a6a | 2011-04-14 10:38:18 -0700 | [diff] [blame] | 602 | else if (!strcmp(str, "raw")) |
| 603 | type = PERF_TYPE_RAW; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 604 | else { |
| 605 | fprintf(stderr, "Invalid event type in field string.\n"); |
| 606 | return -EINVAL; |
| 607 | } |
| 608 | |
| 609 | if (output[type].user_set) |
| 610 | pr_warning("Overriding previous field request for %s events.\n", |
| 611 | event_type(type)); |
| 612 | |
| 613 | output[type].fields = 0; |
| 614 | output[type].user_set = true; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 615 | output[type].wildcard_set = false; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 616 | |
| 617 | } else { |
| 618 | tok = str; |
| 619 | if (strlen(str) == 0) { |
| 620 | fprintf(stderr, |
| 621 | "Cannot set fields to 'none' for all event types.\n"); |
| 622 | rc = -EINVAL; |
| 623 | goto out; |
| 624 | } |
| 625 | |
| 626 | if (output_set_by_user()) |
| 627 | pr_warning("Overriding previous field request for all events.\n"); |
| 628 | |
| 629 | for (j = 0; j < PERF_TYPE_MAX; ++j) { |
| 630 | output[j].fields = 0; |
| 631 | output[j].user_set = true; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 632 | output[j].wildcard_set = true; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 633 | } |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 634 | } |
| 635 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 636 | tok = strtok(tok, ","); |
| 637 | while (tok) { |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 638 | for (i = 0; i < imax; ++i) { |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 639 | if (strcmp(tok, all_output_options[i].str) == 0) |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 640 | break; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 641 | } |
| 642 | if (i == imax) { |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 643 | fprintf(stderr, "Invalid field requested.\n"); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 644 | rc = -EINVAL; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 645 | goto out; |
| 646 | } |
| 647 | |
| 648 | if (type == -1) { |
| 649 | /* add user option to all events types for |
| 650 | * which it is valid |
| 651 | */ |
| 652 | for (j = 0; j < PERF_TYPE_MAX; ++j) { |
| 653 | if (output[j].invalid_fields & all_output_options[i].field) { |
| 654 | pr_warning("\'%s\' not valid for %s events. Ignoring.\n", |
| 655 | all_output_options[i].str, event_type(j)); |
| 656 | } else |
| 657 | output[j].fields |= all_output_options[i].field; |
| 658 | } |
| 659 | } else { |
| 660 | if (output[type].invalid_fields & all_output_options[i].field) { |
| 661 | fprintf(stderr, "\'%s\' not valid for %s events.\n", |
| 662 | all_output_options[i].str, event_type(type)); |
| 663 | |
| 664 | rc = -EINVAL; |
| 665 | goto out; |
| 666 | } |
| 667 | output[type].fields |= all_output_options[i].field; |
| 668 | } |
| 669 | |
| 670 | tok = strtok(NULL, ","); |
| 671 | } |
| 672 | |
| 673 | if (type >= 0) { |
| 674 | if (output[type].fields == 0) { |
| 675 | pr_debug("No fields requested for %s type. " |
| 676 | "Events will not be displayed.\n", event_type(type)); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 677 | } |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 678 | } |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 679 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 680 | out: |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 681 | free(str); |
| 682 | return rc; |
| 683 | } |
| 684 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 685 | /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */ |
| 686 | static int is_directory(const char *base_path, const struct dirent *dent) |
| 687 | { |
| 688 | char path[PATH_MAX]; |
| 689 | struct stat st; |
| 690 | |
| 691 | sprintf(path, "%s/%s", base_path, dent->d_name); |
| 692 | if (stat(path, &st)) |
| 693 | return 0; |
| 694 | |
| 695 | return S_ISDIR(st.st_mode); |
| 696 | } |
| 697 | |
| 698 | #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\ |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 699 | while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \ |
| 700 | lang_next) \ |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 701 | if ((lang_dirent.d_type == DT_DIR || \ |
| 702 | (lang_dirent.d_type == DT_UNKNOWN && \ |
| 703 | is_directory(scripts_path, &lang_dirent))) && \ |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 704 | (strcmp(lang_dirent.d_name, ".")) && \ |
| 705 | (strcmp(lang_dirent.d_name, ".."))) |
| 706 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 707 | #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\ |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 708 | while (!readdir_r(lang_dir, &script_dirent, &script_next) && \ |
| 709 | script_next) \ |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 710 | if (script_dirent.d_type != DT_DIR && \ |
| 711 | (script_dirent.d_type != DT_UNKNOWN || \ |
| 712 | !is_directory(lang_path, &script_dirent))) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 713 | |
| 714 | |
| 715 | #define RECORD_SUFFIX "-record" |
| 716 | #define REPORT_SUFFIX "-report" |
| 717 | |
| 718 | struct script_desc { |
| 719 | struct list_head node; |
| 720 | char *name; |
| 721 | char *half_liner; |
| 722 | char *args; |
| 723 | }; |
| 724 | |
Arnaldo Carvalho de Melo | eccdfe2 | 2011-01-04 16:32:52 -0200 | [diff] [blame] | 725 | static LIST_HEAD(script_descs); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 726 | |
| 727 | static struct script_desc *script_desc__new(const char *name) |
| 728 | { |
| 729 | struct script_desc *s = zalloc(sizeof(*s)); |
| 730 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 731 | if (s != NULL && name) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 732 | s->name = strdup(name); |
| 733 | |
| 734 | return s; |
| 735 | } |
| 736 | |
| 737 | static void script_desc__delete(struct script_desc *s) |
| 738 | { |
| 739 | free(s->name); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 740 | free(s->half_liner); |
| 741 | free(s->args); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 742 | free(s); |
| 743 | } |
| 744 | |
| 745 | static void script_desc__add(struct script_desc *s) |
| 746 | { |
| 747 | list_add_tail(&s->node, &script_descs); |
| 748 | } |
| 749 | |
| 750 | static struct script_desc *script_desc__find(const char *name) |
| 751 | { |
| 752 | struct script_desc *s; |
| 753 | |
| 754 | list_for_each_entry(s, &script_descs, node) |
| 755 | if (strcasecmp(s->name, name) == 0) |
| 756 | return s; |
| 757 | return NULL; |
| 758 | } |
| 759 | |
| 760 | static struct script_desc *script_desc__findnew(const char *name) |
| 761 | { |
| 762 | struct script_desc *s = script_desc__find(name); |
| 763 | |
| 764 | if (s) |
| 765 | return s; |
| 766 | |
| 767 | s = script_desc__new(name); |
| 768 | if (!s) |
| 769 | goto out_delete_desc; |
| 770 | |
| 771 | script_desc__add(s); |
| 772 | |
| 773 | return s; |
| 774 | |
| 775 | out_delete_desc: |
| 776 | script_desc__delete(s); |
| 777 | |
| 778 | return NULL; |
| 779 | } |
| 780 | |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 781 | static const char *ends_with(const char *str, const char *suffix) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 782 | { |
| 783 | size_t suffix_len = strlen(suffix); |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 784 | const char *p = str; |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 785 | |
| 786 | if (strlen(str) > suffix_len) { |
| 787 | p = str + strlen(str) - suffix_len; |
| 788 | if (!strncmp(p, suffix, suffix_len)) |
| 789 | return p; |
| 790 | } |
| 791 | |
| 792 | return NULL; |
| 793 | } |
| 794 | |
| 795 | static char *ltrim(char *str) |
| 796 | { |
| 797 | int len = strlen(str); |
| 798 | |
| 799 | while (len && isspace(*str)) { |
| 800 | len--; |
| 801 | str++; |
| 802 | } |
| 803 | |
| 804 | return str; |
| 805 | } |
| 806 | |
| 807 | static int read_script_info(struct script_desc *desc, const char *filename) |
| 808 | { |
| 809 | char line[BUFSIZ], *p; |
| 810 | FILE *fp; |
| 811 | |
| 812 | fp = fopen(filename, "r"); |
| 813 | if (!fp) |
| 814 | return -1; |
| 815 | |
| 816 | while (fgets(line, sizeof(line), fp)) { |
| 817 | p = ltrim(line); |
| 818 | if (strlen(p) == 0) |
| 819 | continue; |
| 820 | if (*p != '#') |
| 821 | continue; |
| 822 | p++; |
| 823 | if (strlen(p) && *p == '!') |
| 824 | continue; |
| 825 | |
| 826 | p = ltrim(p); |
| 827 | if (strlen(p) && p[strlen(p) - 1] == '\n') |
| 828 | p[strlen(p) - 1] = '\0'; |
| 829 | |
| 830 | if (!strncmp(p, "description:", strlen("description:"))) { |
| 831 | p += strlen("description:"); |
| 832 | desc->half_liner = strdup(ltrim(p)); |
| 833 | continue; |
| 834 | } |
| 835 | |
| 836 | if (!strncmp(p, "args:", strlen("args:"))) { |
| 837 | p += strlen("args:"); |
| 838 | desc->args = strdup(ltrim(p)); |
| 839 | continue; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | fclose(fp); |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static int list_available_scripts(const struct option *opt __used, |
| 849 | const char *s __used, int unset __used) |
| 850 | { |
| 851 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; |
| 852 | char scripts_path[MAXPATHLEN]; |
| 853 | DIR *scripts_dir, *lang_dir; |
| 854 | char script_path[MAXPATHLEN]; |
| 855 | char lang_path[MAXPATHLEN]; |
| 856 | struct script_desc *desc; |
| 857 | char first_half[BUFSIZ]; |
| 858 | char *script_root; |
| 859 | char *str; |
| 860 | |
| 861 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); |
| 862 | |
| 863 | scripts_dir = opendir(scripts_path); |
| 864 | if (!scripts_dir) |
| 865 | return -1; |
| 866 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 867 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 868 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
| 869 | lang_dirent.d_name); |
| 870 | lang_dir = opendir(lang_path); |
| 871 | if (!lang_dir) |
| 872 | continue; |
| 873 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 874 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 875 | script_root = strdup(script_dirent.d_name); |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 876 | str = (char *)ends_with(script_root, REPORT_SUFFIX); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 877 | if (str) { |
| 878 | *str = '\0'; |
| 879 | desc = script_desc__findnew(script_root); |
| 880 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
| 881 | lang_path, script_dirent.d_name); |
| 882 | read_script_info(desc, script_path); |
| 883 | } |
| 884 | free(script_root); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | fprintf(stdout, "List of available trace scripts:\n"); |
| 889 | list_for_each_entry(desc, &script_descs, node) { |
| 890 | sprintf(first_half, "%s %s", desc->name, |
| 891 | desc->args ? desc->args : ""); |
| 892 | fprintf(stdout, " %-36s %s\n", first_half, |
| 893 | desc->half_liner ? desc->half_liner : ""); |
| 894 | } |
| 895 | |
| 896 | exit(0); |
| 897 | } |
| 898 | |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 899 | static char *get_script_path(const char *script_root, const char *suffix) |
| 900 | { |
| 901 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; |
| 902 | char scripts_path[MAXPATHLEN]; |
| 903 | char script_path[MAXPATHLEN]; |
| 904 | DIR *scripts_dir, *lang_dir; |
| 905 | char lang_path[MAXPATHLEN]; |
| 906 | char *str, *__script_root; |
| 907 | char *path = NULL; |
| 908 | |
| 909 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); |
| 910 | |
| 911 | scripts_dir = opendir(scripts_path); |
| 912 | if (!scripts_dir) |
| 913 | return NULL; |
| 914 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 915 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 916 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
| 917 | lang_dirent.d_name); |
| 918 | lang_dir = opendir(lang_path); |
| 919 | if (!lang_dir) |
| 920 | continue; |
| 921 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 922 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 923 | __script_root = strdup(script_dirent.d_name); |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 924 | str = (char *)ends_with(__script_root, suffix); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 925 | if (str) { |
| 926 | *str = '\0'; |
| 927 | if (strcmp(__script_root, script_root)) |
| 928 | continue; |
| 929 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
| 930 | lang_path, script_dirent.d_name); |
| 931 | path = strdup(script_path); |
| 932 | free(__script_root); |
| 933 | break; |
| 934 | } |
| 935 | free(__script_root); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | return path; |
| 940 | } |
| 941 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 942 | static bool is_top_script(const char *script_path) |
| 943 | { |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 944 | return ends_with(script_path, "top") == NULL ? false : true; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | static int has_required_arg(char *script_path) |
| 948 | { |
| 949 | struct script_desc *desc; |
| 950 | int n_args = 0; |
| 951 | char *p; |
| 952 | |
| 953 | desc = script_desc__new(NULL); |
| 954 | |
| 955 | if (read_script_info(desc, script_path)) |
| 956 | goto out; |
| 957 | |
| 958 | if (!desc->args) |
| 959 | goto out; |
| 960 | |
| 961 | for (p = desc->args; *p; p++) |
| 962 | if (*p == '<') |
| 963 | n_args++; |
| 964 | out: |
| 965 | script_desc__delete(desc); |
| 966 | |
| 967 | return n_args; |
| 968 | } |
| 969 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 970 | static const char * const script_usage[] = { |
| 971 | "perf script [<options>]", |
| 972 | "perf script [<options>] record <script> [<record-options>] <command>", |
| 973 | "perf script [<options>] report <script> [script-args]", |
| 974 | "perf script [<options>] <script> [<record-options>] <command>", |
| 975 | "perf script [<options>] <top-script> [script-args]", |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 976 | NULL |
| 977 | }; |
| 978 | |
| 979 | static const struct option options[] = { |
| 980 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 981 | "dump raw trace in ASCII"), |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 982 | OPT_INCR('v', "verbose", &verbose, |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 983 | "be more verbose (show symbol address, etc)"), |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 984 | OPT_BOOLEAN('L', "Latency", &latency_format, |
Steven Rostedt | cda4846 | 2009-10-14 15:43:42 -0400 | [diff] [blame] | 985 | "show latency attributes (irqs/preemption disabled, etc)"), |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 986 | OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", |
| 987 | list_available_scripts), |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 988 | OPT_CALLBACK('s', "script", NULL, "name", |
| 989 | "script file name (lang:script name, script name, or *)", |
| 990 | parse_scriptname), |
| 991 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 992 | "generate perf-script.xx script in specified language"), |
Hitoshi Mitake | 408f0d1 | 2010-01-22 22:45:29 +0900 | [diff] [blame] | 993 | OPT_STRING('i', "input", &input_name, "file", |
| 994 | "input file name"), |
Frederic Weisbecker | ffabd99 | 2010-05-27 16:27:47 +0200 | [diff] [blame] | 995 | OPT_BOOLEAN('d', "debug-mode", &debug_mode, |
| 996 | "do various checks like samples ordering and lost events"), |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 997 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
| 998 | "file", "vmlinux pathname"), |
| 999 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, |
| 1000 | "file", "kallsyms pathname"), |
| 1001 | OPT_BOOLEAN('G', "hide-call-graph", &no_callchain, |
| 1002 | "When printing symbols do not display call chain"), |
| 1003 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", |
| 1004 | "Look for files with symbols relative to this directory"), |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1005 | OPT_CALLBACK('f', "fields", NULL, "str", |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame^] | 1006 | "comma separated output fields prepend with 'type:'. Valid types: hw,sw,trace,raw. Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso", |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1007 | parse_output_fields), |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1008 | |
Masami Hiramatsu | 1909629 | 2009-08-21 14:56:03 -0400 | [diff] [blame] | 1009 | OPT_END() |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1010 | }; |
| 1011 | |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 1012 | static bool have_cmd(int argc, const char **argv) |
| 1013 | { |
| 1014 | char **__argv = malloc(sizeof(const char *) * argc); |
| 1015 | |
| 1016 | if (!__argv) |
| 1017 | die("malloc"); |
| 1018 | memcpy(__argv, argv, sizeof(const char *) * argc); |
| 1019 | argc = parse_options(argc, (const char **)__argv, record_options, |
| 1020 | NULL, PARSE_OPT_STOP_AT_NON_OPTION); |
| 1021 | free(__argv); |
| 1022 | |
| 1023 | return argc != 0; |
| 1024 | } |
| 1025 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1026 | int cmd_script(int argc, const char **argv, const char *prefix __used) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1027 | { |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1028 | char *rec_script_path = NULL; |
| 1029 | char *rep_script_path = NULL; |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1030 | struct perf_session *session; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1031 | char *script_path = NULL; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1032 | const char **__argv; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1033 | bool system_wide; |
| 1034 | int i, j, err; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1035 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1036 | setup_scripting(); |
| 1037 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1038 | argc = parse_options(argc, argv, options, script_usage, |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1039 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1040 | |
| 1041 | if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { |
| 1042 | rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); |
| 1043 | if (!rec_script_path) |
| 1044 | return cmd_record(argc, argv, NULL); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1045 | } |
| 1046 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1047 | if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { |
| 1048 | rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); |
| 1049 | if (!rep_script_path) { |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1050 | fprintf(stderr, |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1051 | "Please specify a valid report script" |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1052 | "(see 'perf script -l' for listing)\n"); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1053 | return -1; |
| 1054 | } |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1055 | } |
| 1056 | |
Ben Hutchings | 44e668c | 2010-10-10 16:10:03 +0100 | [diff] [blame] | 1057 | /* make sure PERF_EXEC_PATH is set for scripts */ |
| 1058 | perf_set_argv_exec_path(perf_exec_path()); |
| 1059 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1060 | if (argc && !script_name && !rec_script_path && !rep_script_path) { |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1061 | int live_pipe[2]; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1062 | int rep_args; |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1063 | pid_t pid; |
| 1064 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1065 | rec_script_path = get_script_path(argv[0], RECORD_SUFFIX); |
| 1066 | rep_script_path = get_script_path(argv[0], REPORT_SUFFIX); |
| 1067 | |
| 1068 | if (!rec_script_path && !rep_script_path) { |
| 1069 | fprintf(stderr, " Couldn't find script %s\n\n See perf" |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1070 | " script -l for available scripts.\n", argv[0]); |
| 1071 | usage_with_options(script_usage, options); |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1072 | } |
| 1073 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1074 | if (is_top_script(argv[0])) { |
| 1075 | rep_args = argc - 1; |
| 1076 | } else { |
| 1077 | int rec_args; |
| 1078 | |
| 1079 | rep_args = has_required_arg(rep_script_path); |
| 1080 | rec_args = (argc - 1) - rep_args; |
| 1081 | if (rec_args < 0) { |
| 1082 | fprintf(stderr, " %s script requires options." |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1083 | "\n\n See perf script -l for available " |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1084 | "scripts and options.\n", argv[0]); |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1085 | usage_with_options(script_usage, options); |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1086 | } |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | if (pipe(live_pipe) < 0) { |
| 1090 | perror("failed to create pipe"); |
| 1091 | exit(-1); |
| 1092 | } |
| 1093 | |
| 1094 | pid = fork(); |
| 1095 | if (pid < 0) { |
| 1096 | perror("failed to fork"); |
| 1097 | exit(-1); |
| 1098 | } |
| 1099 | |
| 1100 | if (!pid) { |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1101 | system_wide = true; |
| 1102 | j = 0; |
| 1103 | |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1104 | dup2(live_pipe[1], 1); |
| 1105 | close(live_pipe[0]); |
| 1106 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1107 | if (!is_top_script(argv[0])) |
| 1108 | system_wide = !have_cmd(argc - rep_args, |
| 1109 | &argv[rep_args]); |
| 1110 | |
| 1111 | __argv = malloc((argc + 6) * sizeof(const char *)); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 1112 | if (!__argv) |
| 1113 | die("malloc"); |
| 1114 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1115 | __argv[j++] = "/bin/sh"; |
| 1116 | __argv[j++] = rec_script_path; |
| 1117 | if (system_wide) |
| 1118 | __argv[j++] = "-a"; |
| 1119 | __argv[j++] = "-q"; |
| 1120 | __argv[j++] = "-o"; |
| 1121 | __argv[j++] = "-"; |
| 1122 | for (i = rep_args + 1; i < argc; i++) |
| 1123 | __argv[j++] = argv[i]; |
| 1124 | __argv[j++] = NULL; |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1125 | |
| 1126 | execvp("/bin/sh", (char **)__argv); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 1127 | free(__argv); |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1128 | exit(-1); |
| 1129 | } |
| 1130 | |
| 1131 | dup2(live_pipe[0], 0); |
| 1132 | close(live_pipe[1]); |
| 1133 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1134 | __argv = malloc((argc + 4) * sizeof(const char *)); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 1135 | if (!__argv) |
| 1136 | die("malloc"); |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1137 | j = 0; |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 1138 | __argv[j++] = "/bin/sh"; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1139 | __argv[j++] = rep_script_path; |
| 1140 | for (i = 1; i < rep_args + 1; i++) |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 1141 | __argv[j++] = argv[i]; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1142 | __argv[j++] = "-i"; |
| 1143 | __argv[j++] = "-"; |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 1144 | __argv[j++] = NULL; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1145 | |
| 1146 | execvp("/bin/sh", (char **)__argv); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 1147 | free(__argv); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1148 | exit(-1); |
| 1149 | } |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1150 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1151 | if (rec_script_path) |
| 1152 | script_path = rec_script_path; |
| 1153 | if (rep_script_path) |
| 1154 | script_path = rep_script_path; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1155 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1156 | if (script_path) { |
| 1157 | system_wide = false; |
| 1158 | j = 0; |
| 1159 | |
| 1160 | if (rec_script_path) |
| 1161 | system_wide = !have_cmd(argc - 1, &argv[1]); |
| 1162 | |
| 1163 | __argv = malloc((argc + 2) * sizeof(const char *)); |
| 1164 | if (!__argv) |
| 1165 | die("malloc"); |
| 1166 | __argv[j++] = "/bin/sh"; |
| 1167 | __argv[j++] = script_path; |
| 1168 | if (system_wide) |
| 1169 | __argv[j++] = "-a"; |
| 1170 | for (i = 2; i < argc; i++) |
| 1171 | __argv[j++] = argv[i]; |
| 1172 | __argv[j++] = NULL; |
| 1173 | |
| 1174 | execvp("/bin/sh", (char **)__argv); |
| 1175 | free(__argv); |
| 1176 | exit(-1); |
| 1177 | } |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1178 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1179 | if (symbol__init() < 0) |
| 1180 | return -1; |
Tom Zanussi | cf4fee5 | 2010-03-03 01:04:33 -0600 | [diff] [blame] | 1181 | if (!script_name) |
| 1182 | setup_pager(); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1183 | |
Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 1184 | 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] | 1185 | if (session == NULL) |
| 1186 | return -ENOMEM; |
| 1187 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 1188 | if (!no_callchain) |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1189 | symbol_conf.use_callchain = true; |
| 1190 | else |
| 1191 | symbol_conf.use_callchain = false; |
| 1192 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1193 | if (generate_script_lang) { |
| 1194 | struct stat perf_stat; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1195 | int input; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1196 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1197 | if (output_set_by_user()) { |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1198 | fprintf(stderr, |
| 1199 | "custom fields not supported for generated scripts"); |
| 1200 | return -1; |
| 1201 | } |
| 1202 | |
| 1203 | input = open(input_name, O_RDONLY); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1204 | if (input < 0) { |
| 1205 | perror("failed to open file"); |
| 1206 | exit(-1); |
| 1207 | } |
| 1208 | |
| 1209 | err = fstat(input, &perf_stat); |
| 1210 | if (err < 0) { |
| 1211 | perror("failed to stat file"); |
| 1212 | exit(-1); |
| 1213 | } |
| 1214 | |
| 1215 | if (!perf_stat.st_size) { |
| 1216 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 1217 | exit(0); |
| 1218 | } |
| 1219 | |
| 1220 | scripting_ops = script_spec__lookup(generate_script_lang); |
| 1221 | if (!scripting_ops) { |
| 1222 | fprintf(stderr, "invalid language specifier"); |
| 1223 | return -1; |
| 1224 | } |
| 1225 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1226 | err = scripting_ops->generate_script("perf-script"); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1227 | goto out; |
| 1228 | } |
| 1229 | |
| 1230 | if (script_name) { |
Tom Zanussi | 586bc5c | 2009-12-15 02:53:35 -0600 | [diff] [blame] | 1231 | err = scripting_ops->start_script(script_name, argc, argv); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1232 | if (err) |
| 1233 | goto out; |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1234 | pr_debug("perf script started with script %s\n\n", script_name); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1235 | } |
| 1236 | |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 1237 | |
| 1238 | err = perf_session__check_output_opt(session); |
| 1239 | if (err < 0) |
| 1240 | goto out; |
| 1241 | |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1242 | err = __cmd_script(session); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1243 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1244 | perf_session__delete(session); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1245 | cleanup_scripting(); |
| 1246 | out: |
| 1247 | return err; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1248 | } |