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