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