1 #include <linux/types.h>
8 static pid_t event__synthesize_comm(pid_t pid, int full,
9 int (*process)(event_t *event,
10 struct perf_session *session),
11 struct perf_session *session)
14 char filename[PATH_MAX];
19 struct dirent dirent, *next;
22 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
24 fp = fopen(filename, "r");
28 * We raced with a task exiting - just return:
30 pr_debug("couldn't open %s\n", filename);
34 memset(&ev.comm, 0, sizeof(ev.comm));
35 while (!ev.comm.comm[0] || !ev.comm.pid) {
36 if (fgets(bf, sizeof(bf), fp) == NULL)
39 if (memcmp(bf, "Name:", 5) == 0) {
41 while (*name && isspace(*name))
43 size = strlen(name) - 1;
44 memcpy(ev.comm.comm, name, size++);
45 } else if (memcmp(bf, "Tgid:", 5) == 0) {
47 while (*tgids && isspace(*tgids))
49 tgid = ev.comm.pid = atoi(tgids);
53 ev.comm.header.type = PERF_RECORD_COMM;
54 size = ALIGN(size, sizeof(u64));
55 ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
60 process(&ev, session);
64 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
66 tasks = opendir(filename);
70 while (!readdir_r(tasks, &dirent, &next) && next) {
72 pid = strtol(dirent.d_name, &end, 10);
78 process(&ev, session);
87 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
91 static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
92 int (*process)(event_t *event,
93 struct perf_session *session),
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_thread(pid_t pid,
158 int (*process)(event_t *event,
159 struct perf_session *session),
160 struct perf_session *session)
162 pid_t tgid = event__synthesize_comm(pid, 1, process, session);
165 return event__synthesize_mmap_events(pid, tgid, process, session);
168 void event__synthesize_threads(int (*process)(event_t *event,
169 struct perf_session *session),
170 struct perf_session *session)
173 struct dirent dirent, *next;
175 proc = opendir("/proc");
177 while (!readdir_r(proc, &dirent, &next) && next) {
179 pid_t pid = strtol(dirent.d_name, &end, 10);
181 if (*end) /* only interested in proper numerical dirents */
184 event__synthesize_thread(pid, process, session);
190 struct events_stats event__stats;
192 int event__process_comm(event_t *self, struct perf_session *session)
194 struct thread *thread = perf_session__findnew(session, self->comm.pid);
196 dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
198 if (thread == NULL || thread__set_comm(thread, self->comm.comm)) {
199 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
206 int event__process_lost(event_t *self, struct perf_session *session __used)
208 dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
209 event__stats.lost += self->lost.lost;
213 int event__process_mmap(event_t *self, struct perf_session *session)
215 struct thread *thread = perf_session__findnew(session, self->mmap.pid);
216 struct map *map = map__new(&self->mmap, MAP__FUNCTION,
217 session->cwd, session->cwdlen);
219 dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
220 self->mmap.pid, self->mmap.tid,
221 (void *)(long)self->mmap.start,
222 (void *)(long)self->mmap.len,
223 (void *)(long)self->mmap.pgoff,
224 self->mmap.filename);
226 if (thread == NULL || map == NULL)
227 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
229 thread__insert_map(thread, map);
234 int event__process_task(event_t *self, struct perf_session *session)
236 struct thread *thread = perf_session__findnew(session, self->fork.pid);
237 struct thread *parent = perf_session__findnew(session, self->fork.ppid);
239 dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
240 self->fork.ppid, self->fork.ptid);
242 * A thread clone will have the same PID for both parent and child.
244 if (thread == parent)
247 if (self->header.type == PERF_RECORD_EXIT)
250 if (thread == NULL || parent == NULL ||
251 thread__fork(thread, parent) < 0) {
252 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
259 void thread__find_addr_location(struct thread *self,
260 struct perf_session *session, u8 cpumode,
261 enum map_type type, u64 addr,
262 struct addr_location *al,
263 symbol_filter_t filter)
265 struct map_groups *mg = &self->mg;
270 if (cpumode & PERF_RECORD_MISC_KERNEL) {
272 mg = &session->kmaps;
273 } else if (cpumode & PERF_RECORD_MISC_USER)
282 al->map = map_groups__find(mg, type, al->addr);
283 if (al->map == NULL) {
285 * If this is outside of all known maps, and is a negative
286 * address, try to look it up in the kernel dso, as it might be
287 * a vsyscall or vdso (which executes in user-mode).
289 * XXX This is nasty, we should have a symbol list in the
290 * "[vdso]" dso, but for now lets use the old trick of looking
291 * in the whole kernel symbol list.
293 if ((long long)al->addr < 0 && mg != &session->kmaps) {
294 mg = &session->kmaps;
299 al->addr = al->map->map_ip(al->map, al->addr);
300 al->sym = map__find_symbol(al->map, session, al->addr, filter);
304 int event__preprocess_sample(const event_t *self, struct perf_session *session,
305 struct addr_location *al, symbol_filter_t filter)
307 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
308 struct thread *thread = perf_session__findnew(session, self->ip.pid);
313 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
315 thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION,
316 self->ip.ip, al, filter);
317 dump_printf(" ...... dso: %s\n",
318 al->map ? al->map->dso->long_name :
319 al->level == 'H' ? "[hypervisor]" : "<not found>");
323 int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
325 u64 *array = event->sample.array;
327 if (type & PERF_SAMPLE_IP) {
328 data->ip = event->ip.ip;
332 if (type & PERF_SAMPLE_TID) {
333 u32 *p = (u32 *)array;
339 if (type & PERF_SAMPLE_TIME) {
344 if (type & PERF_SAMPLE_ADDR) {
349 if (type & PERF_SAMPLE_ID) {
354 if (type & PERF_SAMPLE_STREAM_ID) {
355 data->stream_id = *array;
359 if (type & PERF_SAMPLE_CPU) {
360 u32 *p = (u32 *)array;
365 if (type & PERF_SAMPLE_PERIOD) {
366 data->period = *array;
370 if (type & PERF_SAMPLE_READ) {
371 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
375 if (type & PERF_SAMPLE_CALLCHAIN) {
376 data->callchain = (struct ip_callchain *)array;
377 array += 1 + data->callchain->nr;
380 if (type & PERF_SAMPLE_RAW) {
381 u32 *p = (u32 *)array;