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