1 #include <linux/types.h>
7 static pid_t event__synthesize_comm(pid_t pid, int full,
8 int (*process)(event_t *event))
11 char filename[PATH_MAX];
16 struct dirent dirent, *next;
19 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
21 fp = fopen(filename, "r");
25 * We raced with a task exiting - just return:
27 pr_debug("couldn't open %s\n", filename);
31 memset(&ev.comm, 0, sizeof(ev.comm));
32 while (!ev.comm.comm[0] || !ev.comm.pid) {
33 if (fgets(bf, sizeof(bf), fp) == NULL)
36 if (memcmp(bf, "Name:", 5) == 0) {
38 while (*name && isspace(*name))
40 size = strlen(name) - 1;
41 memcpy(ev.comm.comm, name, size++);
42 } else if (memcmp(bf, "Tgid:", 5) == 0) {
44 while (*tgids && isspace(*tgids))
46 tgid = ev.comm.pid = atoi(tgids);
50 ev.comm.header.type = PERF_RECORD_COMM;
51 size = ALIGN(size, sizeof(u64));
52 ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
61 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
63 tasks = opendir(filename);
67 while (!readdir_r(tasks, &dirent, &next) && next) {
69 pid = strtol(dirent.d_name, &end, 10);
84 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
88 static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
89 int (*process)(event_t *event))
91 char filename[PATH_MAX];
94 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
96 fp = fopen(filename, "r");
99 * We raced with a task exiting - just return:
101 pr_debug("couldn't open %s\n", filename);
106 char bf[BUFSIZ], *pbf = bf;
108 .header = { .type = PERF_RECORD_MMAP },
112 if (fgets(bf, sizeof(bf), fp) == NULL)
115 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
116 n = hex2u64(pbf, &ev.mmap.start);
120 n = hex2u64(pbf, &ev.mmap.len);
124 if (*pbf == 'x') { /* vm_exec */
125 char *execname = strchr(bf, '/');
128 if (execname == NULL)
129 execname = strstr(bf, "[vdso]");
131 if (execname == NULL)
134 size = strlen(execname);
135 execname[size - 1] = '\0'; /* Remove \n */
136 memcpy(ev.mmap.filename, execname, size);
137 size = ALIGN(size, sizeof(u64));
138 ev.mmap.len -= ev.mmap.start;
139 ev.mmap.header.size = (sizeof(ev.mmap) -
140 (sizeof(ev.mmap.filename) - size));
152 int event__synthesize_thread(pid_t pid, int (*process)(event_t *event))
154 pid_t tgid = event__synthesize_comm(pid, 1, process);
157 return event__synthesize_mmap_events(pid, tgid, process);
160 void event__synthesize_threads(int (*process)(event_t *event))
163 struct dirent dirent, *next;
165 proc = opendir("/proc");
167 while (!readdir_r(proc, &dirent, &next) && next) {
169 pid_t pid = strtol(dirent.d_name, &end, 10);
171 if (*end) /* only interested in proper numerical dirents */
174 event__synthesize_thread(pid, process);
183 struct events_stats event__stats;
185 int event__process_comm(event_t *self)
187 struct thread *thread = threads__findnew(self->comm.pid);
189 dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
191 if (thread == NULL || thread__set_comm(thread, self->comm.comm)) {
192 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
199 int event__process_lost(event_t *self)
201 dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
202 event__stats.lost += self->lost.lost;
206 int event__process_mmap(event_t *self)
208 struct thread *thread = threads__findnew(self->mmap.pid);
209 struct map *map = map__new(&self->mmap, MAP__FUNCTION,
210 event__cwd, event__cwdlen);
212 dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
213 self->mmap.pid, self->mmap.tid,
214 (void *)(long)self->mmap.start,
215 (void *)(long)self->mmap.len,
216 (void *)(long)self->mmap.pgoff,
217 self->mmap.filename);
219 if (thread == NULL || map == NULL)
220 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
222 thread__insert_map(thread, map);
227 int event__process_task(event_t *self)
229 struct thread *thread = threads__findnew(self->fork.pid);
230 struct thread *parent = threads__findnew(self->fork.ppid);
232 dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
233 self->fork.ppid, self->fork.ptid);
235 * A thread clone will have the same PID for both parent and child.
237 if (thread == parent)
240 if (self->header.type == PERF_RECORD_EXIT)
243 if (thread == NULL || parent == NULL ||
244 thread__fork(thread, parent) < 0) {
245 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
252 void thread__find_addr_location(struct thread *self, u8 cpumode,
253 enum map_type type, u64 addr,
254 struct addr_location *al,
255 symbol_filter_t filter)
257 struct thread *thread = al->thread = self;
261 if (cpumode & PERF_RECORD_MISC_KERNEL) {
264 } else if (cpumode & PERF_RECORD_MISC_USER)
273 al->map = thread__find_map(thread, type, al->addr);
274 if (al->map == NULL) {
276 * If this is outside of all known maps, and is a negative
277 * address, try to look it up in the kernel dso, as it might be
278 * a vsyscall or vdso (which executes in user-mode).
280 * XXX This is nasty, we should have a symbol list in the
281 * "[vdso]" dso, but for now lets use the old trick of looking
282 * in the whole kernel symbol list.
284 if ((long long)al->addr < 0 && thread != kthread) {
290 al->addr = al->map->map_ip(al->map, al->addr);
291 al->sym = map__find_symbol(al->map, al->addr, filter);
295 int event__preprocess_sample(const event_t *self, struct addr_location *al,
296 symbol_filter_t filter)
298 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
299 struct thread *thread = threads__findnew(self->ip.pid);
304 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
306 thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
307 self->ip.ip, al, filter);
308 dump_printf(" ...... dso: %s\n",
309 al->map ? al->map->dso->long_name :
310 al->level == 'H' ? "[hypervisor]" : "<not found>");
314 int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
316 u64 *array = event->sample.array;
318 if (type & PERF_SAMPLE_IP) {
319 data->ip = event->ip.ip;
323 if (type & PERF_SAMPLE_TID) {
324 u32 *p = (u32 *)array;
330 if (type & PERF_SAMPLE_TIME) {
335 if (type & PERF_SAMPLE_ADDR) {
340 if (type & PERF_SAMPLE_ID) {
345 if (type & PERF_SAMPLE_STREAM_ID) {
346 data->stream_id = *array;
350 if (type & PERF_SAMPLE_CPU) {
351 u32 *p = (u32 *)array;
356 if (type & PERF_SAMPLE_PERIOD) {
357 data->period = *array;
361 if (type & PERF_SAMPLE_READ) {
362 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
366 if (type & PERF_SAMPLE_CALLCHAIN) {
367 data->callchain = (struct ip_callchain *)array;
368 array += 1 + data->callchain->nr;
371 if (type & PERF_SAMPLE_RAW) {
372 u32 *p = (u32 *)array;