blob: 0141b26bae47ea8827c894f88e65e16c8d6613fa [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Meloc23c2a02017-09-11 10:50:26 -03002#include <fcntl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03004#include <linux/kernel.h>
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02005#include <linux/types.h>
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +01006#include <perf/cpumap.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03007#include <sys/types.h>
8#include <sys/stat.h>
9#include <unistd.h>
David Carrillo-Cisneros16eb8132017-04-11 23:49:19 -070010#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030011#include <linux/perf_event.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030012#include <linux/zalloc.h>
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +010013#include "cpumap.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030014#include "dso.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020015#include "event.h"
16#include "debug.h"
Namhyung Kimb3cef7f2014-03-17 16:59:21 -030017#include "hist.h"
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -030018#include "machine.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -020019#include "sort.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030020#include "string2.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -020021#include "strlist.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020022#include "thread.h"
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -020023#include "thread_map.h"
Arnaldo Carvalho de Melof3711022019-08-29 15:16:27 -030024#include "time-utils.h"
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030025#include <linux/ctype.h>
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010026#include "map.h"
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -030027#include "util/namespaces.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010028#include "symbol.h"
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030029#include "symbol/kallsyms.h"
Jiri Olsa67424342015-10-25 15:51:28 +010030#include "asm/bug.h"
31#include "stat.h"
Song Liu9aa0bfa2019-01-17 08:15:17 -080032#include "session.h"
Song Liu45178a92019-01-17 08:15:18 -080033#include "bpf-event.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030034#include "tool.h"
Arnaldo Carvalho de Meloc1a604d2019-08-29 15:20:59 -030035#include "../perf.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020036
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020037static const char *perf_event__names[] = {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020038 [0] = "TOTAL",
39 [PERF_RECORD_MMAP] = "MMAP",
Stephane Eranian5c5e8542013-08-21 12:10:25 +020040 [PERF_RECORD_MMAP2] = "MMAP2",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020041 [PERF_RECORD_LOST] = "LOST",
42 [PERF_RECORD_COMM] = "COMM",
43 [PERF_RECORD_EXIT] = "EXIT",
44 [PERF_RECORD_THROTTLE] = "THROTTLE",
45 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
46 [PERF_RECORD_FORK] = "FORK",
47 [PERF_RECORD_READ] = "READ",
48 [PERF_RECORD_SAMPLE] = "SAMPLE",
Adrian Hunter4a96f7a2015-04-30 17:37:29 +030049 [PERF_RECORD_AUX] = "AUX",
Adrian Hunter0ad21f62015-04-30 17:37:30 +030050 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
Kan Liangc4937a92015-05-10 15:13:15 -040051 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
Adrian Hunter02860392015-07-21 12:44:03 +030052 [PERF_RECORD_SWITCH] = "SWITCH",
53 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
Hari Bathinif3b36142017-03-08 02:11:43 +053054 [PERF_RECORD_NAMESPACES] = "NAMESPACES",
Song Liu9aa0bfa2019-01-17 08:15:17 -080055 [PERF_RECORD_KSYMBOL] = "KSYMBOL",
Song Liu45178a92019-01-17 08:15:18 -080056 [PERF_RECORD_BPF_EVENT] = "BPF_EVENT",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020057 [PERF_RECORD_HEADER_ATTR] = "ATTR",
58 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
59 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
60 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
61 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Adrian Hunter3c659ee2014-10-27 15:49:22 +020062 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
Adrian Huntera16ac022015-04-09 18:53:43 +030063 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
64 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
Adrian Huntere9bf54d2015-04-09 18:53:47 +030065 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
Jiri Olsa5f3339d2015-10-25 15:51:19 +010066 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
Jiri Olsa6640b6c2015-10-25 15:51:23 +010067 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
Jiri Olsa374fb9e2015-10-25 15:51:27 +010068 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
Jiri Olsad80518c2015-10-25 15:51:30 +010069 [PERF_RECORD_STAT] = "STAT",
Jiri Olsa2d8f0f12015-10-25 15:51:33 +010070 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
Jiri Olsaffe777252015-10-25 15:51:36 +010071 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
Adrian Hunter46bc29b2016-03-08 10:38:44 +020072 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -070073 [PERF_RECORD_HEADER_FEATURE] = "FEATURE",
Alexey Budankov42e1fd82019-03-18 20:41:33 +030074 [PERF_RECORD_COMPRESSED] = "COMPRESSED",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030075};
76
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020077const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc002010-12-07 12:48:42 +000078{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020079 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc002010-12-07 12:48:42 +000080 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020081 if (!perf_event__names[id])
Thomas Gleixner3835bc002010-12-07 12:48:42 +000082 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020083 return perf_event__names[id];
Thomas Gleixner3835bc002010-12-07 12:48:42 +000084}
85
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -020086struct process_symbol_args {
87 const char *name;
88 u64 start;
89};
90
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -020091static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -070092 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -020093{
94 struct process_symbol_args *args = arg;
95
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -020096 /*
97 * Must be a function or at least an alias, as in PARISC64, where "_text" is
98 * an 'A' to the same address as "_stext".
99 */
Arnaldo Carvalho de Meloe85e0e02018-04-25 17:16:31 -0300100 if (!(kallsyms__is_function(type) ||
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200101 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200102 return 0;
103
104 args->start = start;
105 return 1;
106}
107
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300108int kallsyms__get_function_start(const char *kallsyms_filename,
109 const char *symbol_name, u64 *addr)
Adrian Hunter29b596b2014-01-29 16:14:37 +0200110{
111 struct process_symbol_args args = { .name = symbol_name, };
112
113 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300114 return -1;
Adrian Hunter29b596b2014-01-29 16:14:37 +0200115
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300116 *addr = args.start;
117 return 0;
Adrian Hunter29b596b2014-01-29 16:14:37 +0200118}
119
Jiri Olsa8e381592015-10-25 15:51:29 +0100120void perf_event__read_stat_config(struct perf_stat_config *config,
Jiri Olsa72932372019-08-28 15:57:16 +0200121 struct perf_record_stat_config *event)
Jiri Olsa8e381592015-10-25 15:51:29 +0100122{
123 unsigned i;
124
125 for (i = 0; i < event->nr; i++) {
126
127 switch (event->data[i].tag) {
128#define CASE(__term, __val) \
129 case PERF_STAT_CONFIG_TERM__##__term: \
130 config->__val = event->data[i].val; \
131 break;
132
133 CASE(AGGR_MODE, aggr_mode)
134 CASE(SCALE, scale)
135 CASE(INTERVAL, interval)
136#undef CASE
137 default:
Jiri Olsac5f416e2019-08-28 15:57:09 +0200138 pr_warning("unknown stat config term %" PRI_lu64 "\n",
Jiri Olsa8e381592015-10-25 15:51:29 +0100139 event->data[i].tag);
140 }
141 }
142}
143
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200144size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
145{
Adrian Hunter022c50d2014-07-14 13:02:27 +0300146 const char *s;
147
148 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
149 s = " exec";
150 else
151 s = "";
152
Arnaldo Carvalho de Melo50674062015-02-24 17:20:31 -0300153 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200154}
155
Hari Bathinif3b36142017-03-08 02:11:43 +0530156size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
157{
158 size_t ret = 0;
159 struct perf_ns_link_info *ns_link_info;
160 u32 nr_namespaces, idx;
161
162 ns_link_info = event->namespaces.link_info;
163 nr_namespaces = event->namespaces.nr_namespaces;
164
165 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
166 event->namespaces.pid,
167 event->namespaces.tid,
168 nr_namespaces);
169
170 for (idx = 0; idx < nr_namespaces; idx++) {
171 if (idx && (idx % 4 == 0))
172 ret += fprintf(fp, "\n\t\t ");
173
174 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
175 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
176 (u64)ns_link_info[idx].ino,
177 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
178 }
179
180 return ret;
181}
182
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300183int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200184 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200185 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200186 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200187{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200188 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200189}
190
Hari Bathinif3b36142017-03-08 02:11:43 +0530191int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
192 union perf_event *event,
193 struct perf_sample *sample,
194 struct machine *machine)
195{
196 return machine__process_namespaces_event(machine, event, sample);
197}
198
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300199int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200200 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200201 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300202 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200203{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200204 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800205}
206
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300207int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
208 union perf_event *event,
209 struct perf_sample *sample __maybe_unused,
210 struct machine *machine)
211{
212 return machine__process_aux_event(machine, event);
213}
214
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300215int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
216 union perf_event *event,
217 struct perf_sample *sample __maybe_unused,
218 struct machine *machine)
219{
220 return machine__process_itrace_start_event(machine, event);
221}
222
Kan Liangc4937a92015-05-10 15:13:15 -0400223int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
224 union perf_event *event,
225 struct perf_sample *sample,
226 struct machine *machine)
227{
228 return machine__process_lost_samples_event(machine, event, sample);
229}
230
Adrian Hunter02860392015-07-21 12:44:03 +0300231int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
232 union perf_event *event,
233 struct perf_sample *sample __maybe_unused,
234 struct machine *machine)
235{
236 return machine__process_switch_event(machine, event);
237}
238
Song Liu9aa0bfa2019-01-17 08:15:17 -0800239int perf_event__process_ksymbol(struct perf_tool *tool __maybe_unused,
240 union perf_event *event,
241 struct perf_sample *sample __maybe_unused,
242 struct machine *machine)
243{
244 return machine__process_ksymbol(machine, event, sample);
245}
246
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -0300247int perf_event__process_bpf(struct perf_tool *tool __maybe_unused,
248 union perf_event *event,
249 struct perf_sample *sample,
250 struct machine *machine)
Song Liu45178a92019-01-17 08:15:18 -0800251{
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -0300252 return machine__process_bpf(machine, event, sample);
Song Liu45178a92019-01-17 08:15:18 -0800253}
254
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200255size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
256{
Jiri Olsa1345e2e2019-08-25 20:17:41 +0200257 return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200258 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300259 event->mmap.len, event->mmap.pgoff,
260 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
261 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200262}
263
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200264size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
265{
Jiri Olsab66ced12019-08-25 20:17:42 +0200266 return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
267 " %02x:%02x %"PRI_lu64" %"PRI_lu64"]: %c%c%c%c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200268 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
269 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
270 event->mmap2.min, event->mmap2.ino,
271 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -0400272 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
273 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
274 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
275 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200276 event->mmap2.filename);
277}
278
Jiri Olsaec7fa5962015-10-25 15:51:22 +0100279size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
280{
Jiri Olsa9749b902019-07-21 13:23:50 +0200281 struct perf_thread_map *threads = thread_map__new_event(&event->thread_map);
Jiri Olsaec7fa5962015-10-25 15:51:22 +0100282 size_t ret;
283
284 ret = fprintf(fp, " nr: ");
285
286 if (threads)
287 ret += thread_map__fprintf(threads, fp);
288 else
289 ret += fprintf(fp, "failed to get threads from event\n");
290
Jiri Olsa7836e522019-07-21 13:24:20 +0200291 perf_thread_map__put(threads);
Jiri Olsaec7fa5962015-10-25 15:51:22 +0100292 return ret;
293}
294
Jiri Olsaeb12a1a2015-10-25 15:51:26 +0100295size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
296{
Jiri Olsaf8548392019-07-21 13:23:49 +0200297 struct perf_cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
Jiri Olsaeb12a1a2015-10-25 15:51:26 +0100298 size_t ret;
299
Jiri Olsaa24020e2016-06-28 13:29:04 +0200300 ret = fprintf(fp, ": ");
Jiri Olsaeb12a1a2015-10-25 15:51:26 +0100301
302 if (cpus)
303 ret += cpu_map__fprintf(cpus, fp);
304 else
305 ret += fprintf(fp, "failed to get cpumap from event\n");
306
Jiri Olsa38f01d82019-07-21 13:24:17 +0200307 perf_cpu_map__put(cpus);
Jiri Olsaeb12a1a2015-10-25 15:51:26 +0100308 return ret;
309}
310
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300311int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200312 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200313 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200314 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800315{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200316 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200317}
318
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200319int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
320 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200321 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200322 struct machine *machine)
323{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200324 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200325}
326
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200327size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
328{
329 return fprintf(fp, "(%d:%d):(%d:%d)\n",
330 event->fork.pid, event->fork.tid,
331 event->fork.ppid, event->fork.ptid);
332}
333
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300334int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200335 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200336 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300337 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200338{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200339 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200340}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200341
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300342int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
343 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200344 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300345 struct machine *machine)
346{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200347 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300348}
349
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300350size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
351{
Jiri Olsaaedebdc2019-08-28 15:57:05 +0200352 return fprintf(fp, " offset: %#"PRI_lx64" size: %#"PRI_lx64" flags: %#"PRI_lx64" [%s%s%s]\n",
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300353 event->aux.aux_offset, event->aux.aux_size,
354 event->aux.flags,
355 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
Alexander Shishkin05a1f472017-03-16 18:41:59 +0200356 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
357 event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : "");
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300358}
359
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300360size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
361{
362 return fprintf(fp, " pid: %u tid: %u\n",
363 event->itrace_start.pid, event->itrace_start.tid);
364}
365
Adrian Hunter02860392015-07-21 12:44:03 +0300366size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
367{
368 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
Alexey Budankovb3f35b52018-04-09 10:26:05 +0300369 const char *in_out = !out ? "IN " :
370 !(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT) ?
371 "OUT " : "OUT preempt";
Adrian Hunter02860392015-07-21 12:44:03 +0300372
373 if (event->header.type == PERF_RECORD_SWITCH)
374 return fprintf(fp, " %s\n", in_out);
375
376 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
377 in_out, out ? "next" : "prev",
378 event->context_switch.next_prev_pid,
379 event->context_switch.next_prev_tid);
380}
381
Jiri Olsa3d7c27b2018-01-07 17:03:53 +0100382static size_t perf_event__fprintf_lost(union perf_event *event, FILE *fp)
383{
Jiri Olsa5290ed62019-08-25 20:17:46 +0200384 return fprintf(fp, " lost %" PRI_lu64 "\n", event->lost.lost);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +0100385}
386
Song Liu9aa0bfa2019-01-17 08:15:17 -0800387size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp)
388{
Jiri Olsaf15e3c22019-08-25 20:17:50 +0200389 return fprintf(fp, " addr %" PRI_lx64 " len %u type %u flags 0x%x name %s\n",
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300390 event->ksymbol.addr, event->ksymbol.len,
391 event->ksymbol.ksym_type,
392 event->ksymbol.flags, event->ksymbol.name);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800393}
394
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -0300395size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp)
Song Liu45178a92019-01-17 08:15:18 -0800396{
Jiri Olsa82017872019-05-08 15:20:09 +0200397 return fprintf(fp, " type %u, flags %u, id %u\n",
Arnaldo Carvalho de Melo6a1b3592019-08-26 19:20:35 -0300398 event->bpf.type, event->bpf.flags, event->bpf.id);
Song Liu45178a92019-01-17 08:15:18 -0800399}
400
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200401size_t perf_event__fprintf(union perf_event *event, FILE *fp)
402{
403 size_t ret = fprintf(fp, "PERF_RECORD_%s",
404 perf_event__name(event->header.type));
405
406 switch (event->header.type) {
407 case PERF_RECORD_COMM:
408 ret += perf_event__fprintf_comm(event, fp);
409 break;
410 case PERF_RECORD_FORK:
411 case PERF_RECORD_EXIT:
412 ret += perf_event__fprintf_task(event, fp);
413 break;
414 case PERF_RECORD_MMAP:
415 ret += perf_event__fprintf_mmap(event, fp);
416 break;
Hari Bathinif3b36142017-03-08 02:11:43 +0530417 case PERF_RECORD_NAMESPACES:
418 ret += perf_event__fprintf_namespaces(event, fp);
419 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200420 case PERF_RECORD_MMAP2:
421 ret += perf_event__fprintf_mmap2(event, fp);
422 break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300423 case PERF_RECORD_AUX:
424 ret += perf_event__fprintf_aux(event, fp);
425 break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300426 case PERF_RECORD_ITRACE_START:
427 ret += perf_event__fprintf_itrace_start(event, fp);
428 break;
Adrian Hunter02860392015-07-21 12:44:03 +0300429 case PERF_RECORD_SWITCH:
430 case PERF_RECORD_SWITCH_CPU_WIDE:
431 ret += perf_event__fprintf_switch(event, fp);
432 break;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +0100433 case PERF_RECORD_LOST:
434 ret += perf_event__fprintf_lost(event, fp);
435 break;
Song Liu9aa0bfa2019-01-17 08:15:17 -0800436 case PERF_RECORD_KSYMBOL:
437 ret += perf_event__fprintf_ksymbol(event, fp);
438 break;
Song Liu45178a92019-01-17 08:15:18 -0800439 case PERF_RECORD_BPF_EVENT:
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -0300440 ret += perf_event__fprintf_bpf(event, fp);
Song Liu45178a92019-01-17 08:15:18 -0800441 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200442 default:
443 ret += fprintf(fp, "\n");
444 }
445
446 return ret;
447}
448
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300449int perf_event__process(struct perf_tool *tool __maybe_unused,
450 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200451 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300452 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530453{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200454 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530455}
456
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300457struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
458 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200459{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300460 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300461 struct machine *machine = mg->machine;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300462 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200463
Arnaldo Carvalho de Melod3a022c2019-11-04 09:59:48 -0300464 al->mg = mg;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300465 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200466 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800467 al->cpumode = cpumode;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300468 al->filtered = 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200469
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200470 if (machine == NULL) {
471 al->map = NULL;
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300472 return NULL;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200473 }
474
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800475 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200476 al->level = 'k';
Arnaldo Carvalho de Melod3a022c2019-11-04 09:59:48 -0300477 al->mg = mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300478 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800479 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200480 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800481 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
482 al->level = 'g';
Arnaldo Carvalho de Melod3a022c2019-11-04 09:59:48 -0300483 al->mg = mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300484 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500485 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
486 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800487 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500488 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200489 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800490
491 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
492 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
493 !perf_guest)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300494 al->filtered |= (1 << HIST_FILTER__GUEST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800495 if ((cpumode == PERF_RECORD_MISC_USER ||
496 cpumode == PERF_RECORD_MISC_KERNEL) &&
497 !perf_host)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300498 al->filtered |= (1 << HIST_FILTER__HOST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800499
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300500 return NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200501 }
Arnaldo Carvalho de Meloedeb0c92018-10-16 17:08:29 -0300502
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300503 al->map = map_groups__find(mg, al->addr);
Arnaldo Carvalho de Meloedeb0c92018-10-16 17:08:29 -0300504 if (al->map != NULL) {
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300505 /*
506 * Kernel maps might be changed when loading symbols so loading
507 * must be done prior to using kernel maps.
508 */
509 if (load_map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300510 map__load(al->map);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200511 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300512 }
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300513
514 return al->map;
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200515}
516
Adrian Hunter8e80ad92018-11-06 23:07:10 +0200517/*
518 * For branch stacks or branch samples, the sample cpumode might not be correct
519 * because it applies only to the sample 'ip' and not necessary to 'addr' or
520 * branch stack addresses. If possible, use a fallback to deal with those cases.
521 */
522struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
523 struct addr_location *al)
524{
525 struct map *map = thread__find_map(thread, cpumode, addr, al);
526 struct machine *machine = thread->mg->machine;
527 u8 addr_cpumode = machine__addr_cpumode(machine, cpumode, addr);
528
529 if (map || addr_cpumode == cpumode)
530 return map;
531
532 return thread__find_map(thread, addr_cpumode, addr, al);
533}
534
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -0300535struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
536 u64 addr, struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200537{
Arnaldo Carvalho de Melod9a5f272018-04-24 12:05:48 -0300538 al->sym = NULL;
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300539 if (thread__find_map(thread, cpumode, addr, al))
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300540 al->sym = map__find_symbol(al->map, al->addr);
Arnaldo Carvalho de Melod9a5f272018-04-24 12:05:48 -0300541 return al->sym;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200542}
543
Adrian Hunter8e80ad92018-11-06 23:07:10 +0200544struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
545 u64 addr, struct addr_location *al)
546{
547 al->sym = NULL;
548 if (thread__find_map_fb(thread, cpumode, addr, al))
549 al->sym = map__find_symbol(al->map, al->addr);
550 return al->sym;
551}
552
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300553/*
554 * Callers need to drop the reference to al->thread, obtained in
555 * machine__findnew_thread()
556 */
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300557int machine__resolve(struct machine *machine, struct addr_location *al,
558 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200559{
Adrian Hunteref893252013-08-27 11:23:06 +0300560 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900561 sample->tid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200562
563 if (thread == NULL)
564 return -1;
565
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200566 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melof07a2d32018-04-24 10:49:50 -0300567 thread__find_map(thread, sample->cpumode, sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200568 dump_printf(" ...... dso: %s\n",
569 al->map ? al->map->dso->long_name :
570 al->level == 'H' ? "[hypervisor]" : "<not found>");
Namhyung Kim466fa762014-03-17 17:12:27 -0300571
572 if (thread__is_filtered(thread))
573 al->filtered |= (1 << HIST_FILTER__THREAD);
574
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300575 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200576 al->cpu = sample->cpu;
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400577 al->socket = -1;
Milian Wolff1fb7d062017-10-19 13:38:35 +0200578 al->srcline = NULL;
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400579
580 if (al->cpu >= 0) {
581 struct perf_env *env = machine->env;
582
583 if (env && env->cpu)
584 al->socket = env->cpu[al->cpu].socket_id;
585 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200586
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300587 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900588 struct dso *dso = al->map->dso;
589
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300590 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900591 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
592 dso->short_name) ||
593 (dso->short_name != dso->long_name &&
594 strlist__has_entry(symbol_conf.dso_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300595 dso->long_name))))) {
596 al->filtered |= (1 << HIST_FILTER__DSO);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300597 }
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300598
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300599 al->sym = map__find_symbol(al->map, al->addr);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300600 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200601
Feng Tang1500b932012-09-07 16:42:23 +0800602 if (symbol_conf.sym_list &&
603 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300604 al->sym->name))) {
605 al->filtered |= (1 << HIST_FILTER__SYMBOL);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300606 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200607
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200608 return 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200609}
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300610
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300611/*
612 * The preprocess_sample method will return with reference counts for the
613 * in it, when done using (and perhaps getting ref counts if needing to
614 * keep a pointer to one of those entries) it must be paired with
615 * addr_location__put(), so that the refcounts can be decremented.
616 */
617void addr_location__put(struct addr_location *al)
618{
619 thread__zput(al->thread);
620}
621
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300622bool is_bts_event(struct perf_event_attr *attr)
623{
624 return attr->type == PERF_TYPE_HARDWARE &&
625 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
626 attr->sample_period == 1;
627}
628
629bool sample_addr_correlates_sym(struct perf_event_attr *attr)
630{
631 if (attr->type == PERF_TYPE_SOFTWARE &&
632 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
633 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
634 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
635 return true;
636
637 if (is_bts_event(attr))
638 return true;
639
640 return false;
641}
642
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300643void thread__resolve(struct thread *thread, struct addr_location *al,
644 struct perf_sample *sample)
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300645{
Adrian Hunter225f99e2018-11-06 23:07:11 +0200646 thread__find_map_fb(thread, sample->cpumode, sample->addr, al);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300647
648 al->cpu = sample->cpu;
649 al->sym = NULL;
650
651 if (al->map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300652 al->sym = map__find_symbol(al->map, al->addr);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300653}