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