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