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