blob: 4d7e65fa9d86c66974c29b9002cf556a219c8a29 [file] [log] [blame]
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02001#include <linux/types.h>
David Carrillo-Cisneros16eb8132017-04-11 23:49:19 -07002#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
Wang Nand7e404a2016-09-06 04:58:29 +00003#include <api/fs/fs.h>
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02004#include "event.h"
5#include "debug.h"
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03006#include "hist.h"
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03007#include "machine.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02008#include "sort.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02009#include "string.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -020010#include "strlist.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020011#include "thread.h"
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -020012#include "thread_map.h"
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030013#include "symbol/kallsyms.h"
Jiri Olsa67424342015-10-25 15:51:28 +010014#include "asm/bug.h"
15#include "stat.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020016
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020017static const char *perf_event__names[] = {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020018 [0] = "TOTAL",
19 [PERF_RECORD_MMAP] = "MMAP",
Stephane Eranian5c5e8542013-08-21 12:10:25 +020020 [PERF_RECORD_MMAP2] = "MMAP2",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020021 [PERF_RECORD_LOST] = "LOST",
22 [PERF_RECORD_COMM] = "COMM",
23 [PERF_RECORD_EXIT] = "EXIT",
24 [PERF_RECORD_THROTTLE] = "THROTTLE",
25 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
26 [PERF_RECORD_FORK] = "FORK",
27 [PERF_RECORD_READ] = "READ",
28 [PERF_RECORD_SAMPLE] = "SAMPLE",
Adrian Hunter4a96f7a2015-04-30 17:37:29 +030029 [PERF_RECORD_AUX] = "AUX",
Adrian Hunter0ad21f62015-04-30 17:37:30 +030030 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
Kan Liangc4937a92015-05-10 15:13:15 -040031 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
Adrian Hunter02860392015-07-21 12:44:03 +030032 [PERF_RECORD_SWITCH] = "SWITCH",
33 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
Hari Bathinif3b36142017-03-08 02:11:43 +053034 [PERF_RECORD_NAMESPACES] = "NAMESPACES",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020035 [PERF_RECORD_HEADER_ATTR] = "ATTR",
36 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
37 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
38 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
39 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Adrian Hunter3c659ee2014-10-27 15:49:22 +020040 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
Adrian Huntera16ac022015-04-09 18:53:43 +030041 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
42 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
Adrian Huntere9bf54d2015-04-09 18:53:47 +030043 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
Jiri Olsa5f3339d2015-10-25 15:51:19 +010044 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
Jiri Olsa6640b6c2015-10-25 15:51:23 +010045 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
Jiri Olsa374fb9e2015-10-25 15:51:27 +010046 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
Jiri Olsad80518c2015-10-25 15:51:30 +010047 [PERF_RECORD_STAT] = "STAT",
Jiri Olsa2d8f0f12015-10-25 15:51:33 +010048 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
Jiri Olsaffe777252015-10-25 15:51:36 +010049 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
Adrian Hunter46bc29b2016-03-08 10:38:44 +020050 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030051};
52
Hari Bathinif3b36142017-03-08 02:11:43 +053053static const char *perf_ns__names[] = {
54 [NET_NS_INDEX] = "net",
55 [UTS_NS_INDEX] = "uts",
56 [IPC_NS_INDEX] = "ipc",
57 [PID_NS_INDEX] = "pid",
58 [USER_NS_INDEX] = "user",
59 [MNT_NS_INDEX] = "mnt",
60 [CGROUP_NS_INDEX] = "cgroup",
61};
62
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020063const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc002010-12-07 12:48:42 +000064{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020065 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc002010-12-07 12:48:42 +000066 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020067 if (!perf_event__names[id])
Thomas Gleixner3835bc002010-12-07 12:48:42 +000068 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020069 return perf_event__names[id];
Thomas Gleixner3835bc002010-12-07 12:48:42 +000070}
71
Hari Bathinif3b36142017-03-08 02:11:43 +053072static const char *perf_ns__name(unsigned int id)
73{
74 if (id >= ARRAY_SIZE(perf_ns__names))
75 return "UNKNOWN";
76 return perf_ns__names[id];
77}
78
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -030079static int perf_tool__process_synth_event(struct perf_tool *tool,
80 union perf_event *event,
81 struct machine *machine,
82 perf_event__handler_t process)
83{
84 struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020085 .pid = -1,
86 .tid = -1,
87 .time = -1,
88 .stream_id = -1,
89 .cpu = -1,
90 .period = 1,
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -030091 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
92 };
93
94 return process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020095};
96
David Ahern5aa0b032015-03-30 14:35:57 -060097/*
98 * Assumes that the first 4095 bytes of /proc/pid/stat contains
David Ahernca6c41c2015-03-30 14:35:58 -060099 * the comm, tgid and ppid.
David Ahern5aa0b032015-03-30 14:35:57 -0600100 */
David Ahernca6c41c2015-03-30 14:35:58 -0600101static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
102 pid_t *tgid, pid_t *ppid)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200103{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200104 char filename[PATH_MAX];
David Ahern5aa0b032015-03-30 14:35:57 -0600105 char bf[4096];
106 int fd;
Andrzej Hajda38349662015-10-06 11:00:17 +0200107 size_t size = 0;
108 ssize_t n;
Taeung Songbdd97ca2017-04-07 23:24:21 +0900109 char *name, *tgids, *ppids;
David Ahernca6c41c2015-03-30 14:35:58 -0600110
111 *tgid = -1;
112 *ppid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200113
114 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
115
David Ahern5aa0b032015-03-30 14:35:57 -0600116 fd = open(filename, O_RDONLY);
117 if (fd < 0) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200118 pr_debug("couldn't open %s\n", filename);
David Ahernca6c41c2015-03-30 14:35:58 -0600119 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200120 }
121
David Ahern5aa0b032015-03-30 14:35:57 -0600122 n = read(fd, bf, sizeof(bf) - 1);
123 close(fd);
124 if (n <= 0) {
David Ahernca6c41c2015-03-30 14:35:58 -0600125 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
David Ahern5aa0b032015-03-30 14:35:57 -0600126 pid);
127 return -1;
128 }
129 bf[n] = '\0';
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200130
David Ahern5aa0b032015-03-30 14:35:57 -0600131 name = strstr(bf, "Name:");
132 tgids = strstr(bf, "Tgid:");
David Ahernca6c41c2015-03-30 14:35:58 -0600133 ppids = strstr(bf, "PPid:");
David Ahernf5faf722011-12-22 11:30:02 -0700134
David Ahern5aa0b032015-03-30 14:35:57 -0600135 if (name) {
136 name += 5; /* strlen("Name:") */
Taeung Songbdd97ca2017-04-07 23:24:21 +0900137 name = rtrim(ltrim(name));
David Ahern5aa0b032015-03-30 14:35:57 -0600138 size = strlen(name);
139 if (size >= len)
140 size = len - 1;
141 memcpy(comm, name, size);
142 comm[size] = '\0';
143 } else {
144 pr_debug("Name: string not found for pid %d\n", pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200145 }
146
David Ahern5aa0b032015-03-30 14:35:57 -0600147 if (tgids) {
148 tgids += 5; /* strlen("Tgid:") */
David Ahernca6c41c2015-03-30 14:35:58 -0600149 *tgid = atoi(tgids);
David Ahern5aa0b032015-03-30 14:35:57 -0600150 } else {
151 pr_debug("Tgid: string not found for pid %d\n", pid);
152 }
David Ahernf5faf722011-12-22 11:30:02 -0700153
David Ahernca6c41c2015-03-30 14:35:58 -0600154 if (ppids) {
155 ppids += 5; /* strlen("PPid:") */
156 *ppid = atoi(ppids);
157 } else {
158 pr_debug("PPid: string not found for pid %d\n", pid);
159 }
160
161 return 0;
David Ahernf5faf722011-12-22 11:30:02 -0700162}
163
David Ahernca6c41c2015-03-30 14:35:58 -0600164static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
165 struct machine *machine,
166 pid_t *tgid, pid_t *ppid)
David Ahernf5faf722011-12-22 11:30:02 -0700167{
David Ahernf5faf722011-12-22 11:30:02 -0700168 size_t size;
David Ahernca6c41c2015-03-30 14:35:58 -0600169
170 *ppid = -1;
David Ahernf5faf722011-12-22 11:30:02 -0700171
172 memset(&event->comm, 0, sizeof(event->comm));
173
David Ahernca6c41c2015-03-30 14:35:58 -0600174 if (machine__is_host(machine)) {
175 if (perf_event__get_comm_ids(pid, event->comm.comm,
176 sizeof(event->comm.comm),
177 tgid, ppid) != 0) {
178 return -1;
179 }
180 } else {
181 *tgid = machine->pid;
182 }
Dongsheng Yangf5db57c42013-12-20 15:52:59 -0500183
David Ahernca6c41c2015-03-30 14:35:58 -0600184 if (*tgid < 0)
185 return -1;
David Ahernf5faf722011-12-22 11:30:02 -0700186
David Ahernca6c41c2015-03-30 14:35:58 -0600187 event->comm.pid = *tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200188 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700189
190 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300191 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200192 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200193 event->comm.header.size = (sizeof(event->comm) -
194 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200195 machine->id_hdr_size);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500196 event->comm.tid = pid;
David Ahernca6c41c2015-03-30 14:35:58 -0600197
198 return 0;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300199}
200
Namhyung Kime803cf92015-09-22 09:24:55 +0900201pid_t perf_event__synthesize_comm(struct perf_tool *tool,
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300202 union perf_event *event, pid_t pid,
203 perf_event__handler_t process,
204 struct machine *machine)
205{
David Ahernca6c41c2015-03-30 14:35:58 -0600206 pid_t tgid, ppid;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300207
David Ahernca6c41c2015-03-30 14:35:58 -0600208 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
209 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200210
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300211 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
Don Zickusbfd66cc2014-02-26 10:45:26 -0500212 return -1;
David Ahern1e6d5322012-08-26 12:24:42 -0600213
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200214 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200215}
216
Hari Bathinie907caf2017-03-08 02:11:51 +0530217static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
218 struct perf_ns_link_info *ns_link_info)
219{
220 struct stat64 st;
221 char proc_ns[128];
222
223 sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
224 if (stat64(proc_ns, &st) == 0) {
225 ns_link_info->dev = st.st_dev;
226 ns_link_info->ino = st.st_ino;
227 }
228}
229
230int perf_event__synthesize_namespaces(struct perf_tool *tool,
231 union perf_event *event,
232 pid_t pid, pid_t tgid,
233 perf_event__handler_t process,
234 struct machine *machine)
235{
236 u32 idx;
237 struct perf_ns_link_info *ns_link_info;
238
239 if (!tool || !tool->namespace_events)
240 return 0;
241
242 memset(&event->namespaces, 0, (sizeof(event->namespaces) +
243 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
244 machine->id_hdr_size));
245
246 event->namespaces.pid = tgid;
247 event->namespaces.tid = pid;
248
249 event->namespaces.nr_namespaces = NR_NAMESPACES;
250
251 ns_link_info = event->namespaces.link_info;
252
253 for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
254 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
255 &ns_link_info[idx]);
256
257 event->namespaces.header.type = PERF_RECORD_NAMESPACES;
258
259 event->namespaces.header.size = (sizeof(event->namespaces) +
260 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
261 machine->id_hdr_size);
262
263 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
264 return -1;
265
266 return 0;
267}
268
Don Zickus363b7852014-03-14 10:43:44 -0400269static int perf_event__synthesize_fork(struct perf_tool *tool,
David Ahernca6c41c2015-03-30 14:35:58 -0600270 union perf_event *event,
271 pid_t pid, pid_t tgid, pid_t ppid,
272 perf_event__handler_t process,
Don Zickus363b7852014-03-14 10:43:44 -0400273 struct machine *machine)
274{
275 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
276
David Ahern7764a382015-04-09 12:48:27 -0400277 /*
278 * for main thread set parent to ppid from status file. For other
279 * threads set parent pid to main thread. ie., assume main thread
280 * spawns all threads in a process
281 */
282 if (tgid == pid) {
283 event->fork.ppid = ppid;
284 event->fork.ptid = ppid;
285 } else {
286 event->fork.ppid = tgid;
287 event->fork.ptid = tgid;
288 }
Don Zickus363b7852014-03-14 10:43:44 -0400289 event->fork.pid = tgid;
290 event->fork.tid = pid;
291 event->fork.header.type = PERF_RECORD_FORK;
292
293 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
294
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300295 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
Don Zickus363b7852014-03-14 10:43:44 -0400296 return -1;
297
298 return 0;
299}
300
Jiri Olsaa18382b2014-01-07 13:47:20 +0100301int perf_event__synthesize_mmap_events(struct perf_tool *tool,
302 union perf_event *event,
303 pid_t pid, pid_t tgid,
304 perf_event__handler_t process,
305 struct machine *machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400306 bool mmap_data,
307 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200308{
309 char filename[PATH_MAX];
310 FILE *fp;
Kan Liang930e6fc2015-06-17 09:51:10 -0400311 unsigned long long t;
312 bool truncation = false;
Kan Liang9d9cad72015-06-17 09:51:11 -0400313 unsigned long long timeout = proc_map_timeout * 1000000ULL;
David Ahern1e6d5322012-08-26 12:24:42 -0600314 int rc = 0;
Wang Nand7e404a2016-09-06 04:58:29 +0000315 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
316 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200317
Dongsheng Yangc239c25a2013-12-20 15:53:00 -0500318 if (machine__is_default_guest(machine))
319 return 0;
320
Stephane Eranian88b897a2017-03-15 10:17:13 -0700321 snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
322 machine->root_dir, pid, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200323
324 fp = fopen(filename, "r");
325 if (fp == NULL) {
326 /*
327 * We raced with a task exiting - just return:
328 */
329 pr_debug("couldn't open %s\n", filename);
330 return -1;
331 }
332
Don Zickusa5a5ba72014-05-30 10:49:42 -0400333 event->header.type = PERF_RECORD_MMAP2;
Kan Liang930e6fc2015-06-17 09:51:10 -0400334 t = rdclock();
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200335
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200336 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900337 char bf[BUFSIZ];
338 char prot[5];
339 char execname[PATH_MAX];
340 char anonstr[] = "//anon";
Don Zickusa5a5ba72014-05-30 10:49:42 -0400341 unsigned int ino;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200342 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200343 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900344
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200345 if (fgets(bf, sizeof(bf), fp) == NULL)
346 break;
347
Kan Liang9d9cad72015-06-17 09:51:11 -0400348 if ((rdclock() - t) > timeout) {
349 pr_warning("Reading %s time out. "
350 "You may want to increase "
351 "the time limit by --proc-map-timeout\n",
352 filename);
Kan Liang930e6fc2015-06-17 09:51:10 -0400353 truncation = true;
354 goto out;
355 }
356
Namhyung Kim60648032012-11-11 23:20:50 +0900357 /* ensure null termination since stack will be reused. */
358 strcpy(execname, "");
359
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200360 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Marcin Åšlusarz89fee592016-01-19 20:03:03 +0100361 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
Don Zickusa5a5ba72014-05-30 10:49:42 -0400362 &event->mmap2.start, &event->mmap2.len, prot,
363 &event->mmap2.pgoff, &event->mmap2.maj,
364 &event->mmap2.min,
365 &ino, execname);
366
Don Zickus9d4ecc82013-11-13 15:32:06 -0300367 /*
368 * Anon maps don't have the execname.
369 */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400370 if (n < 7)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200371 continue;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400372
373 event->mmap2.ino = (u64)ino;
374
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300375 /*
376 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
377 */
Dongsheng Yang73547aa2013-12-20 15:52:58 -0500378 if (machine__is_host(machine))
379 event->header.misc = PERF_RECORD_MISC_USER;
380 else
381 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900382
Don Zickus7ef80702014-05-19 15:13:49 -0400383 /* map protection and flags bits */
384 event->mmap2.prot = 0;
385 event->mmap2.flags = 0;
386 if (prot[0] == 'r')
387 event->mmap2.prot |= PROT_READ;
388 if (prot[1] == 'w')
389 event->mmap2.prot |= PROT_WRITE;
390 if (prot[2] == 'x')
391 event->mmap2.prot |= PROT_EXEC;
392
393 if (prot[3] == 's')
394 event->mmap2.flags |= MAP_SHARED;
395 else
396 event->mmap2.flags |= MAP_PRIVATE;
397
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300398 if (prot[2] != 'x') {
399 if (!mmap_data || prot[0] != 'r')
400 continue;
401
402 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
403 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200404
Kan Liang930e6fc2015-06-17 09:51:10 -0400405out:
406 if (truncation)
407 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
408
Namhyung Kim60648032012-11-11 23:20:50 +0900409 if (!strcmp(execname, ""))
410 strcpy(execname, anonstr);
Arnaldo Carvalho de Melofbef1032016-09-12 16:47:57 -0300411
Adrian Hunter973186c2016-09-23 17:38:34 +0300412 if (hugetlbfs_mnt_len &&
413 !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
Wang Nand7e404a2016-09-06 04:58:29 +0000414 strcpy(execname, anonstr);
415 event->mmap2.flags |= MAP_HUGETLB;
416 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200417
Namhyung Kim60648032012-11-11 23:20:50 +0900418 size = strlen(execname) + 1;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400419 memcpy(event->mmap2.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900420 size = PERF_ALIGN(size, sizeof(u64));
Don Zickusa5a5ba72014-05-30 10:49:42 -0400421 event->mmap2.len -= event->mmap.start;
422 event->mmap2.header.size = (sizeof(event->mmap2) -
423 (sizeof(event->mmap2.filename) - size));
424 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
425 event->mmap2.header.size += machine->id_hdr_size;
426 event->mmap2.pid = tgid;
427 event->mmap2.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000428
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300429 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
Namhyung Kim60648032012-11-11 23:20:50 +0900430 rc = -1;
431 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200432 }
Kan Liang930e6fc2015-06-17 09:51:10 -0400433
434 if (truncation)
435 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200436 }
437
438 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600439 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200440}
441
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200442int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200443 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200444 struct machine *machine)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200445{
David Ahern1e6d5322012-08-26 12:24:42 -0600446 int rc = 0;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300447 struct map *pos;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300448 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300449 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200450 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200451 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200452 if (event == NULL) {
453 pr_debug("Not enough memory synthesizing mmap event "
454 "for kernel modules\n");
455 return -1;
456 }
457
458 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200459
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800460 /*
461 * kernel uses 0 for user space maps, see kernel/perf_event.c
462 * __perf_event_mmap
463 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300464 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200465 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800466 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200467 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800468
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300469 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200470 size_t size;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200471
Arnaldo Carvalho de Meloab9c2bd2015-09-23 15:45:20 -0300472 if (__map__is_kernel(pos))
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200473 continue;
474
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300475 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200476 event->mmap.header.type = PERF_RECORD_MMAP;
477 event->mmap.header.size = (sizeof(event->mmap) -
478 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200479 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
480 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200481 event->mmap.start = pos->start;
482 event->mmap.len = pos->end - pos->start;
483 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200484
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200485 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200486 pos->dso->long_name_len + 1);
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300487 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
David Ahern1e6d5322012-08-26 12:24:42 -0600488 rc = -1;
489 break;
490 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200491 }
492
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200493 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600494 return rc;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200495}
496
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200497static int __event__synthesize_thread(union perf_event *comm_event,
498 union perf_event *mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400499 union perf_event *fork_event,
Hari Bathinie907caf2017-03-08 02:11:51 +0530500 union perf_event *namespaces_event,
David Aherndefd8d32011-12-22 11:30:01 -0700501 pid_t pid, int full,
Hari Bathinie907caf2017-03-08 02:11:51 +0530502 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200503 struct perf_tool *tool,
Kan Liang9d9cad72015-06-17 09:51:11 -0400504 struct machine *machine,
505 bool mmap_data,
506 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200507{
Don Zickusbfd66cc2014-02-26 10:45:26 -0500508 char filename[PATH_MAX];
509 DIR *tasks;
Arnaldo Carvalho de Melo7093b4c2016-04-08 11:32:15 -0300510 struct dirent *dirent;
David Ahernca6c41c2015-03-30 14:35:58 -0600511 pid_t tgid, ppid;
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300512 int rc = 0;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500513
514 /* special case: only send one comm event using passed in pid */
515 if (!full) {
516 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
517 process, machine);
518
519 if (tgid == -1)
520 return -1;
521
Hari Bathinie907caf2017-03-08 02:11:51 +0530522 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
523 tgid, process, machine) < 0)
524 return -1;
525
526
Don Zickusbfd66cc2014-02-26 10:45:26 -0500527 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Kan Liang9d9cad72015-06-17 09:51:11 -0400528 process, machine, mmap_data,
529 proc_map_timeout);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500530 }
531
532 if (machine__is_default_guest(machine))
533 return 0;
534
535 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
536 machine->root_dir, pid);
537
538 tasks = opendir(filename);
539 if (tasks == NULL) {
540 pr_debug("couldn't open %s\n", filename);
541 return 0;
542 }
543
Arnaldo Carvalho de Melo7093b4c2016-04-08 11:32:15 -0300544 while ((dirent = readdir(tasks)) != NULL) {
Don Zickusbfd66cc2014-02-26 10:45:26 -0500545 char *end;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500546 pid_t _pid;
547
Arnaldo Carvalho de Melo7093b4c2016-04-08 11:32:15 -0300548 _pid = strtol(dirent->d_name, &end, 10);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500549 if (*end)
550 continue;
551
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300552 rc = -1;
David Ahernca6c41c2015-03-30 14:35:58 -0600553 if (perf_event__prepare_comm(comm_event, _pid, machine,
554 &tgid, &ppid) != 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300555 break;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500556
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300557 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
David Ahernca6c41c2015-03-30 14:35:58 -0600558 ppid, process, machine) < 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300559 break;
Hari Bathinie907caf2017-03-08 02:11:51 +0530560
561 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
562 tgid, process, machine) < 0)
563 break;
564
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300565 /*
566 * Send the prepared comm event
567 */
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300568 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300569 break;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300570
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300571 rc = 0;
Don Zickus363b7852014-03-14 10:43:44 -0400572 if (_pid == pid) {
573 /* process the parent's maps too */
574 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Kan Liang9d9cad72015-06-17 09:51:11 -0400575 process, machine, mmap_data, proc_map_timeout);
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300576 if (rc)
577 break;
Don Zickus363b7852014-03-14 10:43:44 -0400578 }
Don Zickusbfd66cc2014-02-26 10:45:26 -0500579 }
580
581 closedir(tasks);
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300582 return rc;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200583}
584
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200585int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200586 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200587 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300588 struct machine *machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400589 bool mmap_data,
590 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200591{
Don Zickus363b7852014-03-14 10:43:44 -0400592 union perf_event *comm_event, *mmap_event, *fork_event;
Hari Bathinie907caf2017-03-08 02:11:51 +0530593 union perf_event *namespaces_event;
David Aherndefd8d32011-12-22 11:30:01 -0700594 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200595
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200596 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200597 if (comm_event == NULL)
598 goto out;
599
Wang Nanb0fb9782016-01-12 10:12:04 +0000600 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200601 if (mmap_event == NULL)
602 goto out_free_comm;
603
Don Zickus363b7852014-03-14 10:43:44 -0400604 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
605 if (fork_event == NULL)
606 goto out_free_mmap;
607
Hari Bathinie907caf2017-03-08 02:11:51 +0530608 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
609 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
610 machine->id_hdr_size);
611 if (namespaces_event == NULL)
612 goto out_free_fork;
613
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200614 err = 0;
615 for (thread = 0; thread < threads->nr; ++thread) {
616 if (__event__synthesize_thread(comm_event, mmap_event,
Hari Bathinie907caf2017-03-08 02:11:51 +0530617 fork_event, namespaces_event,
Jiri Olsae13798c2015-06-23 00:36:02 +0200618 thread_map__pid(threads, thread), 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300619 process, tool, machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400620 mmap_data, proc_map_timeout)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200621 err = -1;
622 break;
623 }
David Aherndefd8d32011-12-22 11:30:01 -0700624
625 /*
626 * comm.pid is set to thread group id by
627 * perf_event__synthesize_comm
628 */
Jiri Olsae13798c2015-06-23 00:36:02 +0200629 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
David Aherndefd8d32011-12-22 11:30:01 -0700630 bool need_leader = true;
631
632 /* is thread group leader in thread_map? */
633 for (j = 0; j < threads->nr; ++j) {
Jiri Olsae13798c2015-06-23 00:36:02 +0200634 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
David Aherndefd8d32011-12-22 11:30:01 -0700635 need_leader = false;
636 break;
637 }
638 }
639
640 /* if not, generate events for it */
641 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300642 __event__synthesize_thread(comm_event, mmap_event,
Hari Bathinie907caf2017-03-08 02:11:51 +0530643 fork_event, namespaces_event,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300644 comm_event->comm.pid, 0,
645 process, tool, machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400646 mmap_data, proc_map_timeout)) {
David Aherndefd8d32011-12-22 11:30:01 -0700647 err = -1;
648 break;
649 }
650 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200651 }
Hari Bathinie907caf2017-03-08 02:11:51 +0530652 free(namespaces_event);
653out_free_fork:
Don Zickus363b7852014-03-14 10:43:44 -0400654 free(fork_event);
655out_free_mmap:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200656 free(mmap_event);
657out_free_comm:
658 free(comm_event);
659out:
660 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200661}
662
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200663int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200664 perf_event__handler_t process,
Kan Liang9d9cad72015-06-17 09:51:11 -0400665 struct machine *machine,
666 bool mmap_data,
667 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200668{
669 DIR *proc;
Dongsheng Yang99563462013-12-20 15:52:57 -0500670 char proc_path[PATH_MAX];
Arnaldo Carvalho de Melo7093b4c2016-04-08 11:32:15 -0300671 struct dirent *dirent;
Don Zickus363b7852014-03-14 10:43:44 -0400672 union perf_event *comm_event, *mmap_event, *fork_event;
Hari Bathinie907caf2017-03-08 02:11:51 +0530673 union perf_event *namespaces_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200674 int err = -1;
675
Namhyung Kim574799b2014-03-17 10:45:49 +0900676 if (machine__is_default_guest(machine))
677 return 0;
678
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200679 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200680 if (comm_event == NULL)
681 goto out;
682
Wang Nanb0fb9782016-01-12 10:12:04 +0000683 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200684 if (mmap_event == NULL)
685 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200686
Don Zickus363b7852014-03-14 10:43:44 -0400687 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
688 if (fork_event == NULL)
689 goto out_free_mmap;
690
Hari Bathinie907caf2017-03-08 02:11:51 +0530691 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
692 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
693 machine->id_hdr_size);
694 if (namespaces_event == NULL)
695 goto out_free_fork;
696
Dongsheng Yang99563462013-12-20 15:52:57 -0500697 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
698 proc = opendir(proc_path);
699
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200700 if (proc == NULL)
Hari Bathinie907caf2017-03-08 02:11:51 +0530701 goto out_free_namespaces;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200702
Arnaldo Carvalho de Melo7093b4c2016-04-08 11:32:15 -0300703 while ((dirent = readdir(proc)) != NULL) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200704 char *end;
Arnaldo Carvalho de Melo7093b4c2016-04-08 11:32:15 -0300705 pid_t pid = strtol(dirent->d_name, &end, 10);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200706
707 if (*end) /* only interested in proper numerical dirents */
708 continue;
Arnaldo Carvalho de Meloba361c92012-10-17 18:50:13 -0300709 /*
710 * We may race with exiting thread, so don't stop just because
711 * one thread couldn't be synthesized.
712 */
Hari Bathinie907caf2017-03-08 02:11:51 +0530713 __event__synthesize_thread(comm_event, mmap_event, fork_event,
714 namespaces_event, pid, 1, process,
715 tool, machine, mmap_data,
Kan Liang9d9cad72015-06-17 09:51:11 -0400716 proc_map_timeout);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200717 }
718
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200719 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600720 closedir(proc);
Hari Bathinie907caf2017-03-08 02:11:51 +0530721out_free_namespaces:
722 free(namespaces_event);
Don Zickus363b7852014-03-14 10:43:44 -0400723out_free_fork:
724 free(fork_event);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200725out_free_mmap:
726 free(mmap_event);
727out_free_comm:
728 free(comm_event);
729out:
730 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200731}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200732
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200733struct process_symbol_args {
734 const char *name;
735 u64 start;
736};
737
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200738static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700739 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200740{
741 struct process_symbol_args *args = arg;
742
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200743 /*
744 * Must be a function or at least an alias, as in PARISC64, where "_text" is
745 * an 'A' to the same address as "_stext".
746 */
747 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
748 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200749 return 0;
750
751 args->start = start;
752 return 1;
753}
754
Adrian Hunter29b596b2014-01-29 16:14:37 +0200755u64 kallsyms__get_function_start(const char *kallsyms_filename,
756 const char *symbol_name)
757{
758 struct process_symbol_args args = { .name = symbol_name, };
759
760 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
761 return 0;
762
763 return args.start;
764}
765
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200766int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200767 perf_event__handler_t process,
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200768 struct machine *machine)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200769{
770 size_t size;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200771 const char *mmap_name;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800772 char name_buff[PATH_MAX];
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300773 struct map *map = machine__kernel_map(machine);
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200774 struct kmap *kmap;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200775 int err;
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700776 union perf_event *event;
777
Wang Nan3dc6c1d2016-05-24 09:21:28 +0000778 if (symbol_conf.kptr_restrict)
779 return -1;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300780 if (map == NULL)
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700781 return -1;
782
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200783 /*
784 * We should get this from /sys/kernel/sections/.text, but till that is
785 * available use this, and after it is use this as a fallback for older
786 * kernels.
787 */
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700788 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200789 if (event == NULL) {
790 pr_debug("Not enough memory synthesizing mmap event "
791 "for kernel modules\n");
792 return -1;
793 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200794
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300795 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300796 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800797 /*
798 * kernel uses PERF_RECORD_MISC_USER for user space maps,
799 * see kernel/perf_event.c __perf_event_mmap
800 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200801 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800802 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200803 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100804 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200805
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200806 kmap = map__kmap(map);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200807 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200808 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300809 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200810 event->mmap.header.type = PERF_RECORD_MMAP;
811 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200812 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200813 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200814 event->mmap.start = map->start;
815 event->mmap.len = map->end - event->mmap.start;
816 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200817
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300818 err = perf_tool__process_synth_event(tool, event, machine, process);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200819 free(event);
820
821 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200822}
823
Jiri Olsa99471c92015-10-25 15:51:20 +0100824int perf_event__synthesize_thread_map2(struct perf_tool *tool,
825 struct thread_map *threads,
826 perf_event__handler_t process,
827 struct machine *machine)
828{
829 union perf_event *event;
830 int i, err, size;
831
832 size = sizeof(event->thread_map);
833 size += threads->nr * sizeof(event->thread_map.entries[0]);
834
835 event = zalloc(size);
836 if (!event)
837 return -ENOMEM;
838
839 event->header.type = PERF_RECORD_THREAD_MAP;
840 event->header.size = size;
841 event->thread_map.nr = threads->nr;
842
843 for (i = 0; i < threads->nr; i++) {
844 struct thread_map_event_entry *entry = &event->thread_map.entries[i];
845 char *comm = thread_map__comm(threads, i);
846
847 if (!comm)
848 comm = (char *) "";
849
850 entry->pid = thread_map__pid(threads, i);
851 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
852 }
853
854 err = process(tool, event, NULL, machine);
855
856 free(event);
857 return err;
858}
859
Jiri Olsa6c872902015-10-25 15:51:24 +0100860static void synthesize_cpus(struct cpu_map_entries *cpus,
861 struct cpu_map *map)
862{
863 int i;
864
865 cpus->nr = map->nr;
866
867 for (i = 0; i < map->nr; i++)
868 cpus->cpu[i] = map->map[i];
869}
870
871static void synthesize_mask(struct cpu_map_mask *mask,
872 struct cpu_map *map, int max)
873{
874 int i;
875
876 mask->nr = BITS_TO_LONGS(max);
877 mask->long_size = sizeof(long);
878
879 for (i = 0; i < map->nr; i++)
880 set_bit(map->map[i], mask->mask);
881}
882
883static size_t cpus_size(struct cpu_map *map)
884{
885 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
886}
887
888static size_t mask_size(struct cpu_map *map, int *max)
889{
890 int i;
891
892 *max = 0;
893
894 for (i = 0; i < map->nr; i++) {
895 /* bit possition of the cpu is + 1 */
896 int bit = map->map[i] + 1;
897
898 if (bit > *max)
899 *max = bit;
900 }
901
902 return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
903}
904
905void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
906{
907 size_t size_cpus, size_mask;
908 bool is_dummy = cpu_map__empty(map);
909
910 /*
911 * Both array and mask data have variable size based
912 * on the number of cpus and their actual values.
913 * The size of the 'struct cpu_map_data' is:
914 *
915 * array = size of 'struct cpu_map_entries' +
916 * number of cpus * sizeof(u64)
917 *
918 * mask = size of 'struct cpu_map_mask' +
919 * maximum cpu bit converted to size of longs
920 *
921 * and finaly + the size of 'struct cpu_map_data'.
922 */
923 size_cpus = cpus_size(map);
924 size_mask = mask_size(map, max);
925
926 if (is_dummy || (size_cpus < size_mask)) {
927 *size += size_cpus;
928 *type = PERF_CPU_MAP__CPUS;
929 } else {
930 *size += size_mask;
931 *type = PERF_CPU_MAP__MASK;
932 }
933
934 *size += sizeof(struct cpu_map_data);
935 return zalloc(*size);
936}
937
938void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
939 u16 type, int max)
940{
941 data->type = type;
942
943 switch (type) {
944 case PERF_CPU_MAP__CPUS:
945 synthesize_cpus((struct cpu_map_entries *) data->data, map);
946 break;
947 case PERF_CPU_MAP__MASK:
948 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
949 default:
950 break;
951 };
952}
953
954static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
955{
956 size_t size = sizeof(struct cpu_map_event);
957 struct cpu_map_event *event;
958 int max;
959 u16 type;
960
961 event = cpu_map_data__alloc(map, &size, &type, &max);
962 if (!event)
963 return NULL;
964
965 event->header.type = PERF_RECORD_CPU_MAP;
966 event->header.size = size;
967 event->data.type = type;
968
969 cpu_map_data__synthesize(&event->data, map, type, max);
970 return event;
971}
972
973int perf_event__synthesize_cpu_map(struct perf_tool *tool,
974 struct cpu_map *map,
975 perf_event__handler_t process,
976 struct machine *machine)
977{
978 struct cpu_map_event *event;
979 int err;
980
981 event = cpu_map_event__new(map);
982 if (!event)
983 return -ENOMEM;
984
985 err = process(tool, (union perf_event *) event, NULL, machine);
986
987 free(event);
988 return err;
989}
990
Jiri Olsa67424342015-10-25 15:51:28 +0100991int perf_event__synthesize_stat_config(struct perf_tool *tool,
992 struct perf_stat_config *config,
993 perf_event__handler_t process,
994 struct machine *machine)
995{
996 struct stat_config_event *event;
997 int size, i = 0, err;
998
999 size = sizeof(*event);
1000 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1001
1002 event = zalloc(size);
1003 if (!event)
1004 return -ENOMEM;
1005
1006 event->header.type = PERF_RECORD_STAT_CONFIG;
1007 event->header.size = size;
1008 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1009
1010#define ADD(__term, __val) \
1011 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1012 event->data[i].val = __val; \
1013 i++;
1014
1015 ADD(AGGR_MODE, config->aggr_mode)
1016 ADD(INTERVAL, config->interval)
1017 ADD(SCALE, config->scale)
1018
1019 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1020 "stat config terms unbalanced\n");
1021#undef ADD
1022
1023 err = process(tool, (union perf_event *) event, NULL, machine);
1024
1025 free(event);
1026 return err;
1027}
1028
Jiri Olsa5796f8f2015-10-25 15:51:31 +01001029int perf_event__synthesize_stat(struct perf_tool *tool,
1030 u32 cpu, u32 thread, u64 id,
1031 struct perf_counts_values *count,
1032 perf_event__handler_t process,
1033 struct machine *machine)
1034{
1035 struct stat_event event;
1036
1037 event.header.type = PERF_RECORD_STAT;
1038 event.header.size = sizeof(event);
1039 event.header.misc = 0;
1040
1041 event.id = id;
1042 event.cpu = cpu;
1043 event.thread = thread;
1044 event.val = count->val;
1045 event.ena = count->ena;
1046 event.run = count->run;
1047
1048 return process(tool, (union perf_event *) &event, NULL, machine);
1049}
1050
Jiri Olsad4c22592015-10-25 15:51:34 +01001051int perf_event__synthesize_stat_round(struct perf_tool *tool,
1052 u64 evtime, u64 type,
1053 perf_event__handler_t process,
1054 struct machine *machine)
1055{
1056 struct stat_round_event event;
1057
1058 event.header.type = PERF_RECORD_STAT_ROUND;
1059 event.header.size = sizeof(event);
1060 event.header.misc = 0;
1061
1062 event.time = evtime;
1063 event.type = type;
1064
1065 return process(tool, (union perf_event *) &event, NULL, machine);
1066}
1067
Jiri Olsa8e381592015-10-25 15:51:29 +01001068void perf_event__read_stat_config(struct perf_stat_config *config,
1069 struct stat_config_event *event)
1070{
1071 unsigned i;
1072
1073 for (i = 0; i < event->nr; i++) {
1074
1075 switch (event->data[i].tag) {
1076#define CASE(__term, __val) \
1077 case PERF_STAT_CONFIG_TERM__##__term: \
1078 config->__val = event->data[i].val; \
1079 break;
1080
1081 CASE(AGGR_MODE, aggr_mode)
1082 CASE(SCALE, scale)
1083 CASE(INTERVAL, interval)
1084#undef CASE
1085 default:
1086 pr_warning("unknown stat config term %" PRIu64 "\n",
1087 event->data[i].tag);
1088 }
1089 }
1090}
1091
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001092size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
1093{
Adrian Hunter022c50d2014-07-14 13:02:27 +03001094 const char *s;
1095
1096 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
1097 s = " exec";
1098 else
1099 s = "";
1100
Arnaldo Carvalho de Melo50674062015-02-24 17:20:31 -03001101 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 -02001102}
1103
Hari Bathinif3b36142017-03-08 02:11:43 +05301104size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
1105{
1106 size_t ret = 0;
1107 struct perf_ns_link_info *ns_link_info;
1108 u32 nr_namespaces, idx;
1109
1110 ns_link_info = event->namespaces.link_info;
1111 nr_namespaces = event->namespaces.nr_namespaces;
1112
1113 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
1114 event->namespaces.pid,
1115 event->namespaces.tid,
1116 nr_namespaces);
1117
1118 for (idx = 0; idx < nr_namespaces; idx++) {
1119 if (idx && (idx % 4 == 0))
1120 ret += fprintf(fp, "\n\t\t ");
1121
1122 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
1123 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
1124 (u64)ns_link_info[idx].ino,
1125 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
1126 }
1127
1128 return ret;
1129}
1130
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001131int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001132 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001133 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001134 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001135{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001136 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001137}
1138
Hari Bathinif3b36142017-03-08 02:11:43 +05301139int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
1140 union perf_event *event,
1141 struct perf_sample *sample,
1142 struct machine *machine)
1143{
1144 return machine__process_namespaces_event(machine, event, sample);
1145}
1146
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001147int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001148 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001149 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001150 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001151{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001152 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001153}
1154
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001155int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1156 union perf_event *event,
1157 struct perf_sample *sample __maybe_unused,
1158 struct machine *machine)
1159{
1160 return machine__process_aux_event(machine, event);
1161}
1162
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001163int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1164 union perf_event *event,
1165 struct perf_sample *sample __maybe_unused,
1166 struct machine *machine)
1167{
1168 return machine__process_itrace_start_event(machine, event);
1169}
1170
Kan Liangc4937a92015-05-10 15:13:15 -04001171int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1172 union perf_event *event,
1173 struct perf_sample *sample,
1174 struct machine *machine)
1175{
1176 return machine__process_lost_samples_event(machine, event, sample);
1177}
1178
Adrian Hunter02860392015-07-21 12:44:03 +03001179int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1180 union perf_event *event,
1181 struct perf_sample *sample __maybe_unused,
1182 struct machine *machine)
1183{
1184 return machine__process_switch_event(machine, event);
1185}
1186
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001187size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1188{
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -03001189 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001190 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -03001191 event->mmap.len, event->mmap.pgoff,
1192 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1193 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001194}
1195
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001196size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1197{
1198 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
Don Zickus7ef80702014-05-19 15:13:49 -04001199 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001200 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1201 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1202 event->mmap2.min, event->mmap2.ino,
1203 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001204 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1205 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1206 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1207 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001208 event->mmap2.filename);
1209}
1210
Jiri Olsaec7fa5962015-10-25 15:51:22 +01001211size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1212{
1213 struct thread_map *threads = thread_map__new_event(&event->thread_map);
1214 size_t ret;
1215
1216 ret = fprintf(fp, " nr: ");
1217
1218 if (threads)
1219 ret += thread_map__fprintf(threads, fp);
1220 else
1221 ret += fprintf(fp, "failed to get threads from event\n");
1222
1223 thread_map__put(threads);
1224 return ret;
1225}
1226
Jiri Olsaeb12a1a2015-10-25 15:51:26 +01001227size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1228{
1229 struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1230 size_t ret;
1231
Jiri Olsaa24020e2016-06-28 13:29:04 +02001232 ret = fprintf(fp, ": ");
Jiri Olsaeb12a1a2015-10-25 15:51:26 +01001233
1234 if (cpus)
1235 ret += cpu_map__fprintf(cpus, fp);
1236 else
1237 ret += fprintf(fp, "failed to get cpumap from event\n");
1238
1239 cpu_map__put(cpus);
1240 return ret;
1241}
1242
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001243int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001244 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001245 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001246 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001247{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001248 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001249}
1250
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001251int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1252 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001253 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001254 struct machine *machine)
1255{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001256 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001257}
1258
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001259size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1260{
1261 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1262 event->fork.pid, event->fork.tid,
1263 event->fork.ppid, event->fork.ptid);
1264}
1265
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03001266int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001267 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001268 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03001269 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001270{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001271 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001272}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001273
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03001274int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1275 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001276 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03001277 struct machine *machine)
1278{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001279 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03001280}
1281
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001282size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1283{
Alexander Shishkin05a1f472017-03-16 18:41:59 +02001284 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s%s]\n",
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001285 event->aux.aux_offset, event->aux.aux_size,
1286 event->aux.flags,
1287 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
Alexander Shishkin05a1f472017-03-16 18:41:59 +02001288 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
1289 event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : "");
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001290}
1291
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001292size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1293{
1294 return fprintf(fp, " pid: %u tid: %u\n",
1295 event->itrace_start.pid, event->itrace_start.tid);
1296}
1297
Adrian Hunter02860392015-07-21 12:44:03 +03001298size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1299{
1300 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1301 const char *in_out = out ? "OUT" : "IN ";
1302
1303 if (event->header.type == PERF_RECORD_SWITCH)
1304 return fprintf(fp, " %s\n", in_out);
1305
1306 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1307 in_out, out ? "next" : "prev",
1308 event->context_switch.next_prev_pid,
1309 event->context_switch.next_prev_tid);
1310}
1311
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001312size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1313{
1314 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1315 perf_event__name(event->header.type));
1316
1317 switch (event->header.type) {
1318 case PERF_RECORD_COMM:
1319 ret += perf_event__fprintf_comm(event, fp);
1320 break;
1321 case PERF_RECORD_FORK:
1322 case PERF_RECORD_EXIT:
1323 ret += perf_event__fprintf_task(event, fp);
1324 break;
1325 case PERF_RECORD_MMAP:
1326 ret += perf_event__fprintf_mmap(event, fp);
1327 break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301328 case PERF_RECORD_NAMESPACES:
1329 ret += perf_event__fprintf_namespaces(event, fp);
1330 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001331 case PERF_RECORD_MMAP2:
1332 ret += perf_event__fprintf_mmap2(event, fp);
1333 break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001334 case PERF_RECORD_AUX:
1335 ret += perf_event__fprintf_aux(event, fp);
1336 break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001337 case PERF_RECORD_ITRACE_START:
1338 ret += perf_event__fprintf_itrace_start(event, fp);
1339 break;
Adrian Hunter02860392015-07-21 12:44:03 +03001340 case PERF_RECORD_SWITCH:
1341 case PERF_RECORD_SWITCH_CPU_WIDE:
1342 ret += perf_event__fprintf_switch(event, fp);
1343 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -02001344 default:
1345 ret += fprintf(fp, "\n");
1346 }
1347
1348 return ret;
1349}
1350
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001351int perf_event__process(struct perf_tool *tool __maybe_unused,
1352 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001353 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001354 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +05301355{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001356 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +05301357}
1358
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001359void thread__find_addr_map(struct thread *thread, u8 cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001360 enum map_type type, u64 addr,
Adrian Hunter326f59b2013-08-08 14:32:27 +03001361 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001362{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -03001363 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001364 struct machine *machine = mg->machine;
Adrian Hunter5b7ba822013-08-07 14:38:46 +03001365 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001366
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -03001367 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03001368 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001369 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001370 al->cpumode = cpumode;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001371 al->filtered = 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001372
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001373 if (machine == NULL) {
1374 al->map = NULL;
1375 return;
1376 }
1377
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001378 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001379 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001380 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +03001381 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001382 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001383 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001384 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1385 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001386 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +03001387 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -05001388 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1389 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001390 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -05001391 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001392 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001393
1394 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1395 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1396 !perf_guest)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001397 al->filtered |= (1 << HIST_FILTER__GUEST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001398 if ((cpumode == PERF_RECORD_MISC_USER ||
1399 cpumode == PERF_RECORD_MISC_KERNEL) &&
1400 !perf_host)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001401 al->filtered |= (1 << HIST_FILTER__HOST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001402
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001403 return;
1404 }
1405try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -02001406 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001407 if (al->map == NULL) {
1408 /*
1409 * If this is outside of all known maps, and is a negative
1410 * address, try to look it up in the kernel dso, as it might be
1411 * a vsyscall or vdso (which executes in user-mode).
1412 *
1413 * XXX This is nasty, we should have a symbol list in the
1414 * "[vdso]" dso, but for now lets use the old trick of looking
1415 * in the whole kernel symbol list.
1416 */
Adrian Hunterfbe2af42014-08-15 22:08:39 +03001417 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1418 mg != &machine->kmaps &&
1419 machine__kernel_ip(machine, al->addr)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001420 mg = &machine->kmaps;
Adrian Hunter1f2a7062014-07-14 13:02:31 +03001421 load_map = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001422 goto try_again;
1423 }
Adrian Hunter5b7ba822013-08-07 14:38:46 +03001424 } else {
1425 /*
1426 * Kernel maps might be changed when loading symbols so loading
1427 * must be done prior to using kernel maps.
1428 */
1429 if (load_map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001430 map__load(al->map);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001431 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +03001432 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -02001433}
1434
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001435void thread__find_addr_location(struct thread *thread,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001436 u8 cpumode, enum map_type type, u64 addr,
Adrian Hunter61710bd2013-08-08 14:32:26 +03001437 struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -02001438{
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001439 thread__find_addr_map(thread, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -02001440 if (al->map != NULL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001441 al->sym = map__find_symbol(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -02001442 else
1443 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001444}
1445
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001446/*
1447 * Callers need to drop the reference to al->thread, obtained in
1448 * machine__findnew_thread()
1449 */
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001450int machine__resolve(struct machine *machine, struct addr_location *al,
1451 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001452{
Adrian Hunteref893252013-08-27 11:23:06 +03001453 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +09001454 sample->tid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001455
1456 if (thread == NULL)
1457 return -1;
1458
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001459 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001460 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001461 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001462 *
1463 * This should have happened earlier, when we processed the kernel MMAP
1464 * events, but for older perf.data files there was no such thing, so do
1465 * it now.
1466 */
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001467 if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001468 machine__kernel_map(machine) == NULL)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001469 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001470
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001471 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001472 dump_printf(" ...... dso: %s\n",
1473 al->map ? al->map->dso->long_name :
1474 al->level == 'H' ? "[hypervisor]" : "<not found>");
Namhyung Kim466fa762014-03-17 17:12:27 -03001475
1476 if (thread__is_filtered(thread))
1477 al->filtered |= (1 << HIST_FILTER__THREAD);
1478
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001479 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001480 al->cpu = sample->cpu;
Kan Liang0c4c4deb2015-09-04 10:45:42 -04001481 al->socket = -1;
1482
1483 if (al->cpu >= 0) {
1484 struct perf_env *env = machine->env;
1485
1486 if (env && env->cpu)
1487 al->socket = env->cpu[al->cpu].socket_id;
1488 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001489
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001490 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +09001491 struct dso *dso = al->map->dso;
1492
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001493 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +09001494 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1495 dso->short_name) ||
1496 (dso->short_name != dso->long_name &&
1497 strlist__has_entry(symbol_conf.dso_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001498 dso->long_name))))) {
1499 al->filtered |= (1 << HIST_FILTER__DSO);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001500 }
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001501
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001502 al->sym = map__find_symbol(al->map, al->addr);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001503 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001504
Feng Tang1500b932012-09-07 16:42:23 +08001505 if (symbol_conf.sym_list &&
1506 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001507 al->sym->name))) {
1508 al->filtered |= (1 << HIST_FILTER__SYMBOL);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001509 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001510
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001511 return 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001512}
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001513
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001514/*
1515 * The preprocess_sample method will return with reference counts for the
1516 * in it, when done using (and perhaps getting ref counts if needing to
1517 * keep a pointer to one of those entries) it must be paired with
1518 * addr_location__put(), so that the refcounts can be decremented.
1519 */
1520void addr_location__put(struct addr_location *al)
1521{
1522 thread__zput(al->thread);
1523}
1524
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001525bool is_bts_event(struct perf_event_attr *attr)
1526{
1527 return attr->type == PERF_TYPE_HARDWARE &&
1528 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1529 attr->sample_period == 1;
1530}
1531
1532bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1533{
1534 if (attr->type == PERF_TYPE_SOFTWARE &&
1535 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1536 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1537 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1538 return true;
1539
1540 if (is_bts_event(attr))
1541 return true;
1542
1543 return false;
1544}
1545
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001546void thread__resolve(struct thread *thread, struct addr_location *al,
1547 struct perf_sample *sample)
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001548{
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001549 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001550 if (!al->map)
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001551 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001552 sample->addr, al);
1553
1554 al->cpu = sample->cpu;
1555 al->sym = NULL;
1556
1557 if (al->map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001558 al->sym = map__find_symbol(al->map, al->addr);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001559}