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