blob: 8202f5ca04830dd391e1c946ed638dba6e98bba6 [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 Meloc410a332009-12-15 20:04:41 -02004#include "sort.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02005#include "string.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02006#include "strlist.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02007#include "thread.h"
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -02008#include "thread_map.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02009
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020010static const char *perf_event__names[] = {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020011 [0] = "TOTAL",
12 [PERF_RECORD_MMAP] = "MMAP",
13 [PERF_RECORD_LOST] = "LOST",
14 [PERF_RECORD_COMM] = "COMM",
15 [PERF_RECORD_EXIT] = "EXIT",
16 [PERF_RECORD_THROTTLE] = "THROTTLE",
17 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
18 [PERF_RECORD_FORK] = "FORK",
19 [PERF_RECORD_READ] = "READ",
20 [PERF_RECORD_SAMPLE] = "SAMPLE",
21 [PERF_RECORD_HEADER_ATTR] = "ATTR",
22 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
23 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
24 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
25 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030026};
27
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020028const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc002010-12-07 12:48:42 +000029{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020030 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc002010-12-07 12:48:42 +000031 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020032 if (!perf_event__names[id])
Thomas Gleixner3835bc002010-12-07 12:48:42 +000033 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020034 return perf_event__names[id];
Thomas Gleixner3835bc002010-12-07 12:48:42 +000035}
36
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020037static struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020038 .pid = -1,
39 .tid = -1,
40 .time = -1,
41 .stream_id = -1,
42 .cpu = -1,
43 .period = 1,
44};
45
David Ahernf5faf722011-12-22 11:30:02 -070046static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020047{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020048 char filename[PATH_MAX];
49 char bf[BUFSIZ];
50 FILE *fp;
51 size_t size = 0;
David Ahernf5faf722011-12-22 11:30:02 -070052 pid_t tgid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020053
54 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
55
56 fp = fopen(filename, "r");
57 if (fp == NULL) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020058 pr_debug("couldn't open %s\n", filename);
59 return 0;
60 }
61
David Ahernf5faf722011-12-22 11:30:02 -070062 while (!comm[0] || (tgid < 0)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020063 if (fgets(bf, sizeof(bf), fp) == NULL) {
David Ahernf5faf722011-12-22 11:30:02 -070064 pr_warning("couldn't get COMM and pgid, malformed %s\n",
65 filename);
66 break;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020067 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020068
69 if (memcmp(bf, "Name:", 5) == 0) {
70 char *name = bf + 5;
71 while (*name && isspace(*name))
72 ++name;
73 size = strlen(name) - 1;
David Ahernf5faf722011-12-22 11:30:02 -070074 if (size >= len)
75 size = len - 1;
76 memcpy(comm, name, size);
David Aherncfbd70c2012-02-24 12:31:38 -070077 comm[size] = '\0';
David Ahernf5faf722011-12-22 11:30:02 -070078
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020079 } else if (memcmp(bf, "Tgid:", 5) == 0) {
80 char *tgids = bf + 5;
81 while (*tgids && isspace(*tgids))
82 ++tgids;
David Ahernf5faf722011-12-22 11:30:02 -070083 tgid = atoi(tgids);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020084 }
85 }
86
David Ahernf5faf722011-12-22 11:30:02 -070087 fclose(fp);
88
89 return tgid;
90}
91
92static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
93 union perf_event *event, pid_t pid,
94 int full,
95 perf_event__handler_t process,
96 struct machine *machine)
97{
98 char filename[PATH_MAX];
99 size_t size;
100 DIR *tasks;
101 struct dirent dirent, *next;
102 pid_t tgid;
103
104 memset(&event->comm, 0, sizeof(event->comm));
105
106 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
107 sizeof(event->comm.comm));
108 if (tgid < 0)
109 goto out;
110
111 event->comm.pid = tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200112 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700113
114 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300115 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200116 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200117 event->comm.header.size = (sizeof(event->comm) -
118 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200119 machine->id_hdr_size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200120 if (!full) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200121 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200122
David Ahern1e6d5322012-08-26 12:24:42 -0600123 if (process(tool, event, &synth_sample, machine) != 0)
124 return -1;
125
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200126 goto out;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200127 }
128
129 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
130
131 tasks = opendir(filename);
David Ahernf5faf722011-12-22 11:30:02 -0700132 if (tasks == NULL) {
133 pr_debug("couldn't open %s\n", filename);
134 return 0;
135 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200136
137 while (!readdir_r(tasks, &dirent, &next) && next) {
138 char *end;
139 pid = strtol(dirent.d_name, &end, 10);
140 if (*end)
141 continue;
142
David Ahernf5faf722011-12-22 11:30:02 -0700143 /* already have tgid; jut want to update the comm */
144 (void) perf_event__get_comm_tgid(pid, event->comm.comm,
145 sizeof(event->comm.comm));
146
147 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300148 size = PERF_ALIGN(size, sizeof(u64));
David Ahernf5faf722011-12-22 11:30:02 -0700149 memset(event->comm.comm + size, 0, machine->id_hdr_size);
150 event->comm.header.size = (sizeof(event->comm) -
151 (sizeof(event->comm.comm) - size) +
152 machine->id_hdr_size);
153
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200154 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200155
David Ahern1e6d5322012-08-26 12:24:42 -0600156 if (process(tool, event, &synth_sample, machine) != 0) {
157 tgid = -1;
158 break;
159 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200160 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200161
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200162 closedir(tasks);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200163out:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200164 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200165}
166
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200167static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200168 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200169 pid_t pid, pid_t tgid,
170 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200171 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200172{
173 char filename[PATH_MAX];
174 FILE *fp;
David Ahern1e6d5322012-08-26 12:24:42 -0600175 int rc = 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200176
177 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
178
179 fp = fopen(filename, "r");
180 if (fp == NULL) {
181 /*
182 * We raced with a task exiting - just return:
183 */
184 pr_debug("couldn't open %s\n", filename);
185 return -1;
186 }
187
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200188 event->header.type = PERF_RECORD_MMAP;
189 /*
190 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
191 */
192 event->header.misc = PERF_RECORD_MISC_USER;
193
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200194 while (1) {
195 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200196 int n;
197 size_t size;
198 if (fgets(bf, sizeof(bf), fp) == NULL)
199 break;
200
201 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200202 n = hex2u64(pbf, &event->mmap.start);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200203 if (n < 0)
204 continue;
205 pbf += n + 1;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200206 n = hex2u64(pbf, &event->mmap.len);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200207 if (n < 0)
208 continue;
209 pbf += n + 3;
210 if (*pbf == 'x') { /* vm_exec */
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000211 char anonstr[] = "//anon\n";
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200212 char *execname = strchr(bf, '/');
213
214 /* Catch VDSO */
215 if (execname == NULL)
216 execname = strstr(bf, "[vdso]");
217
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000218 /* Catch anonymous mmaps */
219 if ((execname == NULL) && !strstr(bf, "["))
220 execname = anonstr;
221
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200222 if (execname == NULL)
223 continue;
224
Anton Blanchard4af8b352010-04-03 22:53:31 +1100225 pbf += 3;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200226 n = hex2u64(pbf, &event->mmap.pgoff);
Anton Blanchard4af8b352010-04-03 22:53:31 +1100227
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200228 size = strlen(execname);
229 execname[size - 1] = '\0'; /* Remove \n */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200230 memcpy(event->mmap.filename, execname, size);
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300231 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200232 event->mmap.len -= event->mmap.start;
233 event->mmap.header.size = (sizeof(event->mmap) -
234 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200235 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
236 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200237 event->mmap.pid = tgid;
238 event->mmap.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200239
David Ahern1e6d5322012-08-26 12:24:42 -0600240 if (process(tool, event, &synth_sample, machine) != 0) {
241 rc = -1;
242 break;
243 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200244 }
245 }
246
247 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600248 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200249}
250
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200251int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200252 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200253 struct machine *machine)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200254{
David Ahern1e6d5322012-08-26 12:24:42 -0600255 int rc = 0;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200256 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300257 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200258 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200259 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200260 if (event == NULL) {
261 pr_debug("Not enough memory synthesizing mmap event "
262 "for kernel modules\n");
263 return -1;
264 }
265
266 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200267
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800268 /*
269 * kernel uses 0 for user space maps, see kernel/perf_event.c
270 * __perf_event_mmap
271 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300272 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200273 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800274 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200275 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800276
277 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200278 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200279 size_t size;
280 struct map *pos = rb_entry(nd, struct map, rb_node);
281
282 if (pos->dso->kernel)
283 continue;
284
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300285 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200286 event->mmap.header.type = PERF_RECORD_MMAP;
287 event->mmap.header.size = (sizeof(event->mmap) -
288 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200289 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
290 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200291 event->mmap.start = pos->start;
292 event->mmap.len = pos->end - pos->start;
293 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200294
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200295 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200296 pos->dso->long_name_len + 1);
David Ahern1e6d5322012-08-26 12:24:42 -0600297 if (process(tool, event, &synth_sample, machine) != 0) {
298 rc = -1;
299 break;
300 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200301 }
302
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200303 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600304 return rc;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200305}
306
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200307static int __event__synthesize_thread(union perf_event *comm_event,
308 union perf_event *mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700309 pid_t pid, int full,
310 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200311 struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200312 struct machine *machine)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200313{
David Aherndefd8d32011-12-22 11:30:01 -0700314 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200315 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200316 if (tgid == -1)
317 return -1;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200318 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200319 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200320}
321
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200322int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200323 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200324 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200325 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200326{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200327 union perf_event *comm_event, *mmap_event;
David Aherndefd8d32011-12-22 11:30:01 -0700328 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200329
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200330 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200331 if (comm_event == NULL)
332 goto out;
333
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200334 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200335 if (mmap_event == NULL)
336 goto out_free_comm;
337
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200338 err = 0;
339 for (thread = 0; thread < threads->nr; ++thread) {
340 if (__event__synthesize_thread(comm_event, mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700341 threads->map[thread], 0,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200342 process, tool, machine)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200343 err = -1;
344 break;
345 }
David Aherndefd8d32011-12-22 11:30:01 -0700346
347 /*
348 * comm.pid is set to thread group id by
349 * perf_event__synthesize_comm
350 */
351 if ((int) comm_event->comm.pid != threads->map[thread]) {
352 bool need_leader = true;
353
354 /* is thread group leader in thread_map? */
355 for (j = 0; j < threads->nr; ++j) {
356 if ((int) comm_event->comm.pid == threads->map[j]) {
357 need_leader = false;
358 break;
359 }
360 }
361
362 /* if not, generate events for it */
363 if (need_leader &&
364 __event__synthesize_thread(comm_event,
365 mmap_event,
366 comm_event->comm.pid, 0,
367 process, tool, machine)) {
368 err = -1;
369 break;
370 }
371 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200372 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200373 free(mmap_event);
374out_free_comm:
375 free(comm_event);
376out:
377 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200378}
379
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200380int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200381 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200382 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200383{
384 DIR *proc;
385 struct dirent dirent, *next;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200386 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200387 int err = -1;
388
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200389 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200390 if (comm_event == NULL)
391 goto out;
392
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200393 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200394 if (mmap_event == NULL)
395 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200396
397 proc = opendir("/proc");
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200398 if (proc == NULL)
399 goto out_free_mmap;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200400
401 while (!readdir_r(proc, &dirent, &next) && next) {
402 char *end;
403 pid_t pid = strtol(dirent.d_name, &end, 10);
404
405 if (*end) /* only interested in proper numerical dirents */
406 continue;
407
David Ahern1e6d5322012-08-26 12:24:42 -0600408 if (__event__synthesize_thread(comm_event, mmap_event, pid, 1,
409 process, tool, machine) != 0) {
410 err = -1;
411 goto out_closedir;
412 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200413 }
414
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200415 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600416out_closedir:
417 closedir(proc);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200418out_free_mmap:
419 free(mmap_event);
420out_free_comm:
421 free(comm_event);
422out:
423 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200424}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200425
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200426struct process_symbol_args {
427 const char *name;
428 u64 start;
429};
430
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200431static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700432 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200433{
434 struct process_symbol_args *args = arg;
435
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200436 /*
437 * Must be a function or at least an alias, as in PARISC64, where "_text" is
438 * an 'A' to the same address as "_stext".
439 */
440 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
441 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200442 return 0;
443
444 args->start = start;
445 return 1;
446}
447
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200448int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200449 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200450 struct machine *machine,
451 const char *symbol_name)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200452{
453 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800454 const char *filename, *mmap_name;
455 char path[PATH_MAX];
456 char name_buff[PATH_MAX];
457 struct map *map;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200458 int err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200459 /*
460 * We should get this from /sys/kernel/sections/.text, but till that is
461 * available use this, and after it is use this as a fallback for older
462 * kernels.
463 */
464 struct process_symbol_args args = { .name = symbol_name, };
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200465 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200466 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200467 if (event == NULL) {
468 pr_debug("Not enough memory synthesizing mmap event "
469 "for kernel modules\n");
470 return -1;
471 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200472
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300473 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300474 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800475 /*
476 * kernel uses PERF_RECORD_MISC_USER for user space maps,
477 * see kernel/perf_event.c __perf_event_mmap
478 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200479 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800480 filename = "/proc/kallsyms";
481 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200482 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300483 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800484 filename = (char *) symbol_conf.default_guest_kallsyms;
485 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300486 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800487 filename = path;
488 }
489 }
490
491 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200492 return -ENOENT;
493
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300494 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200495 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800496 "%s%s", mmap_name, symbol_name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300497 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200498 event->mmap.header.type = PERF_RECORD_MMAP;
499 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200500 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200501 event->mmap.pgoff = args.start;
502 event->mmap.start = map->start;
503 event->mmap.len = map->end - event->mmap.start;
504 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200505
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200506 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200507 free(event);
508
509 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200510}
511
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200512size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
513{
514 return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
515}
516
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300517int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200518 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300519 struct perf_sample *sample __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200520 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200521{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200522 struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200523
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200524 if (dump_trace)
525 perf_event__fprintf_comm(event, stdout);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200526
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -0300527 if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200528 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
529 return -1;
530 }
531
532 return 0;
533}
534
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300535int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200536 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300537 struct perf_sample *sample __maybe_unused,
538 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200539{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200540 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200541 event->lost.id, event->lost.lost);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200542 return 0;
543}
544
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200545static void perf_event__set_kernel_mmap_len(union perf_event *event,
546 struct map **maps)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200547{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200548 maps[MAP__FUNCTION]->start = event->mmap.start;
549 maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800550 /*
551 * Be a bit paranoid here, some perf.data file came with
552 * a zero sized synthesized MMAP event for the kernel.
553 */
554 if (maps[MAP__FUNCTION]->end == 0)
Ian Munsie9d1faba2010-11-25 15:12:53 +1100555 maps[MAP__FUNCTION]->end = ~0ULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800556}
557
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300558static int perf_event__process_kernel_mmap(struct perf_tool *tool
559 __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200560 union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200561 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800562{
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200563 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800564 char kmmap_prefix[PATH_MAX];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800565 enum dso_kernel_type kernel_type;
566 bool is_kernel_mmap;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200567
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300568 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300569 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800570 kernel_type = DSO_TYPE_KERNEL;
571 else
572 kernel_type = DSO_TYPE_GUEST_KERNEL;
573
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200574 is_kernel_mmap = memcmp(event->mmap.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800575 kmmap_prefix,
Jiri Olsabf32c9e2012-02-05 18:11:05 +0100576 strlen(kmmap_prefix) - 1) == 0;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200577 if (event->mmap.filename[0] == '/' ||
578 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800579
580 char short_module_name[1024];
581 char *name, *dot;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200582
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200583 if (event->mmap.filename[0] == '/') {
584 name = strrchr(event->mmap.filename, '/');
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200585 if (name == NULL)
586 goto out_problem;
587
588 ++name; /* skip / */
589 dot = strrchr(name, '.');
590 if (dot == NULL)
591 goto out_problem;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200592 snprintf(short_module_name, sizeof(short_module_name),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800593 "[%.*s]", (int)(dot - name), name);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200594 strxfrchar(short_module_name, '-', '_');
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800595 } else
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200596 strcpy(short_module_name, event->mmap.filename);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200597
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200598 map = machine__new_module(machine, event->mmap.start,
599 event->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800600 if (map == NULL)
601 goto out_problem;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200602
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800603 name = strdup(short_module_name);
604 if (name == NULL)
605 goto out_problem;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200606
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800607 map->dso->short_name = name;
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -0300608 map->dso->sname_alloc = 1;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200609 map->end = map->start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800610 } else if (is_kernel_mmap) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200611 const char *symbol_name = (event->mmap.filename +
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800612 strlen(kmmap_prefix));
613 /*
614 * Should be there already, from the build-id table in
615 * the header.
616 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300617 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
618 kmmap_prefix);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800619 if (kernel == NULL)
620 goto out_problem;
621
622 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300623 if (__machine__create_kernel_maps(machine, kernel) < 0)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800624 goto out_problem;
625
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200626 perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300627
628 /*
629 * Avoid using a zero address (kptr_restrict) for the ref reloc
630 * symbol. Effectively having zero here means that at record
631 * time /proc/sys/kernel/kptr_restrict was non zero.
632 */
633 if (event->mmap.pgoff != 0) {
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200634 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
635 symbol_name,
636 event->mmap.pgoff);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300637 }
638
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300639 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200640 /*
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800641 * preload dso of guest kernel and modules
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200642 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300643 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
644 NULL);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200645 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800646 }
647 return 0;
648out_problem:
649 return -1;
650}
651
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200652size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
653{
654 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
655 event->mmap.pid, event->mmap.tid, event->mmap.start,
656 event->mmap.len, event->mmap.pgoff, event->mmap.filename);
657}
658
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200659int perf_event__process_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200660 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300661 struct perf_sample *sample __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200662 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800663{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800664 struct thread *thread;
665 struct map *map;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200666 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800667 int ret = 0;
668
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200669 if (dump_trace)
670 perf_event__fprintf_mmap(event, stdout);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800671
672 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
673 cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200674 ret = perf_event__process_kernel_mmap(tool, event, machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800675 if (ret < 0)
676 goto out_problem;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200677 return 0;
678 }
679
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200680 thread = machine__findnew_thread(machine, event->mmap.pid);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300681 if (thread == NULL)
682 goto out_problem;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200683 map = map__new(&machine->user_dsos, event->mmap.start,
684 event->mmap.len, event->mmap.pgoff,
685 event->mmap.pid, event->mmap.filename,
Dave Martin361d1342010-07-27 16:40:02 +0100686 MAP__FUNCTION);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300687 if (map == NULL)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200688 goto out_problem;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200689
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200690 thread__insert_map(thread, map);
691 return 0;
692
693out_problem:
694 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200695 return 0;
696}
697
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200698size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
699{
700 return fprintf(fp, "(%d:%d):(%d:%d)\n",
701 event->fork.pid, event->fork.tid,
702 event->fork.ppid, event->fork.ptid);
703}
704
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300705int perf_event__process_task(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200706 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300707 struct perf_sample *sample __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200708 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200709{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200710 struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
711 struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200712
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200713 if (dump_trace)
714 perf_event__fprintf_task(event, stdout);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200715
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200716 if (event->header.type == PERF_RECORD_EXIT) {
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200717 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200718 return 0;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300719 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200720
721 if (thread == NULL || parent == NULL ||
722 thread__fork(thread, parent) < 0) {
723 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
724 return -1;
725 }
726
727 return 0;
728}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200729
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200730size_t perf_event__fprintf(union perf_event *event, FILE *fp)
731{
732 size_t ret = fprintf(fp, "PERF_RECORD_%s",
733 perf_event__name(event->header.type));
734
735 switch (event->header.type) {
736 case PERF_RECORD_COMM:
737 ret += perf_event__fprintf_comm(event, fp);
738 break;
739 case PERF_RECORD_FORK:
740 case PERF_RECORD_EXIT:
741 ret += perf_event__fprintf_task(event, fp);
742 break;
743 case PERF_RECORD_MMAP:
744 ret += perf_event__fprintf_mmap(event, fp);
745 break;
746 default:
747 ret += fprintf(fp, "\n");
748 }
749
750 return ret;
751}
752
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200753int perf_event__process(struct perf_tool *tool, union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200754 struct perf_sample *sample, struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530755{
756 switch (event->header.type) {
757 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200758 perf_event__process_comm(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530759 break;
760 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200761 perf_event__process_mmap(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530762 break;
763 case PERF_RECORD_FORK:
764 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200765 perf_event__process_task(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530766 break;
Arnaldo Carvalho de Meloef2bf6d2011-01-29 09:04:40 -0200767 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200768 perf_event__process_lost(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530769 default:
770 break;
771 }
772
773 return 0;
774}
775
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200776void thread__find_addr_map(struct thread *self,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200777 struct machine *machine, u8 cpumode,
778 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200779 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200780{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200781 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200782
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200783 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200784 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800785 al->cpumode = cpumode;
786 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200787
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200788 if (machine == NULL) {
789 al->map = NULL;
790 return;
791 }
792
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800793 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200794 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300795 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800796 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200797 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800798 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
799 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300800 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800801 } else {
802 /*
803 * 'u' means guest os user space.
804 * TODO: We don't support guest user space. Might support late.
805 */
806 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
807 al->level = 'u';
808 else
809 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200810 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800811
812 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
813 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
814 !perf_guest)
815 al->filtered = true;
816 if ((cpumode == PERF_RECORD_MISC_USER ||
817 cpumode == PERF_RECORD_MISC_KERNEL) &&
818 !perf_host)
819 al->filtered = true;
820
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200821 return;
822 }
823try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200824 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200825 if (al->map == NULL) {
826 /*
827 * If this is outside of all known maps, and is a negative
828 * address, try to look it up in the kernel dso, as it might be
829 * a vsyscall or vdso (which executes in user-mode).
830 *
831 * XXX This is nasty, we should have a symbol list in the
832 * "[vdso]" dso, but for now lets use the old trick of looking
833 * in the whole kernel symbol list.
834 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800835 if ((long long)al->addr < 0 &&
Andrew Lutomirski6c6804fb2011-03-24 00:36:56 -0400836 cpumode == PERF_RECORD_MISC_USER &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300837 machine && mg != &machine->kmaps) {
838 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200839 goto try_again;
840 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200841 } else
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200842 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200843}
844
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200845void thread__find_addr_location(struct thread *thread, struct machine *machine,
846 u8 cpumode, enum map_type type, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200847 struct addr_location *al,
848 symbol_filter_t filter)
849{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200850 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200851 if (al->map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200852 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200853 else
854 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200855}
856
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200857int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200858 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200859 struct addr_location *al,
860 struct perf_sample *sample,
861 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200862{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200863 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200864 struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200865
866 if (thread == NULL)
867 return -1;
868
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200869 if (symbol_conf.comm_list &&
870 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
871 goto out_filtered;
872
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200873 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300874 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200875 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300876 *
877 * This should have happened earlier, when we processed the kernel MMAP
878 * events, but for older perf.data files there was no such thing, so do
879 * it now.
880 */
881 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200882 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
883 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200884
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200885 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
886 event->ip.ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200887 dump_printf(" ...... dso: %s\n",
888 al->map ? al->map->dso->long_name :
889 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300890 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200891 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200892
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300893 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900894 struct dso *dso = al->map->dso;
895
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300896 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900897 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
898 dso->short_name) ||
899 (dso->short_name != dso->long_name &&
900 strlist__has_entry(symbol_conf.dso_list,
901 dso->long_name)))))
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300902 goto out_filtered;
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300903
904 al->sym = map__find_symbol(al->map, al->addr, filter);
905 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200906
907 if (symbol_conf.sym_list && al->sym &&
908 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
909 goto out_filtered;
910
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200911 return 0;
912
913out_filtered:
914 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200915 return 0;
916}