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