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