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 | |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 10 | const char *event__name[] = { |
| 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", |
| 25 | }; |
| 26 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 27 | static struct sample_data synth_sample = { |
| 28 | .pid = -1, |
| 29 | .tid = -1, |
| 30 | .time = -1, |
| 31 | .stream_id = -1, |
| 32 | .cpu = -1, |
| 33 | .period = 1, |
| 34 | }; |
| 35 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 36 | 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] | 37 | event__handler_t process, |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 38 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 39 | { |
| 40 | event_t ev; |
| 41 | char filename[PATH_MAX]; |
| 42 | char bf[BUFSIZ]; |
| 43 | FILE *fp; |
| 44 | size_t size = 0; |
| 45 | DIR *tasks; |
| 46 | struct dirent dirent, *next; |
| 47 | pid_t tgid = 0; |
| 48 | |
| 49 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); |
| 50 | |
| 51 | fp = fopen(filename, "r"); |
| 52 | if (fp == NULL) { |
| 53 | out_race: |
| 54 | /* |
| 55 | * We raced with a task exiting - just return: |
| 56 | */ |
| 57 | pr_debug("couldn't open %s\n", filename); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | memset(&ev.comm, 0, sizeof(ev.comm)); |
| 62 | while (!ev.comm.comm[0] || !ev.comm.pid) { |
| 63 | if (fgets(bf, sizeof(bf), fp) == NULL) |
| 64 | goto out_failure; |
| 65 | |
| 66 | if (memcmp(bf, "Name:", 5) == 0) { |
| 67 | char *name = bf + 5; |
| 68 | while (*name && isspace(*name)) |
| 69 | ++name; |
| 70 | size = strlen(name) - 1; |
| 71 | memcpy(ev.comm.comm, name, size++); |
| 72 | } else if (memcmp(bf, "Tgid:", 5) == 0) { |
| 73 | char *tgids = bf + 5; |
| 74 | while (*tgids && isspace(*tgids)) |
| 75 | ++tgids; |
| 76 | tgid = ev.comm.pid = atoi(tgids); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | ev.comm.header.type = PERF_RECORD_COMM; |
| 81 | size = ALIGN(size, sizeof(u64)); |
| 82 | ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size); |
| 83 | |
| 84 | if (!full) { |
| 85 | ev.comm.tid = pid; |
| 86 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 87 | process(&ev, &synth_sample, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 88 | goto out_fclose; |
| 89 | } |
| 90 | |
| 91 | snprintf(filename, sizeof(filename), "/proc/%d/task", pid); |
| 92 | |
| 93 | tasks = opendir(filename); |
| 94 | if (tasks == NULL) |
| 95 | goto out_race; |
| 96 | |
| 97 | while (!readdir_r(tasks, &dirent, &next) && next) { |
| 98 | char *end; |
| 99 | pid = strtol(dirent.d_name, &end, 10); |
| 100 | if (*end) |
| 101 | continue; |
| 102 | |
| 103 | ev.comm.tid = pid; |
| 104 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 105 | process(&ev, &synth_sample, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 106 | } |
| 107 | closedir(tasks); |
| 108 | |
| 109 | out_fclose: |
| 110 | fclose(fp); |
| 111 | return tgid; |
| 112 | |
| 113 | out_failure: |
| 114 | pr_warning("couldn't get COMM and pgid, malformed %s\n", filename); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | 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] | 119 | event__handler_t process, |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 120 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 121 | { |
| 122 | char filename[PATH_MAX]; |
| 123 | FILE *fp; |
| 124 | |
| 125 | snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); |
| 126 | |
| 127 | fp = fopen(filename, "r"); |
| 128 | if (fp == NULL) { |
| 129 | /* |
| 130 | * We raced with a task exiting - just return: |
| 131 | */ |
| 132 | pr_debug("couldn't open %s\n", filename); |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | while (1) { |
| 137 | char bf[BUFSIZ], *pbf = bf; |
| 138 | event_t ev = { |
Arnaldo Carvalho de Melo | 18c3daa | 2010-01-14 23:45:28 -0200 | [diff] [blame] | 139 | .header = { |
| 140 | .type = PERF_RECORD_MMAP, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 141 | /* |
| 142 | * Just like the kernel, see __perf_event_mmap |
| 143 | * in kernel/perf_event.c |
| 144 | */ |
| 145 | .misc = PERF_RECORD_MISC_USER, |
Arnaldo Carvalho de Melo | 18c3daa | 2010-01-14 23:45:28 -0200 | [diff] [blame] | 146 | }, |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 147 | }; |
| 148 | int n; |
| 149 | size_t size; |
| 150 | if (fgets(bf, sizeof(bf), fp) == NULL) |
| 151 | break; |
| 152 | |
| 153 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ |
| 154 | n = hex2u64(pbf, &ev.mmap.start); |
| 155 | if (n < 0) |
| 156 | continue; |
| 157 | pbf += n + 1; |
| 158 | n = hex2u64(pbf, &ev.mmap.len); |
| 159 | if (n < 0) |
| 160 | continue; |
| 161 | pbf += n + 3; |
| 162 | if (*pbf == 'x') { /* vm_exec */ |
| 163 | char *execname = strchr(bf, '/'); |
| 164 | |
| 165 | /* Catch VDSO */ |
| 166 | if (execname == NULL) |
| 167 | execname = strstr(bf, "[vdso]"); |
| 168 | |
| 169 | if (execname == NULL) |
| 170 | continue; |
| 171 | |
Anton Blanchard | 4af8b35 | 2010-04-03 22:53:31 +1100 | [diff] [blame] | 172 | pbf += 3; |
Dave Martin | b5a6325 | 2010-08-03 12:48:35 +0100 | [diff] [blame] | 173 | n = hex2u64(pbf, &ev.mmap.pgoff); |
Anton Blanchard | 4af8b35 | 2010-04-03 22:53:31 +1100 | [diff] [blame] | 174 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 175 | size = strlen(execname); |
| 176 | execname[size - 1] = '\0'; /* Remove \n */ |
| 177 | memcpy(ev.mmap.filename, execname, size); |
| 178 | size = ALIGN(size, sizeof(u64)); |
| 179 | ev.mmap.len -= ev.mmap.start; |
| 180 | ev.mmap.header.size = (sizeof(ev.mmap) - |
| 181 | (sizeof(ev.mmap.filename) - size)); |
| 182 | ev.mmap.pid = tgid; |
| 183 | ev.mmap.tid = pid; |
| 184 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 185 | process(&ev, &synth_sample, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | fclose(fp); |
| 190 | return 0; |
| 191 | } |
| 192 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 193 | int event__synthesize_modules(event__handler_t process, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 194 | struct perf_session *session, |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 195 | struct machine *machine) |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 196 | { |
| 197 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 198 | struct map_groups *kmaps = &machine->kmaps; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 199 | u16 misc; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 200 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 201 | /* |
| 202 | * kernel uses 0 for user space maps, see kernel/perf_event.c |
| 203 | * __perf_event_mmap |
| 204 | */ |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 205 | if (machine__is_host(machine)) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 206 | misc = PERF_RECORD_MISC_KERNEL; |
| 207 | else |
| 208 | misc = PERF_RECORD_MISC_GUEST_KERNEL; |
| 209 | |
| 210 | for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 211 | nd; nd = rb_next(nd)) { |
| 212 | event_t ev; |
| 213 | size_t size; |
| 214 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 215 | |
| 216 | if (pos->dso->kernel) |
| 217 | continue; |
| 218 | |
| 219 | size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64)); |
| 220 | memset(&ev, 0, sizeof(ev)); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 221 | ev.mmap.header.misc = misc; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 222 | ev.mmap.header.type = PERF_RECORD_MMAP; |
| 223 | ev.mmap.header.size = (sizeof(ev.mmap) - |
| 224 | (sizeof(ev.mmap.filename) - size)); |
| 225 | ev.mmap.start = pos->start; |
| 226 | ev.mmap.len = pos->end - pos->start; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 227 | ev.mmap.pid = machine->pid; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 228 | |
| 229 | memcpy(ev.mmap.filename, pos->dso->long_name, |
| 230 | pos->dso->long_name_len + 1); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 231 | process(&ev, &synth_sample, session); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Arnaldo Carvalho de Melo | cf55311 | 2010-01-07 19:59:40 -0200 | [diff] [blame] | 237 | 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] | 238 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 239 | { |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 240 | pid_t tgid = event__synthesize_comm(pid, 1, process, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 241 | if (tgid == -1) |
| 242 | return -1; |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 243 | return event__synthesize_mmap_events(pid, tgid, process, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 244 | } |
| 245 | |
Arnaldo Carvalho de Melo | cf55311 | 2010-01-07 19:59:40 -0200 | [diff] [blame] | 246 | void event__synthesize_threads(event__handler_t process, |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 247 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 248 | { |
| 249 | DIR *proc; |
| 250 | struct dirent dirent, *next; |
| 251 | |
| 252 | proc = opendir("/proc"); |
| 253 | |
| 254 | while (!readdir_r(proc, &dirent, &next) && next) { |
| 255 | char *end; |
| 256 | pid_t pid = strtol(dirent.d_name, &end, 10); |
| 257 | |
| 258 | if (*end) /* only interested in proper numerical dirents */ |
| 259 | continue; |
| 260 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 261 | event__synthesize_thread(pid, process, session); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | closedir(proc); |
| 265 | } |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 266 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 267 | struct process_symbol_args { |
| 268 | const char *name; |
| 269 | u64 start; |
| 270 | }; |
| 271 | |
| 272 | static int find_symbol_cb(void *arg, const char *name, char type, u64 start) |
| 273 | { |
| 274 | struct process_symbol_args *args = arg; |
| 275 | |
Arnaldo Carvalho de Melo | 881516e | 2010-01-15 18:08:27 -0200 | [diff] [blame] | 276 | /* |
| 277 | * Must be a function or at least an alias, as in PARISC64, where "_text" is |
| 278 | * an 'A' to the same address as "_stext". |
| 279 | */ |
| 280 | if (!(symbol_type__is_a(type, MAP__FUNCTION) || |
| 281 | type == 'A') || strcmp(name, args->name)) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 282 | return 0; |
| 283 | |
| 284 | args->start = start; |
| 285 | return 1; |
| 286 | } |
| 287 | |
Arnaldo Carvalho de Melo | cf55311 | 2010-01-07 19:59:40 -0200 | [diff] [blame] | 288 | int event__synthesize_kernel_mmap(event__handler_t process, |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 289 | struct perf_session *session, |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 290 | struct machine *machine, |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 291 | const char *symbol_name) |
| 292 | { |
| 293 | size_t size; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 294 | const char *filename, *mmap_name; |
| 295 | char path[PATH_MAX]; |
| 296 | char name_buff[PATH_MAX]; |
| 297 | struct map *map; |
| 298 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 299 | event_t ev = { |
Arnaldo Carvalho de Melo | 18c3daa | 2010-01-14 23:45:28 -0200 | [diff] [blame] | 300 | .header = { |
| 301 | .type = PERF_RECORD_MMAP, |
Arnaldo Carvalho de Melo | 18c3daa | 2010-01-14 23:45:28 -0200 | [diff] [blame] | 302 | }, |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 303 | }; |
| 304 | /* |
| 305 | * We should get this from /sys/kernel/sections/.text, but till that is |
| 306 | * available use this, and after it is use this as a fallback for older |
| 307 | * kernels. |
| 308 | */ |
| 309 | struct process_symbol_args args = { .name = symbol_name, }; |
| 310 | |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 311 | mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff)); |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 312 | if (machine__is_host(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 313 | /* |
| 314 | * kernel uses PERF_RECORD_MISC_USER for user space maps, |
| 315 | * see kernel/perf_event.c __perf_event_mmap |
| 316 | */ |
| 317 | ev.header.misc = PERF_RECORD_MISC_KERNEL; |
| 318 | filename = "/proc/kallsyms"; |
| 319 | } else { |
| 320 | ev.header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 321 | if (machine__is_default_guest(machine)) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 322 | filename = (char *) symbol_conf.default_guest_kallsyms; |
| 323 | else { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 324 | sprintf(path, "%s/proc/kallsyms", machine->root_dir); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 325 | filename = path; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 330 | return -ENOENT; |
| 331 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 332 | map = machine->vmlinux_maps[MAP__FUNCTION]; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 333 | size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename), |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 334 | "%s%s", mmap_name, symbol_name) + 1; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 335 | size = ALIGN(size, sizeof(u64)); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 336 | ev.mmap.header.size = (sizeof(ev.mmap) - |
| 337 | (sizeof(ev.mmap.filename) - size)); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 338 | ev.mmap.pgoff = args.start; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 339 | ev.mmap.start = map->start; |
| 340 | ev.mmap.len = map->end - ev.mmap.start; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 341 | ev.mmap.pid = machine->pid; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 342 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 343 | return process(&ev, &synth_sample, session); |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 344 | } |
| 345 | |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 346 | static void thread__comm_adjust(struct thread *self, struct hists *hists) |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 347 | { |
| 348 | char *comm = self->comm; |
| 349 | |
| 350 | if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
| 351 | (!symbol_conf.comm_list || |
| 352 | strlist__has_entry(symbol_conf.comm_list, comm))) { |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 353 | u16 slen = strlen(comm); |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 354 | |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 355 | if (hists__new_col_len(hists, HISTC_COMM, slen)) |
| 356 | hists__set_col_len(hists, HISTC_THREAD, slen + 6); |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 360 | static int thread__set_comm_adjust(struct thread *self, const char *comm, |
| 361 | struct hists *hists) |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 362 | { |
| 363 | int ret = thread__set_comm(self, comm); |
| 364 | |
| 365 | if (ret) |
| 366 | return ret; |
| 367 | |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 368 | thread__comm_adjust(self, hists); |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 373 | int event__process_comm(event_t *self, struct sample_data *sample __used, |
| 374 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 375 | { |
Frederic Weisbecker | 13eb04fd | 2010-05-31 23:38:44 +0200 | [diff] [blame] | 376 | struct thread *thread = perf_session__findnew(session, self->comm.tid); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 377 | |
Frederic Weisbecker | 13eb04fd | 2010-05-31 23:38:44 +0200 | [diff] [blame] | 378 | dump_printf(": %s:%d\n", self->comm.comm, self->comm.tid); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 379 | |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 380 | if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm, |
| 381 | &session->hists)) { |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 382 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 389 | int event__process_lost(event_t *self, struct sample_data *sample __used, |
| 390 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 391 | { |
| 392 | dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost); |
Arnaldo Carvalho de Melo | cee75ac | 2010-05-14 13:16:55 -0300 | [diff] [blame] | 393 | session->hists.stats.total_lost += self->lost.lost; |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 397 | static void event_set_kernel_mmap_len(struct map **maps, event_t *self) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 398 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 399 | maps[MAP__FUNCTION]->start = self->mmap.start; |
| 400 | maps[MAP__FUNCTION]->end = self->mmap.start + self->mmap.len; |
| 401 | /* |
| 402 | * Be a bit paranoid here, some perf.data file came with |
| 403 | * a zero sized synthesized MMAP event for the kernel. |
| 404 | */ |
| 405 | if (maps[MAP__FUNCTION]->end == 0) |
Ian Munsie | 9d1faba | 2010-11-25 15:12:53 +1100 | [diff] [blame] | 406 | maps[MAP__FUNCTION]->end = ~0ULL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static int event__process_kernel_mmap(event_t *self, |
| 410 | struct perf_session *session) |
| 411 | { |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 412 | struct map *map; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 413 | char kmmap_prefix[PATH_MAX]; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 414 | struct machine *machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 415 | enum dso_kernel_type kernel_type; |
| 416 | bool is_kernel_mmap; |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 417 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 418 | machine = perf_session__findnew_machine(session, self->mmap.pid); |
| 419 | if (!machine) { |
| 420 | pr_err("Can't find id %d's machine\n", self->mmap.pid); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 421 | goto out_problem; |
| 422 | } |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 423 | |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 424 | machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix)); |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 425 | if (machine__is_host(machine)) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 426 | kernel_type = DSO_TYPE_KERNEL; |
| 427 | else |
| 428 | kernel_type = DSO_TYPE_GUEST_KERNEL; |
| 429 | |
| 430 | is_kernel_mmap = memcmp(self->mmap.filename, |
| 431 | kmmap_prefix, |
| 432 | strlen(kmmap_prefix)) == 0; |
| 433 | if (self->mmap.filename[0] == '/' || |
| 434 | (!is_kernel_mmap && self->mmap.filename[0] == '[')) { |
| 435 | |
| 436 | char short_module_name[1024]; |
| 437 | char *name, *dot; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 438 | |
| 439 | if (self->mmap.filename[0] == '/') { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 440 | name = strrchr(self->mmap.filename, '/'); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 441 | if (name == NULL) |
| 442 | goto out_problem; |
| 443 | |
| 444 | ++name; /* skip / */ |
| 445 | dot = strrchr(name, '.'); |
| 446 | if (dot == NULL) |
| 447 | goto out_problem; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 448 | snprintf(short_module_name, sizeof(short_module_name), |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 449 | "[%.*s]", (int)(dot - name), name); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 450 | strxfrchar(short_module_name, '-', '_'); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 451 | } else |
| 452 | strcpy(short_module_name, self->mmap.filename); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 453 | |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 454 | map = machine__new_module(machine, self->mmap.start, |
| 455 | self->mmap.filename); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 456 | if (map == NULL) |
| 457 | goto out_problem; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 458 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 459 | name = strdup(short_module_name); |
| 460 | if (name == NULL) |
| 461 | goto out_problem; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 462 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 463 | map->dso->short_name = name; |
Arnaldo Carvalho de Melo | 6e40625 | 2010-07-29 15:11:30 -0300 | [diff] [blame] | 464 | map->dso->sname_alloc = 1; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 465 | map->end = map->start + self->mmap.len; |
| 466 | } else if (is_kernel_mmap) { |
| 467 | const char *symbol_name = (self->mmap.filename + |
| 468 | strlen(kmmap_prefix)); |
| 469 | /* |
| 470 | * Should be there already, from the build-id table in |
| 471 | * the header. |
| 472 | */ |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 473 | struct dso *kernel = __dsos__findnew(&machine->kernel_dsos, |
| 474 | kmmap_prefix); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 475 | if (kernel == NULL) |
| 476 | goto out_problem; |
| 477 | |
| 478 | kernel->kernel = kernel_type; |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 479 | if (__machine__create_kernel_maps(machine, kernel) < 0) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 480 | goto out_problem; |
| 481 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 482 | event_set_kernel_mmap_len(machine->vmlinux_maps, self); |
| 483 | perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, |
| 484 | symbol_name, |
| 485 | self->mmap.pgoff); |
| 486 | if (machine__is_default_guest(machine)) { |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 487 | /* |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 488 | * preload dso of guest kernel and modules |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 489 | */ |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 490 | dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION], |
| 491 | NULL); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 492 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 493 | } |
| 494 | return 0; |
| 495 | out_problem: |
| 496 | return -1; |
| 497 | } |
| 498 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 499 | int event__process_mmap(event_t *self, struct sample_data *sample __used, |
| 500 | struct perf_session *session) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 501 | { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 502 | struct machine *machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 503 | struct thread *thread; |
| 504 | struct map *map; |
| 505 | u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 506 | int ret = 0; |
| 507 | |
| 508 | dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n", |
| 509 | self->mmap.pid, self->mmap.tid, self->mmap.start, |
| 510 | self->mmap.len, self->mmap.pgoff, self->mmap.filename); |
| 511 | |
| 512 | if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
| 513 | cpumode == PERF_RECORD_MISC_KERNEL) { |
| 514 | ret = event__process_kernel_mmap(self, session); |
| 515 | if (ret < 0) |
| 516 | goto out_problem; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 520 | machine = perf_session__find_host_machine(session); |
Arnaldo Carvalho de Melo | 4cc4945 | 2010-05-09 21:14:07 -0300 | [diff] [blame] | 521 | if (machine == NULL) |
| 522 | goto out_problem; |
| 523 | thread = perf_session__findnew(session, self->mmap.pid); |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 524 | if (thread == NULL) |
| 525 | goto out_problem; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 526 | map = map__new(&machine->user_dsos, self->mmap.start, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 527 | self->mmap.len, self->mmap.pgoff, |
| 528 | self->mmap.pid, self->mmap.filename, |
Dave Martin | 361d134 | 2010-07-27 16:40:02 +0100 | [diff] [blame] | 529 | MAP__FUNCTION); |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 530 | if (map == NULL) |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 531 | goto out_problem; |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 532 | |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 533 | thread__insert_map(thread, map); |
| 534 | return 0; |
| 535 | |
| 536 | out_problem: |
| 537 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 541 | int event__process_task(event_t *self, struct sample_data *sample __used, |
| 542 | struct perf_session *session) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 543 | { |
Frederic Weisbecker | dd833d7 | 2010-06-01 00:43:07 +0200 | [diff] [blame] | 544 | struct thread *thread = perf_session__findnew(session, self->fork.tid); |
| 545 | struct thread *parent = perf_session__findnew(session, self->fork.ptid); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 546 | |
| 547 | dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid, |
| 548 | self->fork.ppid, self->fork.ptid); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 549 | |
Arnaldo Carvalho de Melo | 720a3ae | 2010-06-17 08:37:44 -0300 | [diff] [blame] | 550 | if (self->header.type == PERF_RECORD_EXIT) { |
| 551 | perf_session__remove_thread(session, thread); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 552 | return 0; |
Arnaldo Carvalho de Melo | 720a3ae | 2010-06-17 08:37:44 -0300 | [diff] [blame] | 553 | } |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 554 | |
| 555 | if (thread == NULL || parent == NULL || |
| 556 | thread__fork(thread, parent) < 0) { |
| 557 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); |
| 558 | return -1; |
| 559 | } |
| 560 | |
| 561 | return 0; |
| 562 | } |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 563 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 564 | int event__process(event_t *event, struct sample_data *sample, |
| 565 | struct perf_session *session) |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 566 | { |
| 567 | switch (event->header.type) { |
| 568 | case PERF_RECORD_COMM: |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 569 | event__process_comm(event, sample, session); |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 570 | break; |
| 571 | case PERF_RECORD_MMAP: |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 572 | event__process_mmap(event, sample, session); |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 573 | break; |
| 574 | case PERF_RECORD_FORK: |
| 575 | case PERF_RECORD_EXIT: |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 576 | event__process_task(event, sample, session); |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 577 | break; |
| 578 | default: |
| 579 | break; |
| 580 | } |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 585 | void thread__find_addr_map(struct thread *self, |
| 586 | struct perf_session *session, u8 cpumode, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 587 | enum map_type type, pid_t pid, u64 addr, |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 588 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 589 | { |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 590 | struct map_groups *mg = &self->mg; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 591 | struct machine *machine = NULL; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 592 | |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 593 | al->thread = self; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 594 | al->addr = addr; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 595 | al->cpumode = cpumode; |
| 596 | al->filtered = false; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 597 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 598 | if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 599 | al->level = 'k'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 600 | machine = perf_session__find_host_machine(session); |
Arnaldo Carvalho de Melo | 4cc4945 | 2010-05-09 21:14:07 -0300 | [diff] [blame] | 601 | if (machine == NULL) { |
| 602 | al->map = NULL; |
| 603 | return; |
| 604 | } |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 605 | mg = &machine->kmaps; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 606 | } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 607 | al->level = '.'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 608 | machine = perf_session__find_host_machine(session); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 609 | } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { |
| 610 | al->level = 'g'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 611 | machine = perf_session__find_machine(session, pid); |
Arnaldo Carvalho de Melo | 4cc4945 | 2010-05-09 21:14:07 -0300 | [diff] [blame] | 612 | if (machine == NULL) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 613 | al->map = NULL; |
| 614 | return; |
| 615 | } |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 616 | mg = &machine->kmaps; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 617 | } else { |
| 618 | /* |
| 619 | * 'u' means guest os user space. |
| 620 | * TODO: We don't support guest user space. Might support late. |
| 621 | */ |
| 622 | if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) |
| 623 | al->level = 'u'; |
| 624 | else |
| 625 | al->level = 'H'; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 626 | al->map = NULL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 627 | |
| 628 | if ((cpumode == PERF_RECORD_MISC_GUEST_USER || |
| 629 | cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && |
| 630 | !perf_guest) |
| 631 | al->filtered = true; |
| 632 | if ((cpumode == PERF_RECORD_MISC_USER || |
| 633 | cpumode == PERF_RECORD_MISC_KERNEL) && |
| 634 | !perf_host) |
| 635 | al->filtered = true; |
| 636 | |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 637 | return; |
| 638 | } |
| 639 | try_again: |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 640 | al->map = map_groups__find(mg, type, al->addr); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 641 | if (al->map == NULL) { |
| 642 | /* |
| 643 | * If this is outside of all known maps, and is a negative |
| 644 | * address, try to look it up in the kernel dso, as it might be |
| 645 | * a vsyscall or vdso (which executes in user-mode). |
| 646 | * |
| 647 | * XXX This is nasty, we should have a symbol list in the |
| 648 | * "[vdso]" dso, but for now lets use the old trick of looking |
| 649 | * in the whole kernel symbol list. |
| 650 | */ |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 651 | if ((long long)al->addr < 0 && |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 652 | cpumode == PERF_RECORD_MISC_KERNEL && |
| 653 | machine && mg != &machine->kmaps) { |
| 654 | mg = &machine->kmaps; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 655 | goto try_again; |
| 656 | } |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 657 | } else |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 658 | al->addr = al->map->map_ip(al->map, al->addr); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | void thread__find_addr_location(struct thread *self, |
| 662 | struct perf_session *session, u8 cpumode, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 663 | enum map_type type, pid_t pid, u64 addr, |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 664 | struct addr_location *al, |
| 665 | symbol_filter_t filter) |
| 666 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 667 | thread__find_addr_map(self, session, cpumode, type, pid, addr, al); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 668 | if (al->map != NULL) |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 669 | al->sym = map__find_symbol(al->map, al->addr, filter); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 670 | else |
| 671 | al->sym = NULL; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 672 | } |
| 673 | |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 674 | static void dso__calc_col_width(struct dso *self, struct hists *hists) |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 675 | { |
| 676 | if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
| 677 | (!symbol_conf.dso_list || |
| 678 | strlist__has_entry(symbol_conf.dso_list, self->name))) { |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 679 | u16 slen = dso__name_len(self); |
| 680 | hists__new_col_len(hists, HISTC_DSO, slen); |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | self->slen_calculated = 1; |
| 684 | } |
| 685 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 686 | int event__preprocess_sample(const event_t *self, struct perf_session *session, |
Arnaldo Carvalho de Melo | 41a37e2 | 2010-06-04 08:02:07 -0300 | [diff] [blame] | 687 | struct addr_location *al, struct sample_data *data, |
| 688 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 689 | { |
| 690 | u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame^] | 691 | struct thread *thread = perf_session__findnew(session, self->ip.pid); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 692 | |
| 693 | if (thread == NULL) |
| 694 | return -1; |
| 695 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 696 | if (symbol_conf.comm_list && |
| 697 | !strlist__has_entry(symbol_conf.comm_list, thread->comm)) |
| 698 | goto out_filtered; |
| 699 | |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 700 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 701 | /* |
| 702 | * Have we already created the kernel maps for the host machine? |
| 703 | * |
| 704 | * This should have happened earlier, when we processed the kernel MMAP |
| 705 | * events, but for older perf.data files there was no such thing, so do |
| 706 | * it now. |
| 707 | */ |
| 708 | if (cpumode == PERF_RECORD_MISC_KERNEL && |
| 709 | session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL) |
| 710 | machine__create_kernel_maps(&session->host_machine); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 711 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 712 | thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 713 | self->ip.pid, self->ip.ip, al); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 714 | dump_printf(" ...... dso: %s\n", |
| 715 | al->map ? al->map->dso->long_name : |
| 716 | al->level == 'H' ? "[hypervisor]" : "<not found>"); |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 717 | al->sym = NULL; |
Arun Sharma | f60f359 | 2010-06-04 11:27:10 -0300 | [diff] [blame] | 718 | al->cpu = data->cpu; |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 719 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 720 | if (al->map) { |
| 721 | if (symbol_conf.dso_list && |
| 722 | (!al->map || !al->map->dso || |
| 723 | !(strlist__has_entry(symbol_conf.dso_list, |
| 724 | al->map->dso->short_name) || |
| 725 | (al->map->dso->short_name != al->map->dso->long_name && |
| 726 | strlist__has_entry(symbol_conf.dso_list, |
| 727 | al->map->dso->long_name))))) |
| 728 | goto out_filtered; |
| 729 | /* |
| 730 | * We have to do this here as we may have a dso with no symbol |
| 731 | * hit that has a name longer than the ones with symbols |
| 732 | * sampled. |
| 733 | */ |
| 734 | if (!sort_dso.elide && !al->map->dso->slen_calculated) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 735 | dso__calc_col_width(al->map->dso, &session->hists); |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 736 | |
| 737 | al->sym = map__find_symbol(al->map, al->addr, filter); |
Arnaldo Carvalho de Melo | 3ceb0d4 | 2010-05-09 16:07:01 -0300 | [diff] [blame] | 738 | } else { |
| 739 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
| 740 | |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 741 | if (hists__col_len(&session->hists, HISTC_DSO) < unresolved_col_width && |
Arnaldo Carvalho de Melo | 3ceb0d4 | 2010-05-09 16:07:01 -0300 | [diff] [blame] | 742 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
| 743 | !symbol_conf.dso_list) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 744 | hists__set_col_len(&session->hists, HISTC_DSO, |
| 745 | unresolved_col_width); |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 746 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 747 | |
| 748 | if (symbol_conf.sym_list && al->sym && |
| 749 | !strlist__has_entry(symbol_conf.sym_list, al->sym->name)) |
| 750 | goto out_filtered; |
| 751 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 752 | return 0; |
| 753 | |
| 754 | out_filtered: |
| 755 | al->filtered = true; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 756 | return 0; |
| 757 | } |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 758 | |
Arnaldo Carvalho de Melo | 41a37e2 | 2010-06-04 08:02:07 -0300 | [diff] [blame] | 759 | int event__parse_sample(const event_t *event, u64 type, struct sample_data *data) |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 760 | { |
Arnaldo Carvalho de Melo | 41a37e2 | 2010-06-04 08:02:07 -0300 | [diff] [blame] | 761 | const u64 *array = event->sample.array; |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 762 | |
| 763 | if (type & PERF_SAMPLE_IP) { |
| 764 | data->ip = event->ip.ip; |
| 765 | array++; |
| 766 | } |
| 767 | |
| 768 | if (type & PERF_SAMPLE_TID) { |
| 769 | u32 *p = (u32 *)array; |
| 770 | data->pid = p[0]; |
| 771 | data->tid = p[1]; |
| 772 | array++; |
| 773 | } |
| 774 | |
| 775 | if (type & PERF_SAMPLE_TIME) { |
| 776 | data->time = *array; |
| 777 | array++; |
| 778 | } |
| 779 | |
| 780 | if (type & PERF_SAMPLE_ADDR) { |
| 781 | data->addr = *array; |
| 782 | array++; |
| 783 | } |
| 784 | |
Anton Blanchard | 02bf60a | 2010-05-04 21:19:15 +1000 | [diff] [blame] | 785 | data->id = -1ULL; |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 786 | if (type & PERF_SAMPLE_ID) { |
| 787 | data->id = *array; |
| 788 | array++; |
| 789 | } |
| 790 | |
| 791 | if (type & PERF_SAMPLE_STREAM_ID) { |
| 792 | data->stream_id = *array; |
| 793 | array++; |
| 794 | } |
| 795 | |
| 796 | if (type & PERF_SAMPLE_CPU) { |
| 797 | u32 *p = (u32 *)array; |
| 798 | data->cpu = *p; |
| 799 | array++; |
Stephane Eranian | 761844b | 2010-05-28 12:08:01 +0200 | [diff] [blame] | 800 | } else |
| 801 | data->cpu = -1; |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 802 | |
| 803 | if (type & PERF_SAMPLE_PERIOD) { |
| 804 | data->period = *array; |
| 805 | array++; |
| 806 | } |
| 807 | |
| 808 | if (type & PERF_SAMPLE_READ) { |
| 809 | pr_debug("PERF_SAMPLE_READ is unsuported for now\n"); |
| 810 | return -1; |
| 811 | } |
| 812 | |
| 813 | if (type & PERF_SAMPLE_CALLCHAIN) { |
| 814 | data->callchain = (struct ip_callchain *)array; |
| 815 | array += 1 + data->callchain->nr; |
| 816 | } |
| 817 | |
| 818 | if (type & PERF_SAMPLE_RAW) { |
| 819 | u32 *p = (u32 *)array; |
| 820 | data->raw_size = *p; |
| 821 | p++; |
| 822 | data->raw_data = p; |
| 823 | } |
| 824 | |
| 825 | return 0; |
| 826 | } |