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