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