Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 1 | #include <linux/types.h> |
| 2 | #include "event.h" |
| 3 | #include "debug.h" |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 4 | #include "session.h" |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 5 | #include "sort.h" |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 6 | #include "string.h" |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 7 | #include "strlist.h" |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 8 | #include "thread.h" |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 9 | |
| 10 | static pid_t event__synthesize_comm(pid_t pid, int full, |
Arnaldo Carvalho de Melo | cf55311 | 2010-01-07 19:59:40 -0200 | [diff] [blame] | 11 | event__handler_t process, |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 12 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 13 | { |
| 14 | event_t ev; |
| 15 | char filename[PATH_MAX]; |
| 16 | char bf[BUFSIZ]; |
| 17 | FILE *fp; |
| 18 | size_t size = 0; |
| 19 | DIR *tasks; |
| 20 | struct dirent dirent, *next; |
| 21 | pid_t tgid = 0; |
| 22 | |
| 23 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); |
| 24 | |
| 25 | fp = fopen(filename, "r"); |
| 26 | if (fp == NULL) { |
| 27 | out_race: |
| 28 | /* |
| 29 | * We raced with a task exiting - just return: |
| 30 | */ |
| 31 | pr_debug("couldn't open %s\n", filename); |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 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) |
| 38 | goto out_failure; |
| 39 | |
| 40 | if (memcmp(bf, "Name:", 5) == 0) { |
| 41 | char *name = bf + 5; |
| 42 | while (*name && isspace(*name)) |
| 43 | ++name; |
| 44 | size = strlen(name) - 1; |
| 45 | memcpy(ev.comm.comm, name, size++); |
| 46 | } else if (memcmp(bf, "Tgid:", 5) == 0) { |
| 47 | char *tgids = bf + 5; |
| 48 | while (*tgids && isspace(*tgids)) |
| 49 | ++tgids; |
| 50 | tgid = ev.comm.pid = atoi(tgids); |
| 51 | } |
| 52 | } |
| 53 | |
| 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); |
| 57 | |
| 58 | if (!full) { |
| 59 | ev.comm.tid = pid; |
| 60 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 61 | process(&ev, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 62 | goto out_fclose; |
| 63 | } |
| 64 | |
| 65 | snprintf(filename, sizeof(filename), "/proc/%d/task", pid); |
| 66 | |
| 67 | tasks = opendir(filename); |
| 68 | if (tasks == NULL) |
| 69 | goto out_race; |
| 70 | |
| 71 | while (!readdir_r(tasks, &dirent, &next) && next) { |
| 72 | char *end; |
| 73 | pid = strtol(dirent.d_name, &end, 10); |
| 74 | if (*end) |
| 75 | continue; |
| 76 | |
| 77 | ev.comm.tid = pid; |
| 78 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 79 | process(&ev, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 80 | } |
| 81 | closedir(tasks); |
| 82 | |
| 83 | out_fclose: |
| 84 | fclose(fp); |
| 85 | return tgid; |
| 86 | |
| 87 | out_failure: |
| 88 | pr_warning("couldn't get COMM and pgid, malformed %s\n", filename); |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | static int event__synthesize_mmap_events(pid_t pid, pid_t tgid, |
Arnaldo Carvalho de Melo | cf55311 | 2010-01-07 19:59:40 -0200 | [diff] [blame] | 93 | event__handler_t process, |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 94 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 95 | { |
| 96 | char filename[PATH_MAX]; |
| 97 | FILE *fp; |
| 98 | |
| 99 | snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); |
| 100 | |
| 101 | fp = fopen(filename, "r"); |
| 102 | if (fp == NULL) { |
| 103 | /* |
| 104 | * We raced with a task exiting - just return: |
| 105 | */ |
| 106 | pr_debug("couldn't open %s\n", filename); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | while (1) { |
| 111 | char bf[BUFSIZ], *pbf = bf; |
| 112 | event_t ev = { |
Arnaldo Carvalho de Melo | 18c3daa | 2010-01-14 23:45:28 -0200 | [diff] [blame] | 113 | .header = { |
| 114 | .type = PERF_RECORD_MMAP, |
| 115 | .misc = 0, /* Just like the kernel, see kernel/perf_event.c __perf_event_mmap */ |
| 116 | }, |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 117 | }; |
| 118 | int n; |
| 119 | size_t size; |
| 120 | if (fgets(bf, sizeof(bf), fp) == NULL) |
| 121 | break; |
| 122 | |
| 123 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ |
| 124 | n = hex2u64(pbf, &ev.mmap.start); |
| 125 | if (n < 0) |
| 126 | continue; |
| 127 | pbf += n + 1; |
| 128 | n = hex2u64(pbf, &ev.mmap.len); |
| 129 | if (n < 0) |
| 130 | continue; |
| 131 | pbf += n + 3; |
| 132 | if (*pbf == 'x') { /* vm_exec */ |
| 133 | char *execname = strchr(bf, '/'); |
| 134 | |
| 135 | /* Catch VDSO */ |
| 136 | if (execname == NULL) |
| 137 | execname = strstr(bf, "[vdso]"); |
| 138 | |
| 139 | if (execname == NULL) |
| 140 | continue; |
| 141 | |
| 142 | size = strlen(execname); |
| 143 | execname[size - 1] = '\0'; /* Remove \n */ |
| 144 | memcpy(ev.mmap.filename, execname, size); |
| 145 | size = ALIGN(size, sizeof(u64)); |
| 146 | ev.mmap.len -= ev.mmap.start; |
| 147 | ev.mmap.header.size = (sizeof(ev.mmap) - |
| 148 | (sizeof(ev.mmap.filename) - size)); |
| 149 | ev.mmap.pid = tgid; |
| 150 | ev.mmap.tid = pid; |
| 151 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 152 | process(&ev, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | fclose(fp); |
| 157 | return 0; |
| 158 | } |
| 159 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 160 | int event__synthesize_modules(event__handler_t process, |
| 161 | struct perf_session *session) |
| 162 | { |
| 163 | struct rb_node *nd; |
| 164 | |
| 165 | for (nd = rb_first(&session->kmaps.maps[MAP__FUNCTION]); |
| 166 | nd; nd = rb_next(nd)) { |
| 167 | event_t ev; |
| 168 | size_t size; |
| 169 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 170 | |
| 171 | if (pos->dso->kernel) |
| 172 | continue; |
| 173 | |
| 174 | size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64)); |
| 175 | memset(&ev, 0, sizeof(ev)); |
Arnaldo Carvalho de Melo | 18c3daa | 2010-01-14 23:45:28 -0200 | [diff] [blame] | 176 | ev.mmap.header.misc = 1; /* kernel uses 0 for user space maps, see kernel/perf_event.c __perf_event_mmap */ |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 177 | ev.mmap.header.type = PERF_RECORD_MMAP; |
| 178 | ev.mmap.header.size = (sizeof(ev.mmap) - |
| 179 | (sizeof(ev.mmap.filename) - size)); |
| 180 | ev.mmap.start = pos->start; |
| 181 | ev.mmap.len = pos->end - pos->start; |
| 182 | |
| 183 | memcpy(ev.mmap.filename, pos->dso->long_name, |
| 184 | pos->dso->long_name_len + 1); |
| 185 | process(&ev, session); |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Arnaldo Carvalho de Melo | cf55311 | 2010-01-07 19:59:40 -0200 | [diff] [blame] | 191 | int event__synthesize_thread(pid_t pid, event__handler_t process, |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 192 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 193 | { |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 194 | pid_t tgid = event__synthesize_comm(pid, 1, process, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 195 | if (tgid == -1) |
| 196 | return -1; |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 197 | return event__synthesize_mmap_events(pid, tgid, process, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 198 | } |
| 199 | |
Arnaldo Carvalho de Melo | cf55311 | 2010-01-07 19:59:40 -0200 | [diff] [blame] | 200 | void event__synthesize_threads(event__handler_t process, |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 201 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 202 | { |
| 203 | DIR *proc; |
| 204 | struct dirent dirent, *next; |
| 205 | |
| 206 | proc = opendir("/proc"); |
| 207 | |
| 208 | while (!readdir_r(proc, &dirent, &next) && next) { |
| 209 | char *end; |
| 210 | pid_t pid = strtol(dirent.d_name, &end, 10); |
| 211 | |
| 212 | if (*end) /* only interested in proper numerical dirents */ |
| 213 | continue; |
| 214 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 215 | event__synthesize_thread(pid, process, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | closedir(proc); |
| 219 | } |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 220 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 221 | struct process_symbol_args { |
| 222 | const char *name; |
| 223 | u64 start; |
| 224 | }; |
| 225 | |
| 226 | static int find_symbol_cb(void *arg, const char *name, char type, u64 start) |
| 227 | { |
| 228 | struct process_symbol_args *args = arg; |
| 229 | |
Arnaldo Carvalho de Melo | 881516e | 2010-01-15 18:08:27 -0200 | [diff] [blame^] | 230 | /* |
| 231 | * Must be a function or at least an alias, as in PARISC64, where "_text" is |
| 232 | * an 'A' to the same address as "_stext". |
| 233 | */ |
| 234 | if (!(symbol_type__is_a(type, MAP__FUNCTION) || |
| 235 | type == 'A') || strcmp(name, args->name)) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 236 | return 0; |
| 237 | |
| 238 | args->start = start; |
| 239 | return 1; |
| 240 | } |
| 241 | |
Arnaldo Carvalho de Melo | cf55311 | 2010-01-07 19:59:40 -0200 | [diff] [blame] | 242 | int event__synthesize_kernel_mmap(event__handler_t process, |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 243 | struct perf_session *session, |
| 244 | const char *symbol_name) |
| 245 | { |
| 246 | size_t size; |
| 247 | event_t ev = { |
Arnaldo Carvalho de Melo | 18c3daa | 2010-01-14 23:45:28 -0200 | [diff] [blame] | 248 | .header = { |
| 249 | .type = PERF_RECORD_MMAP, |
| 250 | .misc = 1, /* kernel uses 0 for user space maps, see kernel/perf_event.c __perf_event_mmap */ |
| 251 | }, |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 252 | }; |
| 253 | /* |
| 254 | * We should get this from /sys/kernel/sections/.text, but till that is |
| 255 | * available use this, and after it is use this as a fallback for older |
| 256 | * kernels. |
| 257 | */ |
| 258 | struct process_symbol_args args = { .name = symbol_name, }; |
| 259 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 260 | if (kallsyms__parse("/proc/kallsyms", &args, find_symbol_cb) <= 0) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 261 | return -ENOENT; |
| 262 | |
| 263 | size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename), |
| 264 | "[kernel.kallsyms.%s]", symbol_name) + 1; |
| 265 | size = ALIGN(size, sizeof(u64)); |
| 266 | ev.mmap.header.size = (sizeof(ev.mmap) - (sizeof(ev.mmap.filename) - size)); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 267 | ev.mmap.pgoff = args.start; |
| 268 | ev.mmap.start = session->vmlinux_maps[MAP__FUNCTION]->start; |
| 269 | ev.mmap.len = session->vmlinux_maps[MAP__FUNCTION]->end - ev.mmap.start ; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 270 | |
| 271 | return process(&ev, session); |
| 272 | } |
| 273 | |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 274 | static void thread__comm_adjust(struct thread *self) |
| 275 | { |
| 276 | char *comm = self->comm; |
| 277 | |
| 278 | if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
| 279 | (!symbol_conf.comm_list || |
| 280 | strlist__has_entry(symbol_conf.comm_list, comm))) { |
| 281 | unsigned int slen = strlen(comm); |
| 282 | |
| 283 | if (slen > comms__col_width) { |
| 284 | comms__col_width = slen; |
| 285 | threads__col_width = slen + 6; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static int thread__set_comm_adjust(struct thread *self, const char *comm) |
| 291 | { |
| 292 | int ret = thread__set_comm(self, comm); |
| 293 | |
| 294 | if (ret) |
| 295 | return ret; |
| 296 | |
| 297 | thread__comm_adjust(self); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 302 | int event__process_comm(event_t *self, struct perf_session *session) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 303 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 304 | struct thread *thread = perf_session__findnew(session, self->comm.pid); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 305 | |
Li Zefan | bab81b6 | 2009-12-01 14:04:49 +0800 | [diff] [blame] | 306 | dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 307 | |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 308 | if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm)) { |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 309 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
Arnaldo Carvalho de Melo | f823e44 | 2009-12-14 15:06:01 -0200 | [diff] [blame] | 316 | int event__process_lost(event_t *self, struct perf_session *session) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 317 | { |
| 318 | dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost); |
Arnaldo Carvalho de Melo | f823e44 | 2009-12-14 15:06:01 -0200 | [diff] [blame] | 319 | session->events_stats.lost += self->lost.lost; |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 323 | int event__process_mmap(event_t *self, struct perf_session *session) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 324 | { |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 325 | struct thread *thread; |
| 326 | struct map *map; |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 327 | |
Arnaldo Carvalho de Melo | 0d75503 | 2010-01-14 12:23:09 -0200 | [diff] [blame] | 328 | dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n", |
| 329 | self->mmap.pid, self->mmap.tid, self->mmap.start, |
| 330 | self->mmap.len, self->mmap.pgoff, self->mmap.filename); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 331 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 332 | if (self->mmap.pid == 0) { |
| 333 | static const char kmmap_prefix[] = "[kernel.kallsyms."; |
| 334 | |
| 335 | if (self->mmap.filename[0] == '/') { |
| 336 | char short_module_name[1024]; |
| 337 | char *name = strrchr(self->mmap.filename, '/'), *dot; |
| 338 | |
| 339 | if (name == NULL) |
| 340 | goto out_problem; |
| 341 | |
| 342 | ++name; /* skip / */ |
| 343 | dot = strrchr(name, '.'); |
| 344 | if (dot == NULL) |
| 345 | goto out_problem; |
| 346 | |
| 347 | snprintf(short_module_name, sizeof(short_module_name), |
| 348 | "[%.*s]", (int)(dot - name), name); |
| 349 | strxfrchar(short_module_name, '-', '_'); |
| 350 | |
| 351 | map = perf_session__new_module_map(session, |
| 352 | self->mmap.start, |
Arnaldo Carvalho de Melo | 460848f | 2010-01-15 13:17:51 -0200 | [diff] [blame] | 353 | self->mmap.filename); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 354 | if (map == NULL) |
| 355 | goto out_problem; |
| 356 | |
Arnaldo Carvalho de Melo | 460848f | 2010-01-15 13:17:51 -0200 | [diff] [blame] | 357 | name = strdup(short_module_name); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 358 | if (name == NULL) |
| 359 | goto out_problem; |
| 360 | |
Arnaldo Carvalho de Melo | 460848f | 2010-01-15 13:17:51 -0200 | [diff] [blame] | 361 | map->dso->short_name = name; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 362 | map->end = map->start + self->mmap.len; |
| 363 | } else if (memcmp(self->mmap.filename, kmmap_prefix, |
| 364 | sizeof(kmmap_prefix) - 1) == 0) { |
| 365 | const char *symbol_name = (self->mmap.filename + |
| 366 | sizeof(kmmap_prefix) - 1); |
| 367 | /* |
| 368 | * Should be there already, from the build-id table in |
| 369 | * the header. |
| 370 | */ |
| 371 | struct dso *kernel = __dsos__findnew(&dsos__kernel, |
| 372 | "[kernel.kallsyms]"); |
| 373 | if (kernel == NULL) |
| 374 | goto out_problem; |
| 375 | |
| 376 | if (__map_groups__create_kernel_maps(&session->kmaps, |
| 377 | session->vmlinux_maps, |
| 378 | kernel) < 0) |
| 379 | goto out_problem; |
| 380 | |
| 381 | session->vmlinux_maps[MAP__FUNCTION]->start = self->mmap.start; |
| 382 | session->vmlinux_maps[MAP__FUNCTION]->end = self->mmap.start + self->mmap.len; |
| 383 | |
| 384 | perf_session__set_kallsyms_ref_reloc_sym(session, symbol_name, |
| 385 | self->mmap.pgoff); |
| 386 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | thread = perf_session__findnew(session, self->mmap.pid); |
| 391 | map = map__new(&self->mmap, MAP__FUNCTION, |
| 392 | session->cwd, session->cwdlen); |
| 393 | |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 394 | if (thread == NULL || map == NULL) |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 395 | goto out_problem; |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 396 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 397 | thread__insert_map(thread, map); |
| 398 | return 0; |
| 399 | |
| 400 | out_problem: |
| 401 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 405 | int event__process_task(event_t *self, struct perf_session *session) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 406 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 407 | struct thread *thread = perf_session__findnew(session, self->fork.pid); |
| 408 | struct thread *parent = perf_session__findnew(session, self->fork.ppid); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 409 | |
| 410 | dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid, |
| 411 | self->fork.ppid, self->fork.ptid); |
| 412 | /* |
| 413 | * A thread clone will have the same PID for both parent and child. |
| 414 | */ |
| 415 | if (thread == parent) |
| 416 | return 0; |
| 417 | |
| 418 | if (self->header.type == PERF_RECORD_EXIT) |
| 419 | return 0; |
| 420 | |
| 421 | if (thread == NULL || parent == NULL || |
| 422 | thread__fork(thread, parent) < 0) { |
| 423 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); |
| 424 | return -1; |
| 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | } |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 429 | |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 430 | void thread__find_addr_map(struct thread *self, |
| 431 | struct perf_session *session, u8 cpumode, |
| 432 | enum map_type type, u64 addr, |
| 433 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 434 | { |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 435 | struct map_groups *mg = &self->mg; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 436 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 437 | al->thread = self; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 438 | al->addr = addr; |
| 439 | |
| 440 | if (cpumode & PERF_RECORD_MISC_KERNEL) { |
| 441 | al->level = 'k'; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 442 | mg = &session->kmaps; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 443 | } else if (cpumode & PERF_RECORD_MISC_USER) |
| 444 | al->level = '.'; |
| 445 | else { |
| 446 | al->level = 'H'; |
| 447 | al->map = NULL; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | try_again: |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 451 | al->map = map_groups__find(mg, type, al->addr); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 452 | if (al->map == NULL) { |
| 453 | /* |
| 454 | * If this is outside of all known maps, and is a negative |
| 455 | * address, try to look it up in the kernel dso, as it might be |
| 456 | * a vsyscall or vdso (which executes in user-mode). |
| 457 | * |
| 458 | * XXX This is nasty, we should have a symbol list in the |
| 459 | * "[vdso]" dso, but for now lets use the old trick of looking |
| 460 | * in the whole kernel symbol list. |
| 461 | */ |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 462 | if ((long long)al->addr < 0 && mg != &session->kmaps) { |
| 463 | mg = &session->kmaps; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 464 | goto try_again; |
| 465 | } |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 466 | } else |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 467 | al->addr = al->map->map_ip(al->map, al->addr); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | void thread__find_addr_location(struct thread *self, |
| 471 | struct perf_session *session, u8 cpumode, |
| 472 | enum map_type type, u64 addr, |
| 473 | struct addr_location *al, |
| 474 | symbol_filter_t filter) |
| 475 | { |
| 476 | thread__find_addr_map(self, session, cpumode, type, addr, al); |
| 477 | if (al->map != NULL) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 478 | al->sym = map__find_symbol(al->map, session, al->addr, filter); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 479 | else |
| 480 | al->sym = NULL; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 481 | } |
| 482 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 483 | static void dso__calc_col_width(struct dso *self) |
| 484 | { |
| 485 | if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
| 486 | (!symbol_conf.dso_list || |
| 487 | strlist__has_entry(symbol_conf.dso_list, self->name))) { |
| 488 | unsigned int slen = strlen(self->name); |
| 489 | if (slen > dsos__col_width) |
| 490 | dsos__col_width = slen; |
| 491 | } |
| 492 | |
| 493 | self->slen_calculated = 1; |
| 494 | } |
| 495 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 496 | int event__preprocess_sample(const event_t *self, struct perf_session *session, |
| 497 | struct addr_location *al, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 498 | { |
| 499 | u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 500 | struct thread *thread = perf_session__findnew(session, self->ip.pid); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 501 | |
| 502 | if (thread == NULL) |
| 503 | return -1; |
| 504 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 505 | if (symbol_conf.comm_list && |
| 506 | !strlist__has_entry(symbol_conf.comm_list, thread->comm)) |
| 507 | goto out_filtered; |
| 508 | |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 509 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 510 | |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 511 | thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION, |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 512 | self->ip.ip, al, filter); |
| 513 | dump_printf(" ...... dso: %s\n", |
| 514 | al->map ? al->map->dso->long_name : |
| 515 | al->level == 'H' ? "[hypervisor]" : "<not found>"); |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 516 | /* |
| 517 | * We have to do this here as we may have a dso with no symbol hit that |
| 518 | * has a name longer than the ones with symbols sampled. |
| 519 | */ |
| 520 | if (al->map && !sort_dso.elide && !al->map->dso->slen_calculated) |
| 521 | dso__calc_col_width(al->map->dso); |
| 522 | |
| 523 | if (symbol_conf.dso_list && |
| 524 | (!al->map || !al->map->dso || |
| 525 | !(strlist__has_entry(symbol_conf.dso_list, al->map->dso->short_name) || |
| 526 | (al->map->dso->short_name != al->map->dso->long_name && |
| 527 | strlist__has_entry(symbol_conf.dso_list, al->map->dso->long_name))))) |
| 528 | goto out_filtered; |
| 529 | |
| 530 | if (symbol_conf.sym_list && al->sym && |
| 531 | !strlist__has_entry(symbol_conf.sym_list, al->sym->name)) |
| 532 | goto out_filtered; |
| 533 | |
| 534 | al->filtered = false; |
| 535 | return 0; |
| 536 | |
| 537 | out_filtered: |
| 538 | al->filtered = true; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 539 | return 0; |
| 540 | } |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 541 | |
| 542 | int event__parse_sample(event_t *event, u64 type, struct sample_data *data) |
| 543 | { |
| 544 | u64 *array = event->sample.array; |
| 545 | |
| 546 | if (type & PERF_SAMPLE_IP) { |
| 547 | data->ip = event->ip.ip; |
| 548 | array++; |
| 549 | } |
| 550 | |
| 551 | if (type & PERF_SAMPLE_TID) { |
| 552 | u32 *p = (u32 *)array; |
| 553 | data->pid = p[0]; |
| 554 | data->tid = p[1]; |
| 555 | array++; |
| 556 | } |
| 557 | |
| 558 | if (type & PERF_SAMPLE_TIME) { |
| 559 | data->time = *array; |
| 560 | array++; |
| 561 | } |
| 562 | |
| 563 | if (type & PERF_SAMPLE_ADDR) { |
| 564 | data->addr = *array; |
| 565 | array++; |
| 566 | } |
| 567 | |
| 568 | if (type & PERF_SAMPLE_ID) { |
| 569 | data->id = *array; |
| 570 | array++; |
| 571 | } |
| 572 | |
| 573 | if (type & PERF_SAMPLE_STREAM_ID) { |
| 574 | data->stream_id = *array; |
| 575 | array++; |
| 576 | } |
| 577 | |
| 578 | if (type & PERF_SAMPLE_CPU) { |
| 579 | u32 *p = (u32 *)array; |
| 580 | data->cpu = *p; |
| 581 | array++; |
| 582 | } |
| 583 | |
| 584 | if (type & PERF_SAMPLE_PERIOD) { |
| 585 | data->period = *array; |
| 586 | array++; |
| 587 | } |
| 588 | |
| 589 | if (type & PERF_SAMPLE_READ) { |
| 590 | pr_debug("PERF_SAMPLE_READ is unsuported for now\n"); |
| 591 | return -1; |
| 592 | } |
| 593 | |
| 594 | if (type & PERF_SAMPLE_CALLCHAIN) { |
| 595 | data->callchain = (struct ip_callchain *)array; |
| 596 | array += 1 + data->callchain->nr; |
| 597 | } |
| 598 | |
| 599 | if (type & PERF_SAMPLE_RAW) { |
| 600 | u32 *p = (u32 *)array; |
| 601 | data->raw_size = *p; |
| 602 | p++; |
| 603 | data->raw_data = p; |
| 604 | } |
| 605 | |
| 606 | return 0; |
| 607 | } |