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