Commit | Line | Data |
---|---|---|
234fbbf5 ACM |
1 | #include <linux/types.h> |
2 | #include "event.h" | |
3 | #include "debug.h" | |
f62d3f0f | 4 | #include "machine.h" |
c410a338 | 5 | #include "sort.h" |
234fbbf5 | 6 | #include "string.h" |
c410a338 | 7 | #include "strlist.h" |
62daacb5 | 8 | #include "thread.h" |
7c940c18 | 9 | #include "thread_map.h" |
234fbbf5 | 10 | |
8115d60c | 11 | static const char *perf_event__names[] = { |
3cb6d154 FW |
12 | [0] = "TOTAL", |
13 | [PERF_RECORD_MMAP] = "MMAP", | |
14 | [PERF_RECORD_LOST] = "LOST", | |
15 | [PERF_RECORD_COMM] = "COMM", | |
16 | [PERF_RECORD_EXIT] = "EXIT", | |
17 | [PERF_RECORD_THROTTLE] = "THROTTLE", | |
18 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", | |
19 | [PERF_RECORD_FORK] = "FORK", | |
20 | [PERF_RECORD_READ] = "READ", | |
21 | [PERF_RECORD_SAMPLE] = "SAMPLE", | |
22 | [PERF_RECORD_HEADER_ATTR] = "ATTR", | |
23 | [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", | |
24 | [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", | |
25 | [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", | |
26 | [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND", | |
c8446b9b ACM |
27 | }; |
28 | ||
8115d60c | 29 | const char *perf_event__name(unsigned int id) |
3835bc00 | 30 | { |
8115d60c | 31 | if (id >= ARRAY_SIZE(perf_event__names)) |
3835bc00 | 32 | return "INVALID"; |
8115d60c | 33 | if (!perf_event__names[id]) |
3835bc00 | 34 | return "UNKNOWN"; |
8115d60c | 35 | return perf_event__names[id]; |
3835bc00 TG |
36 | } |
37 | ||
8d50e5b4 | 38 | static struct perf_sample synth_sample = { |
640c03ce ACM |
39 | .pid = -1, |
40 | .tid = -1, | |
41 | .time = -1, | |
42 | .stream_id = -1, | |
43 | .cpu = -1, | |
44 | .period = 1, | |
45 | }; | |
46 | ||
f5faf726 | 47 | static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len) |
234fbbf5 | 48 | { |
234fbbf5 ACM |
49 | char filename[PATH_MAX]; |
50 | char bf[BUFSIZ]; | |
51 | FILE *fp; | |
52 | size_t size = 0; | |
f5faf726 | 53 | pid_t tgid = -1; |
234fbbf5 ACM |
54 | |
55 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); | |
56 | ||
57 | fp = fopen(filename, "r"); | |
58 | if (fp == NULL) { | |
234fbbf5 ACM |
59 | pr_debug("couldn't open %s\n", filename); |
60 | return 0; | |
61 | } | |
62 | ||
f5faf726 | 63 | while (!comm[0] || (tgid < 0)) { |
9c90a61c | 64 | if (fgets(bf, sizeof(bf), fp) == NULL) { |
f5faf726 DA |
65 | pr_warning("couldn't get COMM and pgid, malformed %s\n", |
66 | filename); | |
67 | break; | |
9c90a61c | 68 | } |
234fbbf5 ACM |
69 | |
70 | if (memcmp(bf, "Name:", 5) == 0) { | |
71 | char *name = bf + 5; | |
72 | while (*name && isspace(*name)) | |
73 | ++name; | |
74 | size = strlen(name) - 1; | |
f5faf726 DA |
75 | if (size >= len) |
76 | size = len - 1; | |
77 | memcpy(comm, name, size); | |
cfbd70c1 | 78 | comm[size] = '\0'; |
f5faf726 | 79 | |
234fbbf5 ACM |
80 | } else if (memcmp(bf, "Tgid:", 5) == 0) { |
81 | char *tgids = bf + 5; | |
82 | while (*tgids && isspace(*tgids)) | |
83 | ++tgids; | |
f5faf726 | 84 | tgid = atoi(tgids); |
234fbbf5 ACM |
85 | } |
86 | } | |
87 | ||
f5faf726 DA |
88 | fclose(fp); |
89 | ||
90 | return tgid; | |
91 | } | |
92 | ||
93 | static pid_t perf_event__synthesize_comm(struct perf_tool *tool, | |
94 | union perf_event *event, pid_t pid, | |
95 | int full, | |
96 | perf_event__handler_t process, | |
97 | struct machine *machine) | |
98 | { | |
99 | char filename[PATH_MAX]; | |
100 | size_t size; | |
101 | DIR *tasks; | |
102 | struct dirent dirent, *next; | |
103 | pid_t tgid; | |
104 | ||
105 | memset(&event->comm, 0, sizeof(event->comm)); | |
106 | ||
107 | tgid = perf_event__get_comm_tgid(pid, event->comm.comm, | |
108 | sizeof(event->comm.comm)); | |
109 | if (tgid < 0) | |
110 | goto out; | |
111 | ||
112 | event->comm.pid = tgid; | |
9c90a61c | 113 | event->comm.header.type = PERF_RECORD_COMM; |
f5faf726 DA |
114 | |
115 | size = strlen(event->comm.comm) + 1; | |
9ac3e487 | 116 | size = PERF_ALIGN(size, sizeof(u64)); |
743eb868 | 117 | memset(event->comm.comm + size, 0, machine->id_hdr_size); |
9c90a61c ACM |
118 | event->comm.header.size = (sizeof(event->comm) - |
119 | (sizeof(event->comm.comm) - size) + | |
743eb868 | 120 | machine->id_hdr_size); |
234fbbf5 | 121 | if (!full) { |
9c90a61c | 122 | event->comm.tid = pid; |
234fbbf5 | 123 | |
1e6d5322 DA |
124 | if (process(tool, event, &synth_sample, machine) != 0) |
125 | return -1; | |
126 | ||
9c90a61c | 127 | goto out; |
234fbbf5 ACM |
128 | } |
129 | ||
130 | snprintf(filename, sizeof(filename), "/proc/%d/task", pid); | |
131 | ||
132 | tasks = opendir(filename); | |
f5faf726 DA |
133 | if (tasks == NULL) { |
134 | pr_debug("couldn't open %s\n", filename); | |
135 | return 0; | |
136 | } | |
234fbbf5 ACM |
137 | |
138 | while (!readdir_r(tasks, &dirent, &next) && next) { | |
139 | char *end; | |
140 | pid = strtol(dirent.d_name, &end, 10); | |
141 | if (*end) | |
142 | continue; | |
143 | ||
f5faf726 DA |
144 | /* already have tgid; jut want to update the comm */ |
145 | (void) perf_event__get_comm_tgid(pid, event->comm.comm, | |
146 | sizeof(event->comm.comm)); | |
147 | ||
148 | size = strlen(event->comm.comm) + 1; | |
9ac3e487 | 149 | size = PERF_ALIGN(size, sizeof(u64)); |
f5faf726 DA |
150 | memset(event->comm.comm + size, 0, machine->id_hdr_size); |
151 | event->comm.header.size = (sizeof(event->comm) - | |
152 | (sizeof(event->comm.comm) - size) + | |
153 | machine->id_hdr_size); | |
154 | ||
9c90a61c | 155 | event->comm.tid = pid; |
234fbbf5 | 156 | |
1e6d5322 DA |
157 | if (process(tool, event, &synth_sample, machine) != 0) { |
158 | tgid = -1; | |
159 | break; | |
160 | } | |
234fbbf5 | 161 | } |
234fbbf5 | 162 | |
9c90a61c ACM |
163 | closedir(tasks); |
164 | out: | |
9c90a61c | 165 | return tgid; |
234fbbf5 ACM |
166 | } |
167 | ||
45694aa7 | 168 | static int perf_event__synthesize_mmap_events(struct perf_tool *tool, |
d20deb64 | 169 | union perf_event *event, |
8115d60c ACM |
170 | pid_t pid, pid_t tgid, |
171 | perf_event__handler_t process, | |
743eb868 | 172 | struct machine *machine) |
234fbbf5 ACM |
173 | { |
174 | char filename[PATH_MAX]; | |
175 | FILE *fp; | |
1e6d5322 | 176 | int rc = 0; |
234fbbf5 ACM |
177 | |
178 | snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); | |
179 | ||
180 | fp = fopen(filename, "r"); | |
181 | if (fp == NULL) { | |
182 | /* | |
183 | * We raced with a task exiting - just return: | |
184 | */ | |
185 | pr_debug("couldn't open %s\n", filename); | |
186 | return -1; | |
187 | } | |
188 | ||
9c90a61c ACM |
189 | event->header.type = PERF_RECORD_MMAP; |
190 | /* | |
191 | * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c | |
192 | */ | |
193 | event->header.misc = PERF_RECORD_MISC_USER; | |
194 | ||
234fbbf5 | 195 | while (1) { |
60648033 NK |
196 | char bf[BUFSIZ]; |
197 | char prot[5]; | |
198 | char execname[PATH_MAX]; | |
199 | char anonstr[] = "//anon"; | |
234fbbf5 | 200 | size_t size; |
60648033 | 201 | |
234fbbf5 ACM |
202 | if (fgets(bf, sizeof(bf), fp) == NULL) |
203 | break; | |
204 | ||
60648033 NK |
205 | /* ensure null termination since stack will be reused. */ |
206 | strcpy(execname, ""); | |
207 | ||
234fbbf5 | 208 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ |
60648033 NK |
209 | sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %*x:%*x %*u %s\n", |
210 | &event->mmap.start, &event->mmap.len, prot, | |
211 | &event->mmap.pgoff, execname); | |
212 | ||
213 | if (prot[2] != 'x') | |
234fbbf5 | 214 | continue; |
60648033 NK |
215 | |
216 | if (!strcmp(execname, "")) | |
217 | strcpy(execname, anonstr); | |
218 | ||
219 | size = strlen(execname) + 1; | |
220 | memcpy(event->mmap.filename, execname, size); | |
221 | size = PERF_ALIGN(size, sizeof(u64)); | |
222 | event->mmap.len -= event->mmap.start; | |
223 | event->mmap.header.size = (sizeof(event->mmap) - | |
224 | (sizeof(event->mmap.filename) - size)); | |
225 | memset(event->mmap.filename + size, 0, machine->id_hdr_size); | |
226 | event->mmap.header.size += machine->id_hdr_size; | |
227 | event->mmap.pid = tgid; | |
228 | event->mmap.tid = pid; | |
229 | ||
230 | if (process(tool, event, &synth_sample, machine) != 0) { | |
231 | rc = -1; | |
232 | break; | |
234fbbf5 ACM |
233 | } |
234 | } | |
235 | ||
236 | fclose(fp); | |
1e6d5322 | 237 | return rc; |
234fbbf5 ACM |
238 | } |
239 | ||
45694aa7 | 240 | int perf_event__synthesize_modules(struct perf_tool *tool, |
d20deb64 | 241 | perf_event__handler_t process, |
8115d60c | 242 | struct machine *machine) |
b7cece76 | 243 | { |
1e6d5322 | 244 | int rc = 0; |
b7cece76 | 245 | struct rb_node *nd; |
23346f21 | 246 | struct map_groups *kmaps = &machine->kmaps; |
8115d60c | 247 | union perf_event *event = zalloc((sizeof(event->mmap) + |
743eb868 | 248 | machine->id_hdr_size)); |
9c90a61c ACM |
249 | if (event == NULL) { |
250 | pr_debug("Not enough memory synthesizing mmap event " | |
251 | "for kernel modules\n"); | |
252 | return -1; | |
253 | } | |
254 | ||
255 | event->header.type = PERF_RECORD_MMAP; | |
b7cece76 | 256 | |
a1645ce1 ZY |
257 | /* |
258 | * kernel uses 0 for user space maps, see kernel/perf_event.c | |
259 | * __perf_event_mmap | |
260 | */ | |
23346f21 | 261 | if (machine__is_host(machine)) |
9c90a61c | 262 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
a1645ce1 | 263 | else |
9c90a61c | 264 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
a1645ce1 ZY |
265 | |
266 | for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]); | |
b7cece76 | 267 | nd; nd = rb_next(nd)) { |
b7cece76 ACM |
268 | size_t size; |
269 | struct map *pos = rb_entry(nd, struct map, rb_node); | |
270 | ||
271 | if (pos->dso->kernel) | |
272 | continue; | |
273 | ||
9ac3e487 | 274 | size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64)); |
9c90a61c ACM |
275 | event->mmap.header.type = PERF_RECORD_MMAP; |
276 | event->mmap.header.size = (sizeof(event->mmap) - | |
277 | (sizeof(event->mmap.filename) - size)); | |
743eb868 ACM |
278 | memset(event->mmap.filename + size, 0, machine->id_hdr_size); |
279 | event->mmap.header.size += machine->id_hdr_size; | |
9c90a61c ACM |
280 | event->mmap.start = pos->start; |
281 | event->mmap.len = pos->end - pos->start; | |
282 | event->mmap.pid = machine->pid; | |
283 | ||
284 | memcpy(event->mmap.filename, pos->dso->long_name, | |
b7cece76 | 285 | pos->dso->long_name_len + 1); |
1e6d5322 DA |
286 | if (process(tool, event, &synth_sample, machine) != 0) { |
287 | rc = -1; | |
288 | break; | |
289 | } | |
b7cece76 ACM |
290 | } |
291 | ||
9c90a61c | 292 | free(event); |
1e6d5322 | 293 | return rc; |
b7cece76 ACM |
294 | } |
295 | ||
8115d60c ACM |
296 | static int __event__synthesize_thread(union perf_event *comm_event, |
297 | union perf_event *mmap_event, | |
defd8d38 DA |
298 | pid_t pid, int full, |
299 | perf_event__handler_t process, | |
45694aa7 | 300 | struct perf_tool *tool, |
743eb868 | 301 | struct machine *machine) |
234fbbf5 | 302 | { |
defd8d38 | 303 | pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full, |
743eb868 | 304 | process, machine); |
234fbbf5 ACM |
305 | if (tgid == -1) |
306 | return -1; | |
45694aa7 | 307 | return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, |
743eb868 | 308 | process, machine); |
234fbbf5 ACM |
309 | } |
310 | ||
45694aa7 | 311 | int perf_event__synthesize_thread_map(struct perf_tool *tool, |
d20deb64 | 312 | struct thread_map *threads, |
7c940c18 | 313 | perf_event__handler_t process, |
743eb868 | 314 | struct machine *machine) |
9c90a61c | 315 | { |
8115d60c | 316 | union perf_event *comm_event, *mmap_event; |
defd8d38 | 317 | int err = -1, thread, j; |
9c90a61c | 318 | |
743eb868 | 319 | comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); |
9c90a61c ACM |
320 | if (comm_event == NULL) |
321 | goto out; | |
322 | ||
743eb868 | 323 | mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size); |
9c90a61c ACM |
324 | if (mmap_event == NULL) |
325 | goto out_free_comm; | |
326 | ||
401b8e13 ACM |
327 | err = 0; |
328 | for (thread = 0; thread < threads->nr; ++thread) { | |
329 | if (__event__synthesize_thread(comm_event, mmap_event, | |
defd8d38 | 330 | threads->map[thread], 0, |
45694aa7 | 331 | process, tool, machine)) { |
401b8e13 ACM |
332 | err = -1; |
333 | break; | |
334 | } | |
defd8d38 DA |
335 | |
336 | /* | |
337 | * comm.pid is set to thread group id by | |
338 | * perf_event__synthesize_comm | |
339 | */ | |
340 | if ((int) comm_event->comm.pid != threads->map[thread]) { | |
341 | bool need_leader = true; | |
342 | ||
343 | /* is thread group leader in thread_map? */ | |
344 | for (j = 0; j < threads->nr; ++j) { | |
345 | if ((int) comm_event->comm.pid == threads->map[j]) { | |
346 | need_leader = false; | |
347 | break; | |
348 | } | |
349 | } | |
350 | ||
351 | /* if not, generate events for it */ | |
352 | if (need_leader && | |
353 | __event__synthesize_thread(comm_event, | |
354 | mmap_event, | |
355 | comm_event->comm.pid, 0, | |
356 | process, tool, machine)) { | |
357 | err = -1; | |
358 | break; | |
359 | } | |
360 | } | |
401b8e13 | 361 | } |
9c90a61c ACM |
362 | free(mmap_event); |
363 | out_free_comm: | |
364 | free(comm_event); | |
365 | out: | |
366 | return err; | |
367 | } | |
368 | ||
45694aa7 | 369 | int perf_event__synthesize_threads(struct perf_tool *tool, |
d20deb64 | 370 | perf_event__handler_t process, |
743eb868 | 371 | struct machine *machine) |
234fbbf5 ACM |
372 | { |
373 | DIR *proc; | |
374 | struct dirent dirent, *next; | |
8115d60c | 375 | union perf_event *comm_event, *mmap_event; |
9c90a61c ACM |
376 | int err = -1; |
377 | ||
743eb868 | 378 | comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); |
9c90a61c ACM |
379 | if (comm_event == NULL) |
380 | goto out; | |
381 | ||
743eb868 | 382 | mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size); |
9c90a61c ACM |
383 | if (mmap_event == NULL) |
384 | goto out_free_comm; | |
234fbbf5 ACM |
385 | |
386 | proc = opendir("/proc"); | |
9c90a61c ACM |
387 | if (proc == NULL) |
388 | goto out_free_mmap; | |
234fbbf5 ACM |
389 | |
390 | while (!readdir_r(proc, &dirent, &next) && next) { | |
391 | char *end; | |
392 | pid_t pid = strtol(dirent.d_name, &end, 10); | |
393 | ||
394 | if (*end) /* only interested in proper numerical dirents */ | |
395 | continue; | |
ba361c92 ACM |
396 | /* |
397 | * We may race with exiting thread, so don't stop just because | |
398 | * one thread couldn't be synthesized. | |
399 | */ | |
400 | __event__synthesize_thread(comm_event, mmap_event, pid, 1, | |
401 | process, tool, machine); | |
234fbbf5 ACM |
402 | } |
403 | ||
9c90a61c | 404 | err = 0; |
1e6d5322 | 405 | closedir(proc); |
9c90a61c ACM |
406 | out_free_mmap: |
407 | free(mmap_event); | |
408 | out_free_comm: | |
409 | free(comm_event); | |
410 | out: | |
411 | return err; | |
234fbbf5 | 412 | } |
62daacb5 | 413 | |
56b03f3c ACM |
414 | struct process_symbol_args { |
415 | const char *name; | |
416 | u64 start; | |
417 | }; | |
418 | ||
3b01a413 | 419 | static int find_symbol_cb(void *arg, const char *name, char type, |
82151520 | 420 | u64 start) |
56b03f3c ACM |
421 | { |
422 | struct process_symbol_args *args = arg; | |
423 | ||
881516eb ACM |
424 | /* |
425 | * Must be a function or at least an alias, as in PARISC64, where "_text" is | |
426 | * an 'A' to the same address as "_stext". | |
427 | */ | |
428 | if (!(symbol_type__is_a(type, MAP__FUNCTION) || | |
429 | type == 'A') || strcmp(name, args->name)) | |
56b03f3c ACM |
430 | return 0; |
431 | ||
432 | args->start = start; | |
433 | return 1; | |
434 | } | |
435 | ||
45694aa7 | 436 | int perf_event__synthesize_kernel_mmap(struct perf_tool *tool, |
d20deb64 | 437 | perf_event__handler_t process, |
8115d60c ACM |
438 | struct machine *machine, |
439 | const char *symbol_name) | |
56b03f3c ACM |
440 | { |
441 | size_t size; | |
a1645ce1 ZY |
442 | const char *filename, *mmap_name; |
443 | char path[PATH_MAX]; | |
444 | char name_buff[PATH_MAX]; | |
445 | struct map *map; | |
9c90a61c | 446 | int err; |
56b03f3c ACM |
447 | /* |
448 | * We should get this from /sys/kernel/sections/.text, but till that is | |
449 | * available use this, and after it is use this as a fallback for older | |
450 | * kernels. | |
451 | */ | |
452 | struct process_symbol_args args = { .name = symbol_name, }; | |
8115d60c | 453 | union perf_event *event = zalloc((sizeof(event->mmap) + |
743eb868 | 454 | machine->id_hdr_size)); |
9c90a61c ACM |
455 | if (event == NULL) { |
456 | pr_debug("Not enough memory synthesizing mmap event " | |
457 | "for kernel modules\n"); | |
458 | return -1; | |
459 | } | |
56b03f3c | 460 | |
48ea8f54 | 461 | mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff)); |
23346f21 | 462 | if (machine__is_host(machine)) { |
a1645ce1 ZY |
463 | /* |
464 | * kernel uses PERF_RECORD_MISC_USER for user space maps, | |
465 | * see kernel/perf_event.c __perf_event_mmap | |
466 | */ | |
9c90a61c | 467 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
a1645ce1 ZY |
468 | filename = "/proc/kallsyms"; |
469 | } else { | |
9c90a61c | 470 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
23346f21 | 471 | if (machine__is_default_guest(machine)) |
a1645ce1 ZY |
472 | filename = (char *) symbol_conf.default_guest_kallsyms; |
473 | else { | |
23346f21 | 474 | sprintf(path, "%s/proc/kallsyms", machine->root_dir); |
a1645ce1 ZY |
475 | filename = path; |
476 | } | |
477 | } | |
478 | ||
0b9e01a4 TJ |
479 | if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0) { |
480 | free(event); | |
56b03f3c | 481 | return -ENOENT; |
0b9e01a4 | 482 | } |
56b03f3c | 483 | |
23346f21 | 484 | map = machine->vmlinux_maps[MAP__FUNCTION]; |
9c90a61c | 485 | size = snprintf(event->mmap.filename, sizeof(event->mmap.filename), |
a1645ce1 | 486 | "%s%s", mmap_name, symbol_name) + 1; |
9ac3e487 | 487 | size = PERF_ALIGN(size, sizeof(u64)); |
9c90a61c ACM |
488 | event->mmap.header.type = PERF_RECORD_MMAP; |
489 | event->mmap.header.size = (sizeof(event->mmap) - | |
743eb868 | 490 | (sizeof(event->mmap.filename) - size) + machine->id_hdr_size); |
9c90a61c ACM |
491 | event->mmap.pgoff = args.start; |
492 | event->mmap.start = map->start; | |
493 | event->mmap.len = map->end - event->mmap.start; | |
494 | event->mmap.pid = machine->pid; | |
495 | ||
45694aa7 | 496 | err = process(tool, event, &synth_sample, machine); |
9c90a61c ACM |
497 | free(event); |
498 | ||
499 | return err; | |
56b03f3c ACM |
500 | } |
501 | ||
482ad897 ACM |
502 | size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp) |
503 | { | |
504 | return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid); | |
505 | } | |
506 | ||
1d037ca1 | 507 | int perf_event__process_comm(struct perf_tool *tool __maybe_unused, |
d20deb64 | 508 | union perf_event *event, |
1d037ca1 | 509 | struct perf_sample *sample __maybe_unused, |
743eb868 | 510 | struct machine *machine) |
62daacb5 | 511 | { |
b0a7d1a0 | 512 | return machine__process_comm_event(machine, event); |
62daacb5 ACM |
513 | } |
514 | ||
1d037ca1 | 515 | int perf_event__process_lost(struct perf_tool *tool __maybe_unused, |
d20deb64 | 516 | union perf_event *event, |
1d037ca1 | 517 | struct perf_sample *sample __maybe_unused, |
b0a7d1a0 | 518 | struct machine *machine) |
62daacb5 | 519 | { |
b0a7d1a0 | 520 | return machine__process_lost_event(machine, event); |
a1645ce1 | 521 | } |
b7cece76 | 522 | |
482ad897 ACM |
523 | size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp) |
524 | { | |
525 | return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n", | |
526 | event->mmap.pid, event->mmap.tid, event->mmap.start, | |
527 | event->mmap.len, event->mmap.pgoff, event->mmap.filename); | |
528 | } | |
529 | ||
b0a7d1a0 | 530 | int perf_event__process_mmap(struct perf_tool *tool __maybe_unused, |
d20deb64 | 531 | union perf_event *event, |
1d037ca1 | 532 | struct perf_sample *sample __maybe_unused, |
743eb868 | 533 | struct machine *machine) |
a1645ce1 | 534 | { |
b0a7d1a0 | 535 | return machine__process_mmap_event(machine, event); |
62daacb5 ACM |
536 | } |
537 | ||
482ad897 ACM |
538 | size_t perf_event__fprintf_task(union perf_event *event, FILE *fp) |
539 | { | |
540 | return fprintf(fp, "(%d:%d):(%d:%d)\n", | |
541 | event->fork.pid, event->fork.tid, | |
542 | event->fork.ppid, event->fork.ptid); | |
543 | } | |
544 | ||
f62d3f0f | 545 | int perf_event__process_fork(struct perf_tool *tool __maybe_unused, |
d20deb64 | 546 | union perf_event *event, |
1d037ca1 | 547 | struct perf_sample *sample __maybe_unused, |
f62d3f0f | 548 | struct machine *machine) |
62daacb5 | 549 | { |
b0a7d1a0 | 550 | return machine__process_fork_event(machine, event); |
62daacb5 | 551 | } |
1ed091c4 | 552 | |
f62d3f0f ACM |
553 | int perf_event__process_exit(struct perf_tool *tool __maybe_unused, |
554 | union perf_event *event, | |
555 | struct perf_sample *sample __maybe_unused, | |
556 | struct machine *machine) | |
557 | { | |
b0a7d1a0 | 558 | return machine__process_exit_event(machine, event); |
f62d3f0f ACM |
559 | } |
560 | ||
482ad897 ACM |
561 | size_t perf_event__fprintf(union perf_event *event, FILE *fp) |
562 | { | |
563 | size_t ret = fprintf(fp, "PERF_RECORD_%s", | |
564 | perf_event__name(event->header.type)); | |
565 | ||
566 | switch (event->header.type) { | |
567 | case PERF_RECORD_COMM: | |
568 | ret += perf_event__fprintf_comm(event, fp); | |
569 | break; | |
570 | case PERF_RECORD_FORK: | |
571 | case PERF_RECORD_EXIT: | |
572 | ret += perf_event__fprintf_task(event, fp); | |
573 | break; | |
574 | case PERF_RECORD_MMAP: | |
575 | ret += perf_event__fprintf_mmap(event, fp); | |
576 | break; | |
577 | default: | |
578 | ret += fprintf(fp, "\n"); | |
579 | } | |
580 | ||
581 | return ret; | |
582 | } | |
583 | ||
b0a7d1a0 ACM |
584 | int perf_event__process(struct perf_tool *tool __maybe_unused, |
585 | union perf_event *event, | |
586 | struct perf_sample *sample __maybe_unused, | |
587 | struct machine *machine) | |
b83f920e | 588 | { |
b0a7d1a0 | 589 | return machine__process_event(machine, event); |
b83f920e SD |
590 | } |
591 | ||
59ee68ec | 592 | void thread__find_addr_map(struct thread *self, |
743eb868 ACM |
593 | struct machine *machine, u8 cpumode, |
594 | enum map_type type, u64 addr, | |
59ee68ec | 595 | struct addr_location *al) |
1ed091c4 | 596 | { |
9958e1f0 | 597 | struct map_groups *mg = &self->mg; |
1ed091c4 | 598 | |
9958e1f0 | 599 | al->thread = self; |
1ed091c4 | 600 | al->addr = addr; |
a1645ce1 ZY |
601 | al->cpumode = cpumode; |
602 | al->filtered = false; | |
1ed091c4 | 603 | |
743eb868 ACM |
604 | if (machine == NULL) { |
605 | al->map = NULL; | |
606 | return; | |
607 | } | |
608 | ||
a1645ce1 | 609 | if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { |
1ed091c4 | 610 | al->level = 'k'; |
23346f21 | 611 | mg = &machine->kmaps; |
a1645ce1 | 612 | } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { |
1ed091c4 | 613 | al->level = '.'; |
a1645ce1 ZY |
614 | } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { |
615 | al->level = 'g'; | |
23346f21 | 616 | mg = &machine->kmaps; |
a1645ce1 ZY |
617 | } else { |
618 | /* | |
619 | * 'u' means guest os user space. | |
620 | * TODO: We don't support guest user space. Might support late. | |
621 | */ | |
622 | if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) | |
623 | al->level = 'u'; | |
624 | else | |
625 | al->level = 'H'; | |
1ed091c4 | 626 | al->map = NULL; |
a1645ce1 ZY |
627 | |
628 | if ((cpumode == PERF_RECORD_MISC_GUEST_USER || | |
629 | cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && | |
630 | !perf_guest) | |
631 | al->filtered = true; | |
632 | if ((cpumode == PERF_RECORD_MISC_USER || | |
633 | cpumode == PERF_RECORD_MISC_KERNEL) && | |
634 | !perf_host) | |
635 | al->filtered = true; | |
636 | ||
1ed091c4 ACM |
637 | return; |
638 | } | |
639 | try_again: | |
9958e1f0 | 640 | al->map = map_groups__find(mg, type, al->addr); |
1ed091c4 ACM |
641 | if (al->map == NULL) { |
642 | /* | |
643 | * If this is outside of all known maps, and is a negative | |
644 | * address, try to look it up in the kernel dso, as it might be | |
645 | * a vsyscall or vdso (which executes in user-mode). | |
646 | * | |
647 | * XXX This is nasty, we should have a symbol list in the | |
648 | * "[vdso]" dso, but for now lets use the old trick of looking | |
649 | * in the whole kernel symbol list. | |
650 | */ | |
a1645ce1 | 651 | if ((long long)al->addr < 0 && |
6c6804fb | 652 | cpumode == PERF_RECORD_MISC_USER && |
23346f21 ACM |
653 | machine && mg != &machine->kmaps) { |
654 | mg = &machine->kmaps; | |
1ed091c4 ACM |
655 | goto try_again; |
656 | } | |
59ee68ec | 657 | } else |
1ed091c4 | 658 | al->addr = al->map->map_ip(al->map, al->addr); |
59ee68ec ACM |
659 | } |
660 | ||
743eb868 ACM |
661 | void thread__find_addr_location(struct thread *thread, struct machine *machine, |
662 | u8 cpumode, enum map_type type, u64 addr, | |
59ee68ec ACM |
663 | struct addr_location *al, |
664 | symbol_filter_t filter) | |
665 | { | |
743eb868 | 666 | thread__find_addr_map(thread, machine, cpumode, type, addr, al); |
59ee68ec | 667 | if (al->map != NULL) |
9de89fe7 | 668 | al->sym = map__find_symbol(al->map, al->addr, filter); |
59ee68ec ACM |
669 | else |
670 | al->sym = NULL; | |
1ed091c4 ACM |
671 | } |
672 | ||
8115d60c | 673 | int perf_event__preprocess_sample(const union perf_event *event, |
743eb868 | 674 | struct machine *machine, |
8115d60c ACM |
675 | struct addr_location *al, |
676 | struct perf_sample *sample, | |
677 | symbol_filter_t filter) | |
1ed091c4 | 678 | { |
8115d60c | 679 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
743eb868 | 680 | struct thread *thread = machine__findnew_thread(machine, event->ip.pid); |
41a37e20 | 681 | |
1ed091c4 ACM |
682 | if (thread == NULL) |
683 | return -1; | |
684 | ||
c410a338 ACM |
685 | if (symbol_conf.comm_list && |
686 | !strlist__has_entry(symbol_conf.comm_list, thread->comm)) | |
687 | goto out_filtered; | |
688 | ||
1ed091c4 | 689 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
1f626bc3 | 690 | /* |
743eb868 | 691 | * Have we already created the kernel maps for this machine? |
1f626bc3 ACM |
692 | * |
693 | * This should have happened earlier, when we processed the kernel MMAP | |
694 | * events, but for older perf.data files there was no such thing, so do | |
695 | * it now. | |
696 | */ | |
697 | if (cpumode == PERF_RECORD_MISC_KERNEL && | |
743eb868 ACM |
698 | machine->vmlinux_maps[MAP__FUNCTION] == NULL) |
699 | machine__create_kernel_maps(machine); | |
1ed091c4 | 700 | |
743eb868 ACM |
701 | thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION, |
702 | event->ip.ip, al); | |
1ed091c4 ACM |
703 | dump_printf(" ...... dso: %s\n", |
704 | al->map ? al->map->dso->long_name : | |
705 | al->level == 'H' ? "[hypervisor]" : "<not found>"); | |
96415e4d | 706 | al->sym = NULL; |
8d50e5b4 | 707 | al->cpu = sample->cpu; |
96415e4d ACM |
708 | |
709 | if (al->map) { | |
cb8f4e9a NK |
710 | struct dso *dso = al->map->dso; |
711 | ||
96415e4d | 712 | if (symbol_conf.dso_list && |
cb8f4e9a NK |
713 | (!dso || !(strlist__has_entry(symbol_conf.dso_list, |
714 | dso->short_name) || | |
715 | (dso->short_name != dso->long_name && | |
716 | strlist__has_entry(symbol_conf.dso_list, | |
717 | dso->long_name))))) | |
96415e4d | 718 | goto out_filtered; |
96415e4d ACM |
719 | |
720 | al->sym = map__find_symbol(al->map, al->addr, filter); | |
721 | } | |
c410a338 | 722 | |
1500b93b FT |
723 | if (symbol_conf.sym_list && |
724 | (!al->sym || !strlist__has_entry(symbol_conf.sym_list, | |
725 | al->sym->name))) | |
c410a338 ACM |
726 | goto out_filtered; |
727 | ||
c410a338 ACM |
728 | return 0; |
729 | ||
730 | out_filtered: | |
731 | al->filtered = true; | |
1ed091c4 ACM |
732 | return 0; |
733 | } |