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