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