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