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