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