]>
Commit | Line | Data |
---|---|---|
234fbbf5 | 1 | #include <linux/types.h> |
7ef80703 | 2 | #include <sys/mman.h> |
234fbbf5 ACM |
3 | #include "event.h" |
4 | #include "debug.h" | |
b3cef7f6 | 5 | #include "hist.h" |
f62d3f0f | 6 | #include "machine.h" |
c410a338 | 7 | #include "sort.h" |
234fbbf5 | 8 | #include "string.h" |
c410a338 | 9 | #include "strlist.h" |
62daacb5 | 10 | #include "thread.h" |
7c940c18 | 11 | #include "thread_map.h" |
c506c96b | 12 | #include "symbol/kallsyms.h" |
234fbbf5 | 13 | |
8115d60c | 14 | static const char *perf_event__names[] = { |
3cb6d154 FW |
15 | [0] = "TOTAL", |
16 | [PERF_RECORD_MMAP] = "MMAP", | |
5c5e854b | 17 | [PERF_RECORD_MMAP2] = "MMAP2", |
3cb6d154 FW |
18 | [PERF_RECORD_LOST] = "LOST", |
19 | [PERF_RECORD_COMM] = "COMM", | |
20 | [PERF_RECORD_EXIT] = "EXIT", | |
21 | [PERF_RECORD_THROTTLE] = "THROTTLE", | |
22 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", | |
23 | [PERF_RECORD_FORK] = "FORK", | |
24 | [PERF_RECORD_READ] = "READ", | |
25 | [PERF_RECORD_SAMPLE] = "SAMPLE", | |
26 | [PERF_RECORD_HEADER_ATTR] = "ATTR", | |
27 | [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", | |
28 | [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", | |
29 | [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", | |
30 | [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND", | |
3c659eed | 31 | [PERF_RECORD_ID_INDEX] = "ID_INDEX", |
c8446b9b ACM |
32 | }; |
33 | ||
8115d60c | 34 | const char *perf_event__name(unsigned int id) |
3835bc00 | 35 | { |
8115d60c | 36 | if (id >= ARRAY_SIZE(perf_event__names)) |
3835bc00 | 37 | return "INVALID"; |
8115d60c | 38 | if (!perf_event__names[id]) |
3835bc00 | 39 | return "UNKNOWN"; |
8115d60c | 40 | return perf_event__names[id]; |
3835bc00 TG |
41 | } |
42 | ||
8d50e5b4 | 43 | static struct perf_sample synth_sample = { |
640c03ce ACM |
44 | .pid = -1, |
45 | .tid = -1, | |
46 | .time = -1, | |
47 | .stream_id = -1, | |
48 | .cpu = -1, | |
49 | .period = 1, | |
50 | }; | |
51 | ||
f5faf726 | 52 | static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len) |
234fbbf5 | 53 | { |
234fbbf5 ACM |
54 | char filename[PATH_MAX]; |
55 | char bf[BUFSIZ]; | |
56 | FILE *fp; | |
57 | size_t size = 0; | |
f5faf726 | 58 | pid_t tgid = -1; |
234fbbf5 ACM |
59 | |
60 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); | |
61 | ||
62 | fp = fopen(filename, "r"); | |
63 | if (fp == NULL) { | |
234fbbf5 ACM |
64 | pr_debug("couldn't open %s\n", filename); |
65 | return 0; | |
66 | } | |
67 | ||
f5faf726 | 68 | while (!comm[0] || (tgid < 0)) { |
9c90a61c | 69 | if (fgets(bf, sizeof(bf), fp) == NULL) { |
f5faf726 DA |
70 | pr_warning("couldn't get COMM and pgid, malformed %s\n", |
71 | filename); | |
72 | break; | |
9c90a61c | 73 | } |
234fbbf5 ACM |
74 | |
75 | if (memcmp(bf, "Name:", 5) == 0) { | |
76 | char *name = bf + 5; | |
77 | while (*name && isspace(*name)) | |
78 | ++name; | |
79 | size = strlen(name) - 1; | |
f5faf726 DA |
80 | if (size >= len) |
81 | size = len - 1; | |
82 | memcpy(comm, name, size); | |
cfbd70c1 | 83 | comm[size] = '\0'; |
f5faf726 | 84 | |
234fbbf5 ACM |
85 | } else if (memcmp(bf, "Tgid:", 5) == 0) { |
86 | char *tgids = bf + 5; | |
87 | while (*tgids && isspace(*tgids)) | |
88 | ++tgids; | |
f5faf726 | 89 | tgid = atoi(tgids); |
234fbbf5 ACM |
90 | } |
91 | } | |
92 | ||
f5faf726 DA |
93 | fclose(fp); |
94 | ||
95 | return tgid; | |
96 | } | |
97 | ||
98 | static pid_t perf_event__synthesize_comm(struct perf_tool *tool, | |
99 | union perf_event *event, pid_t pid, | |
f5faf726 DA |
100 | perf_event__handler_t process, |
101 | struct machine *machine) | |
102 | { | |
f5faf726 | 103 | size_t size; |
f5faf726 DA |
104 | pid_t tgid; |
105 | ||
106 | memset(&event->comm, 0, sizeof(event->comm)); | |
107 | ||
f5db57c4 DY |
108 | if (machine__is_host(machine)) |
109 | tgid = perf_event__get_comm_tgid(pid, event->comm.comm, | |
110 | sizeof(event->comm.comm)); | |
111 | else | |
112 | tgid = machine->pid; | |
113 | ||
f5faf726 DA |
114 | if (tgid < 0) |
115 | goto out; | |
116 | ||
117 | event->comm.pid = tgid; | |
9c90a61c | 118 | event->comm.header.type = PERF_RECORD_COMM; |
f5faf726 DA |
119 | |
120 | size = strlen(event->comm.comm) + 1; | |
9ac3e487 | 121 | size = PERF_ALIGN(size, sizeof(u64)); |
743eb868 | 122 | memset(event->comm.comm + size, 0, machine->id_hdr_size); |
9c90a61c ACM |
123 | event->comm.header.size = (sizeof(event->comm) - |
124 | (sizeof(event->comm.comm) - size) + | |
743eb868 | 125 | machine->id_hdr_size); |
bfd66cc7 | 126 | event->comm.tid = pid; |
f5faf726 | 127 | |
bfd66cc7 DZ |
128 | if (process(tool, event, &synth_sample, machine) != 0) |
129 | return -1; | |
234fbbf5 | 130 | |
9c90a61c | 131 | out: |
9c90a61c | 132 | return tgid; |
234fbbf5 ACM |
133 | } |
134 | ||
363b785f DZ |
135 | static int perf_event__synthesize_fork(struct perf_tool *tool, |
136 | union perf_event *event, pid_t pid, | |
137 | pid_t tgid, perf_event__handler_t process, | |
138 | struct machine *machine) | |
139 | { | |
140 | memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size); | |
141 | ||
142 | /* this is really a clone event but we use fork to synthesize it */ | |
143 | event->fork.ppid = tgid; | |
144 | event->fork.ptid = tgid; | |
145 | event->fork.pid = tgid; | |
146 | event->fork.tid = pid; | |
147 | event->fork.header.type = PERF_RECORD_FORK; | |
148 | ||
149 | event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size); | |
150 | ||
151 | if (process(tool, event, &synth_sample, machine) != 0) | |
152 | return -1; | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
a18382b6 JO |
157 | int perf_event__synthesize_mmap_events(struct perf_tool *tool, |
158 | union perf_event *event, | |
159 | pid_t pid, pid_t tgid, | |
160 | perf_event__handler_t process, | |
161 | struct machine *machine, | |
162 | bool mmap_data) | |
234fbbf5 ACM |
163 | { |
164 | char filename[PATH_MAX]; | |
165 | FILE *fp; | |
1e6d5322 | 166 | int rc = 0; |
234fbbf5 | 167 | |
c239c25a DY |
168 | if (machine__is_default_guest(machine)) |
169 | return 0; | |
170 | ||
99563465 DY |
171 | snprintf(filename, sizeof(filename), "%s/proc/%d/maps", |
172 | machine->root_dir, pid); | |
234fbbf5 ACM |
173 | |
174 | fp = fopen(filename, "r"); | |
175 | if (fp == NULL) { | |
176 | /* | |
177 | * We raced with a task exiting - just return: | |
178 | */ | |
179 | pr_debug("couldn't open %s\n", filename); | |
180 | return -1; | |
181 | } | |
182 | ||
a5a5ba72 | 183 | event->header.type = PERF_RECORD_MMAP2; |
9c90a61c | 184 | |
234fbbf5 | 185 | while (1) { |
60648033 NK |
186 | char bf[BUFSIZ]; |
187 | char prot[5]; | |
188 | char execname[PATH_MAX]; | |
189 | char anonstr[] = "//anon"; | |
a5a5ba72 | 190 | unsigned int ino; |
234fbbf5 | 191 | size_t size; |
5c5e854b | 192 | ssize_t n; |
60648033 | 193 | |
234fbbf5 ACM |
194 | if (fgets(bf, sizeof(bf), fp) == NULL) |
195 | break; | |
196 | ||
60648033 NK |
197 | /* ensure null termination since stack will be reused. */ |
198 | strcpy(execname, ""); | |
199 | ||
234fbbf5 | 200 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ |
a5a5ba72 DZ |
201 | n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n", |
202 | &event->mmap2.start, &event->mmap2.len, prot, | |
203 | &event->mmap2.pgoff, &event->mmap2.maj, | |
204 | &event->mmap2.min, | |
205 | &ino, execname); | |
206 | ||
9d4ecc88 DZ |
207 | /* |
208 | * Anon maps don't have the execname. | |
209 | */ | |
a5a5ba72 | 210 | if (n < 7) |
5c5e854b | 211 | continue; |
a5a5ba72 DZ |
212 | |
213 | event->mmap2.ino = (u64)ino; | |
214 | ||
62605dc5 ACM |
215 | /* |
216 | * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c | |
217 | */ | |
73547aac DY |
218 | if (machine__is_host(machine)) |
219 | event->header.misc = PERF_RECORD_MISC_USER; | |
220 | else | |
221 | event->header.misc = PERF_RECORD_MISC_GUEST_USER; | |
60648033 | 222 | |
7ef80703 DZ |
223 | /* map protection and flags bits */ |
224 | event->mmap2.prot = 0; | |
225 | event->mmap2.flags = 0; | |
226 | if (prot[0] == 'r') | |
227 | event->mmap2.prot |= PROT_READ; | |
228 | if (prot[1] == 'w') | |
229 | event->mmap2.prot |= PROT_WRITE; | |
230 | if (prot[2] == 'x') | |
231 | event->mmap2.prot |= PROT_EXEC; | |
232 | ||
233 | if (prot[3] == 's') | |
234 | event->mmap2.flags |= MAP_SHARED; | |
235 | else | |
236 | event->mmap2.flags |= MAP_PRIVATE; | |
237 | ||
62605dc5 ACM |
238 | if (prot[2] != 'x') { |
239 | if (!mmap_data || prot[0] != 'r') | |
240 | continue; | |
241 | ||
242 | event->header.misc |= PERF_RECORD_MISC_MMAP_DATA; | |
243 | } | |
60648033 NK |
244 | |
245 | if (!strcmp(execname, "")) | |
246 | strcpy(execname, anonstr); | |
247 | ||
248 | size = strlen(execname) + 1; | |
a5a5ba72 | 249 | memcpy(event->mmap2.filename, execname, size); |
60648033 | 250 | size = PERF_ALIGN(size, sizeof(u64)); |
a5a5ba72 DZ |
251 | event->mmap2.len -= event->mmap.start; |
252 | event->mmap2.header.size = (sizeof(event->mmap2) - | |
253 | (sizeof(event->mmap2.filename) - size)); | |
254 | memset(event->mmap2.filename + size, 0, machine->id_hdr_size); | |
255 | event->mmap2.header.size += machine->id_hdr_size; | |
256 | event->mmap2.pid = tgid; | |
257 | event->mmap2.tid = pid; | |
60648033 NK |
258 | |
259 | if (process(tool, event, &synth_sample, machine) != 0) { | |
260 | rc = -1; | |
261 | break; | |
234fbbf5 ACM |
262 | } |
263 | } | |
264 | ||
265 | fclose(fp); | |
1e6d5322 | 266 | return rc; |
234fbbf5 ACM |
267 | } |
268 | ||
45694aa7 | 269 | int perf_event__synthesize_modules(struct perf_tool *tool, |
d20deb64 | 270 | perf_event__handler_t process, |
8115d60c | 271 | struct machine *machine) |
b7cece76 | 272 | { |
1e6d5322 | 273 | int rc = 0; |
b7cece76 | 274 | struct rb_node *nd; |
23346f21 | 275 | struct map_groups *kmaps = &machine->kmaps; |
8115d60c | 276 | union perf_event *event = zalloc((sizeof(event->mmap) + |
743eb868 | 277 | machine->id_hdr_size)); |
9c90a61c ACM |
278 | if (event == NULL) { |
279 | pr_debug("Not enough memory synthesizing mmap event " | |
280 | "for kernel modules\n"); | |
281 | return -1; | |
282 | } | |
283 | ||
284 | event->header.type = PERF_RECORD_MMAP; | |
b7cece76 | 285 | |
a1645ce1 ZY |
286 | /* |
287 | * kernel uses 0 for user space maps, see kernel/perf_event.c | |
288 | * __perf_event_mmap | |
289 | */ | |
23346f21 | 290 | if (machine__is_host(machine)) |
9c90a61c | 291 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
a1645ce1 | 292 | else |
9c90a61c | 293 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
a1645ce1 ZY |
294 | |
295 | for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]); | |
b7cece76 | 296 | nd; nd = rb_next(nd)) { |
b7cece76 ACM |
297 | size_t size; |
298 | struct map *pos = rb_entry(nd, struct map, rb_node); | |
299 | ||
300 | if (pos->dso->kernel) | |
301 | continue; | |
302 | ||
9ac3e487 | 303 | size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64)); |
9c90a61c ACM |
304 | event->mmap.header.type = PERF_RECORD_MMAP; |
305 | event->mmap.header.size = (sizeof(event->mmap) - | |
306 | (sizeof(event->mmap.filename) - size)); | |
743eb868 ACM |
307 | memset(event->mmap.filename + size, 0, machine->id_hdr_size); |
308 | event->mmap.header.size += machine->id_hdr_size; | |
9c90a61c ACM |
309 | event->mmap.start = pos->start; |
310 | event->mmap.len = pos->end - pos->start; | |
311 | event->mmap.pid = machine->pid; | |
312 | ||
313 | memcpy(event->mmap.filename, pos->dso->long_name, | |
b7cece76 | 314 | pos->dso->long_name_len + 1); |
1e6d5322 DA |
315 | if (process(tool, event, &synth_sample, machine) != 0) { |
316 | rc = -1; | |
317 | break; | |
318 | } | |
b7cece76 ACM |
319 | } |
320 | ||
9c90a61c | 321 | free(event); |
1e6d5322 | 322 | return rc; |
b7cece76 ACM |
323 | } |
324 | ||
8115d60c ACM |
325 | static int __event__synthesize_thread(union perf_event *comm_event, |
326 | union perf_event *mmap_event, | |
363b785f | 327 | union perf_event *fork_event, |
defd8d38 DA |
328 | pid_t pid, int full, |
329 | perf_event__handler_t process, | |
45694aa7 | 330 | struct perf_tool *tool, |
62605dc5 | 331 | struct machine *machine, bool mmap_data) |
234fbbf5 | 332 | { |
bfd66cc7 DZ |
333 | char filename[PATH_MAX]; |
334 | DIR *tasks; | |
335 | struct dirent dirent, *next; | |
336 | pid_t tgid; | |
337 | ||
338 | /* special case: only send one comm event using passed in pid */ | |
339 | if (!full) { | |
340 | tgid = perf_event__synthesize_comm(tool, comm_event, pid, | |
341 | process, machine); | |
342 | ||
343 | if (tgid == -1) | |
344 | return -1; | |
345 | ||
346 | return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, | |
347 | process, machine, mmap_data); | |
348 | } | |
349 | ||
350 | if (machine__is_default_guest(machine)) | |
351 | return 0; | |
352 | ||
353 | snprintf(filename, sizeof(filename), "%s/proc/%d/task", | |
354 | machine->root_dir, pid); | |
355 | ||
356 | tasks = opendir(filename); | |
357 | if (tasks == NULL) { | |
358 | pr_debug("couldn't open %s\n", filename); | |
359 | return 0; | |
360 | } | |
361 | ||
362 | while (!readdir_r(tasks, &dirent, &next) && next) { | |
363 | char *end; | |
364 | int rc = 0; | |
365 | pid_t _pid; | |
366 | ||
367 | _pid = strtol(dirent.d_name, &end, 10); | |
368 | if (*end) | |
369 | continue; | |
370 | ||
371 | tgid = perf_event__synthesize_comm(tool, comm_event, _pid, | |
372 | process, machine); | |
373 | if (tgid == -1) | |
374 | return -1; | |
375 | ||
363b785f DZ |
376 | if (_pid == pid) { |
377 | /* process the parent's maps too */ | |
378 | rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, | |
379 | process, machine, mmap_data); | |
380 | } else { | |
381 | /* only fork the tid's map, to save time */ | |
382 | rc = perf_event__synthesize_fork(tool, fork_event, _pid, tgid, | |
383 | process, machine); | |
384 | } | |
bfd66cc7 DZ |
385 | |
386 | if (rc) | |
387 | return rc; | |
388 | } | |
389 | ||
390 | closedir(tasks); | |
391 | return 0; | |
234fbbf5 ACM |
392 | } |
393 | ||
45694aa7 | 394 | int perf_event__synthesize_thread_map(struct perf_tool *tool, |
d20deb64 | 395 | struct thread_map *threads, |
7c940c18 | 396 | perf_event__handler_t process, |
62605dc5 ACM |
397 | struct machine *machine, |
398 | bool mmap_data) | |
9c90a61c | 399 | { |
363b785f | 400 | union perf_event *comm_event, *mmap_event, *fork_event; |
defd8d38 | 401 | int err = -1, thread, j; |
9c90a61c | 402 | |
743eb868 | 403 | comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); |
9c90a61c ACM |
404 | if (comm_event == NULL) |
405 | goto out; | |
406 | ||
743eb868 | 407 | mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size); |
9c90a61c ACM |
408 | if (mmap_event == NULL) |
409 | goto out_free_comm; | |
410 | ||
363b785f DZ |
411 | fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); |
412 | if (fork_event == NULL) | |
413 | goto out_free_mmap; | |
414 | ||
401b8e13 ACM |
415 | err = 0; |
416 | for (thread = 0; thread < threads->nr; ++thread) { | |
417 | if (__event__synthesize_thread(comm_event, mmap_event, | |
363b785f | 418 | fork_event, |
defd8d38 | 419 | threads->map[thread], 0, |
62605dc5 ACM |
420 | process, tool, machine, |
421 | mmap_data)) { | |
401b8e13 ACM |
422 | err = -1; |
423 | break; | |
424 | } | |
defd8d38 DA |
425 | |
426 | /* | |
427 | * comm.pid is set to thread group id by | |
428 | * perf_event__synthesize_comm | |
429 | */ | |
430 | if ((int) comm_event->comm.pid != threads->map[thread]) { | |
431 | bool need_leader = true; | |
432 | ||
433 | /* is thread group leader in thread_map? */ | |
434 | for (j = 0; j < threads->nr; ++j) { | |
435 | if ((int) comm_event->comm.pid == threads->map[j]) { | |
436 | need_leader = false; | |
437 | break; | |
438 | } | |
439 | } | |
440 | ||
441 | /* if not, generate events for it */ | |
442 | if (need_leader && | |
62605dc5 | 443 | __event__synthesize_thread(comm_event, mmap_event, |
363b785f | 444 | fork_event, |
62605dc5 ACM |
445 | comm_event->comm.pid, 0, |
446 | process, tool, machine, | |
447 | mmap_data)) { | |
defd8d38 DA |
448 | err = -1; |
449 | break; | |
450 | } | |
451 | } | |
401b8e13 | 452 | } |
363b785f DZ |
453 | free(fork_event); |
454 | out_free_mmap: | |
9c90a61c ACM |
455 | free(mmap_event); |
456 | out_free_comm: | |
457 | free(comm_event); | |
458 | out: | |
459 | return err; | |
460 | } | |
461 | ||
45694aa7 | 462 | int perf_event__synthesize_threads(struct perf_tool *tool, |
d20deb64 | 463 | perf_event__handler_t process, |
62605dc5 | 464 | struct machine *machine, bool mmap_data) |
234fbbf5 ACM |
465 | { |
466 | DIR *proc; | |
99563465 | 467 | char proc_path[PATH_MAX]; |
234fbbf5 | 468 | struct dirent dirent, *next; |
363b785f | 469 | union perf_event *comm_event, *mmap_event, *fork_event; |
9c90a61c ACM |
470 | int err = -1; |
471 | ||
574799bf NK |
472 | if (machine__is_default_guest(machine)) |
473 | return 0; | |
474 | ||
743eb868 | 475 | comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); |
9c90a61c ACM |
476 | if (comm_event == NULL) |
477 | goto out; | |
478 | ||
743eb868 | 479 | mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size); |
9c90a61c ACM |
480 | if (mmap_event == NULL) |
481 | goto out_free_comm; | |
234fbbf5 | 482 | |
363b785f DZ |
483 | fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); |
484 | if (fork_event == NULL) | |
485 | goto out_free_mmap; | |
486 | ||
99563465 DY |
487 | snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir); |
488 | proc = opendir(proc_path); | |
489 | ||
9c90a61c | 490 | if (proc == NULL) |
363b785f | 491 | goto out_free_fork; |
234fbbf5 ACM |
492 | |
493 | while (!readdir_r(proc, &dirent, &next) && next) { | |
494 | char *end; | |
495 | pid_t pid = strtol(dirent.d_name, &end, 10); | |
496 | ||
497 | if (*end) /* only interested in proper numerical dirents */ | |
498 | continue; | |
ba361c92 ACM |
499 | /* |
500 | * We may race with exiting thread, so don't stop just because | |
501 | * one thread couldn't be synthesized. | |
502 | */ | |
363b785f DZ |
503 | __event__synthesize_thread(comm_event, mmap_event, fork_event, pid, |
504 | 1, process, tool, machine, mmap_data); | |
234fbbf5 ACM |
505 | } |
506 | ||
9c90a61c | 507 | err = 0; |
1e6d5322 | 508 | closedir(proc); |
363b785f DZ |
509 | out_free_fork: |
510 | free(fork_event); | |
9c90a61c ACM |
511 | out_free_mmap: |
512 | free(mmap_event); | |
513 | out_free_comm: | |
514 | free(comm_event); | |
515 | out: | |
516 | return err; | |
234fbbf5 | 517 | } |
62daacb5 | 518 | |
56b03f3c ACM |
519 | struct process_symbol_args { |
520 | const char *name; | |
521 | u64 start; | |
522 | }; | |
523 | ||
3b01a413 | 524 | static int find_symbol_cb(void *arg, const char *name, char type, |
82151520 | 525 | u64 start) |
56b03f3c ACM |
526 | { |
527 | struct process_symbol_args *args = arg; | |
528 | ||
881516eb ACM |
529 | /* |
530 | * Must be a function or at least an alias, as in PARISC64, where "_text" is | |
531 | * an 'A' to the same address as "_stext". | |
532 | */ | |
533 | if (!(symbol_type__is_a(type, MAP__FUNCTION) || | |
534 | type == 'A') || strcmp(name, args->name)) | |
56b03f3c ACM |
535 | return 0; |
536 | ||
537 | args->start = start; | |
538 | return 1; | |
539 | } | |
540 | ||
29b596b5 AH |
541 | u64 kallsyms__get_function_start(const char *kallsyms_filename, |
542 | const char *symbol_name) | |
543 | { | |
544 | struct process_symbol_args args = { .name = symbol_name, }; | |
545 | ||
546 | if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0) | |
547 | return 0; | |
548 | ||
549 | return args.start; | |
550 | } | |
551 | ||
45694aa7 | 552 | int perf_event__synthesize_kernel_mmap(struct perf_tool *tool, |
d20deb64 | 553 | perf_event__handler_t process, |
0ae617be | 554 | struct machine *machine) |
56b03f3c ACM |
555 | { |
556 | size_t size; | |
0ae617be | 557 | const char *mmap_name; |
a1645ce1 ZY |
558 | char name_buff[PATH_MAX]; |
559 | struct map *map; | |
0ae617be | 560 | struct kmap *kmap; |
9c90a61c | 561 | int err; |
a5c2a4c9 AK |
562 | union perf_event *event; |
563 | ||
564 | if (machine->vmlinux_maps[0] == NULL) | |
565 | return -1; | |
566 | ||
56b03f3c ACM |
567 | /* |
568 | * We should get this from /sys/kernel/sections/.text, but till that is | |
569 | * available use this, and after it is use this as a fallback for older | |
570 | * kernels. | |
571 | */ | |
a5c2a4c9 | 572 | event = zalloc((sizeof(event->mmap) + machine->id_hdr_size)); |
9c90a61c ACM |
573 | if (event == NULL) { |
574 | pr_debug("Not enough memory synthesizing mmap event " | |
575 | "for kernel modules\n"); | |
576 | return -1; | |
577 | } | |
56b03f3c | 578 | |
48ea8f54 | 579 | mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff)); |
23346f21 | 580 | if (machine__is_host(machine)) { |
a1645ce1 ZY |
581 | /* |
582 | * kernel uses PERF_RECORD_MISC_USER for user space maps, | |
583 | * see kernel/perf_event.c __perf_event_mmap | |
584 | */ | |
9c90a61c | 585 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
a1645ce1 | 586 | } else { |
9c90a61c | 587 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
0b9e01a4 | 588 | } |
56b03f3c | 589 | |
23346f21 | 590 | map = machine->vmlinux_maps[MAP__FUNCTION]; |
0ae617be | 591 | kmap = map__kmap(map); |
9c90a61c | 592 | size = snprintf(event->mmap.filename, sizeof(event->mmap.filename), |
0ae617be | 593 | "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1; |
9ac3e487 | 594 | size = PERF_ALIGN(size, sizeof(u64)); |
9c90a61c ACM |
595 | event->mmap.header.type = PERF_RECORD_MMAP; |
596 | event->mmap.header.size = (sizeof(event->mmap) - | |
743eb868 | 597 | (sizeof(event->mmap.filename) - size) + machine->id_hdr_size); |
0ae617be | 598 | event->mmap.pgoff = kmap->ref_reloc_sym->addr; |
9c90a61c ACM |
599 | event->mmap.start = map->start; |
600 | event->mmap.len = map->end - event->mmap.start; | |
601 | event->mmap.pid = machine->pid; | |
602 | ||
45694aa7 | 603 | err = process(tool, event, &synth_sample, machine); |
9c90a61c ACM |
604 | free(event); |
605 | ||
606 | return err; | |
56b03f3c ACM |
607 | } |
608 | ||
482ad897 ACM |
609 | size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp) |
610 | { | |
022c50d0 AH |
611 | const char *s; |
612 | ||
613 | if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC) | |
614 | s = " exec"; | |
615 | else | |
616 | s = ""; | |
617 | ||
50674065 | 618 | return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid); |
482ad897 ACM |
619 | } |
620 | ||
1d037ca1 | 621 | int perf_event__process_comm(struct perf_tool *tool __maybe_unused, |
d20deb64 | 622 | union perf_event *event, |
162f0bef | 623 | struct perf_sample *sample, |
743eb868 | 624 | struct machine *machine) |
62daacb5 | 625 | { |
162f0bef | 626 | return machine__process_comm_event(machine, event, sample); |
62daacb5 ACM |
627 | } |
628 | ||
1d037ca1 | 629 | int perf_event__process_lost(struct perf_tool *tool __maybe_unused, |
d20deb64 | 630 | union perf_event *event, |
162f0bef | 631 | struct perf_sample *sample, |
b0a7d1a0 | 632 | struct machine *machine) |
62daacb5 | 633 | { |
162f0bef | 634 | return machine__process_lost_event(machine, event, sample); |
a1645ce1 | 635 | } |
b7cece76 | 636 | |
482ad897 ACM |
637 | size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp) |
638 | { | |
62605dc5 | 639 | return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n", |
482ad897 | 640 | event->mmap.pid, event->mmap.tid, event->mmap.start, |
62605dc5 ACM |
641 | event->mmap.len, event->mmap.pgoff, |
642 | (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x', | |
643 | event->mmap.filename); | |
482ad897 ACM |
644 | } |
645 | ||
5c5e854b SE |
646 | size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp) |
647 | { | |
648 | return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 | |
7ef80703 | 649 | " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n", |
5c5e854b SE |
650 | event->mmap2.pid, event->mmap2.tid, event->mmap2.start, |
651 | event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj, | |
652 | event->mmap2.min, event->mmap2.ino, | |
653 | event->mmap2.ino_generation, | |
7ef80703 DZ |
654 | (event->mmap2.prot & PROT_READ) ? 'r' : '-', |
655 | (event->mmap2.prot & PROT_WRITE) ? 'w' : '-', | |
656 | (event->mmap2.prot & PROT_EXEC) ? 'x' : '-', | |
657 | (event->mmap2.flags & MAP_SHARED) ? 's' : 'p', | |
5c5e854b SE |
658 | event->mmap2.filename); |
659 | } | |
660 | ||
b0a7d1a0 | 661 | int perf_event__process_mmap(struct perf_tool *tool __maybe_unused, |
d20deb64 | 662 | union perf_event *event, |
162f0bef | 663 | struct perf_sample *sample, |
743eb868 | 664 | struct machine *machine) |
a1645ce1 | 665 | { |
162f0bef | 666 | return machine__process_mmap_event(machine, event, sample); |
62daacb5 ACM |
667 | } |
668 | ||
5c5e854b SE |
669 | int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused, |
670 | union perf_event *event, | |
162f0bef | 671 | struct perf_sample *sample, |
5c5e854b SE |
672 | struct machine *machine) |
673 | { | |
162f0bef | 674 | return machine__process_mmap2_event(machine, event, sample); |
5c5e854b SE |
675 | } |
676 | ||
482ad897 ACM |
677 | size_t perf_event__fprintf_task(union perf_event *event, FILE *fp) |
678 | { | |
679 | return fprintf(fp, "(%d:%d):(%d:%d)\n", | |
680 | event->fork.pid, event->fork.tid, | |
681 | event->fork.ppid, event->fork.ptid); | |
682 | } | |
683 | ||
f62d3f0f | 684 | int perf_event__process_fork(struct perf_tool *tool __maybe_unused, |
d20deb64 | 685 | union perf_event *event, |
162f0bef | 686 | struct perf_sample *sample, |
f62d3f0f | 687 | struct machine *machine) |
62daacb5 | 688 | { |
162f0bef | 689 | return machine__process_fork_event(machine, event, sample); |
62daacb5 | 690 | } |
1ed091c4 | 691 | |
f62d3f0f ACM |
692 | int perf_event__process_exit(struct perf_tool *tool __maybe_unused, |
693 | union perf_event *event, | |
162f0bef | 694 | struct perf_sample *sample, |
f62d3f0f ACM |
695 | struct machine *machine) |
696 | { | |
162f0bef | 697 | return machine__process_exit_event(machine, event, sample); |
f62d3f0f ACM |
698 | } |
699 | ||
482ad897 ACM |
700 | size_t perf_event__fprintf(union perf_event *event, FILE *fp) |
701 | { | |
702 | size_t ret = fprintf(fp, "PERF_RECORD_%s", | |
703 | perf_event__name(event->header.type)); | |
704 | ||
705 | switch (event->header.type) { | |
706 | case PERF_RECORD_COMM: | |
707 | ret += perf_event__fprintf_comm(event, fp); | |
708 | break; | |
709 | case PERF_RECORD_FORK: | |
710 | case PERF_RECORD_EXIT: | |
711 | ret += perf_event__fprintf_task(event, fp); | |
712 | break; | |
713 | case PERF_RECORD_MMAP: | |
714 | ret += perf_event__fprintf_mmap(event, fp); | |
715 | break; | |
5c5e854b SE |
716 | case PERF_RECORD_MMAP2: |
717 | ret += perf_event__fprintf_mmap2(event, fp); | |
718 | break; | |
482ad897 ACM |
719 | default: |
720 | ret += fprintf(fp, "\n"); | |
721 | } | |
722 | ||
723 | return ret; | |
724 | } | |
725 | ||
b0a7d1a0 ACM |
726 | int perf_event__process(struct perf_tool *tool __maybe_unused, |
727 | union perf_event *event, | |
162f0bef | 728 | struct perf_sample *sample, |
b0a7d1a0 | 729 | struct machine *machine) |
b83f920e | 730 | { |
162f0bef | 731 | return machine__process_event(machine, event, sample); |
b83f920e SD |
732 | } |
733 | ||
bb871a9c | 734 | void thread__find_addr_map(struct thread *thread, u8 cpumode, |
743eb868 | 735 | enum map_type type, u64 addr, |
326f59bf | 736 | struct addr_location *al) |
1ed091c4 | 737 | { |
93d5731d | 738 | struct map_groups *mg = thread->mg; |
bb871a9c | 739 | struct machine *machine = mg->machine; |
5b7ba82a | 740 | bool load_map = false; |
1ed091c4 | 741 | |
cc22e575 | 742 | al->machine = machine; |
316c7136 | 743 | al->thread = thread; |
1ed091c4 | 744 | al->addr = addr; |
a1645ce1 | 745 | al->cpumode = cpumode; |
b3cef7f6 | 746 | al->filtered = 0; |
1ed091c4 | 747 | |
743eb868 ACM |
748 | if (machine == NULL) { |
749 | al->map = NULL; | |
750 | return; | |
751 | } | |
752 | ||
a1645ce1 | 753 | if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { |
1ed091c4 | 754 | al->level = 'k'; |
23346f21 | 755 | mg = &machine->kmaps; |
5b7ba82a | 756 | load_map = true; |
a1645ce1 | 757 | } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { |
1ed091c4 | 758 | al->level = '.'; |
a1645ce1 ZY |
759 | } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { |
760 | al->level = 'g'; | |
23346f21 | 761 | mg = &machine->kmaps; |
5b7ba82a | 762 | load_map = true; |
fb50bb43 DY |
763 | } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) { |
764 | al->level = 'u'; | |
a1645ce1 | 765 | } else { |
fb50bb43 | 766 | al->level = 'H'; |
1ed091c4 | 767 | al->map = NULL; |
a1645ce1 ZY |
768 | |
769 | if ((cpumode == PERF_RECORD_MISC_GUEST_USER || | |
770 | cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && | |
771 | !perf_guest) | |
b3cef7f6 | 772 | al->filtered |= (1 << HIST_FILTER__GUEST); |
a1645ce1 ZY |
773 | if ((cpumode == PERF_RECORD_MISC_USER || |
774 | cpumode == PERF_RECORD_MISC_KERNEL) && | |
775 | !perf_host) | |
b3cef7f6 | 776 | al->filtered |= (1 << HIST_FILTER__HOST); |
a1645ce1 | 777 | |
1ed091c4 ACM |
778 | return; |
779 | } | |
780 | try_again: | |
9958e1f0 | 781 | al->map = map_groups__find(mg, type, al->addr); |
1ed091c4 ACM |
782 | if (al->map == NULL) { |
783 | /* | |
784 | * If this is outside of all known maps, and is a negative | |
785 | * address, try to look it up in the kernel dso, as it might be | |
786 | * a vsyscall or vdso (which executes in user-mode). | |
787 | * | |
788 | * XXX This is nasty, we should have a symbol list in the | |
789 | * "[vdso]" dso, but for now lets use the old trick of looking | |
790 | * in the whole kernel symbol list. | |
791 | */ | |
fbe2af45 AH |
792 | if (cpumode == PERF_RECORD_MISC_USER && machine && |
793 | mg != &machine->kmaps && | |
794 | machine__kernel_ip(machine, al->addr)) { | |
23346f21 | 795 | mg = &machine->kmaps; |
1f2a7069 | 796 | load_map = true; |
1ed091c4 ACM |
797 | goto try_again; |
798 | } | |
5b7ba82a AH |
799 | } else { |
800 | /* | |
801 | * Kernel maps might be changed when loading symbols so loading | |
802 | * must be done prior to using kernel maps. | |
803 | */ | |
804 | if (load_map) | |
326f59bf | 805 | map__load(al->map, machine->symbol_filter); |
1ed091c4 | 806 | al->addr = al->map->map_ip(al->map, al->addr); |
5b7ba82a | 807 | } |
59ee68ec ACM |
808 | } |
809 | ||
bb871a9c | 810 | void thread__find_addr_location(struct thread *thread, |
743eb868 | 811 | u8 cpumode, enum map_type type, u64 addr, |
61710bde | 812 | struct addr_location *al) |
59ee68ec | 813 | { |
bb871a9c | 814 | thread__find_addr_map(thread, cpumode, type, addr, al); |
59ee68ec | 815 | if (al->map != NULL) |
61710bde | 816 | al->sym = map__find_symbol(al->map, al->addr, |
bb871a9c | 817 | thread->mg->machine->symbol_filter); |
59ee68ec ACM |
818 | else |
819 | al->sym = NULL; | |
1ed091c4 ACM |
820 | } |
821 | ||
8115d60c | 822 | int perf_event__preprocess_sample(const union perf_event *event, |
743eb868 | 823 | struct machine *machine, |
8115d60c | 824 | struct addr_location *al, |
e44baa3e | 825 | struct perf_sample *sample) |
1ed091c4 | 826 | { |
8115d60c | 827 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
ef89325f | 828 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
13ce34df | 829 | sample->tid); |
41a37e20 | 830 | |
1ed091c4 ACM |
831 | if (thread == NULL) |
832 | return -1; | |
833 | ||
b9c5143a | 834 | dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid); |
1f626bc3 | 835 | /* |
743eb868 | 836 | * Have we already created the kernel maps for this machine? |
1f626bc3 ACM |
837 | * |
838 | * This should have happened earlier, when we processed the kernel MMAP | |
839 | * events, but for older perf.data files there was no such thing, so do | |
840 | * it now. | |
841 | */ | |
842 | if (cpumode == PERF_RECORD_MISC_KERNEL && | |
743eb868 ACM |
843 | machine->vmlinux_maps[MAP__FUNCTION] == NULL) |
844 | machine__create_kernel_maps(machine); | |
1ed091c4 | 845 | |
bb871a9c | 846 | thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al); |
1ed091c4 ACM |
847 | dump_printf(" ...... dso: %s\n", |
848 | al->map ? al->map->dso->long_name : | |
849 | al->level == 'H' ? "[hypervisor]" : "<not found>"); | |
466fa764 NK |
850 | |
851 | if (thread__is_filtered(thread)) | |
852 | al->filtered |= (1 << HIST_FILTER__THREAD); | |
853 | ||
96415e4d | 854 | al->sym = NULL; |
8d50e5b4 | 855 | al->cpu = sample->cpu; |
96415e4d ACM |
856 | |
857 | if (al->map) { | |
cb8f4e9a NK |
858 | struct dso *dso = al->map->dso; |
859 | ||
96415e4d | 860 | if (symbol_conf.dso_list && |
cb8f4e9a NK |
861 | (!dso || !(strlist__has_entry(symbol_conf.dso_list, |
862 | dso->short_name) || | |
863 | (dso->short_name != dso->long_name && | |
864 | strlist__has_entry(symbol_conf.dso_list, | |
b3cef7f6 NK |
865 | dso->long_name))))) { |
866 | al->filtered |= (1 << HIST_FILTER__DSO); | |
b3cef7f6 | 867 | } |
96415e4d | 868 | |
e44baa3e AH |
869 | al->sym = map__find_symbol(al->map, al->addr, |
870 | machine->symbol_filter); | |
96415e4d | 871 | } |
c410a338 | 872 | |
1500b93b FT |
873 | if (symbol_conf.sym_list && |
874 | (!al->sym || !strlist__has_entry(symbol_conf.sym_list, | |
b3cef7f6 NK |
875 | al->sym->name))) { |
876 | al->filtered |= (1 << HIST_FILTER__SYMBOL); | |
b3cef7f6 | 877 | } |
c410a338 | 878 | |
c410a338 | 879 | return 0; |
1ed091c4 | 880 | } |
9b0d2d87 AH |
881 | |
882 | bool is_bts_event(struct perf_event_attr *attr) | |
883 | { | |
884 | return attr->type == PERF_TYPE_HARDWARE && | |
885 | (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && | |
886 | attr->sample_period == 1; | |
887 | } | |
888 | ||
889 | bool sample_addr_correlates_sym(struct perf_event_attr *attr) | |
890 | { | |
891 | if (attr->type == PERF_TYPE_SOFTWARE && | |
892 | (attr->config == PERF_COUNT_SW_PAGE_FAULTS || | |
893 | attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN || | |
894 | attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)) | |
895 | return true; | |
896 | ||
897 | if (is_bts_event(attr)) | |
898 | return true; | |
899 | ||
900 | return false; | |
901 | } | |
902 | ||
903 | void perf_event__preprocess_sample_addr(union perf_event *event, | |
904 | struct perf_sample *sample, | |
9b0d2d87 AH |
905 | struct thread *thread, |
906 | struct addr_location *al) | |
907 | { | |
908 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; | |
909 | ||
bb871a9c | 910 | thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al); |
9b0d2d87 | 911 | if (!al->map) |
bb871a9c | 912 | thread__find_addr_map(thread, cpumode, MAP__VARIABLE, |
9b0d2d87 AH |
913 | sample->addr, al); |
914 | ||
915 | al->cpu = sample->cpu; | |
916 | al->sym = NULL; | |
917 | ||
918 | if (al->map) | |
919 | al->sym = map__find_symbol(al->map, al->addr, NULL); | |
920 | } |