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" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 6 | #include <subcmd/exec-cmd.h> |
Andrea Gelmini | b7eead8 | 2010-08-05 15:51:38 +0200 | [diff] [blame] | 7 | #include "util/header.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 8 | #include <subcmd/parse-options.h> |
Stephane Eranian | fc36f94 | 2015-08-31 18:41:10 +0200 | [diff] [blame] | 9 | #include "util/perf_regs.h" |
Andrea Gelmini | b7eead8 | 2010-08-05 15:51:38 +0200 | [diff] [blame] | 10 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 11 | #include "util/tool.h" |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 12 | #include "util/symbol.h" |
| 13 | #include "util/thread.h" |
Ingo Molnar | cf72344 | 2009-11-28 10:11:00 +0100 | [diff] [blame] | 14 | #include "util/trace-event.h" |
Andrea Gelmini | b7eead8 | 2010-08-05 15:51:38 +0200 | [diff] [blame] | 15 | #include "util/util.h" |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 16 | #include "util/evlist.h" |
| 17 | #include "util/evsel.h" |
Feng Tang | 36385be | 2012-09-07 16:42:24 +0800 | [diff] [blame] | 18 | #include "util/sort.h" |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 19 | #include "util/data.h" |
Adrian Hunter | 7a680eb | 2015-04-09 18:53:56 +0300 | [diff] [blame] | 20 | #include "util/auxtrace.h" |
Jiri Olsa | cfc8874 | 2016-01-05 22:09:06 +0100 | [diff] [blame] | 21 | #include "util/cpumap.h" |
| 22 | #include "util/thread_map.h" |
| 23 | #include "util/stat.h" |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 24 | #include <linux/bitmap.h> |
Arnaldo Carvalho de Melo | 6125cc8 | 2016-04-14 18:15:18 -0300 | [diff] [blame] | 25 | #include <linux/stringify.h> |
Jiri Olsa | cfc8874 | 2016-01-05 22:09:06 +0100 | [diff] [blame] | 26 | #include "asm/bug.h" |
Jiri Olsa | c19ac91 | 2016-02-24 09:46:54 +0100 | [diff] [blame] | 27 | #include "util/mem-events.h" |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 28 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 29 | static char const *script_name; |
| 30 | static char const *generate_script_lang; |
Frederic Weisbecker | ffabd99 | 2010-05-27 16:27:47 +0200 | [diff] [blame] | 31 | static bool debug_mode; |
Frederic Weisbecker | e1889d7 | 2010-04-24 01:55:09 +0200 | [diff] [blame] | 32 | static u64 last_timestamp; |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 33 | static u64 nr_unordered; |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 34 | static bool no_callchain; |
Namhyung Kim | 47390ae | 2013-06-04 14:20:28 +0900 | [diff] [blame] | 35 | static bool latency_format; |
Robert Richter | 317df65 | 2011-11-25 15:05:25 +0100 | [diff] [blame] | 36 | static bool system_wide; |
Adrian Hunter | 400ea6d | 2015-04-09 18:54:05 +0300 | [diff] [blame] | 37 | static bool print_flags; |
Adrian Hunter | 83e1986 | 2015-09-25 16:15:36 +0300 | [diff] [blame] | 38 | static bool nanosecs; |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 39 | static const char *cpu_list; |
| 40 | static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); |
Jiri Olsa | 91a2c3d | 2016-01-05 22:09:07 +0100 | [diff] [blame] | 41 | static struct perf_stat_config stat_config; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 42 | |
Adrian Hunter | 44cbe72 | 2015-09-25 16:15:50 +0300 | [diff] [blame] | 43 | unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH; |
Adrian Hunter | 03cd1fe | 2015-09-25 16:15:49 +0300 | [diff] [blame] | 44 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 45 | enum perf_output_field { |
| 46 | PERF_OUTPUT_COMM = 1U << 0, |
| 47 | PERF_OUTPUT_TID = 1U << 1, |
| 48 | PERF_OUTPUT_PID = 1U << 2, |
| 49 | PERF_OUTPUT_TIME = 1U << 3, |
| 50 | PERF_OUTPUT_CPU = 1U << 4, |
| 51 | PERF_OUTPUT_EVNAME = 1U << 5, |
| 52 | PERF_OUTPUT_TRACE = 1U << 6, |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 53 | PERF_OUTPUT_IP = 1U << 7, |
| 54 | PERF_OUTPUT_SYM = 1U << 8, |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame] | 55 | PERF_OUTPUT_DSO = 1U << 9, |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 56 | PERF_OUTPUT_ADDR = 1U << 10, |
Akihiro Nagai | a978f2a | 2012-01-30 13:43:15 +0900 | [diff] [blame] | 57 | PERF_OUTPUT_SYMOFFSET = 1U << 11, |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 58 | PERF_OUTPUT_SRCLINE = 1U << 12, |
Jiri Olsa | 535aeaae | 2014-08-25 16:45:42 +0200 | [diff] [blame] | 59 | PERF_OUTPUT_PERIOD = 1U << 13, |
Stephane Eranian | fc36f94 | 2015-08-31 18:41:10 +0200 | [diff] [blame] | 60 | PERF_OUTPUT_IREGS = 1U << 14, |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 61 | PERF_OUTPUT_BRSTACK = 1U << 15, |
| 62 | PERF_OUTPUT_BRSTACKSYM = 1U << 16, |
Jiri Olsa | 94ddddf | 2016-02-15 09:34:51 +0100 | [diff] [blame] | 63 | PERF_OUTPUT_DATA_SRC = 1U << 17, |
| 64 | PERF_OUTPUT_WEIGHT = 1U << 18, |
Wang Nan | 30372f0 | 2016-02-24 11:20:45 +0000 | [diff] [blame] | 65 | PERF_OUTPUT_BPF_OUTPUT = 1U << 19, |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | struct output_option { |
| 69 | const char *str; |
| 70 | enum perf_output_field field; |
| 71 | } all_output_options[] = { |
| 72 | {.str = "comm", .field = PERF_OUTPUT_COMM}, |
| 73 | {.str = "tid", .field = PERF_OUTPUT_TID}, |
| 74 | {.str = "pid", .field = PERF_OUTPUT_PID}, |
| 75 | {.str = "time", .field = PERF_OUTPUT_TIME}, |
| 76 | {.str = "cpu", .field = PERF_OUTPUT_CPU}, |
| 77 | {.str = "event", .field = PERF_OUTPUT_EVNAME}, |
| 78 | {.str = "trace", .field = PERF_OUTPUT_TRACE}, |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 79 | {.str = "ip", .field = PERF_OUTPUT_IP}, |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 80 | {.str = "sym", .field = PERF_OUTPUT_SYM}, |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame] | 81 | {.str = "dso", .field = PERF_OUTPUT_DSO}, |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 82 | {.str = "addr", .field = PERF_OUTPUT_ADDR}, |
Akihiro Nagai | a978f2a | 2012-01-30 13:43:15 +0900 | [diff] [blame] | 83 | {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET}, |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 84 | {.str = "srcline", .field = PERF_OUTPUT_SRCLINE}, |
Jiri Olsa | 535aeaae | 2014-08-25 16:45:42 +0200 | [diff] [blame] | 85 | {.str = "period", .field = PERF_OUTPUT_PERIOD}, |
Stephane Eranian | fc36f94 | 2015-08-31 18:41:10 +0200 | [diff] [blame] | 86 | {.str = "iregs", .field = PERF_OUTPUT_IREGS}, |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 87 | {.str = "brstack", .field = PERF_OUTPUT_BRSTACK}, |
| 88 | {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM}, |
Jiri Olsa | 94ddddf | 2016-02-15 09:34:51 +0100 | [diff] [blame] | 89 | {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC}, |
| 90 | {.str = "weight", .field = PERF_OUTPUT_WEIGHT}, |
Wang Nan | 30372f0 | 2016-02-24 11:20:45 +0000 | [diff] [blame] | 91 | {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT}, |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* default set to maintain compatibility with current format */ |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 95 | static struct { |
| 96 | bool user_set; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 97 | bool wildcard_set; |
David Ahern | a6ffaf9 | 2013-08-07 22:50:51 -0400 | [diff] [blame] | 98 | unsigned int print_ip_opts; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 99 | u64 fields; |
| 100 | u64 invalid_fields; |
| 101 | } output[PERF_TYPE_MAX] = { |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 102 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 103 | [PERF_TYPE_HARDWARE] = { |
| 104 | .user_set = false, |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 105 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 106 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | |
| 107 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 108 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
Jiri Olsa | e8564b7 | 2014-08-25 16:45:43 +0200 | [diff] [blame] | 109 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | |
| 110 | PERF_OUTPUT_PERIOD, |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 111 | |
Wang Nan | 30372f0 | 2016-02-24 11:20:45 +0000 | [diff] [blame] | 112 | .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 113 | }, |
| 114 | |
| 115 | [PERF_TYPE_SOFTWARE] = { |
| 116 | .user_set = false, |
| 117 | |
| 118 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | |
| 119 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 120 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
Jiri Olsa | e8564b7 | 2014-08-25 16:45:43 +0200 | [diff] [blame] | 121 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | |
Wang Nan | 30372f0 | 2016-02-24 11:20:45 +0000 | [diff] [blame] | 122 | PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT, |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 123 | |
| 124 | .invalid_fields = PERF_OUTPUT_TRACE, |
| 125 | }, |
| 126 | |
| 127 | [PERF_TYPE_TRACEPOINT] = { |
| 128 | .user_set = false, |
| 129 | |
| 130 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | |
| 131 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | |
Wang Nan | 30372f0 | 2016-02-24 11:20:45 +0000 | [diff] [blame] | 132 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 133 | }, |
Arun Sharma | 0817a6a | 2011-04-14 10:38:18 -0700 | [diff] [blame] | 134 | |
| 135 | [PERF_TYPE_RAW] = { |
| 136 | .user_set = false, |
| 137 | |
| 138 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | |
| 139 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 140 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
Jiri Olsa | e8564b7 | 2014-08-25 16:45:43 +0200 | [diff] [blame] | 141 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | |
Jiri Olsa | ff7b191 | 2016-02-15 09:34:52 +0100 | [diff] [blame] | 142 | PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR | |
| 143 | PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT, |
Arun Sharma | 0817a6a | 2011-04-14 10:38:18 -0700 | [diff] [blame] | 144 | |
Wang Nan | 30372f0 | 2016-02-24 11:20:45 +0000 | [diff] [blame] | 145 | .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, |
Arun Sharma | 0817a6a | 2011-04-14 10:38:18 -0700 | [diff] [blame] | 146 | }, |
Wang Nan | 27cfef0 | 2015-12-08 02:25:43 +0000 | [diff] [blame] | 147 | |
| 148 | [PERF_TYPE_BREAKPOINT] = { |
| 149 | .user_set = false, |
| 150 | |
| 151 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | |
| 152 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | |
| 153 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
| 154 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | |
| 155 | PERF_OUTPUT_PERIOD, |
| 156 | |
Wang Nan | 30372f0 | 2016-02-24 11:20:45 +0000 | [diff] [blame] | 157 | .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, |
Wang Nan | 27cfef0 | 2015-12-08 02:25:43 +0000 | [diff] [blame] | 158 | }, |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 159 | }; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 160 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 161 | static bool output_set_by_user(void) |
| 162 | { |
| 163 | int j; |
| 164 | for (j = 0; j < PERF_TYPE_MAX; ++j) { |
| 165 | if (output[j].user_set) |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | } |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 170 | |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 171 | static const char *output_field2str(enum perf_output_field field) |
| 172 | { |
| 173 | int i, imax = ARRAY_SIZE(all_output_options); |
| 174 | const char *str = ""; |
| 175 | |
| 176 | for (i = 0; i < imax; ++i) { |
| 177 | if (all_output_options[i].field == field) { |
| 178 | str = all_output_options[i].str; |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | return str; |
| 183 | } |
| 184 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 185 | #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 186 | |
Adrian Hunter | 6d5cdd6 | 2015-04-24 22:29:44 +0300 | [diff] [blame] | 187 | static int perf_evsel__do_check_stype(struct perf_evsel *evsel, |
| 188 | u64 sample_type, const char *sample_msg, |
| 189 | enum perf_output_field field, |
| 190 | bool allow_user_set) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 191 | { |
Arnaldo Carvalho de Melo | 5bff01f | 2012-06-12 13:35:44 -0300 | [diff] [blame] | 192 | struct perf_event_attr *attr = &evsel->attr; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 193 | int type = attr->type; |
| 194 | const char *evname; |
| 195 | |
| 196 | if (attr->sample_type & sample_type) |
| 197 | return 0; |
| 198 | |
| 199 | if (output[type].user_set) { |
Adrian Hunter | 6d5cdd6 | 2015-04-24 22:29:44 +0300 | [diff] [blame] | 200 | if (allow_user_set) |
| 201 | return 0; |
Arnaldo Carvalho de Melo | 5bff01f | 2012-06-12 13:35:44 -0300 | [diff] [blame] | 202 | evname = perf_evsel__name(evsel); |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 203 | pr_err("Samples for '%s' event do not have %s attribute set. " |
| 204 | "Cannot print '%s' field.\n", |
| 205 | evname, sample_msg, output_field2str(field)); |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | /* user did not ask for it explicitly so remove from the default list */ |
| 210 | output[type].fields &= ~field; |
Arnaldo Carvalho de Melo | 5bff01f | 2012-06-12 13:35:44 -0300 | [diff] [blame] | 211 | evname = perf_evsel__name(evsel); |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 212 | pr_debug("Samples for '%s' event do not have %s attribute set. " |
| 213 | "Skipping '%s' field.\n", |
| 214 | evname, sample_msg, output_field2str(field)); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Adrian Hunter | 6d5cdd6 | 2015-04-24 22:29:44 +0300 | [diff] [blame] | 219 | static int perf_evsel__check_stype(struct perf_evsel *evsel, |
| 220 | u64 sample_type, const char *sample_msg, |
| 221 | enum perf_output_field field) |
| 222 | { |
| 223 | return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field, |
| 224 | false); |
| 225 | } |
| 226 | |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 227 | static int perf_evsel__check_attr(struct perf_evsel *evsel, |
| 228 | struct perf_session *session) |
| 229 | { |
| 230 | struct perf_event_attr *attr = &evsel->attr; |
Adrian Hunter | 6d5cdd6 | 2015-04-24 22:29:44 +0300 | [diff] [blame] | 231 | bool allow_user_set; |
| 232 | |
Jiri Olsa | e099eba | 2016-01-05 22:09:09 +0100 | [diff] [blame] | 233 | if (perf_header__has_feat(&session->header, HEADER_STAT)) |
| 234 | return 0; |
| 235 | |
Adrian Hunter | 6d5cdd6 | 2015-04-24 22:29:44 +0300 | [diff] [blame] | 236 | allow_user_set = perf_header__has_feat(&session->header, |
| 237 | HEADER_AUXTRACE); |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 238 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 239 | if (PRINT_FIELD(TRACE) && |
| 240 | !perf_session__has_traces(session, "record -R")) |
| 241 | return -EINVAL; |
| 242 | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 243 | if (PRINT_FIELD(IP)) { |
Arnaldo Carvalho de Melo | 5bff01f | 2012-06-12 13:35:44 -0300 | [diff] [blame] | 244 | if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", |
| 245 | PERF_OUTPUT_IP)) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 246 | return -EINVAL; |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 247 | } |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 248 | |
| 249 | if (PRINT_FIELD(ADDR) && |
Adrian Hunter | 6d5cdd6 | 2015-04-24 22:29:44 +0300 | [diff] [blame] | 250 | perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", |
| 251 | PERF_OUTPUT_ADDR, allow_user_set)) |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 252 | return -EINVAL; |
| 253 | |
Jiri Olsa | 94ddddf | 2016-02-15 09:34:51 +0100 | [diff] [blame] | 254 | if (PRINT_FIELD(DATA_SRC) && |
| 255 | perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", |
| 256 | PERF_OUTPUT_DATA_SRC)) |
| 257 | return -EINVAL; |
| 258 | |
| 259 | if (PRINT_FIELD(WEIGHT) && |
| 260 | perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT", |
| 261 | PERF_OUTPUT_WEIGHT)) |
| 262 | return -EINVAL; |
| 263 | |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 264 | if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { |
| 265 | pr_err("Display of symbols requested but neither sample IP nor " |
| 266 | "sample address\nis selected. Hence, no addresses to convert " |
| 267 | "to symbols.\n"); |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | } |
Akihiro Nagai | a978f2a | 2012-01-30 13:43:15 +0900 | [diff] [blame] | 270 | if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) { |
| 271 | pr_err("Display of offsets requested but symbol is not" |
| 272 | "selected.\n"); |
| 273 | return -EINVAL; |
| 274 | } |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 275 | if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { |
| 276 | pr_err("Display of DSO requested but neither sample IP nor " |
| 277 | "sample address\nis selected. Hence, no addresses to convert " |
| 278 | "to DSO.\n"); |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame] | 279 | return -EINVAL; |
| 280 | } |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 281 | if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) { |
| 282 | pr_err("Display of source line number requested but sample IP is not\n" |
| 283 | "selected. Hence, no address to lookup the source line number.\n"); |
| 284 | return -EINVAL; |
| 285 | } |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 286 | |
| 287 | if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) && |
Arnaldo Carvalho de Melo | 5bff01f | 2012-06-12 13:35:44 -0300 | [diff] [blame] | 288 | perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", |
| 289 | PERF_OUTPUT_TID|PERF_OUTPUT_PID)) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 290 | return -EINVAL; |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 291 | |
| 292 | if (PRINT_FIELD(TIME) && |
Arnaldo Carvalho de Melo | 5bff01f | 2012-06-12 13:35:44 -0300 | [diff] [blame] | 293 | perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", |
| 294 | PERF_OUTPUT_TIME)) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 295 | return -EINVAL; |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 296 | |
| 297 | if (PRINT_FIELD(CPU) && |
Adrian Hunter | 6d5cdd6 | 2015-04-24 22:29:44 +0300 | [diff] [blame] | 298 | perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", |
| 299 | PERF_OUTPUT_CPU, allow_user_set)) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 300 | return -EINVAL; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 301 | |
Jiri Olsa | 535aeaae | 2014-08-25 16:45:42 +0200 | [diff] [blame] | 302 | if (PRINT_FIELD(PERIOD) && |
| 303 | perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD", |
| 304 | PERF_OUTPUT_PERIOD)) |
| 305 | return -EINVAL; |
| 306 | |
Stephane Eranian | fc36f94 | 2015-08-31 18:41:10 +0200 | [diff] [blame] | 307 | if (PRINT_FIELD(IREGS) && |
| 308 | perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", |
| 309 | PERF_OUTPUT_IREGS)) |
| 310 | return -EINVAL; |
| 311 | |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
Adrian Hunter | 7ea9572 | 2013-11-01 15:51:30 +0200 | [diff] [blame] | 315 | static void set_print_ip_opts(struct perf_event_attr *attr) |
| 316 | { |
| 317 | unsigned int type = attr->type; |
| 318 | |
| 319 | output[type].print_ip_opts = 0; |
| 320 | if (PRINT_FIELD(IP)) |
Arnaldo Carvalho de Melo | e20ab86 | 2016-04-12 15:16:15 -0300 | [diff] [blame] | 321 | output[type].print_ip_opts |= EVSEL__PRINT_IP; |
Adrian Hunter | 7ea9572 | 2013-11-01 15:51:30 +0200 | [diff] [blame] | 322 | |
| 323 | if (PRINT_FIELD(SYM)) |
Arnaldo Carvalho de Melo | e20ab86 | 2016-04-12 15:16:15 -0300 | [diff] [blame] | 324 | output[type].print_ip_opts |= EVSEL__PRINT_SYM; |
Adrian Hunter | 7ea9572 | 2013-11-01 15:51:30 +0200 | [diff] [blame] | 325 | |
| 326 | if (PRINT_FIELD(DSO)) |
Arnaldo Carvalho de Melo | e20ab86 | 2016-04-12 15:16:15 -0300 | [diff] [blame] | 327 | output[type].print_ip_opts |= EVSEL__PRINT_DSO; |
Adrian Hunter | 7ea9572 | 2013-11-01 15:51:30 +0200 | [diff] [blame] | 328 | |
| 329 | if (PRINT_FIELD(SYMOFFSET)) |
Arnaldo Carvalho de Melo | e20ab86 | 2016-04-12 15:16:15 -0300 | [diff] [blame] | 330 | output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET; |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 331 | |
| 332 | if (PRINT_FIELD(SRCLINE)) |
Arnaldo Carvalho de Melo | e20ab86 | 2016-04-12 15:16:15 -0300 | [diff] [blame] | 333 | output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE; |
Adrian Hunter | 7ea9572 | 2013-11-01 15:51:30 +0200 | [diff] [blame] | 334 | } |
| 335 | |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 336 | /* |
| 337 | * verify all user requested events exist and the samples |
| 338 | * have the expected data |
| 339 | */ |
| 340 | static int perf_session__check_output_opt(struct perf_session *session) |
| 341 | { |
He Kuang | 40f20e5 | 2016-05-16 04:51:19 +0000 | [diff] [blame] | 342 | unsigned int j; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 343 | struct perf_evsel *evsel; |
| 344 | |
| 345 | for (j = 0; j < PERF_TYPE_MAX; ++j) { |
| 346 | evsel = perf_session__find_first_evtype(session, j); |
| 347 | |
| 348 | /* |
| 349 | * even if fields is set to 0 (ie., show nothing) event must |
| 350 | * exist if user explicitly includes it on the command line |
| 351 | */ |
| 352 | if (!evsel && output[j].user_set && !output[j].wildcard_set) { |
| 353 | pr_err("%s events do not exist. " |
| 354 | "Remove corresponding -f option to proceed.\n", |
| 355 | event_type(j)); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | if (evsel && output[j].fields && |
| 360 | perf_evsel__check_attr(evsel, session)) |
| 361 | return -1; |
David Ahern | a6ffaf9 | 2013-08-07 22:50:51 -0400 | [diff] [blame] | 362 | |
| 363 | if (evsel == NULL) |
| 364 | continue; |
| 365 | |
Adrian Hunter | 7ea9572 | 2013-11-01 15:51:30 +0200 | [diff] [blame] | 366 | set_print_ip_opts(&evsel->attr); |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Adrian Hunter | 98526ee | 2014-07-31 09:00:59 +0300 | [diff] [blame] | 369 | if (!no_callchain) { |
| 370 | bool use_callchain = false; |
| 371 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 372 | evlist__for_each_entry(session->evlist, evsel) { |
Adrian Hunter | 98526ee | 2014-07-31 09:00:59 +0300 | [diff] [blame] | 373 | if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 374 | use_callchain = true; |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | if (!use_callchain) |
| 379 | symbol_conf.use_callchain = false; |
| 380 | } |
| 381 | |
David Ahern | 80b8b49 | 2013-11-19 21:07:37 -0700 | [diff] [blame] | 382 | /* |
| 383 | * set default for tracepoints to print symbols only |
| 384 | * if callchains are present |
| 385 | */ |
| 386 | if (symbol_conf.use_callchain && |
| 387 | !output[PERF_TYPE_TRACEPOINT].user_set) { |
| 388 | struct perf_event_attr *attr; |
| 389 | |
| 390 | j = PERF_TYPE_TRACEPOINT; |
David Ahern | 80b8b49 | 2013-11-19 21:07:37 -0700 | [diff] [blame] | 391 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 392 | evlist__for_each_entry(session->evlist, evsel) { |
He Kuang | 40f20e5 | 2016-05-16 04:51:19 +0000 | [diff] [blame] | 393 | if (evsel->attr.type != j) |
| 394 | continue; |
David Ahern | 80b8b49 | 2013-11-19 21:07:37 -0700 | [diff] [blame] | 395 | |
He Kuang | 40f20e5 | 2016-05-16 04:51:19 +0000 | [diff] [blame] | 396 | attr = &evsel->attr; |
| 397 | |
| 398 | if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) { |
| 399 | output[j].fields |= PERF_OUTPUT_IP; |
| 400 | output[j].fields |= PERF_OUTPUT_SYM; |
| 401 | output[j].fields |= PERF_OUTPUT_DSO; |
| 402 | set_print_ip_opts(attr); |
| 403 | goto out; |
| 404 | } |
David Ahern | 80b8b49 | 2013-11-19 21:07:37 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | out: |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 409 | return 0; |
| 410 | } |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 411 | |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 412 | static void print_sample_iregs(struct perf_sample *sample, |
Stephane Eranian | fc36f94 | 2015-08-31 18:41:10 +0200 | [diff] [blame] | 413 | struct perf_event_attr *attr) |
| 414 | { |
| 415 | struct regs_dump *regs = &sample->intr_regs; |
| 416 | uint64_t mask = attr->sample_regs_intr; |
| 417 | unsigned i = 0, r; |
| 418 | |
| 419 | if (!regs) |
| 420 | return; |
| 421 | |
| 422 | for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) { |
| 423 | u64 val = regs->regs[i++]; |
| 424 | printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val); |
| 425 | } |
| 426 | } |
| 427 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 428 | static void print_sample_start(struct perf_sample *sample, |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 429 | struct thread *thread, |
Arnaldo Carvalho de Melo | 5bff01f | 2012-06-12 13:35:44 -0300 | [diff] [blame] | 430 | struct perf_evsel *evsel) |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 431 | { |
Arnaldo Carvalho de Melo | 5bff01f | 2012-06-12 13:35:44 -0300 | [diff] [blame] | 432 | struct perf_event_attr *attr = &evsel->attr; |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 433 | unsigned long secs; |
| 434 | unsigned long usecs; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 435 | unsigned long long nsecs; |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 436 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 437 | if (PRINT_FIELD(COMM)) { |
| 438 | if (latency_format) |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 439 | printf("%8.8s ", thread__comm_str(thread)); |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 440 | else if (PRINT_FIELD(IP) && symbol_conf.use_callchain) |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 441 | printf("%s ", thread__comm_str(thread)); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 442 | else |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 443 | printf("%16s ", thread__comm_str(thread)); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 444 | } |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 445 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 446 | if (PRINT_FIELD(PID) && PRINT_FIELD(TID)) |
| 447 | printf("%5d/%-5d ", sample->pid, sample->tid); |
| 448 | else if (PRINT_FIELD(PID)) |
| 449 | printf("%5d ", sample->pid); |
| 450 | else if (PRINT_FIELD(TID)) |
| 451 | printf("%5d ", sample->tid); |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 452 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 453 | if (PRINT_FIELD(CPU)) { |
| 454 | if (latency_format) |
| 455 | printf("%3d ", sample->cpu); |
| 456 | else |
| 457 | printf("[%03d] ", sample->cpu); |
| 458 | } |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 459 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 460 | if (PRINT_FIELD(TIME)) { |
| 461 | nsecs = sample->time; |
| 462 | secs = nsecs / NSECS_PER_SEC; |
| 463 | nsecs -= secs * NSECS_PER_SEC; |
| 464 | usecs = nsecs / NSECS_PER_USEC; |
Adrian Hunter | 83e1986 | 2015-09-25 16:15:36 +0300 | [diff] [blame] | 465 | if (nanosecs) |
| 466 | printf("%5lu.%09llu: ", secs, nsecs); |
| 467 | else |
| 468 | printf("%5lu.%06lu: ", secs, usecs); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 469 | } |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 472 | static inline char |
| 473 | mispred_str(struct branch_entry *br) |
| 474 | { |
| 475 | if (!(br->flags.mispred || br->flags.predicted)) |
| 476 | return '-'; |
| 477 | |
| 478 | return br->flags.predicted ? 'P' : 'M'; |
| 479 | } |
| 480 | |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 481 | static void print_sample_brstack(struct perf_sample *sample) |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 482 | { |
| 483 | struct branch_stack *br = sample->branch_stack; |
| 484 | u64 i; |
| 485 | |
| 486 | if (!(br && br->nr)) |
| 487 | return; |
| 488 | |
| 489 | for (i = 0; i < br->nr; i++) { |
| 490 | printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ", |
| 491 | br->entries[i].from, |
| 492 | br->entries[i].to, |
| 493 | mispred_str( br->entries + i), |
| 494 | br->entries[i].flags.in_tx? 'X' : '-', |
| 495 | br->entries[i].flags.abort? 'A' : '-', |
| 496 | br->entries[i].flags.cycles); |
| 497 | } |
| 498 | } |
| 499 | |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 500 | static void print_sample_brstacksym(struct perf_sample *sample, |
| 501 | struct thread *thread) |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 502 | { |
| 503 | struct branch_stack *br = sample->branch_stack; |
| 504 | struct addr_location alf, alt; |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 505 | u64 i, from, to; |
| 506 | |
| 507 | if (!(br && br->nr)) |
| 508 | return; |
| 509 | |
| 510 | for (i = 0; i < br->nr; i++) { |
| 511 | |
| 512 | memset(&alf, 0, sizeof(alf)); |
| 513 | memset(&alt, 0, sizeof(alt)); |
| 514 | from = br->entries[i].from; |
| 515 | to = br->entries[i].to; |
| 516 | |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 517 | thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf); |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 518 | if (alf.map) |
| 519 | alf.sym = map__find_symbol(alf.map, alf.addr, NULL); |
| 520 | |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 521 | thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt); |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 522 | if (alt.map) |
| 523 | alt.sym = map__find_symbol(alt.map, alt.addr, NULL); |
| 524 | |
| 525 | symbol__fprintf_symname_offs(alf.sym, &alf, stdout); |
| 526 | putchar('/'); |
| 527 | symbol__fprintf_symname_offs(alt.sym, &alt, stdout); |
| 528 | printf("/%c/%c/%c/%d ", |
| 529 | mispred_str( br->entries + i), |
| 530 | br->entries[i].flags.in_tx? 'X' : '-', |
| 531 | br->entries[i].flags.abort? 'A' : '-', |
| 532 | br->entries[i].flags.cycles); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 537 | static void print_sample_addr(struct perf_sample *sample, |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 538 | struct thread *thread, |
| 539 | struct perf_event_attr *attr) |
| 540 | { |
| 541 | struct addr_location al; |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 542 | |
| 543 | printf("%16" PRIx64, sample->addr); |
| 544 | |
| 545 | if (!sample_addr_correlates_sym(attr)) |
| 546 | return; |
| 547 | |
Arnaldo Carvalho de Melo | c2740a8 | 2016-03-22 18:44:46 -0300 | [diff] [blame] | 548 | thread__resolve(thread, &al, sample); |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 549 | |
| 550 | if (PRINT_FIELD(SYM)) { |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 551 | printf(" "); |
Akihiro Nagai | a978f2a | 2012-01-30 13:43:15 +0900 | [diff] [blame] | 552 | if (PRINT_FIELD(SYMOFFSET)) |
| 553 | symbol__fprintf_symname_offs(al.sym, &al, stdout); |
| 554 | else |
| 555 | symbol__fprintf_symname(al.sym, stdout); |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | if (PRINT_FIELD(DSO)) { |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 559 | printf(" ("); |
| 560 | map__fprintf_dsoname(al.map, stdout); |
| 561 | printf(")"); |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 565 | static void print_sample_bts(struct perf_sample *sample, |
Akihiro Nagai | 9558259 | 2012-01-30 13:43:09 +0900 | [diff] [blame] | 566 | struct perf_evsel *evsel, |
Adrian Hunter | a2cb3cf | 2013-12-04 16:16:36 +0200 | [diff] [blame] | 567 | struct thread *thread, |
| 568 | struct addr_location *al) |
Akihiro Nagai | 9558259 | 2012-01-30 13:43:09 +0900 | [diff] [blame] | 569 | { |
| 570 | struct perf_event_attr *attr = &evsel->attr; |
Adrian Hunter | 8066be5f | 2014-07-22 16:17:15 +0300 | [diff] [blame] | 571 | bool print_srcline_last = false; |
Akihiro Nagai | 9558259 | 2012-01-30 13:43:09 +0900 | [diff] [blame] | 572 | |
| 573 | /* print branch_from information */ |
| 574 | if (PRINT_FIELD(IP)) { |
Adrian Hunter | 8066be5f | 2014-07-22 16:17:15 +0300 | [diff] [blame] | 575 | unsigned int print_opts = output[attr->type].print_ip_opts; |
Chris Phlipot | e557b67 | 2016-04-19 19:32:11 -0700 | [diff] [blame] | 576 | struct callchain_cursor *cursor = NULL; |
Adrian Hunter | 8066be5f | 2014-07-22 16:17:15 +0300 | [diff] [blame] | 577 | |
Arnaldo Carvalho de Melo | 6f73673 | 2016-04-14 17:45:51 -0300 | [diff] [blame] | 578 | if (symbol_conf.use_callchain && sample->callchain && |
Chris Phlipot | e557b67 | 2016-04-19 19:32:11 -0700 | [diff] [blame] | 579 | thread__resolve_callchain(al->thread, &callchain_cursor, evsel, |
Arnaldo Carvalho de Melo | 6f73673 | 2016-04-14 17:45:51 -0300 | [diff] [blame] | 580 | sample, NULL, NULL, scripting_max_stack) == 0) |
Chris Phlipot | e557b67 | 2016-04-19 19:32:11 -0700 | [diff] [blame] | 581 | cursor = &callchain_cursor; |
Arnaldo Carvalho de Melo | 6f73673 | 2016-04-14 17:45:51 -0300 | [diff] [blame] | 582 | |
| 583 | if (cursor == NULL) { |
| 584 | putchar(' '); |
Arnaldo Carvalho de Melo | e20ab86 | 2016-04-12 15:16:15 -0300 | [diff] [blame] | 585 | if (print_opts & EVSEL__PRINT_SRCLINE) { |
Adrian Hunter | 8066be5f | 2014-07-22 16:17:15 +0300 | [diff] [blame] | 586 | print_srcline_last = true; |
Arnaldo Carvalho de Melo | e20ab86 | 2016-04-12 15:16:15 -0300 | [diff] [blame] | 587 | print_opts &= ~EVSEL__PRINT_SRCLINE; |
Adrian Hunter | 8066be5f | 2014-07-22 16:17:15 +0300 | [diff] [blame] | 588 | } |
Arnaldo Carvalho de Melo | 6f73673 | 2016-04-14 17:45:51 -0300 | [diff] [blame] | 589 | } else |
| 590 | putchar('\n'); |
| 591 | |
| 592 | sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout); |
Akihiro Nagai | 9558259 | 2012-01-30 13:43:09 +0900 | [diff] [blame] | 593 | } |
| 594 | |
Akihiro Nagai | 9558259 | 2012-01-30 13:43:09 +0900 | [diff] [blame] | 595 | /* print branch_to information */ |
Adrian Hunter | 243be3d | 2013-10-18 15:29:14 +0300 | [diff] [blame] | 596 | if (PRINT_FIELD(ADDR) || |
| 597 | ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) && |
Adrian Hunter | 578bea4 | 2014-07-22 16:17:16 +0300 | [diff] [blame] | 598 | !output[attr->type].user_set)) { |
| 599 | printf(" => "); |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 600 | print_sample_addr(sample, thread, attr); |
Adrian Hunter | 578bea4 | 2014-07-22 16:17:16 +0300 | [diff] [blame] | 601 | } |
Akihiro Nagai | 9558259 | 2012-01-30 13:43:09 +0900 | [diff] [blame] | 602 | |
Adrian Hunter | 8066be5f | 2014-07-22 16:17:15 +0300 | [diff] [blame] | 603 | if (print_srcline_last) |
| 604 | map__fprintf_srcline(al->map, al->addr, "\n ", stdout); |
| 605 | |
Akihiro Nagai | 9558259 | 2012-01-30 13:43:09 +0900 | [diff] [blame] | 606 | printf("\n"); |
| 607 | } |
| 608 | |
Adrian Hunter | 055cd33 | 2016-06-23 16:40:56 +0300 | [diff] [blame^] | 609 | static struct { |
| 610 | u32 flags; |
| 611 | const char *name; |
| 612 | } sample_flags[] = { |
| 613 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"}, |
| 614 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"}, |
| 615 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"}, |
| 616 | {PERF_IP_FLAG_BRANCH, "jmp"}, |
| 617 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"}, |
| 618 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"}, |
| 619 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"}, |
| 620 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"}, |
| 621 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"}, |
| 622 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"}, |
| 623 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"}, |
| 624 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"}, |
| 625 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"}, |
| 626 | {0, NULL} |
| 627 | }; |
| 628 | |
Adrian Hunter | 400ea6d | 2015-04-09 18:54:05 +0300 | [diff] [blame] | 629 | static void print_sample_flags(u32 flags) |
| 630 | { |
| 631 | const char *chars = PERF_IP_FLAG_CHARS; |
| 632 | const int n = strlen(PERF_IP_FLAG_CHARS); |
Adrian Hunter | 055cd33 | 2016-06-23 16:40:56 +0300 | [diff] [blame^] | 633 | bool in_tx = flags & PERF_IP_FLAG_IN_TX; |
| 634 | const char *name = NULL; |
Adrian Hunter | 400ea6d | 2015-04-09 18:54:05 +0300 | [diff] [blame] | 635 | char str[33]; |
| 636 | int i, pos = 0; |
| 637 | |
Adrian Hunter | 055cd33 | 2016-06-23 16:40:56 +0300 | [diff] [blame^] | 638 | for (i = 0; sample_flags[i].name ; i++) { |
| 639 | if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) { |
| 640 | name = sample_flags[i].name; |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | |
Adrian Hunter | 400ea6d | 2015-04-09 18:54:05 +0300 | [diff] [blame] | 645 | for (i = 0; i < n; i++, flags >>= 1) { |
| 646 | if (flags & 1) |
| 647 | str[pos++] = chars[i]; |
| 648 | } |
| 649 | for (; i < 32; i++, flags >>= 1) { |
| 650 | if (flags & 1) |
| 651 | str[pos++] = '?'; |
| 652 | } |
| 653 | str[pos] = 0; |
Adrian Hunter | 055cd33 | 2016-06-23 16:40:56 +0300 | [diff] [blame^] | 654 | |
| 655 | if (name) |
| 656 | printf(" %-7s%4s ", name, in_tx ? "(x)" : ""); |
| 657 | else |
| 658 | printf(" %-11s ", str); |
Adrian Hunter | 400ea6d | 2015-04-09 18:54:05 +0300 | [diff] [blame] | 659 | } |
| 660 | |
Wang Nan | 30372f0 | 2016-02-24 11:20:45 +0000 | [diff] [blame] | 661 | struct printer_data { |
| 662 | int line_no; |
| 663 | bool hit_nul; |
| 664 | bool is_printable; |
| 665 | }; |
| 666 | |
| 667 | static void |
| 668 | print_sample_bpf_output_printer(enum binary_printer_ops op, |
| 669 | unsigned int val, |
| 670 | void *extra) |
| 671 | { |
| 672 | unsigned char ch = (unsigned char)val; |
| 673 | struct printer_data *printer_data = extra; |
| 674 | |
| 675 | switch (op) { |
| 676 | case BINARY_PRINT_DATA_BEGIN: |
| 677 | printf("\n"); |
| 678 | break; |
| 679 | case BINARY_PRINT_LINE_BEGIN: |
| 680 | printf("%17s", !printer_data->line_no ? "BPF output:" : |
| 681 | " "); |
| 682 | break; |
| 683 | case BINARY_PRINT_ADDR: |
| 684 | printf(" %04x:", val); |
| 685 | break; |
| 686 | case BINARY_PRINT_NUM_DATA: |
| 687 | printf(" %02x", val); |
| 688 | break; |
| 689 | case BINARY_PRINT_NUM_PAD: |
| 690 | printf(" "); |
| 691 | break; |
| 692 | case BINARY_PRINT_SEP: |
| 693 | printf(" "); |
| 694 | break; |
| 695 | case BINARY_PRINT_CHAR_DATA: |
| 696 | if (printer_data->hit_nul && ch) |
| 697 | printer_data->is_printable = false; |
| 698 | |
| 699 | if (!isprint(ch)) { |
| 700 | printf("%c", '.'); |
| 701 | |
| 702 | if (!printer_data->is_printable) |
| 703 | break; |
| 704 | |
| 705 | if (ch == '\0') |
| 706 | printer_data->hit_nul = true; |
| 707 | else |
| 708 | printer_data->is_printable = false; |
| 709 | } else { |
| 710 | printf("%c", ch); |
| 711 | } |
| 712 | break; |
| 713 | case BINARY_PRINT_CHAR_PAD: |
| 714 | printf(" "); |
| 715 | break; |
| 716 | case BINARY_PRINT_LINE_END: |
| 717 | printf("\n"); |
| 718 | printer_data->line_no++; |
| 719 | break; |
| 720 | case BINARY_PRINT_DATA_END: |
| 721 | default: |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | static void print_sample_bpf_output(struct perf_sample *sample) |
| 727 | { |
| 728 | unsigned int nr_bytes = sample->raw_size; |
| 729 | struct printer_data printer_data = {0, false, true}; |
| 730 | |
| 731 | print_binary(sample->raw_data, nr_bytes, 8, |
| 732 | print_sample_bpf_output_printer, &printer_data); |
| 733 | |
| 734 | if (printer_data.is_printable && printer_data.hit_nul) |
| 735 | printf("%17s \"%s\"\n", "BPF string:", |
| 736 | (char *)(sample->raw_data)); |
| 737 | } |
| 738 | |
Jiri Olsa | 809e942 | 2015-11-26 18:55:21 +0100 | [diff] [blame] | 739 | struct perf_script { |
| 740 | struct perf_tool tool; |
| 741 | struct perf_session *session; |
| 742 | bool show_task_events; |
| 743 | bool show_mmap_events; |
| 744 | bool show_switch_events; |
Jiri Olsa | cfc8874 | 2016-01-05 22:09:06 +0100 | [diff] [blame] | 745 | bool allocated; |
| 746 | struct cpu_map *cpus; |
| 747 | struct thread_map *threads; |
Jiri Olsa | 9cdbc40 | 2016-01-07 10:14:05 +0100 | [diff] [blame] | 748 | int name_width; |
Jiri Olsa | 809e942 | 2015-11-26 18:55:21 +0100 | [diff] [blame] | 749 | }; |
| 750 | |
Jiri Olsa | 9cdbc40 | 2016-01-07 10:14:05 +0100 | [diff] [blame] | 751 | static int perf_evlist__max_name_len(struct perf_evlist *evlist) |
| 752 | { |
| 753 | struct perf_evsel *evsel; |
| 754 | int max = 0; |
| 755 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 756 | evlist__for_each_entry(evlist, evsel) { |
Jiri Olsa | 9cdbc40 | 2016-01-07 10:14:05 +0100 | [diff] [blame] | 757 | int len = strlen(perf_evsel__name(evsel)); |
| 758 | |
| 759 | max = MAX(len, max); |
| 760 | } |
| 761 | |
| 762 | return max; |
| 763 | } |
| 764 | |
Jiri Olsa | c19ac91 | 2016-02-24 09:46:54 +0100 | [diff] [blame] | 765 | static size_t data_src__printf(u64 data_src) |
| 766 | { |
| 767 | struct mem_info mi = { .data_src.val = data_src }; |
| 768 | char decode[100]; |
| 769 | char out[100]; |
| 770 | static int maxlen; |
| 771 | int len; |
| 772 | |
| 773 | perf_script__meminfo_scnprintf(decode, 100, &mi); |
| 774 | |
| 775 | len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode); |
| 776 | if (maxlen < len) |
| 777 | maxlen = len; |
| 778 | |
| 779 | return printf("%-*s", maxlen, out); |
| 780 | } |
| 781 | |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 782 | static void process_event(struct perf_script *script, |
Jiri Olsa | 809e942 | 2015-11-26 18:55:21 +0100 | [diff] [blame] | 783 | struct perf_sample *sample, struct perf_evsel *evsel, |
| 784 | struct addr_location *al) |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 785 | { |
Arnaldo Carvalho de Melo | f9d5d54 | 2015-04-01 13:29:25 -0300 | [diff] [blame] | 786 | struct thread *thread = al->thread; |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 787 | struct perf_event_attr *attr = &evsel->attr; |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 788 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 789 | if (output[attr->type].fields == 0) |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 790 | return; |
| 791 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 792 | print_sample_start(sample, thread, evsel); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 793 | |
Jiri Olsa | 535aeaae | 2014-08-25 16:45:42 +0200 | [diff] [blame] | 794 | if (PRINT_FIELD(PERIOD)) |
| 795 | printf("%10" PRIu64 " ", sample->period); |
| 796 | |
Namhyung Kim | e944d3d | 2013-11-18 14:34:52 +0900 | [diff] [blame] | 797 | if (PRINT_FIELD(EVNAME)) { |
| 798 | const char *evname = perf_evsel__name(evsel); |
Jiri Olsa | 9cdbc40 | 2016-01-07 10:14:05 +0100 | [diff] [blame] | 799 | |
| 800 | if (!script->name_width) |
| 801 | script->name_width = perf_evlist__max_name_len(script->session->evlist); |
| 802 | |
| 803 | printf("%*s: ", script->name_width, |
| 804 | evname ? evname : "[unknown]"); |
Namhyung Kim | e944d3d | 2013-11-18 14:34:52 +0900 | [diff] [blame] | 805 | } |
| 806 | |
Adrian Hunter | 400ea6d | 2015-04-09 18:54:05 +0300 | [diff] [blame] | 807 | if (print_flags) |
| 808 | print_sample_flags(sample->flags); |
| 809 | |
Akihiro Nagai | 9558259 | 2012-01-30 13:43:09 +0900 | [diff] [blame] | 810 | if (is_bts_event(attr)) { |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 811 | print_sample_bts(sample, evsel, thread, al); |
Akihiro Nagai | 9558259 | 2012-01-30 13:43:09 +0900 | [diff] [blame] | 812 | return; |
| 813 | } |
| 814 | |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 815 | if (PRINT_FIELD(TRACE)) |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 816 | event_format__print(evsel->tp_format, sample->cpu, |
| 817 | sample->raw_data, sample->raw_size); |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 818 | if (PRINT_FIELD(ADDR)) |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 819 | print_sample_addr(sample, thread, attr); |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 820 | |
Jiri Olsa | 94ddddf | 2016-02-15 09:34:51 +0100 | [diff] [blame] | 821 | if (PRINT_FIELD(DATA_SRC)) |
Jiri Olsa | c19ac91 | 2016-02-24 09:46:54 +0100 | [diff] [blame] | 822 | data_src__printf(sample->data_src); |
Jiri Olsa | 94ddddf | 2016-02-15 09:34:51 +0100 | [diff] [blame] | 823 | |
| 824 | if (PRINT_FIELD(WEIGHT)) |
| 825 | printf("%16" PRIu64, sample->weight); |
| 826 | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 827 | if (PRINT_FIELD(IP)) { |
Chris Phlipot | e557b67 | 2016-04-19 19:32:11 -0700 | [diff] [blame] | 828 | struct callchain_cursor *cursor = NULL; |
David Ahern | a6ffaf9 | 2013-08-07 22:50:51 -0400 | [diff] [blame] | 829 | |
Arnaldo Carvalho de Melo | 9223152 | 2016-04-18 11:31:46 -0300 | [diff] [blame] | 830 | if (symbol_conf.use_callchain && sample->callchain && |
Chris Phlipot | e557b67 | 2016-04-19 19:32:11 -0700 | [diff] [blame] | 831 | thread__resolve_callchain(al->thread, &callchain_cursor, evsel, |
Arnaldo Carvalho de Melo | 6f73673 | 2016-04-14 17:45:51 -0300 | [diff] [blame] | 832 | sample, NULL, NULL, scripting_max_stack) == 0) |
Chris Phlipot | e557b67 | 2016-04-19 19:32:11 -0700 | [diff] [blame] | 833 | cursor = &callchain_cursor; |
Arnaldo Carvalho de Melo | 6f73673 | 2016-04-14 17:45:51 -0300 | [diff] [blame] | 834 | |
| 835 | putchar(cursor ? '\n' : ' '); |
| 836 | sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout); |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Stephane Eranian | fc36f94 | 2015-08-31 18:41:10 +0200 | [diff] [blame] | 839 | if (PRINT_FIELD(IREGS)) |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 840 | print_sample_iregs(sample, attr); |
Stephane Eranian | fc36f94 | 2015-08-31 18:41:10 +0200 | [diff] [blame] | 841 | |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 842 | if (PRINT_FIELD(BRSTACK)) |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 843 | print_sample_brstack(sample); |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 844 | else if (PRINT_FIELD(BRSTACKSYM)) |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 845 | print_sample_brstacksym(sample, thread); |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 846 | |
Wang Nan | 30372f0 | 2016-02-24 11:20:45 +0000 | [diff] [blame] | 847 | if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT)) |
| 848 | print_sample_bpf_output(sample); |
| 849 | |
David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 850 | printf("\n"); |
David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 853 | static struct scripting_ops *scripting_ops; |
| 854 | |
Jiri Olsa | 36e33c5 | 2016-01-06 11:49:56 +0100 | [diff] [blame] | 855 | static void __process_stat(struct perf_evsel *counter, u64 tstamp) |
| 856 | { |
| 857 | int nthreads = thread_map__nr(counter->threads); |
| 858 | int ncpus = perf_evsel__nr_cpus(counter); |
| 859 | int cpu, thread; |
| 860 | static int header_printed; |
| 861 | |
| 862 | if (counter->system_wide) |
| 863 | nthreads = 1; |
| 864 | |
| 865 | if (!header_printed) { |
| 866 | printf("%3s %8s %15s %15s %15s %15s %s\n", |
| 867 | "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT"); |
| 868 | header_printed = 1; |
| 869 | } |
| 870 | |
| 871 | for (thread = 0; thread < nthreads; thread++) { |
| 872 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 873 | struct perf_counts_values *counts; |
| 874 | |
| 875 | counts = perf_counts(counter->counts, cpu, thread); |
| 876 | |
| 877 | printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n", |
| 878 | counter->cpus->map[cpu], |
| 879 | thread_map__pid(counter->threads, thread), |
| 880 | counts->val, |
| 881 | counts->ena, |
| 882 | counts->run, |
| 883 | tstamp, |
| 884 | perf_evsel__name(counter)); |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
Jiri Olsa | e099eba | 2016-01-05 22:09:09 +0100 | [diff] [blame] | 889 | static void process_stat(struct perf_evsel *counter, u64 tstamp) |
| 890 | { |
| 891 | if (scripting_ops && scripting_ops->process_stat) |
| 892 | scripting_ops->process_stat(&stat_config, counter, tstamp); |
Jiri Olsa | 36e33c5 | 2016-01-06 11:49:56 +0100 | [diff] [blame] | 893 | else |
| 894 | __process_stat(counter, tstamp); |
Jiri Olsa | e099eba | 2016-01-05 22:09:09 +0100 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static void process_stat_interval(u64 tstamp) |
| 898 | { |
| 899 | if (scripting_ops && scripting_ops->process_stat_interval) |
| 900 | scripting_ops->process_stat_interval(tstamp); |
| 901 | } |
| 902 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 903 | static void setup_scripting(void) |
| 904 | { |
Tom Zanussi | 16c632d | 2009-11-25 01:15:48 -0600 | [diff] [blame] | 905 | setup_perl_scripting(); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 906 | setup_python_scripting(); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 907 | } |
| 908 | |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 909 | static int flush_scripting(void) |
| 910 | { |
Jiri Olsa | 2aaecfc | 2015-11-26 14:55:23 +0100 | [diff] [blame] | 911 | return scripting_ops ? scripting_ops->flush_script() : 0; |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 912 | } |
| 913 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 914 | static int cleanup_scripting(void) |
| 915 | { |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 916 | pr_debug("\nperf script stopped\n"); |
Tom Zanussi | 3824a4e | 2010-05-09 23:46:57 -0500 | [diff] [blame] | 917 | |
Jiri Olsa | 2aaecfc | 2015-11-26 14:55:23 +0100 | [diff] [blame] | 918 | return scripting_ops ? scripting_ops->stop_script() : 0; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 919 | } |
| 920 | |
Jiri Olsa | 809e942 | 2015-11-26 18:55:21 +0100 | [diff] [blame] | 921 | static int process_sample_event(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 922 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 923 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 924 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 925 | struct machine *machine) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 926 | { |
Jiri Olsa | 809e942 | 2015-11-26 18:55:21 +0100 | [diff] [blame] | 927 | struct perf_script *scr = container_of(tool, struct perf_script, tool); |
David Ahern | e7984b7 | 2011-11-21 10:02:52 -0700 | [diff] [blame] | 928 | struct addr_location al; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 929 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 930 | if (debug_mode) { |
| 931 | if (sample->time < last_timestamp) { |
| 932 | pr_err("Samples misordered, previous: %" PRIu64 |
| 933 | " this: %" PRIu64 "\n", last_timestamp, |
| 934 | sample->time); |
| 935 | nr_unordered++; |
Frederic Weisbecker | e1889d7 | 2010-04-24 01:55:09 +0200 | [diff] [blame] | 936 | } |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 937 | last_timestamp = sample->time; |
| 938 | return 0; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 939 | } |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 940 | |
Arnaldo Carvalho de Melo | bb3eb56 | 2016-03-22 18:39:09 -0300 | [diff] [blame] | 941 | if (machine__resolve(machine, &al, sample) < 0) { |
David Ahern | e7984b7 | 2011-11-21 10:02:52 -0700 | [diff] [blame] | 942 | pr_err("problem processing %d event, skipping it.\n", |
| 943 | event->header.type); |
| 944 | return -1; |
| 945 | } |
| 946 | |
| 947 | if (al.filtered) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 948 | goto out_put; |
David Ahern | e7984b7 | 2011-11-21 10:02:52 -0700 | [diff] [blame] | 949 | |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 950 | if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 951 | goto out_put; |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 952 | |
Jiri Olsa | 2aaecfc | 2015-11-26 14:55:23 +0100 | [diff] [blame] | 953 | if (scripting_ops) |
| 954 | scripting_ops->process_event(event, sample, evsel, &al); |
| 955 | else |
Arnaldo Carvalho de Melo | a3dff30 | 2016-03-23 11:55:24 -0300 | [diff] [blame] | 956 | process_event(scr, sample, evsel, &al); |
Jiri Olsa | 2aaecfc | 2015-11-26 14:55:23 +0100 | [diff] [blame] | 957 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 958 | out_put: |
| 959 | addr_location__put(&al); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 960 | return 0; |
| 961 | } |
| 962 | |
Adrian Hunter | 7ea9572 | 2013-11-01 15:51:30 +0200 | [diff] [blame] | 963 | static int process_attr(struct perf_tool *tool, union perf_event *event, |
| 964 | struct perf_evlist **pevlist) |
| 965 | { |
| 966 | struct perf_script *scr = container_of(tool, struct perf_script, tool); |
| 967 | struct perf_evlist *evlist; |
| 968 | struct perf_evsel *evsel, *pos; |
| 969 | int err; |
| 970 | |
| 971 | err = perf_event__process_attr(tool, event, pevlist); |
| 972 | if (err) |
| 973 | return err; |
| 974 | |
| 975 | evlist = *pevlist; |
| 976 | evsel = perf_evlist__last(*pevlist); |
| 977 | |
| 978 | if (evsel->attr.type >= PERF_TYPE_MAX) |
| 979 | return 0; |
| 980 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 981 | evlist__for_each_entry(evlist, pos) { |
Adrian Hunter | 7ea9572 | 2013-11-01 15:51:30 +0200 | [diff] [blame] | 982 | if (pos->attr.type == evsel->attr.type && pos != evsel) |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | set_print_ip_opts(&evsel->attr); |
| 987 | |
Jiri Olsa | d2b5a31 | 2015-10-16 12:41:25 +0200 | [diff] [blame] | 988 | if (evsel->attr.sample_type) |
| 989 | err = perf_evsel__check_attr(evsel, scr->session); |
| 990 | |
| 991 | return err; |
Adrian Hunter | 7ea9572 | 2013-11-01 15:51:30 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 994 | static int process_comm_event(struct perf_tool *tool, |
| 995 | union perf_event *event, |
| 996 | struct perf_sample *sample, |
| 997 | struct machine *machine) |
| 998 | { |
| 999 | struct thread *thread; |
| 1000 | struct perf_script *script = container_of(tool, struct perf_script, tool); |
| 1001 | struct perf_session *session = script->session; |
Adrian Hunter | 06b234e | 2015-07-21 12:44:05 +0300 | [diff] [blame] | 1002 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1003 | int ret = -1; |
| 1004 | |
| 1005 | thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid); |
| 1006 | if (thread == NULL) { |
| 1007 | pr_debug("problem processing COMM event, skipping it.\n"); |
| 1008 | return -1; |
| 1009 | } |
| 1010 | |
| 1011 | if (perf_event__process_comm(tool, event, sample, machine) < 0) |
| 1012 | goto out; |
| 1013 | |
| 1014 | if (!evsel->attr.sample_id_all) { |
| 1015 | sample->cpu = 0; |
| 1016 | sample->time = 0; |
| 1017 | sample->tid = event->comm.tid; |
| 1018 | sample->pid = event->comm.pid; |
| 1019 | } |
| 1020 | print_sample_start(sample, thread, evsel); |
| 1021 | perf_event__fprintf(event, stdout); |
| 1022 | ret = 0; |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1023 | out: |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1024 | thread__put(thread); |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1025 | return ret; |
| 1026 | } |
| 1027 | |
| 1028 | static int process_fork_event(struct perf_tool *tool, |
| 1029 | union perf_event *event, |
| 1030 | struct perf_sample *sample, |
| 1031 | struct machine *machine) |
| 1032 | { |
| 1033 | struct thread *thread; |
| 1034 | struct perf_script *script = container_of(tool, struct perf_script, tool); |
| 1035 | struct perf_session *session = script->session; |
Adrian Hunter | 06b234e | 2015-07-21 12:44:05 +0300 | [diff] [blame] | 1036 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1037 | |
| 1038 | if (perf_event__process_fork(tool, event, sample, machine) < 0) |
| 1039 | return -1; |
| 1040 | |
| 1041 | thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); |
| 1042 | if (thread == NULL) { |
| 1043 | pr_debug("problem processing FORK event, skipping it.\n"); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | if (!evsel->attr.sample_id_all) { |
| 1048 | sample->cpu = 0; |
| 1049 | sample->time = event->fork.time; |
| 1050 | sample->tid = event->fork.tid; |
| 1051 | sample->pid = event->fork.pid; |
| 1052 | } |
| 1053 | print_sample_start(sample, thread, evsel); |
| 1054 | perf_event__fprintf(event, stdout); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1055 | thread__put(thread); |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1056 | |
| 1057 | return 0; |
| 1058 | } |
| 1059 | static int process_exit_event(struct perf_tool *tool, |
| 1060 | union perf_event *event, |
| 1061 | struct perf_sample *sample, |
| 1062 | struct machine *machine) |
| 1063 | { |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1064 | int err = 0; |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1065 | struct thread *thread; |
| 1066 | struct perf_script *script = container_of(tool, struct perf_script, tool); |
| 1067 | struct perf_session *session = script->session; |
Adrian Hunter | 06b234e | 2015-07-21 12:44:05 +0300 | [diff] [blame] | 1068 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1069 | |
| 1070 | thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); |
| 1071 | if (thread == NULL) { |
| 1072 | pr_debug("problem processing EXIT event, skipping it.\n"); |
| 1073 | return -1; |
| 1074 | } |
| 1075 | |
| 1076 | if (!evsel->attr.sample_id_all) { |
| 1077 | sample->cpu = 0; |
| 1078 | sample->time = 0; |
Adrian Hunter | 53ff6bc | 2015-08-18 12:07:05 +0300 | [diff] [blame] | 1079 | sample->tid = event->fork.tid; |
| 1080 | sample->pid = event->fork.pid; |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1081 | } |
| 1082 | print_sample_start(sample, thread, evsel); |
| 1083 | perf_event__fprintf(event, stdout); |
| 1084 | |
| 1085 | if (perf_event__process_exit(tool, event, sample, machine) < 0) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1086 | err = -1; |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1087 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1088 | thread__put(thread); |
| 1089 | return err; |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1090 | } |
| 1091 | |
Namhyung Kim | ba1ddf4 | 2013-11-26 17:54:26 +0900 | [diff] [blame] | 1092 | static int process_mmap_event(struct perf_tool *tool, |
| 1093 | union perf_event *event, |
| 1094 | struct perf_sample *sample, |
| 1095 | struct machine *machine) |
| 1096 | { |
| 1097 | struct thread *thread; |
| 1098 | struct perf_script *script = container_of(tool, struct perf_script, tool); |
| 1099 | struct perf_session *session = script->session; |
Adrian Hunter | 06b234e | 2015-07-21 12:44:05 +0300 | [diff] [blame] | 1100 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
Namhyung Kim | ba1ddf4 | 2013-11-26 17:54:26 +0900 | [diff] [blame] | 1101 | |
| 1102 | if (perf_event__process_mmap(tool, event, sample, machine) < 0) |
| 1103 | return -1; |
| 1104 | |
| 1105 | thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid); |
| 1106 | if (thread == NULL) { |
| 1107 | pr_debug("problem processing MMAP event, skipping it.\n"); |
| 1108 | return -1; |
| 1109 | } |
| 1110 | |
| 1111 | if (!evsel->attr.sample_id_all) { |
| 1112 | sample->cpu = 0; |
| 1113 | sample->time = 0; |
| 1114 | sample->tid = event->mmap.tid; |
| 1115 | sample->pid = event->mmap.pid; |
| 1116 | } |
| 1117 | print_sample_start(sample, thread, evsel); |
| 1118 | perf_event__fprintf(event, stdout); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1119 | thread__put(thread); |
Namhyung Kim | ba1ddf4 | 2013-11-26 17:54:26 +0900 | [diff] [blame] | 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | static int process_mmap2_event(struct perf_tool *tool, |
| 1124 | union perf_event *event, |
| 1125 | struct perf_sample *sample, |
| 1126 | struct machine *machine) |
| 1127 | { |
| 1128 | struct thread *thread; |
| 1129 | struct perf_script *script = container_of(tool, struct perf_script, tool); |
| 1130 | struct perf_session *session = script->session; |
Adrian Hunter | 06b234e | 2015-07-21 12:44:05 +0300 | [diff] [blame] | 1131 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
Namhyung Kim | ba1ddf4 | 2013-11-26 17:54:26 +0900 | [diff] [blame] | 1132 | |
| 1133 | if (perf_event__process_mmap2(tool, event, sample, machine) < 0) |
| 1134 | return -1; |
| 1135 | |
| 1136 | thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid); |
| 1137 | if (thread == NULL) { |
| 1138 | pr_debug("problem processing MMAP2 event, skipping it.\n"); |
| 1139 | return -1; |
| 1140 | } |
| 1141 | |
| 1142 | if (!evsel->attr.sample_id_all) { |
| 1143 | sample->cpu = 0; |
| 1144 | sample->time = 0; |
| 1145 | sample->tid = event->mmap2.tid; |
| 1146 | sample->pid = event->mmap2.pid; |
| 1147 | } |
| 1148 | print_sample_start(sample, thread, evsel); |
| 1149 | perf_event__fprintf(event, stdout); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1150 | thread__put(thread); |
Namhyung Kim | ba1ddf4 | 2013-11-26 17:54:26 +0900 | [diff] [blame] | 1151 | return 0; |
| 1152 | } |
| 1153 | |
Adrian Hunter | 7c14898 | 2015-07-21 12:44:06 +0300 | [diff] [blame] | 1154 | static int process_switch_event(struct perf_tool *tool, |
| 1155 | union perf_event *event, |
| 1156 | struct perf_sample *sample, |
| 1157 | struct machine *machine) |
| 1158 | { |
| 1159 | struct thread *thread; |
| 1160 | struct perf_script *script = container_of(tool, struct perf_script, tool); |
| 1161 | struct perf_session *session = script->session; |
| 1162 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
| 1163 | |
| 1164 | if (perf_event__process_switch(tool, event, sample, machine) < 0) |
| 1165 | return -1; |
| 1166 | |
| 1167 | thread = machine__findnew_thread(machine, sample->pid, |
| 1168 | sample->tid); |
| 1169 | if (thread == NULL) { |
| 1170 | pr_debug("problem processing SWITCH event, skipping it.\n"); |
| 1171 | return -1; |
| 1172 | } |
| 1173 | |
| 1174 | print_sample_start(sample, thread, evsel); |
| 1175 | perf_event__fprintf(event, stdout); |
| 1176 | thread__put(thread); |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1180 | static void sig_handler(int sig __maybe_unused) |
Tom Zanussi | c239da3 | 2010-04-01 23:59:18 -0500 | [diff] [blame] | 1181 | { |
| 1182 | session_done = 1; |
| 1183 | } |
| 1184 | |
Adrian Hunter | 6f3e5ed | 2013-10-22 10:34:07 +0300 | [diff] [blame] | 1185 | static int __cmd_script(struct perf_script *script) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1186 | { |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 1187 | int ret; |
| 1188 | |
Tom Zanussi | c239da3 | 2010-04-01 23:59:18 -0500 | [diff] [blame] | 1189 | signal(SIGINT, sig_handler); |
| 1190 | |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1191 | /* override event processing functions */ |
| 1192 | if (script->show_task_events) { |
| 1193 | script->tool.comm = process_comm_event; |
| 1194 | script->tool.fork = process_fork_event; |
| 1195 | script->tool.exit = process_exit_event; |
| 1196 | } |
Namhyung Kim | ba1ddf4 | 2013-11-26 17:54:26 +0900 | [diff] [blame] | 1197 | if (script->show_mmap_events) { |
| 1198 | script->tool.mmap = process_mmap_event; |
| 1199 | script->tool.mmap2 = process_mmap2_event; |
| 1200 | } |
Adrian Hunter | 7c14898 | 2015-07-21 12:44:06 +0300 | [diff] [blame] | 1201 | if (script->show_switch_events) |
| 1202 | script->tool.context_switch = process_switch_event; |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 1203 | |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 1204 | ret = perf_session__process_events(script->session); |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 1205 | |
Arnaldo Carvalho de Melo | 6d8afb5 | 2011-01-04 16:27:30 -0200 | [diff] [blame] | 1206 | if (debug_mode) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 1207 | pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); |
Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 1208 | |
| 1209 | return ret; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1212 | struct script_spec { |
| 1213 | struct list_head node; |
| 1214 | struct scripting_ops *ops; |
| 1215 | char spec[0]; |
| 1216 | }; |
| 1217 | |
Arnaldo Carvalho de Melo | eccdfe2 | 2011-01-04 16:32:52 -0200 | [diff] [blame] | 1218 | static LIST_HEAD(script_specs); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1219 | |
| 1220 | static struct script_spec *script_spec__new(const char *spec, |
| 1221 | struct scripting_ops *ops) |
| 1222 | { |
| 1223 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); |
| 1224 | |
| 1225 | if (s != NULL) { |
| 1226 | strcpy(s->spec, spec); |
| 1227 | s->ops = ops; |
| 1228 | } |
| 1229 | |
| 1230 | return s; |
| 1231 | } |
| 1232 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1233 | static void script_spec__add(struct script_spec *s) |
| 1234 | { |
| 1235 | list_add_tail(&s->node, &script_specs); |
| 1236 | } |
| 1237 | |
| 1238 | static struct script_spec *script_spec__find(const char *spec) |
| 1239 | { |
| 1240 | struct script_spec *s; |
| 1241 | |
| 1242 | list_for_each_entry(s, &script_specs, node) |
| 1243 | if (strcasecmp(s->spec, spec) == 0) |
| 1244 | return s; |
| 1245 | return NULL; |
| 1246 | } |
| 1247 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1248 | int script_spec_register(const char *spec, struct scripting_ops *ops) |
| 1249 | { |
| 1250 | struct script_spec *s; |
| 1251 | |
| 1252 | s = script_spec__find(spec); |
| 1253 | if (s) |
| 1254 | return -1; |
| 1255 | |
Taeung Song | 8560bae | 2016-02-26 00:13:10 +0900 | [diff] [blame] | 1256 | s = script_spec__new(spec, ops); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1257 | if (!s) |
| 1258 | return -1; |
Taeung Song | 8560bae | 2016-02-26 00:13:10 +0900 | [diff] [blame] | 1259 | else |
| 1260 | script_spec__add(s); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1261 | |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | static struct scripting_ops *script_spec__lookup(const char *spec) |
| 1266 | { |
| 1267 | struct script_spec *s = script_spec__find(spec); |
| 1268 | if (!s) |
| 1269 | return NULL; |
| 1270 | |
| 1271 | return s->ops; |
| 1272 | } |
| 1273 | |
| 1274 | static void list_available_languages(void) |
| 1275 | { |
| 1276 | struct script_spec *s; |
| 1277 | |
| 1278 | fprintf(stderr, "\n"); |
| 1279 | fprintf(stderr, "Scripting language extensions (used in " |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1280 | "perf script -s [spec:]script.[spec]):\n\n"); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1281 | |
| 1282 | list_for_each_entry(s, &script_specs, node) |
| 1283 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); |
| 1284 | |
| 1285 | fprintf(stderr, "\n"); |
| 1286 | } |
| 1287 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1288 | static int parse_scriptname(const struct option *opt __maybe_unused, |
| 1289 | const char *str, int unset __maybe_unused) |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1290 | { |
| 1291 | char spec[PATH_MAX]; |
| 1292 | const char *script, *ext; |
| 1293 | int len; |
| 1294 | |
Tom Zanussi | f526d68 | 2010-01-27 02:27:52 -0600 | [diff] [blame] | 1295 | if (strcmp(str, "lang") == 0) { |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1296 | list_available_languages(); |
Tom Zanussi | f526d68 | 2010-01-27 02:27:52 -0600 | [diff] [blame] | 1297 | exit(0); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | script = strchr(str, ':'); |
| 1301 | if (script) { |
| 1302 | len = script - str; |
| 1303 | if (len >= PATH_MAX) { |
| 1304 | fprintf(stderr, "invalid language specifier"); |
| 1305 | return -1; |
| 1306 | } |
| 1307 | strncpy(spec, str, len); |
| 1308 | spec[len] = '\0'; |
| 1309 | scripting_ops = script_spec__lookup(spec); |
| 1310 | if (!scripting_ops) { |
| 1311 | fprintf(stderr, "invalid language specifier"); |
| 1312 | return -1; |
| 1313 | } |
| 1314 | script++; |
| 1315 | } else { |
| 1316 | script = str; |
Ben Hutchings | d1e95bb | 2010-10-10 16:11:02 +0100 | [diff] [blame] | 1317 | ext = strrchr(script, '.'); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1318 | if (!ext) { |
| 1319 | fprintf(stderr, "invalid script extension"); |
| 1320 | return -1; |
| 1321 | } |
| 1322 | scripting_ops = script_spec__lookup(++ext); |
| 1323 | if (!scripting_ops) { |
| 1324 | fprintf(stderr, "invalid script extension"); |
| 1325 | return -1; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | script_name = strdup(script); |
| 1330 | |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1334 | static int parse_output_fields(const struct option *opt __maybe_unused, |
| 1335 | const char *arg, int unset __maybe_unused) |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1336 | { |
| 1337 | char *tok; |
Sasha Levin | 50ca19a | 2012-12-20 14:11:19 -0500 | [diff] [blame] | 1338 | int i, imax = ARRAY_SIZE(all_output_options); |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1339 | int j; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1340 | int rc = 0; |
| 1341 | char *str = strdup(arg); |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 1342 | int type = -1; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1343 | |
| 1344 | if (!str) |
| 1345 | return -ENOMEM; |
| 1346 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1347 | /* first word can state for which event type the user is specifying |
| 1348 | * the fields. If no type exists, the specified fields apply to all |
| 1349 | * 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] | 1350 | */ |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1351 | tok = strchr(str, ':'); |
| 1352 | if (tok) { |
| 1353 | *tok = '\0'; |
| 1354 | tok++; |
| 1355 | if (!strcmp(str, "hw")) |
| 1356 | type = PERF_TYPE_HARDWARE; |
| 1357 | else if (!strcmp(str, "sw")) |
| 1358 | type = PERF_TYPE_SOFTWARE; |
| 1359 | else if (!strcmp(str, "trace")) |
| 1360 | type = PERF_TYPE_TRACEPOINT; |
Arun Sharma | 0817a6a | 2011-04-14 10:38:18 -0700 | [diff] [blame] | 1361 | else if (!strcmp(str, "raw")) |
| 1362 | type = PERF_TYPE_RAW; |
Wang Nan | 27cfef0 | 2015-12-08 02:25:43 +0000 | [diff] [blame] | 1363 | else if (!strcmp(str, "break")) |
| 1364 | type = PERF_TYPE_BREAKPOINT; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1365 | else { |
| 1366 | fprintf(stderr, "Invalid event type in field string.\n"); |
Robert Richter | 38efb53 | 2011-11-25 11:38:40 +0100 | [diff] [blame] | 1367 | rc = -EINVAL; |
| 1368 | goto out; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | if (output[type].user_set) |
| 1372 | pr_warning("Overriding previous field request for %s events.\n", |
| 1373 | event_type(type)); |
| 1374 | |
| 1375 | output[type].fields = 0; |
| 1376 | output[type].user_set = true; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 1377 | output[type].wildcard_set = false; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1378 | |
| 1379 | } else { |
| 1380 | tok = str; |
| 1381 | if (strlen(str) == 0) { |
| 1382 | fprintf(stderr, |
| 1383 | "Cannot set fields to 'none' for all event types.\n"); |
| 1384 | rc = -EINVAL; |
| 1385 | goto out; |
| 1386 | } |
| 1387 | |
| 1388 | if (output_set_by_user()) |
| 1389 | pr_warning("Overriding previous field request for all events.\n"); |
| 1390 | |
| 1391 | for (j = 0; j < PERF_TYPE_MAX; ++j) { |
| 1392 | output[j].fields = 0; |
| 1393 | output[j].user_set = true; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 1394 | output[j].wildcard_set = true; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1395 | } |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
Adrian Hunter | 400ea6d | 2015-04-09 18:54:05 +0300 | [diff] [blame] | 1398 | for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) { |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1399 | for (i = 0; i < imax; ++i) { |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1400 | if (strcmp(tok, all_output_options[i].str) == 0) |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1401 | break; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1402 | } |
Adrian Hunter | 400ea6d | 2015-04-09 18:54:05 +0300 | [diff] [blame] | 1403 | if (i == imax && strcmp(tok, "flags") == 0) { |
| 1404 | print_flags = true; |
| 1405 | continue; |
| 1406 | } |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1407 | if (i == imax) { |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1408 | fprintf(stderr, "Invalid field requested.\n"); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1409 | rc = -EINVAL; |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1410 | goto out; |
| 1411 | } |
| 1412 | |
| 1413 | if (type == -1) { |
| 1414 | /* add user option to all events types for |
| 1415 | * which it is valid |
| 1416 | */ |
| 1417 | for (j = 0; j < PERF_TYPE_MAX; ++j) { |
| 1418 | if (output[j].invalid_fields & all_output_options[i].field) { |
| 1419 | pr_warning("\'%s\' not valid for %s events. Ignoring.\n", |
| 1420 | all_output_options[i].str, event_type(j)); |
| 1421 | } else |
| 1422 | output[j].fields |= all_output_options[i].field; |
| 1423 | } |
| 1424 | } else { |
| 1425 | if (output[type].invalid_fields & all_output_options[i].field) { |
| 1426 | fprintf(stderr, "\'%s\' not valid for %s events.\n", |
| 1427 | all_output_options[i].str, event_type(type)); |
| 1428 | |
| 1429 | rc = -EINVAL; |
| 1430 | goto out; |
| 1431 | } |
| 1432 | output[type].fields |= all_output_options[i].field; |
| 1433 | } |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | if (type >= 0) { |
| 1437 | if (output[type].fields == 0) { |
| 1438 | pr_debug("No fields requested for %s type. " |
| 1439 | "Events will not be displayed.\n", event_type(type)); |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1440 | } |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 1441 | } |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1442 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1443 | out: |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1444 | free(str); |
| 1445 | return rc; |
| 1446 | } |
| 1447 | |
Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 1448 | /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */ |
| 1449 | static int is_directory(const char *base_path, const struct dirent *dent) |
| 1450 | { |
| 1451 | char path[PATH_MAX]; |
| 1452 | struct stat st; |
| 1453 | |
| 1454 | sprintf(path, "%s/%s", base_path, dent->d_name); |
| 1455 | if (stat(path, &st)) |
| 1456 | return 0; |
| 1457 | |
| 1458 | return S_ISDIR(st.st_mode); |
| 1459 | } |
| 1460 | |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1461 | #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \ |
| 1462 | while ((lang_dirent = readdir(scripts_dir)) != NULL) \ |
| 1463 | if ((lang_dirent->d_type == DT_DIR || \ |
| 1464 | (lang_dirent->d_type == DT_UNKNOWN && \ |
| 1465 | is_directory(scripts_path, lang_dirent))) && \ |
| 1466 | (strcmp(lang_dirent->d_name, ".")) && \ |
| 1467 | (strcmp(lang_dirent->d_name, ".."))) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1468 | |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1469 | #define for_each_script(lang_path, lang_dir, script_dirent) \ |
| 1470 | while ((script_dirent = readdir(lang_dir)) != NULL) \ |
| 1471 | if (script_dirent->d_type != DT_DIR && \ |
| 1472 | (script_dirent->d_type != DT_UNKNOWN || \ |
| 1473 | !is_directory(lang_path, script_dirent))) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1474 | |
| 1475 | |
| 1476 | #define RECORD_SUFFIX "-record" |
| 1477 | #define REPORT_SUFFIX "-report" |
| 1478 | |
| 1479 | struct script_desc { |
| 1480 | struct list_head node; |
| 1481 | char *name; |
| 1482 | char *half_liner; |
| 1483 | char *args; |
| 1484 | }; |
| 1485 | |
Arnaldo Carvalho de Melo | eccdfe2 | 2011-01-04 16:32:52 -0200 | [diff] [blame] | 1486 | static LIST_HEAD(script_descs); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1487 | |
| 1488 | static struct script_desc *script_desc__new(const char *name) |
| 1489 | { |
| 1490 | struct script_desc *s = zalloc(sizeof(*s)); |
| 1491 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1492 | if (s != NULL && name) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1493 | s->name = strdup(name); |
| 1494 | |
| 1495 | return s; |
| 1496 | } |
| 1497 | |
| 1498 | static void script_desc__delete(struct script_desc *s) |
| 1499 | { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 1500 | zfree(&s->name); |
| 1501 | zfree(&s->half_liner); |
| 1502 | zfree(&s->args); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1503 | free(s); |
| 1504 | } |
| 1505 | |
| 1506 | static void script_desc__add(struct script_desc *s) |
| 1507 | { |
| 1508 | list_add_tail(&s->node, &script_descs); |
| 1509 | } |
| 1510 | |
| 1511 | static struct script_desc *script_desc__find(const char *name) |
| 1512 | { |
| 1513 | struct script_desc *s; |
| 1514 | |
| 1515 | list_for_each_entry(s, &script_descs, node) |
| 1516 | if (strcasecmp(s->name, name) == 0) |
| 1517 | return s; |
| 1518 | return NULL; |
| 1519 | } |
| 1520 | |
| 1521 | static struct script_desc *script_desc__findnew(const char *name) |
| 1522 | { |
| 1523 | struct script_desc *s = script_desc__find(name); |
| 1524 | |
| 1525 | if (s) |
| 1526 | return s; |
| 1527 | |
| 1528 | s = script_desc__new(name); |
| 1529 | if (!s) |
| 1530 | goto out_delete_desc; |
| 1531 | |
| 1532 | script_desc__add(s); |
| 1533 | |
| 1534 | return s; |
| 1535 | |
| 1536 | out_delete_desc: |
| 1537 | script_desc__delete(s); |
| 1538 | |
| 1539 | return NULL; |
| 1540 | } |
| 1541 | |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 1542 | static const char *ends_with(const char *str, const char *suffix) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1543 | { |
| 1544 | size_t suffix_len = strlen(suffix); |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 1545 | const char *p = str; |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1546 | |
| 1547 | if (strlen(str) > suffix_len) { |
| 1548 | p = str + strlen(str) - suffix_len; |
| 1549 | if (!strncmp(p, suffix, suffix_len)) |
| 1550 | return p; |
| 1551 | } |
| 1552 | |
| 1553 | return NULL; |
| 1554 | } |
| 1555 | |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1556 | static int read_script_info(struct script_desc *desc, const char *filename) |
| 1557 | { |
| 1558 | char line[BUFSIZ], *p; |
| 1559 | FILE *fp; |
| 1560 | |
| 1561 | fp = fopen(filename, "r"); |
| 1562 | if (!fp) |
| 1563 | return -1; |
| 1564 | |
| 1565 | while (fgets(line, sizeof(line), fp)) { |
| 1566 | p = ltrim(line); |
| 1567 | if (strlen(p) == 0) |
| 1568 | continue; |
| 1569 | if (*p != '#') |
| 1570 | continue; |
| 1571 | p++; |
| 1572 | if (strlen(p) && *p == '!') |
| 1573 | continue; |
| 1574 | |
| 1575 | p = ltrim(p); |
| 1576 | if (strlen(p) && p[strlen(p) - 1] == '\n') |
| 1577 | p[strlen(p) - 1] = '\0'; |
| 1578 | |
| 1579 | if (!strncmp(p, "description:", strlen("description:"))) { |
| 1580 | p += strlen("description:"); |
| 1581 | desc->half_liner = strdup(ltrim(p)); |
| 1582 | continue; |
| 1583 | } |
| 1584 | |
| 1585 | if (!strncmp(p, "args:", strlen("args:"))) { |
| 1586 | p += strlen("args:"); |
| 1587 | desc->args = strdup(ltrim(p)); |
| 1588 | continue; |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | fclose(fp); |
| 1593 | |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
Robert Richter | 38efb53 | 2011-11-25 11:38:40 +0100 | [diff] [blame] | 1597 | static char *get_script_root(struct dirent *script_dirent, const char *suffix) |
| 1598 | { |
| 1599 | char *script_root, *str; |
| 1600 | |
| 1601 | script_root = strdup(script_dirent->d_name); |
| 1602 | if (!script_root) |
| 1603 | return NULL; |
| 1604 | |
| 1605 | str = (char *)ends_with(script_root, suffix); |
| 1606 | if (!str) { |
| 1607 | free(script_root); |
| 1608 | return NULL; |
| 1609 | } |
| 1610 | |
| 1611 | *str = '\0'; |
| 1612 | return script_root; |
| 1613 | } |
| 1614 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1615 | static int list_available_scripts(const struct option *opt __maybe_unused, |
| 1616 | const char *s __maybe_unused, |
| 1617 | int unset __maybe_unused) |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1618 | { |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1619 | struct dirent *script_dirent, *lang_dirent; |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1620 | char scripts_path[MAXPATHLEN]; |
| 1621 | DIR *scripts_dir, *lang_dir; |
| 1622 | char script_path[MAXPATHLEN]; |
| 1623 | char lang_path[MAXPATHLEN]; |
| 1624 | struct script_desc *desc; |
| 1625 | char first_half[BUFSIZ]; |
| 1626 | char *script_root; |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1627 | |
Josh Poimboeuf | 46113a5 | 2015-12-15 09:39:37 -0600 | [diff] [blame] | 1628 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1629 | |
| 1630 | scripts_dir = opendir(scripts_path); |
| 1631 | if (!scripts_dir) |
| 1632 | return -1; |
| 1633 | |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1634 | for_each_lang(scripts_path, scripts_dir, lang_dirent) { |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1635 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1636 | lang_dirent->d_name); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1637 | lang_dir = opendir(lang_path); |
| 1638 | if (!lang_dir) |
| 1639 | continue; |
| 1640 | |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1641 | for_each_script(lang_path, lang_dir, script_dirent) { |
| 1642 | script_root = get_script_root(script_dirent, REPORT_SUFFIX); |
Robert Richter | 38efb53 | 2011-11-25 11:38:40 +0100 | [diff] [blame] | 1643 | if (script_root) { |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1644 | desc = script_desc__findnew(script_root); |
| 1645 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1646 | lang_path, script_dirent->d_name); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1647 | read_script_info(desc, script_path); |
Robert Richter | 38efb53 | 2011-11-25 11:38:40 +0100 | [diff] [blame] | 1648 | free(script_root); |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1649 | } |
Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | fprintf(stdout, "List of available trace scripts:\n"); |
| 1654 | list_for_each_entry(desc, &script_descs, node) { |
| 1655 | sprintf(first_half, "%s %s", desc->name, |
| 1656 | desc->args ? desc->args : ""); |
| 1657 | fprintf(stdout, " %-36s %s\n", first_half, |
| 1658 | desc->half_liner ? desc->half_liner : ""); |
| 1659 | } |
| 1660 | |
| 1661 | exit(0); |
| 1662 | } |
| 1663 | |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1664 | /* |
Feng Tang | 49e639e | 2012-10-30 11:56:03 +0800 | [diff] [blame] | 1665 | * Some scripts specify the required events in their "xxx-record" file, |
| 1666 | * this function will check if the events in perf.data match those |
| 1667 | * mentioned in the "xxx-record". |
| 1668 | * |
| 1669 | * Fixme: All existing "xxx-record" are all in good formats "-e event ", |
| 1670 | * which is covered well now. And new parsing code should be added to |
| 1671 | * cover the future complexing formats like event groups etc. |
| 1672 | */ |
| 1673 | static int check_ev_match(char *dir_name, char *scriptname, |
| 1674 | struct perf_session *session) |
| 1675 | { |
| 1676 | char filename[MAXPATHLEN], evname[128]; |
| 1677 | char line[BUFSIZ], *p; |
| 1678 | struct perf_evsel *pos; |
| 1679 | int match, len; |
| 1680 | FILE *fp; |
| 1681 | |
| 1682 | sprintf(filename, "%s/bin/%s-record", dir_name, scriptname); |
| 1683 | |
| 1684 | fp = fopen(filename, "r"); |
| 1685 | if (!fp) |
| 1686 | return -1; |
| 1687 | |
| 1688 | while (fgets(line, sizeof(line), fp)) { |
| 1689 | p = ltrim(line); |
| 1690 | if (*p == '#') |
| 1691 | continue; |
| 1692 | |
| 1693 | while (strlen(p)) { |
| 1694 | p = strstr(p, "-e"); |
| 1695 | if (!p) |
| 1696 | break; |
| 1697 | |
| 1698 | p += 2; |
| 1699 | p = ltrim(p); |
| 1700 | len = strcspn(p, " \t"); |
| 1701 | if (!len) |
| 1702 | break; |
| 1703 | |
| 1704 | snprintf(evname, len + 1, "%s", p); |
| 1705 | |
| 1706 | match = 0; |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1707 | evlist__for_each_entry(session->evlist, pos) { |
Feng Tang | 49e639e | 2012-10-30 11:56:03 +0800 | [diff] [blame] | 1708 | if (!strcmp(perf_evsel__name(pos), evname)) { |
| 1709 | match = 1; |
| 1710 | break; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | if (!match) { |
| 1715 | fclose(fp); |
| 1716 | return -1; |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | fclose(fp); |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
| 1725 | /* |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1726 | * Return -1 if none is found, otherwise the actual scripts number. |
| 1727 | * |
| 1728 | * Currently the only user of this function is the script browser, which |
| 1729 | * will list all statically runnable scripts, select one, execute it and |
| 1730 | * show the output in a perf browser. |
| 1731 | */ |
| 1732 | int find_scripts(char **scripts_array, char **scripts_path_array) |
| 1733 | { |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1734 | struct dirent *script_dirent, *lang_dirent; |
Feng Tang | 49e639e | 2012-10-30 11:56:03 +0800 | [diff] [blame] | 1735 | char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN]; |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1736 | DIR *scripts_dir, *lang_dir; |
Feng Tang | 49e639e | 2012-10-30 11:56:03 +0800 | [diff] [blame] | 1737 | struct perf_session *session; |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 1738 | struct perf_data_file file = { |
| 1739 | .path = input_name, |
| 1740 | .mode = PERF_DATA_MODE_READ, |
| 1741 | }; |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1742 | char *temp; |
| 1743 | int i = 0; |
| 1744 | |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 1745 | session = perf_session__new(&file, false, NULL); |
Feng Tang | 49e639e | 2012-10-30 11:56:03 +0800 | [diff] [blame] | 1746 | if (!session) |
| 1747 | return -1; |
| 1748 | |
Josh Poimboeuf | 46113a5 | 2015-12-15 09:39:37 -0600 | [diff] [blame] | 1749 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1750 | |
| 1751 | scripts_dir = opendir(scripts_path); |
Feng Tang | 49e639e | 2012-10-30 11:56:03 +0800 | [diff] [blame] | 1752 | if (!scripts_dir) { |
| 1753 | perf_session__delete(session); |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1754 | return -1; |
Feng Tang | 49e639e | 2012-10-30 11:56:03 +0800 | [diff] [blame] | 1755 | } |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1756 | |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1757 | for_each_lang(scripts_path, scripts_dir, lang_dirent) { |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1758 | snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path, |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1759 | lang_dirent->d_name); |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1760 | #ifdef NO_LIBPERL |
| 1761 | if (strstr(lang_path, "perl")) |
| 1762 | continue; |
| 1763 | #endif |
| 1764 | #ifdef NO_LIBPYTHON |
| 1765 | if (strstr(lang_path, "python")) |
| 1766 | continue; |
| 1767 | #endif |
| 1768 | |
| 1769 | lang_dir = opendir(lang_path); |
| 1770 | if (!lang_dir) |
| 1771 | continue; |
| 1772 | |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1773 | for_each_script(lang_path, lang_dir, script_dirent) { |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1774 | /* Skip those real time scripts: xxxtop.p[yl] */ |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1775 | if (strstr(script_dirent->d_name, "top.")) |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1776 | continue; |
| 1777 | sprintf(scripts_path_array[i], "%s/%s", lang_path, |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1778 | script_dirent->d_name); |
| 1779 | temp = strchr(script_dirent->d_name, '.'); |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1780 | snprintf(scripts_array[i], |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1781 | (temp - script_dirent->d_name) + 1, |
| 1782 | "%s", script_dirent->d_name); |
Feng Tang | 49e639e | 2012-10-30 11:56:03 +0800 | [diff] [blame] | 1783 | |
| 1784 | if (check_ev_match(lang_path, |
| 1785 | scripts_array[i], session)) |
| 1786 | continue; |
| 1787 | |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1788 | i++; |
| 1789 | } |
Feng Tang | 49e639e | 2012-10-30 11:56:03 +0800 | [diff] [blame] | 1790 | closedir(lang_dir); |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1791 | } |
| 1792 | |
Feng Tang | 49e639e | 2012-10-30 11:56:03 +0800 | [diff] [blame] | 1793 | closedir(scripts_dir); |
| 1794 | perf_session__delete(session); |
Feng Tang | e5f3705 | 2012-09-07 16:42:26 +0800 | [diff] [blame] | 1795 | return i; |
| 1796 | } |
| 1797 | |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1798 | static char *get_script_path(const char *script_root, const char *suffix) |
| 1799 | { |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1800 | struct dirent *script_dirent, *lang_dirent; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1801 | char scripts_path[MAXPATHLEN]; |
| 1802 | char script_path[MAXPATHLEN]; |
| 1803 | DIR *scripts_dir, *lang_dir; |
| 1804 | char lang_path[MAXPATHLEN]; |
Robert Richter | 38efb53 | 2011-11-25 11:38:40 +0100 | [diff] [blame] | 1805 | char *__script_root; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1806 | |
Josh Poimboeuf | 46113a5 | 2015-12-15 09:39:37 -0600 | [diff] [blame] | 1807 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1808 | |
| 1809 | scripts_dir = opendir(scripts_path); |
| 1810 | if (!scripts_dir) |
| 1811 | return NULL; |
| 1812 | |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1813 | for_each_lang(scripts_path, scripts_dir, lang_dirent) { |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1814 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1815 | lang_dirent->d_name); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1816 | lang_dir = opendir(lang_path); |
| 1817 | if (!lang_dir) |
| 1818 | continue; |
| 1819 | |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1820 | for_each_script(lang_path, lang_dir, script_dirent) { |
| 1821 | __script_root = get_script_root(script_dirent, suffix); |
Robert Richter | 38efb53 | 2011-11-25 11:38:40 +0100 | [diff] [blame] | 1822 | if (__script_root && !strcmp(script_root, __script_root)) { |
| 1823 | free(__script_root); |
Namhyung Kim | 946ef2a | 2012-01-08 02:25:25 +0900 | [diff] [blame] | 1824 | closedir(lang_dir); |
| 1825 | closedir(scripts_dir); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1826 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
Arnaldo Carvalho de Melo | a5e8e82 | 2016-04-08 11:25:59 -0300 | [diff] [blame] | 1827 | lang_path, script_dirent->d_name); |
Robert Richter | 38efb53 | 2011-11-25 11:38:40 +0100 | [diff] [blame] | 1828 | return strdup(script_path); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1829 | } |
| 1830 | free(__script_root); |
| 1831 | } |
Namhyung Kim | 946ef2a | 2012-01-08 02:25:25 +0900 | [diff] [blame] | 1832 | closedir(lang_dir); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1833 | } |
Namhyung Kim | 946ef2a | 2012-01-08 02:25:25 +0900 | [diff] [blame] | 1834 | closedir(scripts_dir); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1835 | |
Robert Richter | 38efb53 | 2011-11-25 11:38:40 +0100 | [diff] [blame] | 1836 | return NULL; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1837 | } |
| 1838 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1839 | static bool is_top_script(const char *script_path) |
| 1840 | { |
Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 1841 | return ends_with(script_path, "top") == NULL ? false : true; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | static int has_required_arg(char *script_path) |
| 1845 | { |
| 1846 | struct script_desc *desc; |
| 1847 | int n_args = 0; |
| 1848 | char *p; |
| 1849 | |
| 1850 | desc = script_desc__new(NULL); |
| 1851 | |
| 1852 | if (read_script_info(desc, script_path)) |
| 1853 | goto out; |
| 1854 | |
| 1855 | if (!desc->args) |
| 1856 | goto out; |
| 1857 | |
| 1858 | for (p = desc->args; *p; p++) |
| 1859 | if (*p == '<') |
| 1860 | n_args++; |
| 1861 | out: |
| 1862 | script_desc__delete(desc); |
| 1863 | |
| 1864 | return n_args; |
| 1865 | } |
| 1866 | |
David Ahern | d54b1a9 | 2012-08-26 12:24:46 -0600 | [diff] [blame] | 1867 | static int have_cmd(int argc, const char **argv) |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 1868 | { |
| 1869 | char **__argv = malloc(sizeof(const char *) * argc); |
| 1870 | |
David Ahern | d54b1a9 | 2012-08-26 12:24:46 -0600 | [diff] [blame] | 1871 | if (!__argv) { |
| 1872 | pr_err("malloc failed\n"); |
| 1873 | return -1; |
| 1874 | } |
| 1875 | |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 1876 | memcpy(__argv, argv, sizeof(const char *) * argc); |
| 1877 | argc = parse_options(argc, (const char **)__argv, record_options, |
| 1878 | NULL, PARSE_OPT_STOP_AT_NON_OPTION); |
| 1879 | free(__argv); |
| 1880 | |
David Ahern | d54b1a9 | 2012-08-26 12:24:46 -0600 | [diff] [blame] | 1881 | system_wide = (argc == 0); |
| 1882 | |
| 1883 | return 0; |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 1884 | } |
| 1885 | |
Jiri Olsa | 7322d6c | 2015-08-13 09:17:24 +0200 | [diff] [blame] | 1886 | static void script__setup_sample_type(struct perf_script *script) |
| 1887 | { |
| 1888 | struct perf_session *session = script->session; |
| 1889 | u64 sample_type = perf_evlist__combined_sample_type(session->evlist); |
| 1890 | |
| 1891 | if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) { |
| 1892 | if ((sample_type & PERF_SAMPLE_REGS_USER) && |
| 1893 | (sample_type & PERF_SAMPLE_STACK_USER)) |
| 1894 | callchain_param.record_mode = CALLCHAIN_DWARF; |
| 1895 | else if (sample_type & PERF_SAMPLE_BRANCH_STACK) |
| 1896 | callchain_param.record_mode = CALLCHAIN_LBR; |
| 1897 | else |
| 1898 | callchain_param.record_mode = CALLCHAIN_FP; |
| 1899 | } |
| 1900 | } |
| 1901 | |
Jiri Olsa | e099eba | 2016-01-05 22:09:09 +0100 | [diff] [blame] | 1902 | static int process_stat_round_event(struct perf_tool *tool __maybe_unused, |
| 1903 | union perf_event *event, |
| 1904 | struct perf_session *session) |
| 1905 | { |
| 1906 | struct stat_round_event *round = &event->stat_round; |
| 1907 | struct perf_evsel *counter; |
| 1908 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1909 | evlist__for_each_entry(session->evlist, counter) { |
Jiri Olsa | e099eba | 2016-01-05 22:09:09 +0100 | [diff] [blame] | 1910 | perf_stat_process_counter(&stat_config, counter); |
| 1911 | process_stat(counter, round->time); |
| 1912 | } |
| 1913 | |
| 1914 | process_stat_interval(round->time); |
| 1915 | return 0; |
| 1916 | } |
| 1917 | |
Jiri Olsa | 91a2c3d | 2016-01-05 22:09:07 +0100 | [diff] [blame] | 1918 | static int process_stat_config_event(struct perf_tool *tool __maybe_unused, |
| 1919 | union perf_event *event, |
| 1920 | struct perf_session *session __maybe_unused) |
| 1921 | { |
| 1922 | perf_event__read_stat_config(&stat_config, &event->stat_config); |
| 1923 | return 0; |
| 1924 | } |
| 1925 | |
Jiri Olsa | cfc8874 | 2016-01-05 22:09:06 +0100 | [diff] [blame] | 1926 | static int set_maps(struct perf_script *script) |
| 1927 | { |
| 1928 | struct perf_evlist *evlist = script->session->evlist; |
| 1929 | |
| 1930 | if (!script->cpus || !script->threads) |
| 1931 | return 0; |
| 1932 | |
| 1933 | if (WARN_ONCE(script->allocated, "stats double allocation\n")) |
| 1934 | return -EINVAL; |
| 1935 | |
| 1936 | perf_evlist__set_maps(evlist, script->cpus, script->threads); |
| 1937 | |
| 1938 | if (perf_evlist__alloc_stats(evlist, true)) |
| 1939 | return -ENOMEM; |
| 1940 | |
| 1941 | script->allocated = true; |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | static |
| 1946 | int process_thread_map_event(struct perf_tool *tool, |
| 1947 | union perf_event *event, |
| 1948 | struct perf_session *session __maybe_unused) |
| 1949 | { |
| 1950 | struct perf_script *script = container_of(tool, struct perf_script, tool); |
| 1951 | |
| 1952 | if (script->threads) { |
| 1953 | pr_warning("Extra thread map event, ignoring.\n"); |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
| 1957 | script->threads = thread_map__new_event(&event->thread_map); |
| 1958 | if (!script->threads) |
| 1959 | return -ENOMEM; |
| 1960 | |
| 1961 | return set_maps(script); |
| 1962 | } |
| 1963 | |
| 1964 | static |
| 1965 | int process_cpu_map_event(struct perf_tool *tool __maybe_unused, |
| 1966 | union perf_event *event, |
| 1967 | struct perf_session *session __maybe_unused) |
| 1968 | { |
| 1969 | struct perf_script *script = container_of(tool, struct perf_script, tool); |
| 1970 | |
| 1971 | if (script->cpus) { |
| 1972 | pr_warning("Extra cpu map event, ignoring.\n"); |
| 1973 | return 0; |
| 1974 | } |
| 1975 | |
| 1976 | script->cpus = cpu_map__new_data(&event->cpu_map.data); |
| 1977 | if (!script->cpus) |
| 1978 | return -ENOMEM; |
| 1979 | |
| 1980 | return set_maps(script); |
| 1981 | } |
| 1982 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1983 | 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] | 1984 | { |
Arnaldo Carvalho de Melo | 69b6470 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1985 | bool show_full_info = false; |
Jiri Olsa | e90debd | 2013-12-09 11:02:50 +0100 | [diff] [blame] | 1986 | bool header = false; |
| 1987 | bool header_only = false; |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 1988 | bool script_started = false; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1989 | char *rec_script_path = NULL; |
| 1990 | char *rep_script_path = NULL; |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1991 | struct perf_session *session; |
Adrian Hunter | 7a680eb | 2015-04-09 18:53:56 +0300 | [diff] [blame] | 1992 | struct itrace_synth_opts itrace_synth_opts = { .set = false, }; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1993 | char *script_path = NULL; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1994 | const char **__argv; |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 1995 | int i, j, err = 0; |
Adrian Hunter | 6f3e5ed | 2013-10-22 10:34:07 +0300 | [diff] [blame] | 1996 | struct perf_script script = { |
| 1997 | .tool = { |
| 1998 | .sample = process_sample_event, |
| 1999 | .mmap = perf_event__process_mmap, |
| 2000 | .mmap2 = perf_event__process_mmap2, |
| 2001 | .comm = perf_event__process_comm, |
| 2002 | .exit = perf_event__process_exit, |
| 2003 | .fork = perf_event__process_fork, |
Adrian Hunter | 7ea9572 | 2013-11-01 15:51:30 +0200 | [diff] [blame] | 2004 | .attr = process_attr, |
Jiri Olsa | 91daee3 | 2016-04-07 09:11:13 +0200 | [diff] [blame] | 2005 | .event_update = perf_event__process_event_update, |
Adrian Hunter | 6f3e5ed | 2013-10-22 10:34:07 +0300 | [diff] [blame] | 2006 | .tracing_data = perf_event__process_tracing_data, |
| 2007 | .build_id = perf_event__process_build_id, |
Adrian Hunter | 7a680eb | 2015-04-09 18:53:56 +0300 | [diff] [blame] | 2008 | .id_index = perf_event__process_id_index, |
| 2009 | .auxtrace_info = perf_event__process_auxtrace_info, |
| 2010 | .auxtrace = perf_event__process_auxtrace, |
| 2011 | .auxtrace_error = perf_event__process_auxtrace_error, |
Jiri Olsa | e099eba | 2016-01-05 22:09:09 +0100 | [diff] [blame] | 2012 | .stat = perf_event__process_stat_event, |
| 2013 | .stat_round = process_stat_round_event, |
Jiri Olsa | 91a2c3d | 2016-01-05 22:09:07 +0100 | [diff] [blame] | 2014 | .stat_config = process_stat_config_event, |
Jiri Olsa | cfc8874 | 2016-01-05 22:09:06 +0100 | [diff] [blame] | 2015 | .thread_map = process_thread_map_event, |
| 2016 | .cpu_map = process_cpu_map_event, |
Jiri Olsa | 0a8cb85 | 2014-07-06 14:18:21 +0200 | [diff] [blame] | 2017 | .ordered_events = true, |
Adrian Hunter | 6f3e5ed | 2013-10-22 10:34:07 +0300 | [diff] [blame] | 2018 | .ordering_requires_timestamps = true, |
| 2019 | }, |
| 2020 | }; |
Yunlong Song | 06af0f2 | 2015-04-02 21:47:16 +0800 | [diff] [blame] | 2021 | struct perf_data_file file = { |
| 2022 | .mode = PERF_DATA_MODE_READ, |
| 2023 | }; |
Arnaldo Carvalho de Melo | 69b6470 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2024 | const struct option options[] = { |
| 2025 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 2026 | "dump raw trace in ASCII"), |
| 2027 | OPT_INCR('v', "verbose", &verbose, |
| 2028 | "be more verbose (show symbol address, etc)"), |
| 2029 | OPT_BOOLEAN('L', "Latency", &latency_format, |
| 2030 | "show latency attributes (irqs/preemption disabled, etc)"), |
| 2031 | OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", |
| 2032 | list_available_scripts), |
| 2033 | OPT_CALLBACK('s', "script", NULL, "name", |
| 2034 | "script file name (lang:script name, script name, or *)", |
| 2035 | parse_scriptname), |
| 2036 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", |
| 2037 | "generate perf-script.xx script in specified language"), |
| 2038 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
| 2039 | OPT_BOOLEAN('d', "debug-mode", &debug_mode, |
| 2040 | "do various checks like samples ordering and lost events"), |
Jiri Olsa | e90debd | 2013-12-09 11:02:50 +0100 | [diff] [blame] | 2041 | OPT_BOOLEAN(0, "header", &header, "Show data header."), |
| 2042 | OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."), |
Arnaldo Carvalho de Melo | 69b6470 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2043 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
| 2044 | "file", "vmlinux pathname"), |
| 2045 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, |
| 2046 | "file", "kallsyms pathname"), |
| 2047 | OPT_BOOLEAN('G', "hide-call-graph", &no_callchain, |
| 2048 | "When printing symbols do not display call chain"), |
He Kuang | a706670 | 2016-05-19 11:47:37 +0000 | [diff] [blame] | 2049 | OPT_CALLBACK(0, "symfs", NULL, "directory", |
| 2050 | "Look for files with symbols relative to this directory", |
| 2051 | symbol__config_symfs), |
Yunlong Song | 06af0f2 | 2015-04-02 21:47:16 +0800 | [diff] [blame] | 2052 | OPT_CALLBACK('F', "fields", NULL, "str", |
Arnaldo Carvalho de Melo | 69b6470 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2053 | "comma separated output fields prepend with 'type:'. " |
| 2054 | "Valid types: hw,sw,trace,raw. " |
| 2055 | "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso," |
Stephane Eranian | dc323ce | 2015-08-31 18:41:13 +0200 | [diff] [blame] | 2056 | "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields), |
Arnaldo Carvalho de Melo | 69b6470 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2057 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
| 2058 | "system-wide collection from all CPUs"), |
| 2059 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", |
| 2060 | "only consider these symbols"), |
| 2061 | OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"), |
| 2062 | OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", |
| 2063 | "only display events for these comms"), |
David Ahern | e03eaa4 | 2015-03-24 09:52:41 -0600 | [diff] [blame] | 2064 | OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]", |
| 2065 | "only consider symbols in these pids"), |
| 2066 | OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]", |
| 2067 | "only consider symbols in these tids"), |
Arnaldo Carvalho de Melo | 6125cc8 | 2016-04-14 18:15:18 -0300 | [diff] [blame] | 2068 | OPT_UINTEGER(0, "max-stack", &scripting_max_stack, |
| 2069 | "Set the maximum stack depth when parsing the callchain, " |
| 2070 | "anything beyond the specified depth will be ignored. " |
Arnaldo Carvalho de Melo | 4cb9344 | 2016-04-27 10:16:24 -0300 | [diff] [blame] | 2071 | "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)), |
Arnaldo Carvalho de Melo | 69b6470 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2072 | OPT_BOOLEAN('I', "show-info", &show_full_info, |
| 2073 | "display extended information from perf.data file"), |
| 2074 | OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path, |
| 2075 | "Show the path of [kernel.kallsyms]"), |
Namhyung Kim | ad7ebb9 | 2013-11-26 17:51:12 +0900 | [diff] [blame] | 2076 | OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events, |
| 2077 | "Show the fork/comm/exit events"), |
Namhyung Kim | ba1ddf4 | 2013-11-26 17:54:26 +0900 | [diff] [blame] | 2078 | OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events, |
| 2079 | "Show the mmap events"), |
Adrian Hunter | 7c14898 | 2015-07-21 12:44:06 +0300 | [diff] [blame] | 2080 | OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events, |
| 2081 | "Show context switch events (if recorded)"), |
Yunlong Song | 06af0f2 | 2015-04-02 21:47:16 +0800 | [diff] [blame] | 2082 | OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"), |
Adrian Hunter | 83e1986 | 2015-09-25 16:15:36 +0300 | [diff] [blame] | 2083 | OPT_BOOLEAN(0, "ns", &nanosecs, |
| 2084 | "Use 9 decimal places when displaying time"), |
Adrian Hunter | 7a680eb | 2015-04-09 18:53:56 +0300 | [diff] [blame] | 2085 | OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts", |
| 2086 | "Instruction Tracing options", |
| 2087 | itrace_parse_synth_opts), |
Andi Kleen | a9710ba | 2015-08-07 15:24:05 -0700 | [diff] [blame] | 2088 | OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename, |
| 2089 | "Show full source file name path for source lines"), |
Mark Drayton | 77e0070 | 2015-08-26 12:18:15 -0700 | [diff] [blame] | 2090 | OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, |
| 2091 | "Enable symbol demangling"), |
| 2092 | OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, |
| 2093 | "Enable kernel symbol demangling"), |
| 2094 | |
Arnaldo Carvalho de Melo | 69b6470 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2095 | OPT_END() |
| 2096 | }; |
Yunlong Song | 40cae2b | 2015-03-18 21:35:54 +0800 | [diff] [blame] | 2097 | const char * const script_subcommands[] = { "record", "report", NULL }; |
| 2098 | const char *script_usage[] = { |
Arnaldo Carvalho de Melo | 69b6470 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2099 | "perf script [<options>]", |
| 2100 | "perf script [<options>] record <script> [<record-options>] <command>", |
| 2101 | "perf script [<options>] report <script> [script-args]", |
| 2102 | "perf script [<options>] <script> [<record-options>] <command>", |
| 2103 | "perf script [<options>] <top-script> [script-args]", |
| 2104 | NULL |
| 2105 | }; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 2106 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2107 | setup_scripting(); |
| 2108 | |
Yunlong Song | 40cae2b | 2015-03-18 21:35:54 +0800 | [diff] [blame] | 2109 | argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage, |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2110 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 2111 | |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 2112 | file.path = input_name; |
| 2113 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2114 | if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { |
| 2115 | rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); |
| 2116 | if (!rec_script_path) |
| 2117 | return cmd_record(argc, argv, NULL); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 2118 | } |
| 2119 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2120 | if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { |
| 2121 | rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); |
| 2122 | if (!rep_script_path) { |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 2123 | fprintf(stderr, |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2124 | "Please specify a valid report script" |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 2125 | "(see 'perf script -l' for listing)\n"); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 2126 | return -1; |
| 2127 | } |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 2128 | } |
| 2129 | |
Adrian Hunter | 3c5b645 | 2015-09-25 16:15:51 +0300 | [diff] [blame] | 2130 | if (itrace_synth_opts.callchain && |
| 2131 | itrace_synth_opts.callchain_sz > scripting_max_stack) |
| 2132 | scripting_max_stack = itrace_synth_opts.callchain_sz; |
| 2133 | |
Ben Hutchings | 44e668c | 2010-10-10 16:10:03 +0100 | [diff] [blame] | 2134 | /* make sure PERF_EXEC_PATH is set for scripts */ |
Josh Poimboeuf | 46113a5 | 2015-12-15 09:39:37 -0600 | [diff] [blame] | 2135 | set_argv_exec_path(get_argv_exec_path()); |
Ben Hutchings | 44e668c | 2010-10-10 16:10:03 +0100 | [diff] [blame] | 2136 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2137 | if (argc && !script_name && !rec_script_path && !rep_script_path) { |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 2138 | int live_pipe[2]; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2139 | int rep_args; |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 2140 | pid_t pid; |
| 2141 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2142 | rec_script_path = get_script_path(argv[0], RECORD_SUFFIX); |
| 2143 | rep_script_path = get_script_path(argv[0], REPORT_SUFFIX); |
| 2144 | |
| 2145 | if (!rec_script_path && !rep_script_path) { |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 2146 | usage_with_options_msg(script_usage, options, |
| 2147 | "Couldn't find script `%s'\n\n See perf" |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 2148 | " script -l for available scripts.\n", argv[0]); |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 2149 | } |
| 2150 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2151 | if (is_top_script(argv[0])) { |
| 2152 | rep_args = argc - 1; |
| 2153 | } else { |
| 2154 | int rec_args; |
| 2155 | |
| 2156 | rep_args = has_required_arg(rep_script_path); |
| 2157 | rec_args = (argc - 1) - rep_args; |
| 2158 | if (rec_args < 0) { |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 2159 | usage_with_options_msg(script_usage, options, |
| 2160 | "`%s' script requires options." |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 2161 | "\n\n See perf script -l for available " |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2162 | "scripts and options.\n", argv[0]); |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2163 | } |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | if (pipe(live_pipe) < 0) { |
| 2167 | perror("failed to create pipe"); |
David Ahern | d54b1a9 | 2012-08-26 12:24:46 -0600 | [diff] [blame] | 2168 | return -1; |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | pid = fork(); |
| 2172 | if (pid < 0) { |
| 2173 | perror("failed to fork"); |
David Ahern | d54b1a9 | 2012-08-26 12:24:46 -0600 | [diff] [blame] | 2174 | return -1; |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | if (!pid) { |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2178 | j = 0; |
| 2179 | |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 2180 | dup2(live_pipe[1], 1); |
| 2181 | close(live_pipe[0]); |
| 2182 | |
Robert Richter | 317df65 | 2011-11-25 15:05:25 +0100 | [diff] [blame] | 2183 | if (is_top_script(argv[0])) { |
| 2184 | system_wide = true; |
| 2185 | } else if (!system_wide) { |
David Ahern | d54b1a9 | 2012-08-26 12:24:46 -0600 | [diff] [blame] | 2186 | if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) { |
| 2187 | err = -1; |
| 2188 | goto out; |
| 2189 | } |
Robert Richter | 317df65 | 2011-11-25 15:05:25 +0100 | [diff] [blame] | 2190 | } |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2191 | |
| 2192 | __argv = malloc((argc + 6) * sizeof(const char *)); |
David Ahern | d54b1a9 | 2012-08-26 12:24:46 -0600 | [diff] [blame] | 2193 | if (!__argv) { |
| 2194 | pr_err("malloc failed\n"); |
| 2195 | err = -ENOMEM; |
| 2196 | goto out; |
| 2197 | } |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 2198 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2199 | __argv[j++] = "/bin/sh"; |
| 2200 | __argv[j++] = rec_script_path; |
| 2201 | if (system_wide) |
| 2202 | __argv[j++] = "-a"; |
| 2203 | __argv[j++] = "-q"; |
| 2204 | __argv[j++] = "-o"; |
| 2205 | __argv[j++] = "-"; |
| 2206 | for (i = rep_args + 1; i < argc; i++) |
| 2207 | __argv[j++] = argv[i]; |
| 2208 | __argv[j++] = NULL; |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 2209 | |
| 2210 | execvp("/bin/sh", (char **)__argv); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 2211 | free(__argv); |
Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 2212 | exit(-1); |
| 2213 | } |
| 2214 | |
| 2215 | dup2(live_pipe[0], 0); |
| 2216 | close(live_pipe[1]); |
| 2217 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2218 | __argv = malloc((argc + 4) * sizeof(const char *)); |
David Ahern | d54b1a9 | 2012-08-26 12:24:46 -0600 | [diff] [blame] | 2219 | if (!__argv) { |
| 2220 | pr_err("malloc failed\n"); |
| 2221 | err = -ENOMEM; |
| 2222 | goto out; |
| 2223 | } |
| 2224 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2225 | j = 0; |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 2226 | __argv[j++] = "/bin/sh"; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2227 | __argv[j++] = rep_script_path; |
| 2228 | for (i = 1; i < rep_args + 1; i++) |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 2229 | __argv[j++] = argv[i]; |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2230 | __argv[j++] = "-i"; |
| 2231 | __argv[j++] = "-"; |
Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 2232 | __argv[j++] = NULL; |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 2233 | |
| 2234 | execvp("/bin/sh", (char **)__argv); |
Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 2235 | free(__argv); |
Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 2236 | exit(-1); |
| 2237 | } |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2238 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2239 | if (rec_script_path) |
| 2240 | script_path = rec_script_path; |
| 2241 | if (rep_script_path) |
| 2242 | script_path = rep_script_path; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2243 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2244 | if (script_path) { |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2245 | j = 0; |
| 2246 | |
Robert Richter | 317df65 | 2011-11-25 15:05:25 +0100 | [diff] [blame] | 2247 | if (!rec_script_path) |
| 2248 | system_wide = false; |
David Ahern | d54b1a9 | 2012-08-26 12:24:46 -0600 | [diff] [blame] | 2249 | else if (!system_wide) { |
| 2250 | if (have_cmd(argc - 1, &argv[1]) != 0) { |
| 2251 | err = -1; |
| 2252 | goto out; |
| 2253 | } |
| 2254 | } |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2255 | |
| 2256 | __argv = malloc((argc + 2) * sizeof(const char *)); |
David Ahern | d54b1a9 | 2012-08-26 12:24:46 -0600 | [diff] [blame] | 2257 | if (!__argv) { |
| 2258 | pr_err("malloc failed\n"); |
| 2259 | err = -ENOMEM; |
| 2260 | goto out; |
| 2261 | } |
| 2262 | |
Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 2263 | __argv[j++] = "/bin/sh"; |
| 2264 | __argv[j++] = script_path; |
| 2265 | if (system_wide) |
| 2266 | __argv[j++] = "-a"; |
| 2267 | for (i = 2; i < argc; i++) |
| 2268 | __argv[j++] = argv[i]; |
| 2269 | __argv[j++] = NULL; |
| 2270 | |
| 2271 | execvp("/bin/sh", (char **)__argv); |
| 2272 | free(__argv); |
| 2273 | exit(-1); |
| 2274 | } |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 2275 | |
Tom Zanussi | cf4fee5 | 2010-03-03 01:04:33 -0600 | [diff] [blame] | 2276 | if (!script_name) |
| 2277 | setup_pager(); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 2278 | |
Adrian Hunter | 6f3e5ed | 2013-10-22 10:34:07 +0300 | [diff] [blame] | 2279 | session = perf_session__new(&file, false, &script.tool); |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 2280 | if (session == NULL) |
Taeung Song | 52e02834 | 2014-09-24 10:33:37 +0900 | [diff] [blame] | 2281 | return -1; |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 2282 | |
Jiri Olsa | e90debd | 2013-12-09 11:02:50 +0100 | [diff] [blame] | 2283 | if (header || header_only) { |
| 2284 | perf_session__fprintf_info(session, stdout, show_full_info); |
| 2285 | if (header_only) |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2286 | goto out_delete; |
Jiri Olsa | e90debd | 2013-12-09 11:02:50 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 2289 | if (symbol__init(&session->header.env) < 0) |
Namhyung Kim | 38520dc | 2014-08-12 15:40:42 +0900 | [diff] [blame] | 2290 | goto out_delete; |
| 2291 | |
Adrian Hunter | 6f3e5ed | 2013-10-22 10:34:07 +0300 | [diff] [blame] | 2292 | script.session = session; |
Jiri Olsa | 7322d6c | 2015-08-13 09:17:24 +0200 | [diff] [blame] | 2293 | script__setup_sample_type(&script); |
Adrian Hunter | 6f3e5ed | 2013-10-22 10:34:07 +0300 | [diff] [blame] | 2294 | |
Adrian Hunter | 7a680eb | 2015-04-09 18:53:56 +0300 | [diff] [blame] | 2295 | session->itrace_synth_opts = &itrace_synth_opts; |
| 2296 | |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 2297 | if (cpu_list) { |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2298 | err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap); |
| 2299 | if (err < 0) |
| 2300 | goto out_delete; |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 2301 | } |
| 2302 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 2303 | if (!no_callchain) |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 2304 | symbol_conf.use_callchain = true; |
| 2305 | else |
| 2306 | symbol_conf.use_callchain = false; |
| 2307 | |
Arnaldo Carvalho de Melo | 9ee6742 | 2015-08-03 16:27:40 -0300 | [diff] [blame] | 2308 | if (session->tevent.pevent && |
| 2309 | pevent_set_function_resolver(session->tevent.pevent, |
Arnaldo Carvalho de Melo | ccb3a82 | 2015-07-22 16:43:37 -0300 | [diff] [blame] | 2310 | machine__resolve_kernel_addr, |
| 2311 | &session->machines.host) < 0) { |
| 2312 | pr_err("%s: failed to set libtraceevent function resolver\n", __func__); |
| 2313 | return -1; |
| 2314 | } |
| 2315 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2316 | if (generate_script_lang) { |
| 2317 | struct stat perf_stat; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 2318 | int input; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2319 | |
David Ahern | 2c9e45f7 | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 2320 | if (output_set_by_user()) { |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 2321 | fprintf(stderr, |
| 2322 | "custom fields not supported for generated scripts"); |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2323 | err = -EINVAL; |
| 2324 | goto out_delete; |
David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 2325 | } |
| 2326 | |
Jiri Olsa | cc9784bd | 2013-10-15 16:27:34 +0200 | [diff] [blame] | 2327 | input = open(file.path, O_RDONLY); /* input_name */ |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2328 | if (input < 0) { |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2329 | err = -errno; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2330 | perror("failed to open file"); |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2331 | goto out_delete; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | err = fstat(input, &perf_stat); |
| 2335 | if (err < 0) { |
| 2336 | perror("failed to stat file"); |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2337 | goto out_delete; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | if (!perf_stat.st_size) { |
| 2341 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2342 | goto out_delete; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | scripting_ops = script_spec__lookup(generate_script_lang); |
| 2346 | if (!scripting_ops) { |
| 2347 | fprintf(stderr, "invalid language specifier"); |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2348 | err = -ENOENT; |
| 2349 | goto out_delete; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2350 | } |
| 2351 | |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 2352 | err = scripting_ops->generate_script(session->tevent.pevent, |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 2353 | "perf-script"); |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2354 | goto out_delete; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2355 | } |
| 2356 | |
| 2357 | if (script_name) { |
Tom Zanussi | 586bc5c | 2009-12-15 02:53:35 -0600 | [diff] [blame] | 2358 | err = scripting_ops->start_script(script_name, argc, argv); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2359 | if (err) |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2360 | goto out_delete; |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 2361 | pr_debug("perf script started with script %s\n\n", script_name); |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2362 | script_started = true; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2363 | } |
| 2364 | |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 2365 | |
| 2366 | err = perf_session__check_output_opt(session); |
| 2367 | if (err < 0) |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2368 | goto out_delete; |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 2369 | |
Adrian Hunter | 6f3e5ed | 2013-10-22 10:34:07 +0300 | [diff] [blame] | 2370 | err = __cmd_script(&script); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2371 | |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 2372 | flush_scripting(); |
| 2373 | |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2374 | out_delete: |
Jiri Olsa | cfc8874 | 2016-01-05 22:09:06 +0100 | [diff] [blame] | 2375 | perf_evlist__free_stats(session->evlist); |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 2376 | perf_session__delete(session); |
Namhyung Kim | 6cc870f | 2014-08-12 15:40:33 +0900 | [diff] [blame] | 2377 | |
| 2378 | if (script_started) |
| 2379 | cleanup_scripting(); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 2380 | out: |
| 2381 | return err; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 2382 | } |