blob: 7fa7a303e476a5dfd02e8d2892e7b0776ca0f1a9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -03002#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03003#include <errno.h>
Arnaldo Carvalho de Meloc23c2a02017-09-11 10:50:26 -03004#include <fcntl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03005#include <inttypes.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03006#include <linux/kernel.h>
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02007#include <linux/types.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03008#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
David Carrillo-Cisneros16eb8132017-04-11 23:49:19 -070011#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
Wang Nand7e404a2016-09-06 04:58:29 +000012#include <api/fs/fs.h>
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030013#include <linux/perf_event.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030014#include <linux/zalloc.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 Melodaecf9e2019-01-28 00:03:34 +010027#include "symbol.h"
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030028#include "symbol/kallsyms.h"
Jiri Olsa67424342015-10-25 15:51:28 +010029#include "asm/bug.h"
30#include "stat.h"
Song Liu9aa0bfa2019-01-17 08:15:17 -080031#include "session.h"
Song Liu45178a92019-01-17 08:15:18 -080032#include "bpf-event.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020033
Mark Drayton3fcb10e2018-12-04 12:34:20 -080034#define DEFAULT_PROC_MAP_PARSE_TIMEOUT 500
35
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020036static const char *perf_event__names[] = {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020037 [0] = "TOTAL",
38 [PERF_RECORD_MMAP] = "MMAP",
Stephane Eranian5c5e8542013-08-21 12:10:25 +020039 [PERF_RECORD_MMAP2] = "MMAP2",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020040 [PERF_RECORD_LOST] = "LOST",
41 [PERF_RECORD_COMM] = "COMM",
42 [PERF_RECORD_EXIT] = "EXIT",
43 [PERF_RECORD_THROTTLE] = "THROTTLE",
44 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
45 [PERF_RECORD_FORK] = "FORK",
46 [PERF_RECORD_READ] = "READ",
47 [PERF_RECORD_SAMPLE] = "SAMPLE",
Adrian Hunter4a96f7a2015-04-30 17:37:29 +030048 [PERF_RECORD_AUX] = "AUX",
Adrian Hunter0ad21f62015-04-30 17:37:30 +030049 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
Kan Liangc4937a92015-05-10 15:13:15 -040050 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
Adrian Hunter02860392015-07-21 12:44:03 +030051 [PERF_RECORD_SWITCH] = "SWITCH",
52 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
Hari Bathinif3b36142017-03-08 02:11:43 +053053 [PERF_RECORD_NAMESPACES] = "NAMESPACES",
Song Liu9aa0bfa2019-01-17 08:15:17 -080054 [PERF_RECORD_KSYMBOL] = "KSYMBOL",
Song Liu45178a92019-01-17 08:15:18 -080055 [PERF_RECORD_BPF_EVENT] = "BPF_EVENT",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020056 [PERF_RECORD_HEADER_ATTR] = "ATTR",
57 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
58 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
59 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
60 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Adrian Hunter3c659ee2014-10-27 15:49:22 +020061 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
Adrian Huntera16ac022015-04-09 18:53:43 +030062 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
63 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
Adrian Huntere9bf54d2015-04-09 18:53:47 +030064 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
Jiri Olsa5f3339d2015-10-25 15:51:19 +010065 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
Jiri Olsa6640b6c2015-10-25 15:51:23 +010066 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
Jiri Olsa374fb9e2015-10-25 15:51:27 +010067 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
Jiri Olsad80518c2015-10-25 15:51:30 +010068 [PERF_RECORD_STAT] = "STAT",
Jiri Olsa2d8f0f12015-10-25 15:51:33 +010069 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
Jiri Olsaffe777252015-10-25 15:51:36 +010070 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
Adrian Hunter46bc29b2016-03-08 10:38:44 +020071 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -070072 [PERF_RECORD_HEADER_FEATURE] = "FEATURE",
Alexey Budankov42e1fd82019-03-18 20:41:33 +030073 [PERF_RECORD_COMPRESSED] = "COMPRESSED",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030074};
75
Hari Bathinif3b36142017-03-08 02:11:43 +053076static const char *perf_ns__names[] = {
77 [NET_NS_INDEX] = "net",
78 [UTS_NS_INDEX] = "uts",
79 [IPC_NS_INDEX] = "ipc",
80 [PID_NS_INDEX] = "pid",
81 [USER_NS_INDEX] = "user",
82 [MNT_NS_INDEX] = "mnt",
83 [CGROUP_NS_INDEX] = "cgroup",
84};
85
Mark Drayton3fcb10e2018-12-04 12:34:20 -080086unsigned int proc_map_timeout = DEFAULT_PROC_MAP_PARSE_TIMEOUT;
87
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020088const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc002010-12-07 12:48:42 +000089{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020090 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc002010-12-07 12:48:42 +000091 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020092 if (!perf_event__names[id])
Thomas Gleixner3835bc002010-12-07 12:48:42 +000093 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020094 return perf_event__names[id];
Thomas Gleixner3835bc002010-12-07 12:48:42 +000095}
96
Hari Bathinif3b36142017-03-08 02:11:43 +053097static const char *perf_ns__name(unsigned int id)
98{
99 if (id >= ARRAY_SIZE(perf_ns__names))
100 return "UNKNOWN";
101 return perf_ns__names[id];
102}
103
Adrian Huntera8ce99b2018-05-22 13:54:37 +0300104int perf_tool__process_synth_event(struct perf_tool *tool,
105 union perf_event *event,
106 struct machine *machine,
107 perf_event__handler_t process)
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300108{
109 struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200110 .pid = -1,
111 .tid = -1,
112 .time = -1,
113 .stream_id = -1,
114 .cpu = -1,
115 .period = 1,
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300116 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
117 };
118
119 return process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200120};
121
David Ahern5aa0b032015-03-30 14:35:57 -0600122/*
123 * Assumes that the first 4095 bytes of /proc/pid/stat contains
David Ahernca6c41c2015-03-30 14:35:58 -0600124 * the comm, tgid and ppid.
David Ahern5aa0b032015-03-30 14:35:57 -0600125 */
David Ahernca6c41c2015-03-30 14:35:58 -0600126static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
127 pid_t *tgid, pid_t *ppid)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200128{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200129 char filename[PATH_MAX];
David Ahern5aa0b032015-03-30 14:35:57 -0600130 char bf[4096];
131 int fd;
Andrzej Hajda38349662015-10-06 11:00:17 +0200132 size_t size = 0;
133 ssize_t n;
Taeung Songbdd97ca2017-04-07 23:24:21 +0900134 char *name, *tgids, *ppids;
David Ahernca6c41c2015-03-30 14:35:58 -0600135
136 *tgid = -1;
137 *ppid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200138
139 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
140
David Ahern5aa0b032015-03-30 14:35:57 -0600141 fd = open(filename, O_RDONLY);
142 if (fd < 0) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200143 pr_debug("couldn't open %s\n", filename);
David Ahernca6c41c2015-03-30 14:35:58 -0600144 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200145 }
146
David Ahern5aa0b032015-03-30 14:35:57 -0600147 n = read(fd, bf, sizeof(bf) - 1);
148 close(fd);
149 if (n <= 0) {
David Ahernca6c41c2015-03-30 14:35:58 -0600150 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
David Ahern5aa0b032015-03-30 14:35:57 -0600151 pid);
152 return -1;
153 }
154 bf[n] = '\0';
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200155
David Ahern5aa0b032015-03-30 14:35:57 -0600156 name = strstr(bf, "Name:");
157 tgids = strstr(bf, "Tgid:");
David Ahernca6c41c2015-03-30 14:35:58 -0600158 ppids = strstr(bf, "PPid:");
David Ahernf5faf722011-12-22 11:30:02 -0700159
David Ahern5aa0b032015-03-30 14:35:57 -0600160 if (name) {
Jiri Olsa9d43f5e2017-04-20 11:24:30 +0200161 char *nl;
162
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -0300163 name = skip_spaces(name + 5); /* strlen("Name:") */
Jiri Olsa9d43f5e2017-04-20 11:24:30 +0200164 nl = strchr(name, '\n');
165 if (nl)
166 *nl = '\0';
167
David Ahern5aa0b032015-03-30 14:35:57 -0600168 size = strlen(name);
169 if (size >= len)
170 size = len - 1;
171 memcpy(comm, name, size);
172 comm[size] = '\0';
173 } else {
174 pr_debug("Name: string not found for pid %d\n", pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200175 }
176
David Ahern5aa0b032015-03-30 14:35:57 -0600177 if (tgids) {
178 tgids += 5; /* strlen("Tgid:") */
David Ahernca6c41c2015-03-30 14:35:58 -0600179 *tgid = atoi(tgids);
David Ahern5aa0b032015-03-30 14:35:57 -0600180 } else {
181 pr_debug("Tgid: string not found for pid %d\n", pid);
182 }
David Ahernf5faf722011-12-22 11:30:02 -0700183
David Ahernca6c41c2015-03-30 14:35:58 -0600184 if (ppids) {
185 ppids += 5; /* strlen("PPid:") */
186 *ppid = atoi(ppids);
187 } else {
188 pr_debug("PPid: string not found for pid %d\n", pid);
189 }
190
191 return 0;
David Ahernf5faf722011-12-22 11:30:02 -0700192}
193
David Ahernca6c41c2015-03-30 14:35:58 -0600194static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
195 struct machine *machine,
196 pid_t *tgid, pid_t *ppid)
David Ahernf5faf722011-12-22 11:30:02 -0700197{
David Ahernf5faf722011-12-22 11:30:02 -0700198 size_t size;
David Ahernca6c41c2015-03-30 14:35:58 -0600199
200 *ppid = -1;
David Ahernf5faf722011-12-22 11:30:02 -0700201
202 memset(&event->comm, 0, sizeof(event->comm));
203
David Ahernca6c41c2015-03-30 14:35:58 -0600204 if (machine__is_host(machine)) {
205 if (perf_event__get_comm_ids(pid, event->comm.comm,
206 sizeof(event->comm.comm),
207 tgid, ppid) != 0) {
208 return -1;
209 }
210 } else {
211 *tgid = machine->pid;
212 }
Dongsheng Yangf5db57c42013-12-20 15:52:59 -0500213
David Ahernca6c41c2015-03-30 14:35:58 -0600214 if (*tgid < 0)
215 return -1;
David Ahernf5faf722011-12-22 11:30:02 -0700216
David Ahernca6c41c2015-03-30 14:35:58 -0600217 event->comm.pid = *tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200218 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700219
220 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300221 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200222 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200223 event->comm.header.size = (sizeof(event->comm) -
224 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200225 machine->id_hdr_size);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500226 event->comm.tid = pid;
David Ahernca6c41c2015-03-30 14:35:58 -0600227
228 return 0;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300229}
230
Namhyung Kime803cf92015-09-22 09:24:55 +0900231pid_t perf_event__synthesize_comm(struct perf_tool *tool,
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300232 union perf_event *event, pid_t pid,
233 perf_event__handler_t process,
234 struct machine *machine)
235{
David Ahernca6c41c2015-03-30 14:35:58 -0600236 pid_t tgid, ppid;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300237
David Ahernca6c41c2015-03-30 14:35:58 -0600238 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
239 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200240
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300241 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
Don Zickusbfd66cc2014-02-26 10:45:26 -0500242 return -1;
David Ahern1e6d5322012-08-26 12:24:42 -0600243
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200244 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200245}
246
Hari Bathinie907caf2017-03-08 02:11:51 +0530247static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
248 struct perf_ns_link_info *ns_link_info)
249{
250 struct stat64 st;
251 char proc_ns[128];
252
253 sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
254 if (stat64(proc_ns, &st) == 0) {
255 ns_link_info->dev = st.st_dev;
256 ns_link_info->ino = st.st_ino;
257 }
258}
259
260int perf_event__synthesize_namespaces(struct perf_tool *tool,
261 union perf_event *event,
262 pid_t pid, pid_t tgid,
263 perf_event__handler_t process,
264 struct machine *machine)
265{
266 u32 idx;
267 struct perf_ns_link_info *ns_link_info;
268
269 if (!tool || !tool->namespace_events)
270 return 0;
271
272 memset(&event->namespaces, 0, (sizeof(event->namespaces) +
273 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
274 machine->id_hdr_size));
275
276 event->namespaces.pid = tgid;
277 event->namespaces.tid = pid;
278
279 event->namespaces.nr_namespaces = NR_NAMESPACES;
280
281 ns_link_info = event->namespaces.link_info;
282
283 for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
284 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
285 &ns_link_info[idx]);
286
287 event->namespaces.header.type = PERF_RECORD_NAMESPACES;
288
289 event->namespaces.header.size = (sizeof(event->namespaces) +
290 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
291 machine->id_hdr_size);
292
293 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
294 return -1;
295
296 return 0;
297}
298
Don Zickus363b7852014-03-14 10:43:44 -0400299static int perf_event__synthesize_fork(struct perf_tool *tool,
David Ahernca6c41c2015-03-30 14:35:58 -0600300 union perf_event *event,
301 pid_t pid, pid_t tgid, pid_t ppid,
302 perf_event__handler_t process,
Don Zickus363b7852014-03-14 10:43:44 -0400303 struct machine *machine)
304{
305 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
306
David Ahern7764a382015-04-09 12:48:27 -0400307 /*
308 * for main thread set parent to ppid from status file. For other
309 * threads set parent pid to main thread. ie., assume main thread
310 * spawns all threads in a process
311 */
312 if (tgid == pid) {
313 event->fork.ppid = ppid;
314 event->fork.ptid = ppid;
315 } else {
316 event->fork.ppid = tgid;
317 event->fork.ptid = tgid;
318 }
Don Zickus363b7852014-03-14 10:43:44 -0400319 event->fork.pid = tgid;
320 event->fork.tid = pid;
321 event->fork.header.type = PERF_RECORD_FORK;
David Miller4f8f3822018-10-30 22:24:04 -0700322 event->fork.header.misc = PERF_RECORD_MISC_FORK_EXEC;
Don Zickus363b7852014-03-14 10:43:44 -0400323
324 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
325
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300326 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
Don Zickus363b7852014-03-14 10:43:44 -0400327 return -1;
328
329 return 0;
330}
331
Jiri Olsaa18382b2014-01-07 13:47:20 +0100332int perf_event__synthesize_mmap_events(struct perf_tool *tool,
333 union perf_event *event,
334 pid_t pid, pid_t tgid,
335 perf_event__handler_t process,
336 struct machine *machine,
Mark Drayton3fcb10e2018-12-04 12:34:20 -0800337 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200338{
339 char filename[PATH_MAX];
340 FILE *fp;
Kan Liang930e6fc2015-06-17 09:51:10 -0400341 unsigned long long t;
342 bool truncation = false;
Kan Liang9d9cad72015-06-17 09:51:11 -0400343 unsigned long long timeout = proc_map_timeout * 1000000ULL;
David Ahern1e6d5322012-08-26 12:24:42 -0600344 int rc = 0;
Wang Nand7e404a2016-09-06 04:58:29 +0000345 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
346 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200347
Dongsheng Yangc239c25a2013-12-20 15:53:00 -0500348 if (machine__is_default_guest(machine))
349 return 0;
350
Stephane Eranian88b897a2017-03-15 10:17:13 -0700351 snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
352 machine->root_dir, pid, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200353
354 fp = fopen(filename, "r");
355 if (fp == NULL) {
356 /*
357 * We raced with a task exiting - just return:
358 */
359 pr_debug("couldn't open %s\n", filename);
360 return -1;
361 }
362
Don Zickusa5a5ba72014-05-30 10:49:42 -0400363 event->header.type = PERF_RECORD_MMAP2;
Kan Liang930e6fc2015-06-17 09:51:10 -0400364 t = rdclock();
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200365
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200366 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900367 char bf[BUFSIZ];
368 char prot[5];
369 char execname[PATH_MAX];
370 char anonstr[] = "//anon";
Don Zickusa5a5ba72014-05-30 10:49:42 -0400371 unsigned int ino;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200372 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200373 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900374
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200375 if (fgets(bf, sizeof(bf), fp) == NULL)
376 break;
377
Kan Liang9d9cad72015-06-17 09:51:11 -0400378 if ((rdclock() - t) > timeout) {
379 pr_warning("Reading %s time out. "
380 "You may want to increase "
381 "the time limit by --proc-map-timeout\n",
382 filename);
Kan Liang930e6fc2015-06-17 09:51:10 -0400383 truncation = true;
384 goto out;
385 }
386
Namhyung Kim60648032012-11-11 23:20:50 +0900387 /* ensure null termination since stack will be reused. */
388 strcpy(execname, "");
389
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200390 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Jiri Olsab66ced12019-08-25 20:17:42 +0200391 n = sscanf(bf, "%"PRI_lx64"-%"PRI_lx64" %s %"PRI_lx64" %x:%x %u %[^\n]\n",
Don Zickusa5a5ba72014-05-30 10:49:42 -0400392 &event->mmap2.start, &event->mmap2.len, prot,
393 &event->mmap2.pgoff, &event->mmap2.maj,
394 &event->mmap2.min,
395 &ino, execname);
396
Don Zickus9d4ecc82013-11-13 15:32:06 -0300397 /*
398 * Anon maps don't have the execname.
399 */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400400 if (n < 7)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200401 continue;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400402
403 event->mmap2.ino = (u64)ino;
404
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300405 /*
406 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
407 */
Dongsheng Yang73547aa2013-12-20 15:52:58 -0500408 if (machine__is_host(machine))
409 event->header.misc = PERF_RECORD_MISC_USER;
410 else
411 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900412
Don Zickus7ef80702014-05-19 15:13:49 -0400413 /* map protection and flags bits */
414 event->mmap2.prot = 0;
415 event->mmap2.flags = 0;
416 if (prot[0] == 'r')
417 event->mmap2.prot |= PROT_READ;
418 if (prot[1] == 'w')
419 event->mmap2.prot |= PROT_WRITE;
420 if (prot[2] == 'x')
421 event->mmap2.prot |= PROT_EXEC;
422
423 if (prot[3] == 's')
424 event->mmap2.flags |= MAP_SHARED;
425 else
426 event->mmap2.flags |= MAP_PRIVATE;
427
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300428 if (prot[2] != 'x') {
429 if (!mmap_data || prot[0] != 'r')
430 continue;
431
432 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
433 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200434
Kan Liang930e6fc2015-06-17 09:51:10 -0400435out:
436 if (truncation)
437 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
438
Namhyung Kim60648032012-11-11 23:20:50 +0900439 if (!strcmp(execname, ""))
440 strcpy(execname, anonstr);
Arnaldo Carvalho de Melofbef1032016-09-12 16:47:57 -0300441
Adrian Hunter973186c2016-09-23 17:38:34 +0300442 if (hugetlbfs_mnt_len &&
443 !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
Wang Nand7e404a2016-09-06 04:58:29 +0000444 strcpy(execname, anonstr);
445 event->mmap2.flags |= MAP_HUGETLB;
446 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200447
Namhyung Kim60648032012-11-11 23:20:50 +0900448 size = strlen(execname) + 1;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400449 memcpy(event->mmap2.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900450 size = PERF_ALIGN(size, sizeof(u64));
Don Zickusa5a5ba72014-05-30 10:49:42 -0400451 event->mmap2.len -= event->mmap.start;
452 event->mmap2.header.size = (sizeof(event->mmap2) -
453 (sizeof(event->mmap2.filename) - size));
454 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
455 event->mmap2.header.size += machine->id_hdr_size;
456 event->mmap2.pid = tgid;
457 event->mmap2.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000458
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300459 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
Namhyung Kim60648032012-11-11 23:20:50 +0900460 rc = -1;
461 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200462 }
Kan Liang930e6fc2015-06-17 09:51:10 -0400463
464 if (truncation)
465 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200466 }
467
468 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600469 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200470}
471
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200472int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200473 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200474 struct machine *machine)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200475{
David Ahern1e6d5322012-08-26 12:24:42 -0600476 int rc = 0;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300477 struct map *pos;
Arnaldo Carvalho de Melo68a74182018-04-24 17:06:25 -0300478 struct maps *maps = machine__kernel_maps(machine);
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200479 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200480 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200481 if (event == NULL) {
482 pr_debug("Not enough memory synthesizing mmap event "
483 "for kernel modules\n");
484 return -1;
485 }
486
487 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200488
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800489 /*
490 * kernel uses 0 for user space maps, see kernel/perf_event.c
491 * __perf_event_mmap
492 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300493 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200494 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800495 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200496 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800497
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300498 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200499 size_t size;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200500
Adrian Hunter5759a682018-05-22 13:54:35 +0300501 if (!__map__is_kmodule(pos))
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200502 continue;
503
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300504 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200505 event->mmap.header.type = PERF_RECORD_MMAP;
506 event->mmap.header.size = (sizeof(event->mmap) -
507 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200508 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
509 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200510 event->mmap.start = pos->start;
511 event->mmap.len = pos->end - pos->start;
512 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200513
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200514 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200515 pos->dso->long_name_len + 1);
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300516 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
David Ahern1e6d5322012-08-26 12:24:42 -0600517 rc = -1;
518 break;
519 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200520 }
521
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200522 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600523 return rc;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200524}
525
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200526static int __event__synthesize_thread(union perf_event *comm_event,
527 union perf_event *mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400528 union perf_event *fork_event,
Hari Bathinie907caf2017-03-08 02:11:51 +0530529 union perf_event *namespaces_event,
David Aherndefd8d32011-12-22 11:30:01 -0700530 pid_t pid, int full,
Hari Bathinie907caf2017-03-08 02:11:51 +0530531 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200532 struct perf_tool *tool,
Kan Liang9d9cad72015-06-17 09:51:11 -0400533 struct machine *machine,
Mark Drayton3fcb10e2018-12-04 12:34:20 -0800534 bool mmap_data)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200535{
Don Zickusbfd66cc2014-02-26 10:45:26 -0500536 char filename[PATH_MAX];
537 DIR *tasks;
Arnaldo Carvalho de Melo7093b4c2016-04-08 11:32:15 -0300538 struct dirent *dirent;
David Ahernca6c41c2015-03-30 14:35:58 -0600539 pid_t tgid, ppid;
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300540 int rc = 0;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500541
542 /* special case: only send one comm event using passed in pid */
543 if (!full) {
544 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
545 process, machine);
546
547 if (tgid == -1)
548 return -1;
549
Hari Bathinie907caf2017-03-08 02:11:51 +0530550 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
551 tgid, process, machine) < 0)
552 return -1;
553
Konstantin Khlebnikove5adfc32018-08-07 12:09:01 +0300554 /*
555 * send mmap only for thread group leader
556 * see thread__init_map_groups
557 */
558 if (pid == tgid &&
559 perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Mark Drayton3fcb10e2018-12-04 12:34:20 -0800560 process, machine, mmap_data))
Konstantin Khlebnikove5adfc32018-08-07 12:09:01 +0300561 return -1;
Hari Bathinie907caf2017-03-08 02:11:51 +0530562
Konstantin Khlebnikove5adfc32018-08-07 12:09:01 +0300563 return 0;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500564 }
565
566 if (machine__is_default_guest(machine))
567 return 0;
568
569 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
570 machine->root_dir, pid);
571
572 tasks = opendir(filename);
573 if (tasks == NULL) {
574 pr_debug("couldn't open %s\n", filename);
575 return 0;
576 }
577
Arnaldo Carvalho de Melo7093b4c2016-04-08 11:32:15 -0300578 while ((dirent = readdir(tasks)) != NULL) {
Don Zickusbfd66cc2014-02-26 10:45:26 -0500579 char *end;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500580 pid_t _pid;
581
Arnaldo Carvalho de Melo7093b4c2016-04-08 11:32:15 -0300582 _pid = strtol(dirent->d_name, &end, 10);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500583 if (*end)
584 continue;
585
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300586 rc = -1;
David Ahernca6c41c2015-03-30 14:35:58 -0600587 if (perf_event__prepare_comm(comm_event, _pid, machine,
588 &tgid, &ppid) != 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300589 break;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500590
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300591 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
David Ahernca6c41c2015-03-30 14:35:58 -0600592 ppid, process, machine) < 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300593 break;
Hari Bathinie907caf2017-03-08 02:11:51 +0530594
595 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
596 tgid, process, machine) < 0)
597 break;
598
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300599 /*
600 * Send the prepared comm event
601 */
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300602 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300603 break;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300604
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300605 rc = 0;
Don Zickus363b7852014-03-14 10:43:44 -0400606 if (_pid == pid) {
607 /* process the parent's maps too */
608 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Mark Drayton3fcb10e2018-12-04 12:34:20 -0800609 process, machine, mmap_data);
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300610 if (rc)
611 break;
Don Zickus363b7852014-03-14 10:43:44 -0400612 }
Don Zickusbfd66cc2014-02-26 10:45:26 -0500613 }
614
615 closedir(tasks);
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300616 return rc;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200617}
618
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200619int perf_event__synthesize_thread_map(struct perf_tool *tool,
Jiri Olsa9749b902019-07-21 13:23:50 +0200620 struct perf_thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200621 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300622 struct machine *machine,
Mark Drayton3fcb10e2018-12-04 12:34:20 -0800623 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200624{
Don Zickus363b7852014-03-14 10:43:44 -0400625 union perf_event *comm_event, *mmap_event, *fork_event;
Hari Bathinie907caf2017-03-08 02:11:51 +0530626 union perf_event *namespaces_event;
David Aherndefd8d32011-12-22 11:30:01 -0700627 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200628
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200629 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200630 if (comm_event == NULL)
631 goto out;
632
Wang Nanb0fb9782016-01-12 10:12:04 +0000633 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200634 if (mmap_event == NULL)
635 goto out_free_comm;
636
Don Zickus363b7852014-03-14 10:43:44 -0400637 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
638 if (fork_event == NULL)
639 goto out_free_mmap;
640
Hari Bathinie907caf2017-03-08 02:11:51 +0530641 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
642 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
643 machine->id_hdr_size);
644 if (namespaces_event == NULL)
645 goto out_free_fork;
646
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200647 err = 0;
648 for (thread = 0; thread < threads->nr; ++thread) {
649 if (__event__synthesize_thread(comm_event, mmap_event,
Hari Bathinie907caf2017-03-08 02:11:51 +0530650 fork_event, namespaces_event,
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200651 perf_thread_map__pid(threads, thread), 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300652 process, tool, machine,
Mark Drayton3fcb10e2018-12-04 12:34:20 -0800653 mmap_data)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200654 err = -1;
655 break;
656 }
David Aherndefd8d32011-12-22 11:30:01 -0700657
658 /*
659 * comm.pid is set to thread group id by
660 * perf_event__synthesize_comm
661 */
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200662 if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
David Aherndefd8d32011-12-22 11:30:01 -0700663 bool need_leader = true;
664
665 /* is thread group leader in thread_map? */
666 for (j = 0; j < threads->nr; ++j) {
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200667 if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
David Aherndefd8d32011-12-22 11:30:01 -0700668 need_leader = false;
669 break;
670 }
671 }
672
673 /* if not, generate events for it */
674 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300675 __event__synthesize_thread(comm_event, mmap_event,
Hari Bathinie907caf2017-03-08 02:11:51 +0530676 fork_event, namespaces_event,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300677 comm_event->comm.pid, 0,
678 process, tool, machine,
Mark Drayton3fcb10e2018-12-04 12:34:20 -0800679 mmap_data)) {
David Aherndefd8d32011-12-22 11:30:01 -0700680 err = -1;
681 break;
682 }
683 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200684 }
Hari Bathinie907caf2017-03-08 02:11:51 +0530685 free(namespaces_event);
686out_free_fork:
Don Zickus363b7852014-03-14 10:43:44 -0400687 free(fork_event);
688out_free_mmap:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200689 free(mmap_event);
690out_free_comm:
691 free(comm_event);
692out:
693 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200694}
695
Kan Liang340b47f2017-09-29 07:47:54 -0700696static int __perf_event__synthesize_threads(struct perf_tool *tool,
697 perf_event__handler_t process,
698 struct machine *machine,
699 bool mmap_data,
Kan Liang340b47f2017-09-29 07:47:54 -0700700 struct dirent **dirent,
701 int start,
702 int num)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200703{
Don Zickus363b7852014-03-14 10:43:44 -0400704 union perf_event *comm_event, *mmap_event, *fork_event;
Hari Bathinie907caf2017-03-08 02:11:51 +0530705 union perf_event *namespaces_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200706 int err = -1;
Kan Liangecdad242017-09-07 10:55:46 -0700707 char *end;
708 pid_t pid;
Kan Liang340b47f2017-09-29 07:47:54 -0700709 int i;
Namhyung Kim574799b2014-03-17 10:45:49 +0900710
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200711 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200712 if (comm_event == NULL)
713 goto out;
714
Wang Nanb0fb9782016-01-12 10:12:04 +0000715 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200716 if (mmap_event == NULL)
717 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200718
Don Zickus363b7852014-03-14 10:43:44 -0400719 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
720 if (fork_event == NULL)
721 goto out_free_mmap;
722
Hari Bathinie907caf2017-03-08 02:11:51 +0530723 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
724 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
725 machine->id_hdr_size);
726 if (namespaces_event == NULL)
727 goto out_free_fork;
728
Kan Liang340b47f2017-09-29 07:47:54 -0700729 for (i = start; i < start + num; i++) {
Kan Liangecdad242017-09-07 10:55:46 -0700730 if (!isdigit(dirent[i]->d_name[0]))
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200731 continue;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200732
Kan Liangecdad242017-09-07 10:55:46 -0700733 pid = (pid_t)strtol(dirent[i]->d_name, &end, 10);
734 /* only interested in proper numerical dirents */
Kan Liang340b47f2017-09-29 07:47:54 -0700735 if (*end)
736 continue;
737 /*
738 * We may race with exiting thread, so don't stop just because
739 * one thread couldn't be synthesized.
740 */
741 __event__synthesize_thread(comm_event, mmap_event, fork_event,
742 namespaces_event, pid, 1, process,
Mark Drayton3fcb10e2018-12-04 12:34:20 -0800743 tool, machine, mmap_data);
Kan Liangecdad242017-09-07 10:55:46 -0700744 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200745 err = 0;
Kan Liangecdad242017-09-07 10:55:46 -0700746
Hari Bathinie907caf2017-03-08 02:11:51 +0530747 free(namespaces_event);
Don Zickus363b7852014-03-14 10:43:44 -0400748out_free_fork:
749 free(fork_event);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200750out_free_mmap:
751 free(mmap_event);
752out_free_comm:
753 free(comm_event);
754out:
755 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200756}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200757
Kan Liang340b47f2017-09-29 07:47:54 -0700758struct synthesize_threads_arg {
759 struct perf_tool *tool;
760 perf_event__handler_t process;
761 struct machine *machine;
762 bool mmap_data;
Kan Liang340b47f2017-09-29 07:47:54 -0700763 struct dirent **dirent;
764 int num;
765 int start;
766};
767
768static void *synthesize_threads_worker(void *arg)
769{
770 struct synthesize_threads_arg *args = arg;
771
772 __perf_event__synthesize_threads(args->tool, args->process,
773 args->machine, args->mmap_data,
Mark Drayton3fcb10e2018-12-04 12:34:20 -0800774 args->dirent,
Kan Liang340b47f2017-09-29 07:47:54 -0700775 args->start, args->num);
776 return NULL;
777}
778
779int perf_event__synthesize_threads(struct perf_tool *tool,
780 perf_event__handler_t process,
781 struct machine *machine,
782 bool mmap_data,
Kan Liang340b47f2017-09-29 07:47:54 -0700783 unsigned int nr_threads_synthesize)
784{
785 struct synthesize_threads_arg *args = NULL;
786 pthread_t *synthesize_threads = NULL;
787 char proc_path[PATH_MAX];
788 struct dirent **dirent;
789 int num_per_thread;
790 int m, n, i, j;
791 int thread_nr;
792 int base = 0;
793 int err = -1;
794
795
796 if (machine__is_default_guest(machine))
797 return 0;
798
799 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
800 n = scandir(proc_path, &dirent, 0, alphasort);
801 if (n < 0)
802 return err;
803
Kan Liang0c6b4992017-09-29 07:47:55 -0700804 if (nr_threads_synthesize == UINT_MAX)
805 thread_nr = sysconf(_SC_NPROCESSORS_ONLN);
806 else
807 thread_nr = nr_threads_synthesize;
Kan Liang340b47f2017-09-29 07:47:54 -0700808
809 if (thread_nr <= 1) {
810 err = __perf_event__synthesize_threads(tool, process,
811 machine, mmap_data,
Kan Liang340b47f2017-09-29 07:47:54 -0700812 dirent, base, n);
813 goto free_dirent;
814 }
815 if (thread_nr > n)
816 thread_nr = n;
817
818 synthesize_threads = calloc(sizeof(pthread_t), thread_nr);
819 if (synthesize_threads == NULL)
820 goto free_dirent;
821
822 args = calloc(sizeof(*args), thread_nr);
823 if (args == NULL)
824 goto free_threads;
825
826 num_per_thread = n / thread_nr;
827 m = n % thread_nr;
828 for (i = 0; i < thread_nr; i++) {
829 args[i].tool = tool;
830 args[i].process = process;
831 args[i].machine = machine;
832 args[i].mmap_data = mmap_data;
Kan Liang340b47f2017-09-29 07:47:54 -0700833 args[i].dirent = dirent;
834 }
835 for (i = 0; i < m; i++) {
836 args[i].num = num_per_thread + 1;
837 args[i].start = i * args[i].num;
838 }
839 if (i != 0)
840 base = args[i-1].start + args[i-1].num;
841 for (j = i; j < thread_nr; j++) {
842 args[j].num = num_per_thread;
843 args[j].start = base + (j - i) * args[i].num;
844 }
845
846 for (i = 0; i < thread_nr; i++) {
847 if (pthread_create(&synthesize_threads[i], NULL,
848 synthesize_threads_worker, &args[i]))
849 goto out_join;
850 }
851 err = 0;
852out_join:
853 for (i = 0; i < thread_nr; i++)
854 pthread_join(synthesize_threads[i], NULL);
855 free(args);
856free_threads:
857 free(synthesize_threads);
858free_dirent:
859 for (i = 0; i < n; i++)
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300860 zfree(&dirent[i]);
Kan Liang340b47f2017-09-29 07:47:54 -0700861 free(dirent);
862
863 return err;
864}
865
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200866struct process_symbol_args {
867 const char *name;
868 u64 start;
869};
870
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200871static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700872 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200873{
874 struct process_symbol_args *args = arg;
875
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200876 /*
877 * Must be a function or at least an alias, as in PARISC64, where "_text" is
878 * an 'A' to the same address as "_stext".
879 */
Arnaldo Carvalho de Meloe85e0e02018-04-25 17:16:31 -0300880 if (!(kallsyms__is_function(type) ||
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200881 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200882 return 0;
883
884 args->start = start;
885 return 1;
886}
887
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300888int kallsyms__get_function_start(const char *kallsyms_filename,
889 const char *symbol_name, u64 *addr)
Adrian Hunter29b596b2014-01-29 16:14:37 +0200890{
891 struct process_symbol_args args = { .name = symbol_name, };
892
893 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300894 return -1;
Adrian Hunter29b596b2014-01-29 16:14:37 +0200895
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300896 *addr = args.start;
897 return 0;
Adrian Hunter29b596b2014-01-29 16:14:37 +0200898}
899
Adrian Huntera8ce99b2018-05-22 13:54:37 +0300900int __weak perf_event__synthesize_extra_kmaps(struct perf_tool *tool __maybe_unused,
901 perf_event__handler_t process __maybe_unused,
902 struct machine *machine __maybe_unused)
903{
904 return 0;
905}
906
907static int __perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
908 perf_event__handler_t process,
909 struct machine *machine)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200910{
911 size_t size;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300912 struct map *map = machine__kernel_map(machine);
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200913 struct kmap *kmap;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200914 int err;
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700915 union perf_event *event;
916
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300917 if (map == NULL)
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700918 return -1;
919
Igor Lubasheve9a68822019-08-26 21:39:12 -0400920 kmap = map__kmap(map);
921 if (!kmap->ref_reloc_sym)
922 return -1;
923
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200924 /*
925 * We should get this from /sys/kernel/sections/.text, but till that is
926 * available use this, and after it is use this as a fallback for older
927 * kernels.
928 */
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700929 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200930 if (event == NULL) {
931 pr_debug("Not enough memory synthesizing mmap event "
932 "for kernel modules\n");
933 return -1;
934 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200935
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300936 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800937 /*
938 * kernel uses PERF_RECORD_MISC_USER for user space maps,
939 * see kernel/perf_event.c __perf_event_mmap
940 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200941 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800942 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200943 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100944 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200945
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200946 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100947 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300948 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200949 event->mmap.header.type = PERF_RECORD_MMAP;
950 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200951 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200952 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200953 event->mmap.start = map->start;
954 event->mmap.len = map->end - event->mmap.start;
955 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200956
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300957 err = perf_tool__process_synth_event(tool, event, machine, process);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200958 free(event);
959
960 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200961}
962
Adrian Huntera8ce99b2018-05-22 13:54:37 +0300963int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
964 perf_event__handler_t process,
965 struct machine *machine)
966{
967 int err;
968
969 err = __perf_event__synthesize_kernel_mmap(tool, process, machine);
970 if (err < 0)
971 return err;
972
973 return perf_event__synthesize_extra_kmaps(tool, process, machine);
974}
975
Jiri Olsa99471c92015-10-25 15:51:20 +0100976int perf_event__synthesize_thread_map2(struct perf_tool *tool,
Jiri Olsa9749b902019-07-21 13:23:50 +0200977 struct perf_thread_map *threads,
Jiri Olsa99471c92015-10-25 15:51:20 +0100978 perf_event__handler_t process,
979 struct machine *machine)
980{
981 union perf_event *event;
982 int i, err, size;
983
984 size = sizeof(event->thread_map);
985 size += threads->nr * sizeof(event->thread_map.entries[0]);
986
987 event = zalloc(size);
988 if (!event)
989 return -ENOMEM;
990
991 event->header.type = PERF_RECORD_THREAD_MAP;
992 event->header.size = size;
993 event->thread_map.nr = threads->nr;
994
995 for (i = 0; i < threads->nr; i++) {
Jiri Olsa72932372019-08-28 15:57:16 +0200996 struct perf_record_thread_map_entry *entry = &event->thread_map.entries[i];
Jiri Olsa7836e522019-07-21 13:24:20 +0200997 char *comm = perf_thread_map__comm(threads, i);
Jiri Olsa99471c92015-10-25 15:51:20 +0100998
999 if (!comm)
1000 comm = (char *) "";
1001
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001002 entry->pid = perf_thread_map__pid(threads, i);
Jiri Olsa99471c92015-10-25 15:51:20 +01001003 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
1004 }
1005
1006 err = process(tool, event, NULL, machine);
1007
1008 free(event);
1009 return err;
1010}
1011
Jiri Olsa6c872902015-10-25 15:51:24 +01001012static void synthesize_cpus(struct cpu_map_entries *cpus,
Jiri Olsaf8548392019-07-21 13:23:49 +02001013 struct perf_cpu_map *map)
Jiri Olsa6c872902015-10-25 15:51:24 +01001014{
1015 int i;
1016
1017 cpus->nr = map->nr;
1018
1019 for (i = 0; i < map->nr; i++)
1020 cpus->cpu[i] = map->map[i];
1021}
1022
Jiri Olsa72932372019-08-28 15:57:16 +02001023static void synthesize_mask(struct perf_record_record_cpu_map *mask,
Jiri Olsaf8548392019-07-21 13:23:49 +02001024 struct perf_cpu_map *map, int max)
Jiri Olsa6c872902015-10-25 15:51:24 +01001025{
1026 int i;
1027
1028 mask->nr = BITS_TO_LONGS(max);
1029 mask->long_size = sizeof(long);
1030
1031 for (i = 0; i < map->nr; i++)
1032 set_bit(map->map[i], mask->mask);
1033}
1034
Jiri Olsaf8548392019-07-21 13:23:49 +02001035static size_t cpus_size(struct perf_cpu_map *map)
Jiri Olsa6c872902015-10-25 15:51:24 +01001036{
1037 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
1038}
1039
Jiri Olsaf8548392019-07-21 13:23:49 +02001040static size_t mask_size(struct perf_cpu_map *map, int *max)
Jiri Olsa6c872902015-10-25 15:51:24 +01001041{
1042 int i;
1043
1044 *max = 0;
1045
1046 for (i = 0; i < map->nr; i++) {
1047 /* bit possition of the cpu is + 1 */
1048 int bit = map->map[i] + 1;
1049
1050 if (bit > *max)
1051 *max = bit;
1052 }
1053
Jiri Olsa72932372019-08-28 15:57:16 +02001054 return sizeof(struct perf_record_record_cpu_map) + BITS_TO_LONGS(*max) * sizeof(long);
Jiri Olsa6c872902015-10-25 15:51:24 +01001055}
1056
Jiri Olsaf8548392019-07-21 13:23:49 +02001057void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max)
Jiri Olsa6c872902015-10-25 15:51:24 +01001058{
1059 size_t size_cpus, size_mask;
Jiri Olsa315c0a12019-08-22 13:11:39 +02001060 bool is_dummy = perf_cpu_map__empty(map);
Jiri Olsa6c872902015-10-25 15:51:24 +01001061
1062 /*
1063 * Both array and mask data have variable size based
1064 * on the number of cpus and their actual values.
Jiri Olsa72932372019-08-28 15:57:16 +02001065 * The size of the 'struct perf_record_cpu_map_data' is:
Jiri Olsa6c872902015-10-25 15:51:24 +01001066 *
1067 * array = size of 'struct cpu_map_entries' +
1068 * number of cpus * sizeof(u64)
1069 *
Jiri Olsa72932372019-08-28 15:57:16 +02001070 * mask = size of 'struct perf_record_record_cpu_map' +
Jiri Olsa6c872902015-10-25 15:51:24 +01001071 * maximum cpu bit converted to size of longs
1072 *
Jiri Olsa72932372019-08-28 15:57:16 +02001073 * and finaly + the size of 'struct perf_record_cpu_map_data'.
Jiri Olsa6c872902015-10-25 15:51:24 +01001074 */
1075 size_cpus = cpus_size(map);
1076 size_mask = mask_size(map, max);
1077
1078 if (is_dummy || (size_cpus < size_mask)) {
1079 *size += size_cpus;
1080 *type = PERF_CPU_MAP__CPUS;
1081 } else {
1082 *size += size_mask;
1083 *type = PERF_CPU_MAP__MASK;
1084 }
1085
Jiri Olsa72932372019-08-28 15:57:16 +02001086 *size += sizeof(struct perf_record_cpu_map_data);
David Miller0ed149c2018-10-11 22:46:55 -07001087 *size = PERF_ALIGN(*size, sizeof(u64));
Jiri Olsa6c872902015-10-25 15:51:24 +01001088 return zalloc(*size);
1089}
1090
Jiri Olsa72932372019-08-28 15:57:16 +02001091void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
Jiri Olsa6c872902015-10-25 15:51:24 +01001092 u16 type, int max)
1093{
1094 data->type = type;
1095
1096 switch (type) {
1097 case PERF_CPU_MAP__CPUS:
1098 synthesize_cpus((struct cpu_map_entries *) data->data, map);
1099 break;
1100 case PERF_CPU_MAP__MASK:
Jiri Olsa72932372019-08-28 15:57:16 +02001101 synthesize_mask((struct perf_record_record_cpu_map *)data->data, map, max);
Jiri Olsa6c872902015-10-25 15:51:24 +01001102 default:
1103 break;
1104 };
1105}
1106
Jiri Olsa72932372019-08-28 15:57:16 +02001107static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map)
Jiri Olsa6c872902015-10-25 15:51:24 +01001108{
Jiri Olsa72932372019-08-28 15:57:16 +02001109 size_t size = sizeof(struct perf_record_cpu_map);
1110 struct perf_record_cpu_map *event;
Jiri Olsa6c872902015-10-25 15:51:24 +01001111 int max;
1112 u16 type;
1113
1114 event = cpu_map_data__alloc(map, &size, &type, &max);
1115 if (!event)
1116 return NULL;
1117
1118 event->header.type = PERF_RECORD_CPU_MAP;
1119 event->header.size = size;
1120 event->data.type = type;
1121
1122 cpu_map_data__synthesize(&event->data, map, type, max);
1123 return event;
1124}
1125
1126int perf_event__synthesize_cpu_map(struct perf_tool *tool,
Jiri Olsaf8548392019-07-21 13:23:49 +02001127 struct perf_cpu_map *map,
Jiri Olsa6c872902015-10-25 15:51:24 +01001128 perf_event__handler_t process,
1129 struct machine *machine)
1130{
Jiri Olsa72932372019-08-28 15:57:16 +02001131 struct perf_record_cpu_map *event;
Jiri Olsa6c872902015-10-25 15:51:24 +01001132 int err;
1133
1134 event = cpu_map_event__new(map);
1135 if (!event)
1136 return -ENOMEM;
1137
1138 err = process(tool, (union perf_event *) event, NULL, machine);
1139
1140 free(event);
1141 return err;
1142}
1143
Jiri Olsa67424342015-10-25 15:51:28 +01001144int perf_event__synthesize_stat_config(struct perf_tool *tool,
1145 struct perf_stat_config *config,
1146 perf_event__handler_t process,
1147 struct machine *machine)
1148{
Jiri Olsa72932372019-08-28 15:57:16 +02001149 struct perf_record_stat_config *event;
Jiri Olsa67424342015-10-25 15:51:28 +01001150 int size, i = 0, err;
1151
1152 size = sizeof(*event);
1153 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1154
1155 event = zalloc(size);
1156 if (!event)
1157 return -ENOMEM;
1158
1159 event->header.type = PERF_RECORD_STAT_CONFIG;
1160 event->header.size = size;
1161 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1162
1163#define ADD(__term, __val) \
1164 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1165 event->data[i].val = __val; \
1166 i++;
1167
1168 ADD(AGGR_MODE, config->aggr_mode)
1169 ADD(INTERVAL, config->interval)
1170 ADD(SCALE, config->scale)
1171
1172 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1173 "stat config terms unbalanced\n");
1174#undef ADD
1175
1176 err = process(tool, (union perf_event *) event, NULL, machine);
1177
1178 free(event);
1179 return err;
1180}
1181
Jiri Olsa5796f8f2015-10-25 15:51:31 +01001182int perf_event__synthesize_stat(struct perf_tool *tool,
1183 u32 cpu, u32 thread, u64 id,
1184 struct perf_counts_values *count,
1185 perf_event__handler_t process,
1186 struct machine *machine)
1187{
Jiri Olsa72932372019-08-28 15:57:16 +02001188 struct perf_record_stat event;
Jiri Olsa5796f8f2015-10-25 15:51:31 +01001189
1190 event.header.type = PERF_RECORD_STAT;
1191 event.header.size = sizeof(event);
1192 event.header.misc = 0;
1193
1194 event.id = id;
1195 event.cpu = cpu;
1196 event.thread = thread;
1197 event.val = count->val;
1198 event.ena = count->ena;
1199 event.run = count->run;
1200
1201 return process(tool, (union perf_event *) &event, NULL, machine);
1202}
1203
Jiri Olsad4c22592015-10-25 15:51:34 +01001204int perf_event__synthesize_stat_round(struct perf_tool *tool,
1205 u64 evtime, u64 type,
1206 perf_event__handler_t process,
1207 struct machine *machine)
1208{
Jiri Olsa72932372019-08-28 15:57:16 +02001209 struct perf_record_stat_round event;
Jiri Olsad4c22592015-10-25 15:51:34 +01001210
1211 event.header.type = PERF_RECORD_STAT_ROUND;
1212 event.header.size = sizeof(event);
1213 event.header.misc = 0;
1214
1215 event.time = evtime;
1216 event.type = type;
1217
1218 return process(tool, (union perf_event *) &event, NULL, machine);
1219}
1220
Jiri Olsa8e381592015-10-25 15:51:29 +01001221void perf_event__read_stat_config(struct perf_stat_config *config,
Jiri Olsa72932372019-08-28 15:57:16 +02001222 struct perf_record_stat_config *event)
Jiri Olsa8e381592015-10-25 15:51:29 +01001223{
1224 unsigned i;
1225
1226 for (i = 0; i < event->nr; i++) {
1227
1228 switch (event->data[i].tag) {
1229#define CASE(__term, __val) \
1230 case PERF_STAT_CONFIG_TERM__##__term: \
1231 config->__val = event->data[i].val; \
1232 break;
1233
1234 CASE(AGGR_MODE, aggr_mode)
1235 CASE(SCALE, scale)
1236 CASE(INTERVAL, interval)
1237#undef CASE
1238 default:
Jiri Olsac5f416e2019-08-28 15:57:09 +02001239 pr_warning("unknown stat config term %" PRI_lu64 "\n",
Jiri Olsa8e381592015-10-25 15:51:29 +01001240 event->data[i].tag);
1241 }
1242 }
1243}
1244
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001245size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
1246{
Adrian Hunter022c50d2014-07-14 13:02:27 +03001247 const char *s;
1248
1249 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
1250 s = " exec";
1251 else
1252 s = "";
1253
Arnaldo Carvalho de Melo50674062015-02-24 17:20:31 -03001254 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 -02001255}
1256
Hari Bathinif3b36142017-03-08 02:11:43 +05301257size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
1258{
1259 size_t ret = 0;
1260 struct perf_ns_link_info *ns_link_info;
1261 u32 nr_namespaces, idx;
1262
1263 ns_link_info = event->namespaces.link_info;
1264 nr_namespaces = event->namespaces.nr_namespaces;
1265
1266 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
1267 event->namespaces.pid,
1268 event->namespaces.tid,
1269 nr_namespaces);
1270
1271 for (idx = 0; idx < nr_namespaces; idx++) {
1272 if (idx && (idx % 4 == 0))
1273 ret += fprintf(fp, "\n\t\t ");
1274
1275 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
1276 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
1277 (u64)ns_link_info[idx].ino,
1278 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
1279 }
1280
1281 return ret;
1282}
1283
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001284int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001285 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001286 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001287 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001288{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001289 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001290}
1291
Hari Bathinif3b36142017-03-08 02:11:43 +05301292int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
1293 union perf_event *event,
1294 struct perf_sample *sample,
1295 struct machine *machine)
1296{
1297 return machine__process_namespaces_event(machine, event, sample);
1298}
1299
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001300int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001301 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001302 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001303 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001304{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001305 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001306}
1307
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001308int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1309 union perf_event *event,
1310 struct perf_sample *sample __maybe_unused,
1311 struct machine *machine)
1312{
1313 return machine__process_aux_event(machine, event);
1314}
1315
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001316int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1317 union perf_event *event,
1318 struct perf_sample *sample __maybe_unused,
1319 struct machine *machine)
1320{
1321 return machine__process_itrace_start_event(machine, event);
1322}
1323
Kan Liangc4937a92015-05-10 15:13:15 -04001324int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1325 union perf_event *event,
1326 struct perf_sample *sample,
1327 struct machine *machine)
1328{
1329 return machine__process_lost_samples_event(machine, event, sample);
1330}
1331
Adrian Hunter02860392015-07-21 12:44:03 +03001332int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1333 union perf_event *event,
1334 struct perf_sample *sample __maybe_unused,
1335 struct machine *machine)
1336{
1337 return machine__process_switch_event(machine, event);
1338}
1339
Song Liu9aa0bfa2019-01-17 08:15:17 -08001340int perf_event__process_ksymbol(struct perf_tool *tool __maybe_unused,
1341 union perf_event *event,
1342 struct perf_sample *sample __maybe_unused,
1343 struct machine *machine)
1344{
1345 return machine__process_ksymbol(machine, event, sample);
1346}
1347
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03001348int perf_event__process_bpf(struct perf_tool *tool __maybe_unused,
1349 union perf_event *event,
1350 struct perf_sample *sample,
1351 struct machine *machine)
Song Liu45178a92019-01-17 08:15:18 -08001352{
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03001353 return machine__process_bpf(machine, event, sample);
Song Liu45178a92019-01-17 08:15:18 -08001354}
1355
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001356size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1357{
Jiri Olsa1345e2e2019-08-25 20:17:41 +02001358 return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001359 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -03001360 event->mmap.len, event->mmap.pgoff,
1361 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1362 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001363}
1364
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001365size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1366{
Jiri Olsab66ced12019-08-25 20:17:42 +02001367 return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
1368 " %02x:%02x %"PRI_lu64" %"PRI_lu64"]: %c%c%c%c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001369 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1370 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1371 event->mmap2.min, event->mmap2.ino,
1372 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001373 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1374 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1375 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1376 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001377 event->mmap2.filename);
1378}
1379
Jiri Olsaec7fa5962015-10-25 15:51:22 +01001380size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1381{
Jiri Olsa9749b902019-07-21 13:23:50 +02001382 struct perf_thread_map *threads = thread_map__new_event(&event->thread_map);
Jiri Olsaec7fa5962015-10-25 15:51:22 +01001383 size_t ret;
1384
1385 ret = fprintf(fp, " nr: ");
1386
1387 if (threads)
1388 ret += thread_map__fprintf(threads, fp);
1389 else
1390 ret += fprintf(fp, "failed to get threads from event\n");
1391
Jiri Olsa7836e522019-07-21 13:24:20 +02001392 perf_thread_map__put(threads);
Jiri Olsaec7fa5962015-10-25 15:51:22 +01001393 return ret;
1394}
1395
Jiri Olsaeb12a1a2015-10-25 15:51:26 +01001396size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1397{
Jiri Olsaf8548392019-07-21 13:23:49 +02001398 struct perf_cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
Jiri Olsaeb12a1a2015-10-25 15:51:26 +01001399 size_t ret;
1400
Jiri Olsaa24020e2016-06-28 13:29:04 +02001401 ret = fprintf(fp, ": ");
Jiri Olsaeb12a1a2015-10-25 15:51:26 +01001402
1403 if (cpus)
1404 ret += cpu_map__fprintf(cpus, fp);
1405 else
1406 ret += fprintf(fp, "failed to get cpumap from event\n");
1407
Jiri Olsa38f01d82019-07-21 13:24:17 +02001408 perf_cpu_map__put(cpus);
Jiri Olsaeb12a1a2015-10-25 15:51:26 +01001409 return ret;
1410}
1411
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001412int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001413 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001414 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001415 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001416{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001417 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001418}
1419
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001420int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1421 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001422 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001423 struct machine *machine)
1424{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001425 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001426}
1427
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001428size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1429{
1430 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1431 event->fork.pid, event->fork.tid,
1432 event->fork.ppid, event->fork.ptid);
1433}
1434
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03001435int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001436 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001437 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03001438 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001439{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001440 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001441}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001442
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03001443int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1444 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001445 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03001446 struct machine *machine)
1447{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001448 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03001449}
1450
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001451size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1452{
Jiri Olsaaedebdc2019-08-28 15:57:05 +02001453 return fprintf(fp, " offset: %#"PRI_lx64" size: %#"PRI_lx64" flags: %#"PRI_lx64" [%s%s%s]\n",
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001454 event->aux.aux_offset, event->aux.aux_size,
1455 event->aux.flags,
1456 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
Alexander Shishkin05a1f472017-03-16 18:41:59 +02001457 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
1458 event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : "");
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001459}
1460
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001461size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1462{
1463 return fprintf(fp, " pid: %u tid: %u\n",
1464 event->itrace_start.pid, event->itrace_start.tid);
1465}
1466
Adrian Hunter02860392015-07-21 12:44:03 +03001467size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1468{
1469 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
Alexey Budankovb3f35b52018-04-09 10:26:05 +03001470 const char *in_out = !out ? "IN " :
1471 !(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT) ?
1472 "OUT " : "OUT preempt";
Adrian Hunter02860392015-07-21 12:44:03 +03001473
1474 if (event->header.type == PERF_RECORD_SWITCH)
1475 return fprintf(fp, " %s\n", in_out);
1476
1477 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1478 in_out, out ? "next" : "prev",
1479 event->context_switch.next_prev_pid,
1480 event->context_switch.next_prev_tid);
1481}
1482
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001483static size_t perf_event__fprintf_lost(union perf_event *event, FILE *fp)
1484{
Jiri Olsa5290ed62019-08-25 20:17:46 +02001485 return fprintf(fp, " lost %" PRI_lu64 "\n", event->lost.lost);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001486}
1487
Song Liu9aa0bfa2019-01-17 08:15:17 -08001488size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp)
1489{
Jiri Olsaf15e3c22019-08-25 20:17:50 +02001490 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 -03001491 event->ksymbol.addr, event->ksymbol.len,
1492 event->ksymbol.ksym_type,
1493 event->ksymbol.flags, event->ksymbol.name);
Song Liu9aa0bfa2019-01-17 08:15:17 -08001494}
1495
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03001496size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp)
Song Liu45178a92019-01-17 08:15:18 -08001497{
Jiri Olsa82017872019-05-08 15:20:09 +02001498 return fprintf(fp, " type %u, flags %u, id %u\n",
Arnaldo Carvalho de Melo6a1b3592019-08-26 19:20:35 -03001499 event->bpf.type, event->bpf.flags, event->bpf.id);
Song Liu45178a92019-01-17 08:15:18 -08001500}
1501
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001502size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1503{
1504 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1505 perf_event__name(event->header.type));
1506
1507 switch (event->header.type) {
1508 case PERF_RECORD_COMM:
1509 ret += perf_event__fprintf_comm(event, fp);
1510 break;
1511 case PERF_RECORD_FORK:
1512 case PERF_RECORD_EXIT:
1513 ret += perf_event__fprintf_task(event, fp);
1514 break;
1515 case PERF_RECORD_MMAP:
1516 ret += perf_event__fprintf_mmap(event, fp);
1517 break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301518 case PERF_RECORD_NAMESPACES:
1519 ret += perf_event__fprintf_namespaces(event, fp);
1520 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001521 case PERF_RECORD_MMAP2:
1522 ret += perf_event__fprintf_mmap2(event, fp);
1523 break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001524 case PERF_RECORD_AUX:
1525 ret += perf_event__fprintf_aux(event, fp);
1526 break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001527 case PERF_RECORD_ITRACE_START:
1528 ret += perf_event__fprintf_itrace_start(event, fp);
1529 break;
Adrian Hunter02860392015-07-21 12:44:03 +03001530 case PERF_RECORD_SWITCH:
1531 case PERF_RECORD_SWITCH_CPU_WIDE:
1532 ret += perf_event__fprintf_switch(event, fp);
1533 break;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001534 case PERF_RECORD_LOST:
1535 ret += perf_event__fprintf_lost(event, fp);
1536 break;
Song Liu9aa0bfa2019-01-17 08:15:17 -08001537 case PERF_RECORD_KSYMBOL:
1538 ret += perf_event__fprintf_ksymbol(event, fp);
1539 break;
Song Liu45178a92019-01-17 08:15:18 -08001540 case PERF_RECORD_BPF_EVENT:
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03001541 ret += perf_event__fprintf_bpf(event, fp);
Song Liu45178a92019-01-17 08:15:18 -08001542 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001543 default:
1544 ret += fprintf(fp, "\n");
1545 }
1546
1547 return ret;
1548}
1549
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001550int perf_event__process(struct perf_tool *tool __maybe_unused,
1551 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001552 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001553 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +05301554{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001555 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +05301556}
1557
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001558struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
1559 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001560{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -03001561 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001562 struct machine *machine = mg->machine;
Adrian Hunter5b7ba822013-08-07 14:38:46 +03001563 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001564
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -03001565 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001566 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001567 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001568 al->cpumode = cpumode;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001569 al->filtered = 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001570
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001571 if (machine == NULL) {
1572 al->map = NULL;
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -03001573 return NULL;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001574 }
1575
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001576 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001577 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001578 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +03001579 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001580 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001581 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001582 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1583 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001584 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +03001585 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -05001586 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1587 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001588 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -05001589 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001590 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001591
1592 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1593 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1594 !perf_guest)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001595 al->filtered |= (1 << HIST_FILTER__GUEST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001596 if ((cpumode == PERF_RECORD_MISC_USER ||
1597 cpumode == PERF_RECORD_MISC_KERNEL) &&
1598 !perf_host)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001599 al->filtered |= (1 << HIST_FILTER__HOST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001600
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -03001601 return NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001602 }
Arnaldo Carvalho de Meloedeb0c92018-10-16 17:08:29 -03001603
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001604 al->map = map_groups__find(mg, al->addr);
Arnaldo Carvalho de Meloedeb0c92018-10-16 17:08:29 -03001605 if (al->map != NULL) {
Adrian Hunter5b7ba822013-08-07 14:38:46 +03001606 /*
1607 * Kernel maps might be changed when loading symbols so loading
1608 * must be done prior to using kernel maps.
1609 */
1610 if (load_map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001611 map__load(al->map);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001612 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +03001613 }
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -03001614
1615 return al->map;
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -02001616}
1617
Adrian Hunter8e80ad92018-11-06 23:07:10 +02001618/*
1619 * For branch stacks or branch samples, the sample cpumode might not be correct
1620 * because it applies only to the sample 'ip' and not necessary to 'addr' or
1621 * branch stack addresses. If possible, use a fallback to deal with those cases.
1622 */
1623struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
1624 struct addr_location *al)
1625{
1626 struct map *map = thread__find_map(thread, cpumode, addr, al);
1627 struct machine *machine = thread->mg->machine;
1628 u8 addr_cpumode = machine__addr_cpumode(machine, cpumode, addr);
1629
1630 if (map || addr_cpumode == cpumode)
1631 return map;
1632
1633 return thread__find_map(thread, addr_cpumode, addr, al);
1634}
1635
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03001636struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
1637 u64 addr, struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -02001638{
Arnaldo Carvalho de Melod9a5f272018-04-24 12:05:48 -03001639 al->sym = NULL;
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -03001640 if (thread__find_map(thread, cpumode, addr, al))
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001641 al->sym = map__find_symbol(al->map, al->addr);
Arnaldo Carvalho de Melod9a5f272018-04-24 12:05:48 -03001642 return al->sym;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001643}
1644
Adrian Hunter8e80ad92018-11-06 23:07:10 +02001645struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
1646 u64 addr, struct addr_location *al)
1647{
1648 al->sym = NULL;
1649 if (thread__find_map_fb(thread, cpumode, addr, al))
1650 al->sym = map__find_symbol(al->map, al->addr);
1651 return al->sym;
1652}
1653
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001654/*
1655 * Callers need to drop the reference to al->thread, obtained in
1656 * machine__findnew_thread()
1657 */
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001658int machine__resolve(struct machine *machine, struct addr_location *al,
1659 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001660{
Adrian Hunteref893252013-08-27 11:23:06 +03001661 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +09001662 sample->tid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001663
1664 if (thread == NULL)
1665 return -1;
1666
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001667 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melof07a2d32018-04-24 10:49:50 -03001668 thread__find_map(thread, sample->cpumode, sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001669 dump_printf(" ...... dso: %s\n",
1670 al->map ? al->map->dso->long_name :
1671 al->level == 'H' ? "[hypervisor]" : "<not found>");
Namhyung Kim466fa762014-03-17 17:12:27 -03001672
1673 if (thread__is_filtered(thread))
1674 al->filtered |= (1 << HIST_FILTER__THREAD);
1675
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001676 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001677 al->cpu = sample->cpu;
Kan Liang0c4c4deb2015-09-04 10:45:42 -04001678 al->socket = -1;
Milian Wolff1fb7d062017-10-19 13:38:35 +02001679 al->srcline = NULL;
Kan Liang0c4c4deb2015-09-04 10:45:42 -04001680
1681 if (al->cpu >= 0) {
1682 struct perf_env *env = machine->env;
1683
1684 if (env && env->cpu)
1685 al->socket = env->cpu[al->cpu].socket_id;
1686 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001687
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001688 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +09001689 struct dso *dso = al->map->dso;
1690
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001691 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +09001692 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1693 dso->short_name) ||
1694 (dso->short_name != dso->long_name &&
1695 strlist__has_entry(symbol_conf.dso_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001696 dso->long_name))))) {
1697 al->filtered |= (1 << HIST_FILTER__DSO);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001698 }
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001699
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001700 al->sym = map__find_symbol(al->map, al->addr);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001701 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001702
Feng Tang1500b932012-09-07 16:42:23 +08001703 if (symbol_conf.sym_list &&
1704 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001705 al->sym->name))) {
1706 al->filtered |= (1 << HIST_FILTER__SYMBOL);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001707 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001708
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001709 return 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001710}
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001711
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001712/*
1713 * The preprocess_sample method will return with reference counts for the
1714 * in it, when done using (and perhaps getting ref counts if needing to
1715 * keep a pointer to one of those entries) it must be paired with
1716 * addr_location__put(), so that the refcounts can be decremented.
1717 */
1718void addr_location__put(struct addr_location *al)
1719{
1720 thread__zput(al->thread);
1721}
1722
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001723bool is_bts_event(struct perf_event_attr *attr)
1724{
1725 return attr->type == PERF_TYPE_HARDWARE &&
1726 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1727 attr->sample_period == 1;
1728}
1729
1730bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1731{
1732 if (attr->type == PERF_TYPE_SOFTWARE &&
1733 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1734 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1735 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1736 return true;
1737
1738 if (is_bts_event(attr))
1739 return true;
1740
1741 return false;
1742}
1743
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001744void thread__resolve(struct thread *thread, struct addr_location *al,
1745 struct perf_sample *sample)
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001746{
Adrian Hunter225f99e2018-11-06 23:07:11 +02001747 thread__find_map_fb(thread, sample->cpumode, sample->addr, al);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001748
1749 al->cpu = sample->cpu;
1750 al->sym = NULL;
1751
1752 if (al->map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001753 al->sym = map__find_symbol(al->map, al->addr);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001754}