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