blob: 07c07833de53a2b6063880af4314038f3b5d57b7 [file] [log] [blame]
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02001#include <linux/types.h>
2#include "event.h"
3#include "debug.h"
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03004#include "machine.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02005#include "sort.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02006#include "string.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02007#include "strlist.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02008#include "thread.h"
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -02009#include "thread_map.h"
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030010#include "symbol/kallsyms.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020011
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020012static const char *perf_event__names[] = {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020013 [0] = "TOTAL",
14 [PERF_RECORD_MMAP] = "MMAP",
Stephane Eranian5c5e8542013-08-21 12:10:25 +020015 [PERF_RECORD_MMAP2] = "MMAP2",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020016 [PERF_RECORD_LOST] = "LOST",
17 [PERF_RECORD_COMM] = "COMM",
18 [PERF_RECORD_EXIT] = "EXIT",
19 [PERF_RECORD_THROTTLE] = "THROTTLE",
20 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
21 [PERF_RECORD_FORK] = "FORK",
22 [PERF_RECORD_READ] = "READ",
23 [PERF_RECORD_SAMPLE] = "SAMPLE",
24 [PERF_RECORD_HEADER_ATTR] = "ATTR",
25 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
26 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
27 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
28 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030029};
30
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020031const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc02010-12-07 12:48:42 +000032{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020033 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc02010-12-07 12:48:42 +000034 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020035 if (!perf_event__names[id])
Thomas Gleixner3835bc02010-12-07 12:48:42 +000036 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020037 return perf_event__names[id];
Thomas Gleixner3835bc02010-12-07 12:48:42 +000038}
39
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020040static struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020041 .pid = -1,
42 .tid = -1,
43 .time = -1,
44 .stream_id = -1,
45 .cpu = -1,
46 .period = 1,
47};
48
David Ahernf5faf722011-12-22 11:30:02 -070049static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020050{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020051 char filename[PATH_MAX];
52 char bf[BUFSIZ];
53 FILE *fp;
54 size_t size = 0;
David Ahernf5faf722011-12-22 11:30:02 -070055 pid_t tgid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020056
57 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
58
59 fp = fopen(filename, "r");
60 if (fp == NULL) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020061 pr_debug("couldn't open %s\n", filename);
62 return 0;
63 }
64
David Ahernf5faf722011-12-22 11:30:02 -070065 while (!comm[0] || (tgid < 0)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020066 if (fgets(bf, sizeof(bf), fp) == NULL) {
David Ahernf5faf722011-12-22 11:30:02 -070067 pr_warning("couldn't get COMM and pgid, malformed %s\n",
68 filename);
69 break;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020070 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020071
72 if (memcmp(bf, "Name:", 5) == 0) {
73 char *name = bf + 5;
74 while (*name && isspace(*name))
75 ++name;
76 size = strlen(name) - 1;
David Ahernf5faf722011-12-22 11:30:02 -070077 if (size >= len)
78 size = len - 1;
79 memcpy(comm, name, size);
David Aherncfbd70c2012-02-24 12:31:38 -070080 comm[size] = '\0';
David Ahernf5faf722011-12-22 11:30:02 -070081
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020082 } else if (memcmp(bf, "Tgid:", 5) == 0) {
83 char *tgids = bf + 5;
84 while (*tgids && isspace(*tgids))
85 ++tgids;
David Ahernf5faf722011-12-22 11:30:02 -070086 tgid = atoi(tgids);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020087 }
88 }
89
David Ahernf5faf722011-12-22 11:30:02 -070090 fclose(fp);
91
92 return tgid;
93}
94
95static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
96 union perf_event *event, pid_t pid,
97 int full,
98 perf_event__handler_t process,
99 struct machine *machine)
100{
101 char filename[PATH_MAX];
102 size_t size;
103 DIR *tasks;
104 struct dirent dirent, *next;
105 pid_t tgid;
106
107 memset(&event->comm, 0, sizeof(event->comm));
108
109 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
110 sizeof(event->comm.comm));
111 if (tgid < 0)
112 goto out;
113
114 event->comm.pid = tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200115 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700116
117 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300118 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200119 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200120 event->comm.header.size = (sizeof(event->comm) -
121 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200122 machine->id_hdr_size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200123 if (!full) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200124 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200125
David Ahern1e6d5322012-08-26 12:24:42 -0600126 if (process(tool, event, &synth_sample, machine) != 0)
127 return -1;
128
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200129 goto out;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200130 }
131
Dongsheng Yang99563462013-12-20 15:52:57 -0500132 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
133 machine->root_dir, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200134
135 tasks = opendir(filename);
David Ahernf5faf722011-12-22 11:30:02 -0700136 if (tasks == NULL) {
137 pr_debug("couldn't open %s\n", filename);
138 return 0;
139 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200140
141 while (!readdir_r(tasks, &dirent, &next) && next) {
142 char *end;
143 pid = strtol(dirent.d_name, &end, 10);
144 if (*end)
145 continue;
146
David Ahernf5faf722011-12-22 11:30:02 -0700147 /* already have tgid; jut want to update the comm */
148 (void) perf_event__get_comm_tgid(pid, event->comm.comm,
149 sizeof(event->comm.comm));
150
151 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300152 size = PERF_ALIGN(size, sizeof(u64));
David Ahernf5faf722011-12-22 11:30:02 -0700153 memset(event->comm.comm + size, 0, machine->id_hdr_size);
154 event->comm.header.size = (sizeof(event->comm) -
155 (sizeof(event->comm.comm) - size) +
156 machine->id_hdr_size);
157
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200158 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200159
David Ahern1e6d5322012-08-26 12:24:42 -0600160 if (process(tool, event, &synth_sample, machine) != 0) {
161 tgid = -1;
162 break;
163 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200164 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200165
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200166 closedir(tasks);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200167out:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200168 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200169}
170
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200171static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200172 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200173 pid_t pid, pid_t tgid,
174 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300175 struct machine *machine,
176 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200177{
178 char filename[PATH_MAX];
179 FILE *fp;
David Ahern1e6d5322012-08-26 12:24:42 -0600180 int rc = 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200181
Dongsheng Yang99563462013-12-20 15:52:57 -0500182 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
183 machine->root_dir, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200184
185 fp = fopen(filename, "r");
186 if (fp == NULL) {
187 /*
188 * We raced with a task exiting - just return:
189 */
190 pr_debug("couldn't open %s\n", filename);
191 return -1;
192 }
193
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200194 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200195
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200196 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900197 char bf[BUFSIZ];
198 char prot[5];
199 char execname[PATH_MAX];
200 char anonstr[] = "//anon";
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200201 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200202 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900203
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200204 if (fgets(bf, sizeof(bf), fp) == NULL)
205 break;
206
Namhyung Kim60648032012-11-11 23:20:50 +0900207 /* ensure null termination since stack will be reused. */
208 strcpy(execname, "");
209
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200210 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200211 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %*x:%*x %*u %s\n",
212 &event->mmap.start, &event->mmap.len, prot,
213 &event->mmap.pgoff,
214 execname);
Don Zickus9d4ecc82013-11-13 15:32:06 -0300215 /*
216 * Anon maps don't have the execname.
217 */
218 if (n < 4)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200219 continue;
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300220 /*
221 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
222 */
Dongsheng Yang73547aa2013-12-20 15:52:58 -0500223 if (machine__is_host(machine))
224 event->header.misc = PERF_RECORD_MISC_USER;
225 else
226 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900227
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300228 if (prot[2] != 'x') {
229 if (!mmap_data || prot[0] != 'r')
230 continue;
231
232 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
233 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200234
Namhyung Kim60648032012-11-11 23:20:50 +0900235 if (!strcmp(execname, ""))
236 strcpy(execname, anonstr);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200237
Namhyung Kim60648032012-11-11 23:20:50 +0900238 size = strlen(execname) + 1;
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200239 memcpy(event->mmap.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900240 size = PERF_ALIGN(size, sizeof(u64));
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200241 event->mmap.len -= event->mmap.start;
242 event->mmap.header.size = (sizeof(event->mmap) -
243 (sizeof(event->mmap.filename) - size));
244 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
245 event->mmap.header.size += machine->id_hdr_size;
246 event->mmap.pid = tgid;
247 event->mmap.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000248
Namhyung Kim60648032012-11-11 23:20:50 +0900249 if (process(tool, event, &synth_sample, machine) != 0) {
250 rc = -1;
251 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200252 }
253 }
254
255 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600256 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200257}
258
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200259int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200260 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200261 struct machine *machine)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200262{
David Ahern1e6d5322012-08-26 12:24:42 -0600263 int rc = 0;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200264 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300265 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200266 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200267 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200268 if (event == NULL) {
269 pr_debug("Not enough memory synthesizing mmap event "
270 "for kernel modules\n");
271 return -1;
272 }
273
274 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200275
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800276 /*
277 * kernel uses 0 for user space maps, see kernel/perf_event.c
278 * __perf_event_mmap
279 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300280 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200281 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800282 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200283 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800284
285 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200286 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200287 size_t size;
288 struct map *pos = rb_entry(nd, struct map, rb_node);
289
290 if (pos->dso->kernel)
291 continue;
292
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300293 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200294 event->mmap.header.type = PERF_RECORD_MMAP;
295 event->mmap.header.size = (sizeof(event->mmap) -
296 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200297 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
298 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200299 event->mmap.start = pos->start;
300 event->mmap.len = pos->end - pos->start;
301 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200302
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200303 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200304 pos->dso->long_name_len + 1);
David Ahern1e6d5322012-08-26 12:24:42 -0600305 if (process(tool, event, &synth_sample, machine) != 0) {
306 rc = -1;
307 break;
308 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200309 }
310
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200311 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600312 return rc;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200313}
314
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200315static int __event__synthesize_thread(union perf_event *comm_event,
316 union perf_event *mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700317 pid_t pid, int full,
318 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200319 struct perf_tool *tool,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300320 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200321{
David Aherndefd8d32011-12-22 11:30:01 -0700322 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200323 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200324 if (tgid == -1)
325 return -1;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200326 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300327 process, machine, mmap_data);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200328}
329
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200330int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200331 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200332 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300333 struct machine *machine,
334 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200335{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200336 union perf_event *comm_event, *mmap_event;
David Aherndefd8d32011-12-22 11:30:01 -0700337 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200338
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200339 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200340 if (comm_event == NULL)
341 goto out;
342
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200343 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200344 if (mmap_event == NULL)
345 goto out_free_comm;
346
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200347 err = 0;
348 for (thread = 0; thread < threads->nr; ++thread) {
349 if (__event__synthesize_thread(comm_event, mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700350 threads->map[thread], 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300351 process, tool, machine,
352 mmap_data)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200353 err = -1;
354 break;
355 }
David Aherndefd8d32011-12-22 11:30:01 -0700356
357 /*
358 * comm.pid is set to thread group id by
359 * perf_event__synthesize_comm
360 */
361 if ((int) comm_event->comm.pid != threads->map[thread]) {
362 bool need_leader = true;
363
364 /* is thread group leader in thread_map? */
365 for (j = 0; j < threads->nr; ++j) {
366 if ((int) comm_event->comm.pid == threads->map[j]) {
367 need_leader = false;
368 break;
369 }
370 }
371
372 /* if not, generate events for it */
373 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300374 __event__synthesize_thread(comm_event, mmap_event,
375 comm_event->comm.pid, 0,
376 process, tool, machine,
377 mmap_data)) {
David Aherndefd8d32011-12-22 11:30:01 -0700378 err = -1;
379 break;
380 }
381 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200382 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200383 free(mmap_event);
384out_free_comm:
385 free(comm_event);
386out:
387 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200388}
389
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200390int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200391 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300392 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200393{
394 DIR *proc;
Dongsheng Yang99563462013-12-20 15:52:57 -0500395 char proc_path[PATH_MAX];
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200396 struct dirent dirent, *next;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200397 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200398 int err = -1;
399
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200400 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200401 if (comm_event == NULL)
402 goto out;
403
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200404 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200405 if (mmap_event == NULL)
406 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200407
Dongsheng Yang99563462013-12-20 15:52:57 -0500408 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
409 proc = opendir(proc_path);
410
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200411 if (proc == NULL)
412 goto out_free_mmap;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200413
414 while (!readdir_r(proc, &dirent, &next) && next) {
415 char *end;
416 pid_t pid = strtol(dirent.d_name, &end, 10);
417
418 if (*end) /* only interested in proper numerical dirents */
419 continue;
Arnaldo Carvalho de Meloba361c92012-10-17 18:50:13 -0300420 /*
421 * We may race with exiting thread, so don't stop just because
422 * one thread couldn't be synthesized.
423 */
424 __event__synthesize_thread(comm_event, mmap_event, pid, 1,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300425 process, tool, machine, mmap_data);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200426 }
427
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200428 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600429 closedir(proc);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200430out_free_mmap:
431 free(mmap_event);
432out_free_comm:
433 free(comm_event);
434out:
435 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200436}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200437
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200438struct process_symbol_args {
439 const char *name;
440 u64 start;
441};
442
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200443static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700444 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200445{
446 struct process_symbol_args *args = arg;
447
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200448 /*
449 * Must be a function or at least an alias, as in PARISC64, where "_text" is
450 * an 'A' to the same address as "_stext".
451 */
452 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
453 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200454 return 0;
455
456 args->start = start;
457 return 1;
458}
459
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200460int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200461 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200462 struct machine *machine,
463 const char *symbol_name)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200464{
465 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800466 const char *filename, *mmap_name;
467 char path[PATH_MAX];
468 char name_buff[PATH_MAX];
469 struct map *map;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200470 int err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200471 /*
472 * We should get this from /sys/kernel/sections/.text, but till that is
473 * available use this, and after it is use this as a fallback for older
474 * kernels.
475 */
476 struct process_symbol_args args = { .name = symbol_name, };
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200477 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200478 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200479 if (event == NULL) {
480 pr_debug("Not enough memory synthesizing mmap event "
481 "for kernel modules\n");
482 return -1;
483 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200484
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300485 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300486 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800487 /*
488 * kernel uses PERF_RECORD_MISC_USER for user space maps,
489 * see kernel/perf_event.c __perf_event_mmap
490 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200491 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800492 filename = "/proc/kallsyms";
493 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200494 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300495 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800496 filename = (char *) symbol_conf.default_guest_kallsyms;
497 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300498 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800499 filename = path;
500 }
501 }
502
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100503 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0) {
504 free(event);
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200505 return -ENOENT;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100506 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200507
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300508 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200509 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800510 "%s%s", mmap_name, symbol_name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300511 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200512 event->mmap.header.type = PERF_RECORD_MMAP;
513 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200514 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200515 event->mmap.pgoff = args.start;
516 event->mmap.start = map->start;
517 event->mmap.len = map->end - event->mmap.start;
518 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200519
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200520 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200521 free(event);
522
523 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200524}
525
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200526size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
527{
528 return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
529}
530
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300531int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200532 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200533 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200534 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200535{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200536 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200537}
538
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300539int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200540 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200541 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300542 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200543{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200544 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800545}
546
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200547size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
548{
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300549 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200550 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300551 event->mmap.len, event->mmap.pgoff,
552 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
553 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200554}
555
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200556size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
557{
558 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300559 " %02x:%02x %"PRIu64" %"PRIu64"]: %c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200560 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
561 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
562 event->mmap2.min, event->mmap2.ino,
563 event->mmap2.ino_generation,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300564 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200565 event->mmap2.filename);
566}
567
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300568int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200569 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200570 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200571 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800572{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200573 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200574}
575
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200576int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
577 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200578 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200579 struct machine *machine)
580{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200581 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200582}
583
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200584size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
585{
586 return fprintf(fp, "(%d:%d):(%d:%d)\n",
587 event->fork.pid, event->fork.tid,
588 event->fork.ppid, event->fork.ptid);
589}
590
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300591int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200592 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200593 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300594 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200595{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200596 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200597}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200598
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300599int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
600 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200601 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300602 struct machine *machine)
603{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200604 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300605}
606
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200607size_t perf_event__fprintf(union perf_event *event, FILE *fp)
608{
609 size_t ret = fprintf(fp, "PERF_RECORD_%s",
610 perf_event__name(event->header.type));
611
612 switch (event->header.type) {
613 case PERF_RECORD_COMM:
614 ret += perf_event__fprintf_comm(event, fp);
615 break;
616 case PERF_RECORD_FORK:
617 case PERF_RECORD_EXIT:
618 ret += perf_event__fprintf_task(event, fp);
619 break;
620 case PERF_RECORD_MMAP:
621 ret += perf_event__fprintf_mmap(event, fp);
622 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200623 case PERF_RECORD_MMAP2:
624 ret += perf_event__fprintf_mmap2(event, fp);
625 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200626 default:
627 ret += fprintf(fp, "\n");
628 }
629
630 return ret;
631}
632
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300633int perf_event__process(struct perf_tool *tool __maybe_unused,
634 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200635 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300636 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530637{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200638 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530639}
640
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300641void thread__find_addr_map(struct thread *thread,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200642 struct machine *machine, u8 cpumode,
643 enum map_type type, u64 addr,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300644 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200645{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300646 struct map_groups *mg = &thread->mg;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300647 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200648
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300649 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300650 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200651 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800652 al->cpumode = cpumode;
653 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200654
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200655 if (machine == NULL) {
656 al->map = NULL;
657 return;
658 }
659
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800660 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200661 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300662 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300663 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800664 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200665 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800666 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
667 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300668 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300669 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500670 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
671 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800672 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500673 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200674 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800675
676 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
677 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
678 !perf_guest)
679 al->filtered = true;
680 if ((cpumode == PERF_RECORD_MISC_USER ||
681 cpumode == PERF_RECORD_MISC_KERNEL) &&
682 !perf_host)
683 al->filtered = true;
684
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200685 return;
686 }
687try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200688 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200689 if (al->map == NULL) {
690 /*
691 * If this is outside of all known maps, and is a negative
692 * address, try to look it up in the kernel dso, as it might be
693 * a vsyscall or vdso (which executes in user-mode).
694 *
695 * XXX This is nasty, we should have a symbol list in the
696 * "[vdso]" dso, but for now lets use the old trick of looking
697 * in the whole kernel symbol list.
698 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800699 if ((long long)al->addr < 0 &&
Andrew Lutomirski6c6804fb2011-03-24 00:36:56 -0400700 cpumode == PERF_RECORD_MISC_USER &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300701 machine && mg != &machine->kmaps) {
702 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200703 goto try_again;
704 }
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300705 } else {
706 /*
707 * Kernel maps might be changed when loading symbols so loading
708 * must be done prior to using kernel maps.
709 */
710 if (load_map)
Adrian Hunter326f59b2013-08-08 14:32:27 +0300711 map__load(al->map, machine->symbol_filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200712 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300713 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200714}
715
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200716void thread__find_addr_location(struct thread *thread, struct machine *machine,
717 u8 cpumode, enum map_type type, u64 addr,
Adrian Hunter61710bd2013-08-08 14:32:26 +0300718 struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200719{
Adrian Hunter326f59b2013-08-08 14:32:27 +0300720 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200721 if (al->map != NULL)
Adrian Hunter61710bd2013-08-08 14:32:26 +0300722 al->sym = map__find_symbol(al->map, al->addr,
723 machine->symbol_filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200724 else
725 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200726}
727
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200728int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200729 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200730 struct addr_location *al,
Adrian Huntere44baa32013-08-08 14:32:25 +0300731 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200732{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200733 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Adrian Hunteref893252013-08-27 11:23:06 +0300734 struct thread *thread = machine__findnew_thread(machine, sample->pid,
735 sample->pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200736
737 if (thread == NULL)
738 return -1;
739
David Ahern1f3878c2013-11-18 13:32:47 -0700740 if (thread__is_filtered(thread))
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200741 goto out_filtered;
742
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200743 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300744 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200745 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300746 *
747 * This should have happened earlier, when we processed the kernel MMAP
748 * events, but for older perf.data files there was no such thing, so do
749 * it now.
750 */
751 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200752 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
753 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200754
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200755 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
Adrian Hunteref893252013-08-27 11:23:06 +0300756 sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200757 dump_printf(" ...... dso: %s\n",
758 al->map ? al->map->dso->long_name :
759 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300760 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200761 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200762
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300763 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900764 struct dso *dso = al->map->dso;
765
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300766 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900767 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
768 dso->short_name) ||
769 (dso->short_name != dso->long_name &&
770 strlist__has_entry(symbol_conf.dso_list,
771 dso->long_name)))))
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300772 goto out_filtered;
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300773
Adrian Huntere44baa32013-08-08 14:32:25 +0300774 al->sym = map__find_symbol(al->map, al->addr,
775 machine->symbol_filter);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300776 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200777
Feng Tang1500b932012-09-07 16:42:23 +0800778 if (symbol_conf.sym_list &&
779 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
780 al->sym->name)))
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200781 goto out_filtered;
782
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200783 return 0;
784
785out_filtered:
786 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200787 return 0;
788}