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