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