blob: 484e99464a0081c425bd15264675310a0cc1dde0 [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
132 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
133
134 tasks = opendir(filename);
David Ahernf5faf722011-12-22 11:30:02 -0700135 if (tasks == NULL) {
136 pr_debug("couldn't open %s\n", filename);
137 return 0;
138 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200139
140 while (!readdir_r(tasks, &dirent, &next) && next) {
141 char *end;
142 pid = strtol(dirent.d_name, &end, 10);
143 if (*end)
144 continue;
145
David Ahernf5faf722011-12-22 11:30:02 -0700146 /* already have tgid; jut want to update the comm */
147 (void) perf_event__get_comm_tgid(pid, event->comm.comm,
148 sizeof(event->comm.comm));
149
150 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300151 size = PERF_ALIGN(size, sizeof(u64));
David Ahernf5faf722011-12-22 11:30:02 -0700152 memset(event->comm.comm + size, 0, machine->id_hdr_size);
153 event->comm.header.size = (sizeof(event->comm) -
154 (sizeof(event->comm.comm) - size) +
155 machine->id_hdr_size);
156
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200157 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200158
David Ahern1e6d5322012-08-26 12:24:42 -0600159 if (process(tool, event, &synth_sample, machine) != 0) {
160 tgid = -1;
161 break;
162 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200163 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200164
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200165 closedir(tasks);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200166out:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200167 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200168}
169
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200170static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200171 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200172 pid_t pid, pid_t tgid,
173 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300174 struct machine *machine,
175 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200176{
177 char filename[PATH_MAX];
178 FILE *fp;
David Ahern1e6d5322012-08-26 12:24:42 -0600179 int rc = 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200180
181 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
182
183 fp = fopen(filename, "r");
184 if (fp == NULL) {
185 /*
186 * We raced with a task exiting - just return:
187 */
188 pr_debug("couldn't open %s\n", filename);
189 return -1;
190 }
191
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200192 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200193
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200194 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900195 char bf[BUFSIZ];
196 char prot[5];
197 char execname[PATH_MAX];
198 char anonstr[] = "//anon";
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200199 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200200 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900201
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200202 if (fgets(bf, sizeof(bf), fp) == NULL)
203 break;
204
Namhyung Kim60648032012-11-11 23:20:50 +0900205 /* ensure null termination since stack will be reused. */
206 strcpy(execname, "");
207
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200208 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200209 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %*x:%*x %*u %s\n",
210 &event->mmap.start, &event->mmap.len, prot,
211 &event->mmap.pgoff,
212 execname);
Don Zickus9d4ecc82013-11-13 15:32:06 -0300213 /*
214 * Anon maps don't have the execname.
215 */
216 if (n < 4)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200217 continue;
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300218 /*
219 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
220 */
221 event->header.misc = PERF_RECORD_MISC_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900222
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300223 if (prot[2] != 'x') {
224 if (!mmap_data || prot[0] != 'r')
225 continue;
226
227 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
228 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200229
Namhyung Kim60648032012-11-11 23:20:50 +0900230 if (!strcmp(execname, ""))
231 strcpy(execname, anonstr);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200232
Namhyung Kim60648032012-11-11 23:20:50 +0900233 size = strlen(execname) + 1;
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200234 memcpy(event->mmap.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900235 size = PERF_ALIGN(size, sizeof(u64));
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200236 event->mmap.len -= event->mmap.start;
237 event->mmap.header.size = (sizeof(event->mmap) -
238 (sizeof(event->mmap.filename) - size));
239 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
240 event->mmap.header.size += machine->id_hdr_size;
241 event->mmap.pid = tgid;
242 event->mmap.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000243
Namhyung Kim60648032012-11-11 23:20:50 +0900244 if (process(tool, event, &synth_sample, machine) != 0) {
245 rc = -1;
246 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200247 }
248 }
249
250 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600251 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200252}
253
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200254int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200255 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200256 struct machine *machine)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200257{
David Ahern1e6d5322012-08-26 12:24:42 -0600258 int rc = 0;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200259 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300260 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200261 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200262 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200263 if (event == NULL) {
264 pr_debug("Not enough memory synthesizing mmap event "
265 "for kernel modules\n");
266 return -1;
267 }
268
269 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200270
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800271 /*
272 * kernel uses 0 for user space maps, see kernel/perf_event.c
273 * __perf_event_mmap
274 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300275 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200276 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800277 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200278 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800279
280 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200281 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200282 size_t size;
283 struct map *pos = rb_entry(nd, struct map, rb_node);
284
285 if (pos->dso->kernel)
286 continue;
287
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300288 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200289 event->mmap.header.type = PERF_RECORD_MMAP;
290 event->mmap.header.size = (sizeof(event->mmap) -
291 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200292 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
293 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200294 event->mmap.start = pos->start;
295 event->mmap.len = pos->end - pos->start;
296 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200297
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200298 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200299 pos->dso->long_name_len + 1);
David Ahern1e6d5322012-08-26 12:24:42 -0600300 if (process(tool, event, &synth_sample, machine) != 0) {
301 rc = -1;
302 break;
303 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200304 }
305
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200306 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600307 return rc;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200308}
309
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200310static int __event__synthesize_thread(union perf_event *comm_event,
311 union perf_event *mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700312 pid_t pid, int full,
313 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200314 struct perf_tool *tool,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300315 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200316{
David Aherndefd8d32011-12-22 11:30:01 -0700317 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200318 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200319 if (tgid == -1)
320 return -1;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200321 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300322 process, machine, mmap_data);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200323}
324
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200325int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200326 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200327 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300328 struct machine *machine,
329 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200330{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200331 union perf_event *comm_event, *mmap_event;
David Aherndefd8d32011-12-22 11:30:01 -0700332 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200333
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200334 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200335 if (comm_event == NULL)
336 goto out;
337
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200338 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200339 if (mmap_event == NULL)
340 goto out_free_comm;
341
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200342 err = 0;
343 for (thread = 0; thread < threads->nr; ++thread) {
344 if (__event__synthesize_thread(comm_event, mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700345 threads->map[thread], 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300346 process, tool, machine,
347 mmap_data)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200348 err = -1;
349 break;
350 }
David Aherndefd8d32011-12-22 11:30:01 -0700351
352 /*
353 * comm.pid is set to thread group id by
354 * perf_event__synthesize_comm
355 */
356 if ((int) comm_event->comm.pid != threads->map[thread]) {
357 bool need_leader = true;
358
359 /* is thread group leader in thread_map? */
360 for (j = 0; j < threads->nr; ++j) {
361 if ((int) comm_event->comm.pid == threads->map[j]) {
362 need_leader = false;
363 break;
364 }
365 }
366
367 /* if not, generate events for it */
368 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300369 __event__synthesize_thread(comm_event, mmap_event,
370 comm_event->comm.pid, 0,
371 process, tool, machine,
372 mmap_data)) {
David Aherndefd8d32011-12-22 11:30:01 -0700373 err = -1;
374 break;
375 }
376 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200377 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200378 free(mmap_event);
379out_free_comm:
380 free(comm_event);
381out:
382 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200383}
384
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200385int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200386 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300387 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200388{
389 DIR *proc;
390 struct dirent dirent, *next;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200391 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200392 int err = -1;
393
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200394 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200395 if (comm_event == NULL)
396 goto out;
397
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200398 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200399 if (mmap_event == NULL)
400 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200401
402 proc = opendir("/proc");
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200403 if (proc == NULL)
404 goto out_free_mmap;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200405
406 while (!readdir_r(proc, &dirent, &next) && next) {
407 char *end;
408 pid_t pid = strtol(dirent.d_name, &end, 10);
409
410 if (*end) /* only interested in proper numerical dirents */
411 continue;
Arnaldo Carvalho de Meloba361c92012-10-17 18:50:13 -0300412 /*
413 * We may race with exiting thread, so don't stop just because
414 * one thread couldn't be synthesized.
415 */
416 __event__synthesize_thread(comm_event, mmap_event, pid, 1,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300417 process, tool, machine, mmap_data);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200418 }
419
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200420 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600421 closedir(proc);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200422out_free_mmap:
423 free(mmap_event);
424out_free_comm:
425 free(comm_event);
426out:
427 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200428}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200429
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200430struct process_symbol_args {
431 const char *name;
432 u64 start;
433};
434
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200435static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700436 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200437{
438 struct process_symbol_args *args = arg;
439
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200440 /*
441 * Must be a function or at least an alias, as in PARISC64, where "_text" is
442 * an 'A' to the same address as "_stext".
443 */
444 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
445 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200446 return 0;
447
448 args->start = start;
449 return 1;
450}
451
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200452int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200453 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200454 struct machine *machine,
455 const char *symbol_name)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200456{
457 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800458 const char *filename, *mmap_name;
459 char path[PATH_MAX];
460 char name_buff[PATH_MAX];
461 struct map *map;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200462 int err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200463 /*
464 * We should get this from /sys/kernel/sections/.text, but till that is
465 * available use this, and after it is use this as a fallback for older
466 * kernels.
467 */
468 struct process_symbol_args args = { .name = symbol_name, };
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200469 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200470 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200471 if (event == NULL) {
472 pr_debug("Not enough memory synthesizing mmap event "
473 "for kernel modules\n");
474 return -1;
475 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200476
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300477 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300478 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800479 /*
480 * kernel uses PERF_RECORD_MISC_USER for user space maps,
481 * see kernel/perf_event.c __perf_event_mmap
482 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200483 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800484 filename = "/proc/kallsyms";
485 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200486 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300487 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800488 filename = (char *) symbol_conf.default_guest_kallsyms;
489 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300490 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800491 filename = path;
492 }
493 }
494
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100495 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0) {
496 free(event);
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200497 return -ENOENT;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100498 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200499
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300500 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200501 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800502 "%s%s", mmap_name, symbol_name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300503 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200504 event->mmap.header.type = PERF_RECORD_MMAP;
505 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200506 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200507 event->mmap.pgoff = args.start;
508 event->mmap.start = map->start;
509 event->mmap.len = map->end - event->mmap.start;
510 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200511
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200512 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200513 free(event);
514
515 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200516}
517
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200518size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
519{
520 return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
521}
522
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300523int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200524 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200525 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200526 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200527{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200528 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200529}
530
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300531int perf_event__process_lost(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 Melob0a7d1a2012-10-06 16:26:02 -0300534 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200535{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200536 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800537}
538
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200539size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
540{
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300541 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200542 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300543 event->mmap.len, event->mmap.pgoff,
544 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
545 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200546}
547
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200548size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
549{
550 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300551 " %02x:%02x %"PRIu64" %"PRIu64"]: %c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200552 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
553 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
554 event->mmap2.min, event->mmap2.ino,
555 event->mmap2.ino_generation,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300556 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200557 event->mmap2.filename);
558}
559
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300560int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200561 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200562 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200563 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800564{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200565 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200566}
567
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200568int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
569 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200570 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200571 struct machine *machine)
572{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200573 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200574}
575
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200576size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
577{
578 return fprintf(fp, "(%d:%d):(%d:%d)\n",
579 event->fork.pid, event->fork.tid,
580 event->fork.ppid, event->fork.ptid);
581}
582
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300583int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200584 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200585 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300586 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200587{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200588 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200589}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200590
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300591int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
592 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)
595{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200596 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300597}
598
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200599size_t perf_event__fprintf(union perf_event *event, FILE *fp)
600{
601 size_t ret = fprintf(fp, "PERF_RECORD_%s",
602 perf_event__name(event->header.type));
603
604 switch (event->header.type) {
605 case PERF_RECORD_COMM:
606 ret += perf_event__fprintf_comm(event, fp);
607 break;
608 case PERF_RECORD_FORK:
609 case PERF_RECORD_EXIT:
610 ret += perf_event__fprintf_task(event, fp);
611 break;
612 case PERF_RECORD_MMAP:
613 ret += perf_event__fprintf_mmap(event, fp);
614 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200615 case PERF_RECORD_MMAP2:
616 ret += perf_event__fprintf_mmap2(event, fp);
617 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200618 default:
619 ret += fprintf(fp, "\n");
620 }
621
622 return ret;
623}
624
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300625int perf_event__process(struct perf_tool *tool __maybe_unused,
626 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200627 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300628 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530629{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200630 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530631}
632
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300633void thread__find_addr_map(struct thread *thread,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200634 struct machine *machine, u8 cpumode,
635 enum map_type type, u64 addr,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300636 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200637{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300638 struct map_groups *mg = &thread->mg;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300639 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200640
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300641 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300642 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200643 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800644 al->cpumode = cpumode;
645 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200646
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200647 if (machine == NULL) {
648 al->map = NULL;
649 return;
650 }
651
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800652 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200653 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300654 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300655 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800656 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200657 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800658 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
659 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300660 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300661 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500662 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
663 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800664 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500665 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200666 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800667
668 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
669 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
670 !perf_guest)
671 al->filtered = true;
672 if ((cpumode == PERF_RECORD_MISC_USER ||
673 cpumode == PERF_RECORD_MISC_KERNEL) &&
674 !perf_host)
675 al->filtered = true;
676
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200677 return;
678 }
679try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200680 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200681 if (al->map == NULL) {
682 /*
683 * If this is outside of all known maps, and is a negative
684 * address, try to look it up in the kernel dso, as it might be
685 * a vsyscall or vdso (which executes in user-mode).
686 *
687 * XXX This is nasty, we should have a symbol list in the
688 * "[vdso]" dso, but for now lets use the old trick of looking
689 * in the whole kernel symbol list.
690 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800691 if ((long long)al->addr < 0 &&
Andrew Lutomirski6c6804fb2011-03-24 00:36:56 -0400692 cpumode == PERF_RECORD_MISC_USER &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300693 machine && mg != &machine->kmaps) {
694 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200695 goto try_again;
696 }
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300697 } else {
698 /*
699 * Kernel maps might be changed when loading symbols so loading
700 * must be done prior to using kernel maps.
701 */
702 if (load_map)
Adrian Hunter326f59b2013-08-08 14:32:27 +0300703 map__load(al->map, machine->symbol_filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200704 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300705 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200706}
707
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200708void thread__find_addr_location(struct thread *thread, struct machine *machine,
709 u8 cpumode, enum map_type type, u64 addr,
Adrian Hunter61710bd2013-08-08 14:32:26 +0300710 struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200711{
Adrian Hunter326f59b2013-08-08 14:32:27 +0300712 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200713 if (al->map != NULL)
Adrian Hunter61710bd2013-08-08 14:32:26 +0300714 al->sym = map__find_symbol(al->map, al->addr,
715 machine->symbol_filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200716 else
717 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200718}
719
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200720int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200721 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200722 struct addr_location *al,
Adrian Huntere44baa32013-08-08 14:32:25 +0300723 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200724{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200725 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Adrian Hunteref893252013-08-27 11:23:06 +0300726 struct thread *thread = machine__findnew_thread(machine, sample->pid,
727 sample->pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200728
729 if (thread == NULL)
730 return -1;
731
David Ahern1f3878c2013-11-18 13:32:47 -0700732 if (thread__is_filtered(thread))
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200733 goto out_filtered;
734
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200735 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300736 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200737 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300738 *
739 * This should have happened earlier, when we processed the kernel MMAP
740 * events, but for older perf.data files there was no such thing, so do
741 * it now.
742 */
743 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200744 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
745 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200746
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200747 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
Adrian Hunteref893252013-08-27 11:23:06 +0300748 sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200749 dump_printf(" ...... dso: %s\n",
750 al->map ? al->map->dso->long_name :
751 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300752 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200753 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200754
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300755 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900756 struct dso *dso = al->map->dso;
757
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300758 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900759 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
760 dso->short_name) ||
761 (dso->short_name != dso->long_name &&
762 strlist__has_entry(symbol_conf.dso_list,
763 dso->long_name)))))
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300764 goto out_filtered;
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300765
Adrian Huntere44baa32013-08-08 14:32:25 +0300766 al->sym = map__find_symbol(al->map, al->addr,
767 machine->symbol_filter);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300768 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200769
Feng Tang1500b932012-09-07 16:42:23 +0800770 if (symbol_conf.sym_list &&
771 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
772 al->sym->name)))
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200773 goto out_filtered;
774
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200775 return 0;
776
777out_filtered:
778 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200779 return 0;
780}