Commit | Line | Data |
---|---|---|
234fbbf5 ACM |
1 | #include <linux/types.h> |
2 | #include "event.h" | |
3 | #include "debug.h" | |
ec913369 | 4 | #include "session.h" |
c410a338 | 5 | #include "sort.h" |
234fbbf5 | 6 | #include "string.h" |
c410a338 | 7 | #include "strlist.h" |
62daacb5 | 8 | #include "thread.h" |
234fbbf5 | 9 | |
3835bc00 | 10 | static const char *event__name[] = { |
c8446b9b ACM |
11 | [0] = "TOTAL", |
12 | [PERF_RECORD_MMAP] = "MMAP", | |
13 | [PERF_RECORD_LOST] = "LOST", | |
14 | [PERF_RECORD_COMM] = "COMM", | |
15 | [PERF_RECORD_EXIT] = "EXIT", | |
16 | [PERF_RECORD_THROTTLE] = "THROTTLE", | |
17 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", | |
18 | [PERF_RECORD_FORK] = "FORK", | |
19 | [PERF_RECORD_READ] = "READ", | |
20 | [PERF_RECORD_SAMPLE] = "SAMPLE", | |
21 | [PERF_RECORD_HEADER_ATTR] = "ATTR", | |
22 | [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", | |
23 | [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", | |
24 | [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", | |
3835bc00 | 25 | [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND", |
c8446b9b ACM |
26 | }; |
27 | ||
3835bc00 TG |
28 | const char *event__get_event_name(unsigned int id) |
29 | { | |
30 | if (id >= ARRAY_SIZE(event__name)) | |
31 | return "INVALID"; | |
32 | if (!event__name[id]) | |
33 | return "UNKNOWN"; | |
34 | return event__name[id]; | |
35 | } | |
36 | ||
640c03ce ACM |
37 | static struct sample_data synth_sample = { |
38 | .pid = -1, | |
39 | .tid = -1, | |
40 | .time = -1, | |
41 | .stream_id = -1, | |
42 | .cpu = -1, | |
43 | .period = 1, | |
44 | }; | |
45 | ||
9c90a61c | 46 | static pid_t event__synthesize_comm(event_t *event, pid_t pid, int full, |
cf553114 | 47 | event__handler_t process, |
d8f66248 | 48 | struct perf_session *session) |
234fbbf5 | 49 | { |
234fbbf5 ACM |
50 | char filename[PATH_MAX]; |
51 | char bf[BUFSIZ]; | |
52 | FILE *fp; | |
53 | size_t size = 0; | |
54 | DIR *tasks; | |
55 | struct dirent dirent, *next; | |
56 | pid_t tgid = 0; | |
57 | ||
58 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); | |
59 | ||
60 | fp = fopen(filename, "r"); | |
61 | if (fp == NULL) { | |
62 | out_race: | |
63 | /* | |
64 | * We raced with a task exiting - just return: | |
65 | */ | |
66 | pr_debug("couldn't open %s\n", filename); | |
67 | return 0; | |
68 | } | |
69 | ||
9c90a61c ACM |
70 | memset(&event->comm, 0, sizeof(event->comm)); |
71 | ||
72 | while (!event->comm.comm[0] || !event->comm.pid) { | |
73 | if (fgets(bf, sizeof(bf), fp) == NULL) { | |
74 | pr_warning("couldn't get COMM and pgid, malformed %s\n", filename); | |
75 | goto out; | |
76 | } | |
234fbbf5 ACM |
77 | |
78 | if (memcmp(bf, "Name:", 5) == 0) { | |
79 | char *name = bf + 5; | |
80 | while (*name && isspace(*name)) | |
81 | ++name; | |
82 | size = strlen(name) - 1; | |
9c90a61c | 83 | memcpy(event->comm.comm, name, size++); |
234fbbf5 ACM |
84 | } else if (memcmp(bf, "Tgid:", 5) == 0) { |
85 | char *tgids = bf + 5; | |
86 | while (*tgids && isspace(*tgids)) | |
87 | ++tgids; | |
9c90a61c | 88 | tgid = event->comm.pid = atoi(tgids); |
234fbbf5 ACM |
89 | } |
90 | } | |
91 | ||
9c90a61c | 92 | event->comm.header.type = PERF_RECORD_COMM; |
234fbbf5 | 93 | size = ALIGN(size, sizeof(u64)); |
9c90a61c ACM |
94 | memset(event->comm.comm + size, 0, session->id_hdr_size); |
95 | event->comm.header.size = (sizeof(event->comm) - | |
96 | (sizeof(event->comm.comm) - size) + | |
97 | session->id_hdr_size); | |
234fbbf5 | 98 | if (!full) { |
9c90a61c | 99 | event->comm.tid = pid; |
234fbbf5 | 100 | |
9c90a61c ACM |
101 | process(event, &synth_sample, session); |
102 | goto out; | |
234fbbf5 ACM |
103 | } |
104 | ||
105 | snprintf(filename, sizeof(filename), "/proc/%d/task", pid); | |
106 | ||
107 | tasks = opendir(filename); | |
108 | if (tasks == NULL) | |
109 | goto out_race; | |
110 | ||
111 | while (!readdir_r(tasks, &dirent, &next) && next) { | |
112 | char *end; | |
113 | pid = strtol(dirent.d_name, &end, 10); | |
114 | if (*end) | |
115 | continue; | |
116 | ||
9c90a61c | 117 | event->comm.tid = pid; |
234fbbf5 | 118 | |
9c90a61c | 119 | process(event, &synth_sample, session); |
234fbbf5 | 120 | } |
234fbbf5 | 121 | |
9c90a61c ACM |
122 | closedir(tasks); |
123 | out: | |
234fbbf5 | 124 | fclose(fp); |
234fbbf5 | 125 | |
9c90a61c | 126 | return tgid; |
234fbbf5 ACM |
127 | } |
128 | ||
9c90a61c | 129 | static int event__synthesize_mmap_events(event_t *event, pid_t pid, pid_t tgid, |
cf553114 | 130 | event__handler_t process, |
d8f66248 | 131 | struct perf_session *session) |
234fbbf5 ACM |
132 | { |
133 | char filename[PATH_MAX]; | |
134 | FILE *fp; | |
135 | ||
136 | snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); | |
137 | ||
138 | fp = fopen(filename, "r"); | |
139 | if (fp == NULL) { | |
140 | /* | |
141 | * We raced with a task exiting - just return: | |
142 | */ | |
143 | pr_debug("couldn't open %s\n", filename); | |
144 | return -1; | |
145 | } | |
146 | ||
9c90a61c ACM |
147 | event->header.type = PERF_RECORD_MMAP; |
148 | /* | |
149 | * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c | |
150 | */ | |
151 | event->header.misc = PERF_RECORD_MISC_USER; | |
152 | ||
234fbbf5 ACM |
153 | while (1) { |
154 | char bf[BUFSIZ], *pbf = bf; | |
234fbbf5 ACM |
155 | int n; |
156 | size_t size; | |
157 | if (fgets(bf, sizeof(bf), fp) == NULL) | |
158 | break; | |
159 | ||
160 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ | |
9c90a61c | 161 | n = hex2u64(pbf, &event->mmap.start); |
234fbbf5 ACM |
162 | if (n < 0) |
163 | continue; | |
164 | pbf += n + 1; | |
9c90a61c | 165 | n = hex2u64(pbf, &event->mmap.len); |
234fbbf5 ACM |
166 | if (n < 0) |
167 | continue; | |
168 | pbf += n + 3; | |
169 | if (*pbf == 'x') { /* vm_exec */ | |
170 | char *execname = strchr(bf, '/'); | |
171 | ||
172 | /* Catch VDSO */ | |
173 | if (execname == NULL) | |
174 | execname = strstr(bf, "[vdso]"); | |
175 | ||
176 | if (execname == NULL) | |
177 | continue; | |
178 | ||
4af8b35d | 179 | pbf += 3; |
9c90a61c | 180 | n = hex2u64(pbf, &event->mmap.pgoff); |
4af8b35d | 181 | |
234fbbf5 ACM |
182 | size = strlen(execname); |
183 | execname[size - 1] = '\0'; /* Remove \n */ | |
9c90a61c | 184 | memcpy(event->mmap.filename, execname, size); |
234fbbf5 | 185 | size = ALIGN(size, sizeof(u64)); |
9c90a61c ACM |
186 | event->mmap.len -= event->mmap.start; |
187 | event->mmap.header.size = (sizeof(event->mmap) - | |
188 | (sizeof(event->mmap.filename) - size)); | |
189 | memset(event->mmap.filename + size, 0, session->id_hdr_size); | |
190 | event->mmap.header.size += session->id_hdr_size; | |
191 | event->mmap.pid = tgid; | |
192 | event->mmap.tid = pid; | |
193 | ||
194 | process(event, &synth_sample, session); | |
234fbbf5 ACM |
195 | } |
196 | } | |
197 | ||
198 | fclose(fp); | |
199 | return 0; | |
200 | } | |
201 | ||
b7cece76 | 202 | int event__synthesize_modules(event__handler_t process, |
a1645ce1 | 203 | struct perf_session *session, |
23346f21 | 204 | struct machine *machine) |
b7cece76 ACM |
205 | { |
206 | struct rb_node *nd; | |
23346f21 | 207 | struct map_groups *kmaps = &machine->kmaps; |
9c90a61c ACM |
208 | event_t *event = zalloc(sizeof(event->mmap) + session->id_hdr_size); |
209 | ||
210 | if (event == NULL) { | |
211 | pr_debug("Not enough memory synthesizing mmap event " | |
212 | "for kernel modules\n"); | |
213 | return -1; | |
214 | } | |
215 | ||
216 | event->header.type = PERF_RECORD_MMAP; | |
b7cece76 | 217 | |
a1645ce1 ZY |
218 | /* |
219 | * kernel uses 0 for user space maps, see kernel/perf_event.c | |
220 | * __perf_event_mmap | |
221 | */ | |
23346f21 | 222 | if (machine__is_host(machine)) |
9c90a61c | 223 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
a1645ce1 | 224 | else |
9c90a61c | 225 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
a1645ce1 ZY |
226 | |
227 | for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]); | |
b7cece76 | 228 | nd; nd = rb_next(nd)) { |
b7cece76 ACM |
229 | size_t size; |
230 | struct map *pos = rb_entry(nd, struct map, rb_node); | |
231 | ||
232 | if (pos->dso->kernel) | |
233 | continue; | |
234 | ||
235 | size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64)); | |
9c90a61c ACM |
236 | event->mmap.header.type = PERF_RECORD_MMAP; |
237 | event->mmap.header.size = (sizeof(event->mmap) - | |
238 | (sizeof(event->mmap.filename) - size)); | |
239 | memset(event->mmap.filename + size, 0, session->id_hdr_size); | |
240 | event->mmap.header.size += session->id_hdr_size; | |
241 | event->mmap.start = pos->start; | |
242 | event->mmap.len = pos->end - pos->start; | |
243 | event->mmap.pid = machine->pid; | |
244 | ||
245 | memcpy(event->mmap.filename, pos->dso->long_name, | |
b7cece76 | 246 | pos->dso->long_name_len + 1); |
9c90a61c | 247 | process(event, &synth_sample, session); |
b7cece76 ACM |
248 | } |
249 | ||
9c90a61c | 250 | free(event); |
b7cece76 ACM |
251 | return 0; |
252 | } | |
253 | ||
9c90a61c ACM |
254 | static int __event__synthesize_thread(event_t *comm_event, event_t *mmap_event, |
255 | pid_t pid, event__handler_t process, | |
256 | struct perf_session *session) | |
234fbbf5 | 257 | { |
9c90a61c ACM |
258 | pid_t tgid = event__synthesize_comm(comm_event, pid, 1, process, |
259 | session); | |
234fbbf5 ACM |
260 | if (tgid == -1) |
261 | return -1; | |
9c90a61c ACM |
262 | return event__synthesize_mmap_events(mmap_event, pid, tgid, |
263 | process, session); | |
234fbbf5 ACM |
264 | } |
265 | ||
9c90a61c ACM |
266 | int event__synthesize_thread(pid_t pid, event__handler_t process, |
267 | struct perf_session *session) | |
268 | { | |
269 | event_t *comm_event, *mmap_event; | |
270 | int err = -1; | |
271 | ||
272 | comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size); | |
273 | if (comm_event == NULL) | |
274 | goto out; | |
275 | ||
276 | mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size); | |
277 | if (mmap_event == NULL) | |
278 | goto out_free_comm; | |
279 | ||
280 | err = __event__synthesize_thread(comm_event, mmap_event, pid, | |
281 | process, session); | |
282 | free(mmap_event); | |
283 | out_free_comm: | |
284 | free(comm_event); | |
285 | out: | |
286 | return err; | |
287 | } | |
288 | ||
289 | int event__synthesize_threads(event__handler_t process, | |
290 | struct perf_session *session) | |
234fbbf5 ACM |
291 | { |
292 | DIR *proc; | |
293 | struct dirent dirent, *next; | |
9c90a61c ACM |
294 | event_t *comm_event, *mmap_event; |
295 | int err = -1; | |
296 | ||
297 | comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size); | |
298 | if (comm_event == NULL) | |
299 | goto out; | |
300 | ||
301 | mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size); | |
302 | if (mmap_event == NULL) | |
303 | goto out_free_comm; | |
234fbbf5 ACM |
304 | |
305 | proc = opendir("/proc"); | |
9c90a61c ACM |
306 | if (proc == NULL) |
307 | goto out_free_mmap; | |
234fbbf5 ACM |
308 | |
309 | while (!readdir_r(proc, &dirent, &next) && next) { | |
310 | char *end; | |
311 | pid_t pid = strtol(dirent.d_name, &end, 10); | |
312 | ||
313 | if (*end) /* only interested in proper numerical dirents */ | |
314 | continue; | |
315 | ||
9c90a61c ACM |
316 | __event__synthesize_thread(comm_event, mmap_event, pid, |
317 | process, session); | |
234fbbf5 ACM |
318 | } |
319 | ||
320 | closedir(proc); | |
9c90a61c ACM |
321 | err = 0; |
322 | out_free_mmap: | |
323 | free(mmap_event); | |
324 | out_free_comm: | |
325 | free(comm_event); | |
326 | out: | |
327 | return err; | |
234fbbf5 | 328 | } |
62daacb5 | 329 | |
56b03f3c ACM |
330 | struct process_symbol_args { |
331 | const char *name; | |
332 | u64 start; | |
333 | }; | |
334 | ||
3b01a413 ACM |
335 | static int find_symbol_cb(void *arg, const char *name, char type, |
336 | u64 start, u64 end __used) | |
56b03f3c ACM |
337 | { |
338 | struct process_symbol_args *args = arg; | |
339 | ||
881516eb ACM |
340 | /* |
341 | * Must be a function or at least an alias, as in PARISC64, where "_text" is | |
342 | * an 'A' to the same address as "_stext". | |
343 | */ | |
344 | if (!(symbol_type__is_a(type, MAP__FUNCTION) || | |
345 | type == 'A') || strcmp(name, args->name)) | |
56b03f3c ACM |
346 | return 0; |
347 | ||
348 | args->start = start; | |
349 | return 1; | |
350 | } | |
351 | ||
cf553114 | 352 | int event__synthesize_kernel_mmap(event__handler_t process, |
56b03f3c | 353 | struct perf_session *session, |
23346f21 | 354 | struct machine *machine, |
56b03f3c ACM |
355 | const char *symbol_name) |
356 | { | |
357 | size_t size; | |
a1645ce1 ZY |
358 | const char *filename, *mmap_name; |
359 | char path[PATH_MAX]; | |
360 | char name_buff[PATH_MAX]; | |
361 | struct map *map; | |
9c90a61c | 362 | int err; |
56b03f3c ACM |
363 | /* |
364 | * We should get this from /sys/kernel/sections/.text, but till that is | |
365 | * available use this, and after it is use this as a fallback for older | |
366 | * kernels. | |
367 | */ | |
368 | struct process_symbol_args args = { .name = symbol_name, }; | |
9c90a61c ACM |
369 | event_t *event = zalloc(sizeof(event->mmap) + session->id_hdr_size); |
370 | ||
371 | if (event == NULL) { | |
372 | pr_debug("Not enough memory synthesizing mmap event " | |
373 | "for kernel modules\n"); | |
374 | return -1; | |
375 | } | |
56b03f3c | 376 | |
48ea8f54 | 377 | mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff)); |
23346f21 | 378 | if (machine__is_host(machine)) { |
a1645ce1 ZY |
379 | /* |
380 | * kernel uses PERF_RECORD_MISC_USER for user space maps, | |
381 | * see kernel/perf_event.c __perf_event_mmap | |
382 | */ | |
9c90a61c | 383 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
a1645ce1 ZY |
384 | filename = "/proc/kallsyms"; |
385 | } else { | |
9c90a61c | 386 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
23346f21 | 387 | if (machine__is_default_guest(machine)) |
a1645ce1 ZY |
388 | filename = (char *) symbol_conf.default_guest_kallsyms; |
389 | else { | |
23346f21 | 390 | sprintf(path, "%s/proc/kallsyms", machine->root_dir); |
a1645ce1 ZY |
391 | filename = path; |
392 | } | |
393 | } | |
394 | ||
395 | if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0) | |
56b03f3c ACM |
396 | return -ENOENT; |
397 | ||
23346f21 | 398 | map = machine->vmlinux_maps[MAP__FUNCTION]; |
9c90a61c | 399 | size = snprintf(event->mmap.filename, sizeof(event->mmap.filename), |
a1645ce1 | 400 | "%s%s", mmap_name, symbol_name) + 1; |
56b03f3c | 401 | size = ALIGN(size, sizeof(u64)); |
9c90a61c ACM |
402 | event->mmap.header.type = PERF_RECORD_MMAP; |
403 | event->mmap.header.size = (sizeof(event->mmap) - | |
404 | (sizeof(event->mmap.filename) - size) + session->id_hdr_size); | |
405 | event->mmap.pgoff = args.start; | |
406 | event->mmap.start = map->start; | |
407 | event->mmap.len = map->end - event->mmap.start; | |
408 | event->mmap.pid = machine->pid; | |
409 | ||
410 | err = process(event, &synth_sample, session); | |
411 | free(event); | |
412 | ||
413 | return err; | |
56b03f3c ACM |
414 | } |
415 | ||
8a6c5b26 | 416 | static void thread__comm_adjust(struct thread *self, struct hists *hists) |
d599db3f ACM |
417 | { |
418 | char *comm = self->comm; | |
419 | ||
420 | if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep && | |
421 | (!symbol_conf.comm_list || | |
422 | strlist__has_entry(symbol_conf.comm_list, comm))) { | |
8a6c5b26 | 423 | u16 slen = strlen(comm); |
d599db3f | 424 | |
8a6c5b26 ACM |
425 | if (hists__new_col_len(hists, HISTC_COMM, slen)) |
426 | hists__set_col_len(hists, HISTC_THREAD, slen + 6); | |
d599db3f ACM |
427 | } |
428 | } | |
429 | ||
8a6c5b26 ACM |
430 | static int thread__set_comm_adjust(struct thread *self, const char *comm, |
431 | struct hists *hists) | |
d599db3f ACM |
432 | { |
433 | int ret = thread__set_comm(self, comm); | |
434 | ||
435 | if (ret) | |
436 | return ret; | |
437 | ||
8a6c5b26 | 438 | thread__comm_adjust(self, hists); |
d599db3f ACM |
439 | |
440 | return 0; | |
441 | } | |
442 | ||
640c03ce ACM |
443 | int event__process_comm(event_t *self, struct sample_data *sample __used, |
444 | struct perf_session *session) | |
62daacb5 | 445 | { |
13eb04fd | 446 | struct thread *thread = perf_session__findnew(session, self->comm.tid); |
62daacb5 | 447 | |
13eb04fd | 448 | dump_printf(": %s:%d\n", self->comm.comm, self->comm.tid); |
62daacb5 | 449 | |
8a6c5b26 ACM |
450 | if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm, |
451 | &session->hists)) { | |
62daacb5 ACM |
452 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
453 | return -1; | |
454 | } | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
640c03ce ACM |
459 | int event__process_lost(event_t *self, struct sample_data *sample __used, |
460 | struct perf_session *session) | |
62daacb5 | 461 | { |
9486aa38 ACM |
462 | dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n", |
463 | self->lost.id, self->lost.lost); | |
cee75ac7 | 464 | session->hists.stats.total_lost += self->lost.lost; |
62daacb5 ACM |
465 | return 0; |
466 | } | |
467 | ||
a1645ce1 ZY |
468 | static void event_set_kernel_mmap_len(struct map **maps, event_t *self) |
469 | { | |
470 | maps[MAP__FUNCTION]->start = self->mmap.start; | |
471 | maps[MAP__FUNCTION]->end = self->mmap.start + self->mmap.len; | |
472 | /* | |
473 | * Be a bit paranoid here, some perf.data file came with | |
474 | * a zero sized synthesized MMAP event for the kernel. | |
475 | */ | |
476 | if (maps[MAP__FUNCTION]->end == 0) | |
9d1faba5 | 477 | maps[MAP__FUNCTION]->end = ~0ULL; |
a1645ce1 ZY |
478 | } |
479 | ||
480 | static int event__process_kernel_mmap(event_t *self, | |
481 | struct perf_session *session) | |
62daacb5 | 482 | { |
56b03f3c | 483 | struct map *map; |
a1645ce1 | 484 | char kmmap_prefix[PATH_MAX]; |
23346f21 | 485 | struct machine *machine; |
a1645ce1 ZY |
486 | enum dso_kernel_type kernel_type; |
487 | bool is_kernel_mmap; | |
488 | ||
23346f21 ACM |
489 | machine = perf_session__findnew_machine(session, self->mmap.pid); |
490 | if (!machine) { | |
491 | pr_err("Can't find id %d's machine\n", self->mmap.pid); | |
a1645ce1 ZY |
492 | goto out_problem; |
493 | } | |
62daacb5 | 494 | |
48ea8f54 | 495 | machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix)); |
23346f21 | 496 | if (machine__is_host(machine)) |
a1645ce1 ZY |
497 | kernel_type = DSO_TYPE_KERNEL; |
498 | else | |
499 | kernel_type = DSO_TYPE_GUEST_KERNEL; | |
62daacb5 | 500 | |
a1645ce1 ZY |
501 | is_kernel_mmap = memcmp(self->mmap.filename, |
502 | kmmap_prefix, | |
503 | strlen(kmmap_prefix)) == 0; | |
504 | if (self->mmap.filename[0] == '/' || | |
505 | (!is_kernel_mmap && self->mmap.filename[0] == '[')) { | |
b7cece76 | 506 | |
a1645ce1 ZY |
507 | char short_module_name[1024]; |
508 | char *name, *dot; | |
b7cece76 | 509 | |
a1645ce1 ZY |
510 | if (self->mmap.filename[0] == '/') { |
511 | name = strrchr(self->mmap.filename, '/'); | |
b7cece76 ACM |
512 | if (name == NULL) |
513 | goto out_problem; | |
514 | ||
515 | ++name; /* skip / */ | |
516 | dot = strrchr(name, '.'); | |
517 | if (dot == NULL) | |
518 | goto out_problem; | |
b7cece76 | 519 | snprintf(short_module_name, sizeof(short_module_name), |
a1645ce1 | 520 | "[%.*s]", (int)(dot - name), name); |
b7cece76 | 521 | strxfrchar(short_module_name, '-', '_'); |
a1645ce1 ZY |
522 | } else |
523 | strcpy(short_module_name, self->mmap.filename); | |
524 | ||
d28c6223 ACM |
525 | map = machine__new_module(machine, self->mmap.start, |
526 | self->mmap.filename); | |
a1645ce1 ZY |
527 | if (map == NULL) |
528 | goto out_problem; | |
529 | ||
530 | name = strdup(short_module_name); | |
531 | if (name == NULL) | |
532 | goto out_problem; | |
533 | ||
534 | map->dso->short_name = name; | |
6e406257 | 535 | map->dso->sname_alloc = 1; |
a1645ce1 ZY |
536 | map->end = map->start + self->mmap.len; |
537 | } else if (is_kernel_mmap) { | |
538 | const char *symbol_name = (self->mmap.filename + | |
539 | strlen(kmmap_prefix)); | |
540 | /* | |
541 | * Should be there already, from the build-id table in | |
542 | * the header. | |
543 | */ | |
23346f21 ACM |
544 | struct dso *kernel = __dsos__findnew(&machine->kernel_dsos, |
545 | kmmap_prefix); | |
a1645ce1 ZY |
546 | if (kernel == NULL) |
547 | goto out_problem; | |
548 | ||
549 | kernel->kernel = kernel_type; | |
d28c6223 | 550 | if (__machine__create_kernel_maps(machine, kernel) < 0) |
a1645ce1 ZY |
551 | goto out_problem; |
552 | ||
23346f21 ACM |
553 | event_set_kernel_mmap_len(machine->vmlinux_maps, self); |
554 | perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, | |
555 | symbol_name, | |
556 | self->mmap.pgoff); | |
557 | if (machine__is_default_guest(machine)) { | |
b7cece76 | 558 | /* |
a1645ce1 | 559 | * preload dso of guest kernel and modules |
b7cece76 | 560 | */ |
23346f21 ACM |
561 | dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION], |
562 | NULL); | |
a1645ce1 ZY |
563 | } |
564 | } | |
565 | return 0; | |
566 | out_problem: | |
567 | return -1; | |
568 | } | |
b7cece76 | 569 | |
640c03ce ACM |
570 | int event__process_mmap(event_t *self, struct sample_data *sample __used, |
571 | struct perf_session *session) | |
a1645ce1 | 572 | { |
23346f21 | 573 | struct machine *machine; |
a1645ce1 ZY |
574 | struct thread *thread; |
575 | struct map *map; | |
576 | u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; | |
577 | int ret = 0; | |
b7cece76 | 578 | |
9486aa38 | 579 | dump_printf(" %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n", |
a1645ce1 ZY |
580 | self->mmap.pid, self->mmap.tid, self->mmap.start, |
581 | self->mmap.len, self->mmap.pgoff, self->mmap.filename); | |
582 | ||
583 | if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL || | |
584 | cpumode == PERF_RECORD_MISC_KERNEL) { | |
585 | ret = event__process_kernel_mmap(self, session); | |
586 | if (ret < 0) | |
587 | goto out_problem; | |
56b03f3c ACM |
588 | return 0; |
589 | } | |
590 | ||
23346f21 | 591 | machine = perf_session__find_host_machine(session); |
4cc49458 ACM |
592 | if (machine == NULL) |
593 | goto out_problem; | |
594 | thread = perf_session__findnew(session, self->mmap.pid); | |
d65a458b ACM |
595 | if (thread == NULL) |
596 | goto out_problem; | |
23346f21 | 597 | map = map__new(&machine->user_dsos, self->mmap.start, |
a1645ce1 ZY |
598 | self->mmap.len, self->mmap.pgoff, |
599 | self->mmap.pid, self->mmap.filename, | |
361d1346 | 600 | MAP__FUNCTION); |
d65a458b | 601 | if (map == NULL) |
b7cece76 ACM |
602 | goto out_problem; |
603 | ||
604 | thread__insert_map(thread, map); | |
605 | return 0; | |
62daacb5 | 606 | |
b7cece76 ACM |
607 | out_problem: |
608 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); | |
62daacb5 ACM |
609 | return 0; |
610 | } | |
611 | ||
640c03ce ACM |
612 | int event__process_task(event_t *self, struct sample_data *sample __used, |
613 | struct perf_session *session) | |
62daacb5 | 614 | { |
dd833d71 FW |
615 | struct thread *thread = perf_session__findnew(session, self->fork.tid); |
616 | struct thread *parent = perf_session__findnew(session, self->fork.ptid); | |
62daacb5 ACM |
617 | |
618 | dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid, | |
619 | self->fork.ppid, self->fork.ptid); | |
62daacb5 | 620 | |
720a3aeb ACM |
621 | if (self->header.type == PERF_RECORD_EXIT) { |
622 | perf_session__remove_thread(session, thread); | |
62daacb5 | 623 | return 0; |
720a3aeb | 624 | } |
62daacb5 ACM |
625 | |
626 | if (thread == NULL || parent == NULL || | |
627 | thread__fork(thread, parent) < 0) { | |
628 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); | |
629 | return -1; | |
630 | } | |
631 | ||
632 | return 0; | |
633 | } | |
1ed091c4 | 634 | |
640c03ce ACM |
635 | int event__process(event_t *event, struct sample_data *sample, |
636 | struct perf_session *session) | |
b83f920e SD |
637 | { |
638 | switch (event->header.type) { | |
639 | case PERF_RECORD_COMM: | |
640c03ce | 640 | event__process_comm(event, sample, session); |
b83f920e SD |
641 | break; |
642 | case PERF_RECORD_MMAP: | |
640c03ce | 643 | event__process_mmap(event, sample, session); |
b83f920e SD |
644 | break; |
645 | case PERF_RECORD_FORK: | |
646 | case PERF_RECORD_EXIT: | |
640c03ce | 647 | event__process_task(event, sample, session); |
b83f920e SD |
648 | break; |
649 | default: | |
650 | break; | |
651 | } | |
652 | ||
653 | return 0; | |
654 | } | |
655 | ||
59ee68ec ACM |
656 | void thread__find_addr_map(struct thread *self, |
657 | struct perf_session *session, u8 cpumode, | |
a1645ce1 | 658 | enum map_type type, pid_t pid, u64 addr, |
59ee68ec | 659 | struct addr_location *al) |
1ed091c4 | 660 | { |
9958e1f0 | 661 | struct map_groups *mg = &self->mg; |
23346f21 | 662 | struct machine *machine = NULL; |
1ed091c4 | 663 | |
9958e1f0 | 664 | al->thread = self; |
1ed091c4 | 665 | al->addr = addr; |
a1645ce1 ZY |
666 | al->cpumode = cpumode; |
667 | al->filtered = false; | |
1ed091c4 | 668 | |
a1645ce1 | 669 | if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { |
1ed091c4 | 670 | al->level = 'k'; |
23346f21 | 671 | machine = perf_session__find_host_machine(session); |
4cc49458 ACM |
672 | if (machine == NULL) { |
673 | al->map = NULL; | |
674 | return; | |
675 | } | |
23346f21 | 676 | mg = &machine->kmaps; |
a1645ce1 | 677 | } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { |
1ed091c4 | 678 | al->level = '.'; |
23346f21 | 679 | machine = perf_session__find_host_machine(session); |
a1645ce1 ZY |
680 | } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { |
681 | al->level = 'g'; | |
23346f21 | 682 | machine = perf_session__find_machine(session, pid); |
4cc49458 | 683 | if (machine == NULL) { |
a1645ce1 ZY |
684 | al->map = NULL; |
685 | return; | |
686 | } | |
23346f21 | 687 | mg = &machine->kmaps; |
a1645ce1 ZY |
688 | } else { |
689 | /* | |
690 | * 'u' means guest os user space. | |
691 | * TODO: We don't support guest user space. Might support late. | |
692 | */ | |
693 | if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) | |
694 | al->level = 'u'; | |
695 | else | |
696 | al->level = 'H'; | |
1ed091c4 | 697 | al->map = NULL; |
a1645ce1 ZY |
698 | |
699 | if ((cpumode == PERF_RECORD_MISC_GUEST_USER || | |
700 | cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && | |
701 | !perf_guest) | |
702 | al->filtered = true; | |
703 | if ((cpumode == PERF_RECORD_MISC_USER || | |
704 | cpumode == PERF_RECORD_MISC_KERNEL) && | |
705 | !perf_host) | |
706 | al->filtered = true; | |
707 | ||
1ed091c4 ACM |
708 | return; |
709 | } | |
710 | try_again: | |
9958e1f0 | 711 | al->map = map_groups__find(mg, type, al->addr); |
1ed091c4 ACM |
712 | if (al->map == NULL) { |
713 | /* | |
714 | * If this is outside of all known maps, and is a negative | |
715 | * address, try to look it up in the kernel dso, as it might be | |
716 | * a vsyscall or vdso (which executes in user-mode). | |
717 | * | |
718 | * XXX This is nasty, we should have a symbol list in the | |
719 | * "[vdso]" dso, but for now lets use the old trick of looking | |
720 | * in the whole kernel symbol list. | |
721 | */ | |
a1645ce1 | 722 | if ((long long)al->addr < 0 && |
23346f21 ACM |
723 | cpumode == PERF_RECORD_MISC_KERNEL && |
724 | machine && mg != &machine->kmaps) { | |
725 | mg = &machine->kmaps; | |
1ed091c4 ACM |
726 | goto try_again; |
727 | } | |
59ee68ec | 728 | } else |
1ed091c4 | 729 | al->addr = al->map->map_ip(al->map, al->addr); |
59ee68ec ACM |
730 | } |
731 | ||
732 | void thread__find_addr_location(struct thread *self, | |
733 | struct perf_session *session, u8 cpumode, | |
a1645ce1 | 734 | enum map_type type, pid_t pid, u64 addr, |
59ee68ec ACM |
735 | struct addr_location *al, |
736 | symbol_filter_t filter) | |
737 | { | |
a1645ce1 | 738 | thread__find_addr_map(self, session, cpumode, type, pid, addr, al); |
59ee68ec | 739 | if (al->map != NULL) |
9de89fe7 | 740 | al->sym = map__find_symbol(al->map, al->addr, filter); |
59ee68ec ACM |
741 | else |
742 | al->sym = NULL; | |
1ed091c4 ACM |
743 | } |
744 | ||
8a6c5b26 | 745 | static void dso__calc_col_width(struct dso *self, struct hists *hists) |
c410a338 ACM |
746 | { |
747 | if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep && | |
748 | (!symbol_conf.dso_list || | |
749 | strlist__has_entry(symbol_conf.dso_list, self->name))) { | |
8a6c5b26 ACM |
750 | u16 slen = dso__name_len(self); |
751 | hists__new_col_len(hists, HISTC_DSO, slen); | |
c410a338 ACM |
752 | } |
753 | ||
754 | self->slen_calculated = 1; | |
755 | } | |
756 | ||
b3165f41 | 757 | int event__preprocess_sample(const event_t *self, struct perf_session *session, |
41a37e20 ACM |
758 | struct addr_location *al, struct sample_data *data, |
759 | symbol_filter_t filter) | |
1ed091c4 ACM |
760 | { |
761 | u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; | |
640c03ce | 762 | struct thread *thread = perf_session__findnew(session, self->ip.pid); |
41a37e20 | 763 | |
1ed091c4 ACM |
764 | if (thread == NULL) |
765 | return -1; | |
766 | ||
c410a338 ACM |
767 | if (symbol_conf.comm_list && |
768 | !strlist__has_entry(symbol_conf.comm_list, thread->comm)) | |
769 | goto out_filtered; | |
770 | ||
1ed091c4 | 771 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
1f626bc3 ACM |
772 | /* |
773 | * Have we already created the kernel maps for the host machine? | |
774 | * | |
775 | * This should have happened earlier, when we processed the kernel MMAP | |
776 | * events, but for older perf.data files there was no such thing, so do | |
777 | * it now. | |
778 | */ | |
779 | if (cpumode == PERF_RECORD_MISC_KERNEL && | |
780 | session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL) | |
781 | machine__create_kernel_maps(&session->host_machine); | |
1ed091c4 | 782 | |
96415e4d | 783 | thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION, |
a1645ce1 | 784 | self->ip.pid, self->ip.ip, al); |
1ed091c4 ACM |
785 | dump_printf(" ...... dso: %s\n", |
786 | al->map ? al->map->dso->long_name : | |
787 | al->level == 'H' ? "[hypervisor]" : "<not found>"); | |
96415e4d | 788 | al->sym = NULL; |
f60f3593 | 789 | al->cpu = data->cpu; |
96415e4d ACM |
790 | |
791 | if (al->map) { | |
792 | if (symbol_conf.dso_list && | |
793 | (!al->map || !al->map->dso || | |
794 | !(strlist__has_entry(symbol_conf.dso_list, | |
795 | al->map->dso->short_name) || | |
796 | (al->map->dso->short_name != al->map->dso->long_name && | |
797 | strlist__has_entry(symbol_conf.dso_list, | |
798 | al->map->dso->long_name))))) | |
799 | goto out_filtered; | |
800 | /* | |
801 | * We have to do this here as we may have a dso with no symbol | |
802 | * hit that has a name longer than the ones with symbols | |
803 | * sampled. | |
804 | */ | |
805 | if (!sort_dso.elide && !al->map->dso->slen_calculated) | |
8a6c5b26 | 806 | dso__calc_col_width(al->map->dso, &session->hists); |
96415e4d ACM |
807 | |
808 | al->sym = map__find_symbol(al->map, al->addr, filter); | |
3ceb0d44 ACM |
809 | } else { |
810 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; | |
811 | ||
8a6c5b26 | 812 | if (hists__col_len(&session->hists, HISTC_DSO) < unresolved_col_width && |
3ceb0d44 ACM |
813 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
814 | !symbol_conf.dso_list) | |
8a6c5b26 ACM |
815 | hists__set_col_len(&session->hists, HISTC_DSO, |
816 | unresolved_col_width); | |
96415e4d | 817 | } |
c410a338 ACM |
818 | |
819 | if (symbol_conf.sym_list && al->sym && | |
820 | !strlist__has_entry(symbol_conf.sym_list, al->sym->name)) | |
821 | goto out_filtered; | |
822 | ||
c410a338 ACM |
823 | return 0; |
824 | ||
825 | out_filtered: | |
826 | al->filtered = true; | |
1ed091c4 ACM |
827 | return 0; |
828 | } | |
180f95e2 | 829 | |
9c90a61c ACM |
830 | static int event__parse_id_sample(const event_t *event, |
831 | struct perf_session *session, | |
832 | struct sample_data *sample) | |
833 | { | |
834 | const u64 *array; | |
835 | u64 type; | |
836 | ||
837 | sample->cpu = sample->pid = sample->tid = -1; | |
838 | sample->stream_id = sample->id = sample->time = -1ULL; | |
839 | ||
840 | if (!session->sample_id_all) | |
841 | return 0; | |
842 | ||
843 | array = event->sample.array; | |
844 | array += ((event->header.size - | |
845 | sizeof(event->header)) / sizeof(u64)) - 1; | |
846 | type = session->sample_type; | |
847 | ||
848 | if (type & PERF_SAMPLE_CPU) { | |
849 | u32 *p = (u32 *)array; | |
850 | sample->cpu = *p; | |
851 | array--; | |
852 | } | |
853 | ||
854 | if (type & PERF_SAMPLE_STREAM_ID) { | |
855 | sample->stream_id = *array; | |
856 | array--; | |
857 | } | |
858 | ||
859 | if (type & PERF_SAMPLE_ID) { | |
860 | sample->id = *array; | |
861 | array--; | |
862 | } | |
863 | ||
864 | if (type & PERF_SAMPLE_TIME) { | |
865 | sample->time = *array; | |
866 | array--; | |
867 | } | |
868 | ||
869 | if (type & PERF_SAMPLE_TID) { | |
870 | u32 *p = (u32 *)array; | |
871 | sample->pid = p[0]; | |
872 | sample->tid = p[1]; | |
873 | } | |
874 | ||
875 | return 0; | |
876 | } | |
877 | ||
878 | int event__parse_sample(const event_t *event, struct perf_session *session, | |
879 | struct sample_data *data) | |
180f95e2 | 880 | { |
9c90a61c ACM |
881 | const u64 *array; |
882 | u64 type; | |
883 | ||
884 | if (event->header.type != PERF_RECORD_SAMPLE) | |
885 | return event__parse_id_sample(event, session, data); | |
886 | ||
887 | array = event->sample.array; | |
888 | type = session->sample_type; | |
180f95e2 OH |
889 | |
890 | if (type & PERF_SAMPLE_IP) { | |
891 | data->ip = event->ip.ip; | |
892 | array++; | |
893 | } | |
894 | ||
895 | if (type & PERF_SAMPLE_TID) { | |
896 | u32 *p = (u32 *)array; | |
897 | data->pid = p[0]; | |
898 | data->tid = p[1]; | |
899 | array++; | |
900 | } | |
901 | ||
902 | if (type & PERF_SAMPLE_TIME) { | |
903 | data->time = *array; | |
904 | array++; | |
905 | } | |
906 | ||
907 | if (type & PERF_SAMPLE_ADDR) { | |
908 | data->addr = *array; | |
909 | array++; | |
910 | } | |
911 | ||
02bf60aa | 912 | data->id = -1ULL; |
180f95e2 OH |
913 | if (type & PERF_SAMPLE_ID) { |
914 | data->id = *array; | |
915 | array++; | |
916 | } | |
917 | ||
918 | if (type & PERF_SAMPLE_STREAM_ID) { | |
919 | data->stream_id = *array; | |
920 | array++; | |
921 | } | |
922 | ||
923 | if (type & PERF_SAMPLE_CPU) { | |
924 | u32 *p = (u32 *)array; | |
925 | data->cpu = *p; | |
926 | array++; | |
761844b9 SE |
927 | } else |
928 | data->cpu = -1; | |
180f95e2 OH |
929 | |
930 | if (type & PERF_SAMPLE_PERIOD) { | |
931 | data->period = *array; | |
932 | array++; | |
933 | } | |
934 | ||
935 | if (type & PERF_SAMPLE_READ) { | |
936 | pr_debug("PERF_SAMPLE_READ is unsuported for now\n"); | |
937 | return -1; | |
938 | } | |
939 | ||
940 | if (type & PERF_SAMPLE_CALLCHAIN) { | |
941 | data->callchain = (struct ip_callchain *)array; | |
942 | array += 1 + data->callchain->nr; | |
943 | } | |
944 | ||
945 | if (type & PERF_SAMPLE_RAW) { | |
946 | u32 *p = (u32 *)array; | |
947 | data->raw_size = *p; | |
948 | p++; | |
949 | data->raw_data = p; | |
950 | } | |
951 | ||
952 | return 0; | |
953 | } |