1 #include <linux/types.h>
10 static pid_t
event__synthesize_comm(pid_t pid
, int full
,
11 event__handler_t process
,
12 struct perf_session
*session
)
15 char filename
[PATH_MAX
];
20 struct dirent dirent
, *next
;
23 snprintf(filename
, sizeof(filename
), "/proc/%d/status", pid
);
25 fp
= fopen(filename
, "r");
29 * We raced with a task exiting - just return:
31 pr_debug("couldn't open %s\n", filename
);
35 memset(&ev
.comm
, 0, sizeof(ev
.comm
));
36 while (!ev
.comm
.comm
[0] || !ev
.comm
.pid
) {
37 if (fgets(bf
, sizeof(bf
), fp
) == NULL
)
40 if (memcmp(bf
, "Name:", 5) == 0) {
42 while (*name
&& isspace(*name
))
44 size
= strlen(name
) - 1;
45 memcpy(ev
.comm
.comm
, name
, size
++);
46 } else if (memcmp(bf
, "Tgid:", 5) == 0) {
48 while (*tgids
&& isspace(*tgids
))
50 tgid
= ev
.comm
.pid
= atoi(tgids
);
54 ev
.comm
.header
.type
= PERF_RECORD_COMM
;
55 size
= ALIGN(size
, sizeof(u64
));
56 ev
.comm
.header
.size
= sizeof(ev
.comm
) - (sizeof(ev
.comm
.comm
) - size
);
61 process(&ev
, session
);
65 snprintf(filename
, sizeof(filename
), "/proc/%d/task", pid
);
67 tasks
= opendir(filename
);
71 while (!readdir_r(tasks
, &dirent
, &next
) && next
) {
73 pid
= strtol(dirent
.d_name
, &end
, 10);
79 process(&ev
, session
);
88 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename
);
92 static int event__synthesize_mmap_events(pid_t pid
, pid_t tgid
,
93 event__handler_t process
,
94 struct perf_session
*session
)
96 char filename
[PATH_MAX
];
99 snprintf(filename
, sizeof(filename
), "/proc/%d/maps", pid
);
101 fp
= fopen(filename
, "r");
104 * We raced with a task exiting - just return:
106 pr_debug("couldn't open %s\n", filename
);
111 char bf
[BUFSIZ
], *pbf
= bf
;
113 .header
= { .type
= PERF_RECORD_MMAP
},
117 if (fgets(bf
, sizeof(bf
), fp
) == NULL
)
120 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
121 n
= hex2u64(pbf
, &ev
.mmap
.start
);
125 n
= hex2u64(pbf
, &ev
.mmap
.len
);
129 if (*pbf
== 'x') { /* vm_exec */
130 char *execname
= strchr(bf
, '/');
133 if (execname
== NULL
)
134 execname
= strstr(bf
, "[vdso]");
136 if (execname
== NULL
)
139 size
= strlen(execname
);
140 execname
[size
- 1] = '\0'; /* Remove \n */
141 memcpy(ev
.mmap
.filename
, execname
, size
);
142 size
= ALIGN(size
, sizeof(u64
));
143 ev
.mmap
.len
-= ev
.mmap
.start
;
144 ev
.mmap
.header
.size
= (sizeof(ev
.mmap
) -
145 (sizeof(ev
.mmap
.filename
) - size
));
149 process(&ev
, session
);
157 int event__synthesize_modules(event__handler_t process
,
158 struct perf_session
*session
)
162 for (nd
= rb_first(&session
->kmaps
.maps
[MAP__FUNCTION
]);
163 nd
; nd
= rb_next(nd
)) {
166 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
168 if (pos
->dso
->kernel
)
171 size
= ALIGN(pos
->dso
->long_name_len
+ 1, sizeof(u64
));
172 memset(&ev
, 0, sizeof(ev
));
173 ev
.mmap
.header
.type
= PERF_RECORD_MMAP
;
174 ev
.mmap
.header
.size
= (sizeof(ev
.mmap
) -
175 (sizeof(ev
.mmap
.filename
) - size
));
176 ev
.mmap
.start
= pos
->start
;
177 ev
.mmap
.len
= pos
->end
- pos
->start
;
179 memcpy(ev
.mmap
.filename
, pos
->dso
->long_name
,
180 pos
->dso
->long_name_len
+ 1);
181 process(&ev
, session
);
187 int event__synthesize_thread(pid_t pid
, event__handler_t process
,
188 struct perf_session
*session
)
190 pid_t tgid
= event__synthesize_comm(pid
, 1, process
, session
);
193 return event__synthesize_mmap_events(pid
, tgid
, process
, session
);
196 void event__synthesize_threads(event__handler_t process
,
197 struct perf_session
*session
)
200 struct dirent dirent
, *next
;
202 proc
= opendir("/proc");
204 while (!readdir_r(proc
, &dirent
, &next
) && next
) {
206 pid_t pid
= strtol(dirent
.d_name
, &end
, 10);
208 if (*end
) /* only interested in proper numerical dirents */
211 event__synthesize_thread(pid
, process
, session
);
217 struct process_symbol_args
{
222 static int find_symbol_cb(void *arg
, const char *name
, char type
, u64 start
)
224 struct process_symbol_args
*args
= arg
;
226 if (!symbol_type__is_a(type
, MAP__FUNCTION
) || strcmp(name
, args
->name
))
233 int event__synthesize_kernel_mmap(event__handler_t process
,
234 struct perf_session
*session
,
235 const char *symbol_name
)
239 .header
= { .type
= PERF_RECORD_MMAP
},
242 * We should get this from /sys/kernel/sections/.text, but till that is
243 * available use this, and after it is use this as a fallback for older
246 struct process_symbol_args args
= { .name
= symbol_name
, };
248 if (kallsyms__parse(&args
, find_symbol_cb
) <= 0)
251 size
= snprintf(ev
.mmap
.filename
, sizeof(ev
.mmap
.filename
),
252 "[kernel.kallsyms.%s]", symbol_name
) + 1;
253 size
= ALIGN(size
, sizeof(u64
));
254 ev
.mmap
.header
.size
= (sizeof(ev
.mmap
) - (sizeof(ev
.mmap
.filename
) - size
));
255 ev
.mmap
.pgoff
= args
.start
;
256 ev
.mmap
.start
= session
->vmlinux_maps
[MAP__FUNCTION
]->start
;
257 ev
.mmap
.len
= session
->vmlinux_maps
[MAP__FUNCTION
]->end
- ev
.mmap
.start
;
259 return process(&ev
, session
);
262 static void thread__comm_adjust(struct thread
*self
)
264 char *comm
= self
->comm
;
266 if (!symbol_conf
.col_width_list_str
&& !symbol_conf
.field_sep
&&
267 (!symbol_conf
.comm_list
||
268 strlist__has_entry(symbol_conf
.comm_list
, comm
))) {
269 unsigned int slen
= strlen(comm
);
271 if (slen
> comms__col_width
) {
272 comms__col_width
= slen
;
273 threads__col_width
= slen
+ 6;
278 static int thread__set_comm_adjust(struct thread
*self
, const char *comm
)
280 int ret
= thread__set_comm(self
, comm
);
285 thread__comm_adjust(self
);
290 int event__process_comm(event_t
*self
, struct perf_session
*session
)
292 struct thread
*thread
= perf_session__findnew(session
, self
->comm
.pid
);
294 dump_printf(": %s:%d\n", self
->comm
.comm
, self
->comm
.pid
);
296 if (thread
== NULL
|| thread__set_comm_adjust(thread
, self
->comm
.comm
)) {
297 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
304 int event__process_lost(event_t
*self
, struct perf_session
*session
)
306 dump_printf(": id:%Ld: lost:%Ld\n", self
->lost
.id
, self
->lost
.lost
);
307 session
->events_stats
.lost
+= self
->lost
.lost
;
311 int event__process_mmap(event_t
*self
, struct perf_session
*session
)
313 struct thread
*thread
;
316 dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
317 self
->mmap
.pid
, self
->mmap
.tid
,
318 (void *)(long)self
->mmap
.start
,
319 (void *)(long)self
->mmap
.len
,
320 (void *)(long)self
->mmap
.pgoff
,
321 self
->mmap
.filename
);
323 if (self
->mmap
.pid
== 0) {
324 static const char kmmap_prefix
[] = "[kernel.kallsyms.";
326 if (self
->mmap
.filename
[0] == '/') {
327 char short_module_name
[1024];
328 char *name
= strrchr(self
->mmap
.filename
, '/'), *dot
;
334 dot
= strrchr(name
, '.');
338 snprintf(short_module_name
, sizeof(short_module_name
),
339 "[%.*s]", (int)(dot
- name
), name
);
340 strxfrchar(short_module_name
, '-', '_');
342 map
= perf_session__new_module_map(session
,
348 name
= strdup(self
->mmap
.filename
);
352 dso__set_long_name(map
->dso
, name
);
353 map
->end
= map
->start
+ self
->mmap
.len
;
354 } else if (memcmp(self
->mmap
.filename
, kmmap_prefix
,
355 sizeof(kmmap_prefix
) - 1) == 0) {
356 const char *symbol_name
= (self
->mmap
.filename
+
357 sizeof(kmmap_prefix
) - 1);
359 * Should be there already, from the build-id table in
362 struct dso
*kernel
= __dsos__findnew(&dsos__kernel
,
363 "[kernel.kallsyms]");
367 if (__map_groups__create_kernel_maps(&session
->kmaps
,
368 session
->vmlinux_maps
,
372 session
->vmlinux_maps
[MAP__FUNCTION
]->start
= self
->mmap
.start
;
373 session
->vmlinux_maps
[MAP__FUNCTION
]->end
= self
->mmap
.start
+ self
->mmap
.len
;
375 perf_session__set_kallsyms_ref_reloc_sym(session
, symbol_name
,
381 thread
= perf_session__findnew(session
, self
->mmap
.pid
);
382 map
= map__new(&self
->mmap
, MAP__FUNCTION
,
383 session
->cwd
, session
->cwdlen
);
385 if (thread
== NULL
|| map
== NULL
)
388 thread__insert_map(thread
, map
);
392 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
396 int event__process_task(event_t
*self
, struct perf_session
*session
)
398 struct thread
*thread
= perf_session__findnew(session
, self
->fork
.pid
);
399 struct thread
*parent
= perf_session__findnew(session
, self
->fork
.ppid
);
401 dump_printf("(%d:%d):(%d:%d)\n", self
->fork
.pid
, self
->fork
.tid
,
402 self
->fork
.ppid
, self
->fork
.ptid
);
404 * A thread clone will have the same PID for both parent and child.
406 if (thread
== parent
)
409 if (self
->header
.type
== PERF_RECORD_EXIT
)
412 if (thread
== NULL
|| parent
== NULL
||
413 thread__fork(thread
, parent
) < 0) {
414 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
421 void thread__find_addr_location(struct thread
*self
,
422 struct perf_session
*session
, u8 cpumode
,
423 enum map_type type
, u64 addr
,
424 struct addr_location
*al
,
425 symbol_filter_t filter
)
427 struct map_groups
*mg
= &self
->mg
;
432 if (cpumode
& PERF_RECORD_MISC_KERNEL
) {
434 mg
= &session
->kmaps
;
435 } else if (cpumode
& PERF_RECORD_MISC_USER
)
444 al
->map
= map_groups__find(mg
, type
, al
->addr
);
445 if (al
->map
== NULL
) {
447 * If this is outside of all known maps, and is a negative
448 * address, try to look it up in the kernel dso, as it might be
449 * a vsyscall or vdso (which executes in user-mode).
451 * XXX This is nasty, we should have a symbol list in the
452 * "[vdso]" dso, but for now lets use the old trick of looking
453 * in the whole kernel symbol list.
455 if ((long long)al
->addr
< 0 && mg
!= &session
->kmaps
) {
456 mg
= &session
->kmaps
;
461 al
->addr
= al
->map
->map_ip(al
->map
, al
->addr
);
462 al
->sym
= map__find_symbol(al
->map
, session
, al
->addr
, filter
);
466 static void dso__calc_col_width(struct dso
*self
)
468 if (!symbol_conf
.col_width_list_str
&& !symbol_conf
.field_sep
&&
469 (!symbol_conf
.dso_list
||
470 strlist__has_entry(symbol_conf
.dso_list
, self
->name
))) {
471 unsigned int slen
= strlen(self
->name
);
472 if (slen
> dsos__col_width
)
473 dsos__col_width
= slen
;
476 self
->slen_calculated
= 1;
479 int event__preprocess_sample(const event_t
*self
, struct perf_session
*session
,
480 struct addr_location
*al
, symbol_filter_t filter
)
482 u8 cpumode
= self
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
483 struct thread
*thread
= perf_session__findnew(session
, self
->ip
.pid
);
488 if (symbol_conf
.comm_list
&&
489 !strlist__has_entry(symbol_conf
.comm_list
, thread
->comm
))
492 dump_printf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
494 thread__find_addr_location(thread
, session
, cpumode
, MAP__FUNCTION
,
495 self
->ip
.ip
, al
, filter
);
496 dump_printf(" ...... dso: %s\n",
497 al
->map
? al
->map
->dso
->long_name
:
498 al
->level
== 'H' ? "[hypervisor]" : "<not found>");
500 * We have to do this here as we may have a dso with no symbol hit that
501 * has a name longer than the ones with symbols sampled.
503 if (al
->map
&& !sort_dso
.elide
&& !al
->map
->dso
->slen_calculated
)
504 dso__calc_col_width(al
->map
->dso
);
506 if (symbol_conf
.dso_list
&&
507 (!al
->map
|| !al
->map
->dso
||
508 !(strlist__has_entry(symbol_conf
.dso_list
, al
->map
->dso
->short_name
) ||
509 (al
->map
->dso
->short_name
!= al
->map
->dso
->long_name
&&
510 strlist__has_entry(symbol_conf
.dso_list
, al
->map
->dso
->long_name
)))))
513 if (symbol_conf
.sym_list
&& al
->sym
&&
514 !strlist__has_entry(symbol_conf
.sym_list
, al
->sym
->name
))
517 al
->filtered
= false;
525 int event__parse_sample(event_t
*event
, u64 type
, struct sample_data
*data
)
527 u64
*array
= event
->sample
.array
;
529 if (type
& PERF_SAMPLE_IP
) {
530 data
->ip
= event
->ip
.ip
;
534 if (type
& PERF_SAMPLE_TID
) {
535 u32
*p
= (u32
*)array
;
541 if (type
& PERF_SAMPLE_TIME
) {
546 if (type
& PERF_SAMPLE_ADDR
) {
551 if (type
& PERF_SAMPLE_ID
) {
556 if (type
& PERF_SAMPLE_STREAM_ID
) {
557 data
->stream_id
= *array
;
561 if (type
& PERF_SAMPLE_CPU
) {
562 u32
*p
= (u32
*)array
;
567 if (type
& PERF_SAMPLE_PERIOD
) {
568 data
->period
= *array
;
572 if (type
& PERF_SAMPLE_READ
) {
573 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
577 if (type
& PERF_SAMPLE_CALLCHAIN
) {
578 data
->callchain
= (struct ip_callchain
*)array
;
579 array
+= 1 + data
->callchain
->nr
;
582 if (type
& PERF_SAMPLE_RAW
) {
583 u32
*p
= (u32
*)array
;