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