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