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