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