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