Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 1 | #include <linux/types.h> |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 2 | #include <sys/mman.h> |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 3 | #include "event.h" |
| 4 | #include "debug.h" |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 5 | #include "hist.h" |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 6 | #include "machine.h" |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 7 | #include "sort.h" |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 8 | #include "string.h" |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 9 | #include "strlist.h" |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 10 | #include "thread.h" |
Arnaldo Carvalho de Melo | 7c940c1 | 2011-02-11 11:45:54 -0200 | [diff] [blame] | 11 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | c506c96 | 2013-12-11 09:15:00 -0300 | [diff] [blame] | 12 | #include "symbol/kallsyms.h" |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 13 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 14 | static const char *perf_event__names[] = { |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 15 | [0] = "TOTAL", |
| 16 | [PERF_RECORD_MMAP] = "MMAP", |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 17 | [PERF_RECORD_MMAP2] = "MMAP2", |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 18 | [PERF_RECORD_LOST] = "LOST", |
| 19 | [PERF_RECORD_COMM] = "COMM", |
| 20 | [PERF_RECORD_EXIT] = "EXIT", |
| 21 | [PERF_RECORD_THROTTLE] = "THROTTLE", |
| 22 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", |
| 23 | [PERF_RECORD_FORK] = "FORK", |
| 24 | [PERF_RECORD_READ] = "READ", |
| 25 | [PERF_RECORD_SAMPLE] = "SAMPLE", |
| 26 | [PERF_RECORD_HEADER_ATTR] = "ATTR", |
| 27 | [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", |
| 28 | [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", |
| 29 | [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", |
| 30 | [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND", |
Adrian Hunter | 3c659ee | 2014-10-27 15:49:22 +0200 | [diff] [blame] | 31 | [PERF_RECORD_ID_INDEX] = "ID_INDEX", |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 32 | }; |
| 33 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 34 | const char *perf_event__name(unsigned int id) |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 35 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 36 | if (id >= ARRAY_SIZE(perf_event__names)) |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 37 | return "INVALID"; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 38 | if (!perf_event__names[id]) |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 39 | return "UNKNOWN"; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 40 | return perf_event__names[id]; |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 43 | static struct perf_sample synth_sample = { |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 44 | .pid = -1, |
| 45 | .tid = -1, |
| 46 | .time = -1, |
| 47 | .stream_id = -1, |
| 48 | .cpu = -1, |
| 49 | .period = 1, |
| 50 | }; |
| 51 | |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 52 | 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] | 53 | { |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 54 | char filename[PATH_MAX]; |
| 55 | char bf[BUFSIZ]; |
| 56 | FILE *fp; |
| 57 | size_t size = 0; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 58 | pid_t tgid = -1; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 59 | |
| 60 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); |
| 61 | |
| 62 | fp = fopen(filename, "r"); |
| 63 | if (fp == NULL) { |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 64 | pr_debug("couldn't open %s\n", filename); |
| 65 | return 0; |
| 66 | } |
| 67 | |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 68 | while (!comm[0] || (tgid < 0)) { |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 69 | if (fgets(bf, sizeof(bf), fp) == NULL) { |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 70 | pr_warning("couldn't get COMM and pgid, malformed %s\n", |
| 71 | filename); |
| 72 | break; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 73 | } |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 74 | |
| 75 | if (memcmp(bf, "Name:", 5) == 0) { |
| 76 | char *name = bf + 5; |
| 77 | while (*name && isspace(*name)) |
| 78 | ++name; |
| 79 | size = strlen(name) - 1; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 80 | if (size >= len) |
| 81 | size = len - 1; |
| 82 | memcpy(comm, name, size); |
David Ahern | cfbd70c | 2012-02-24 12:31:38 -0700 | [diff] [blame] | 83 | comm[size] = '\0'; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 84 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 85 | } else if (memcmp(bf, "Tgid:", 5) == 0) { |
| 86 | char *tgids = bf + 5; |
| 87 | while (*tgids && isspace(*tgids)) |
| 88 | ++tgids; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 89 | tgid = atoi(tgids); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 93 | fclose(fp); |
| 94 | |
| 95 | return tgid; |
| 96 | } |
| 97 | |
| 98 | static pid_t perf_event__synthesize_comm(struct perf_tool *tool, |
| 99 | union perf_event *event, pid_t pid, |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 100 | perf_event__handler_t process, |
| 101 | struct machine *machine) |
| 102 | { |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 103 | size_t size; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 104 | pid_t tgid; |
| 105 | |
| 106 | memset(&event->comm, 0, sizeof(event->comm)); |
| 107 | |
Dongsheng Yang | f5db57c4 | 2013-12-20 15:52:59 -0500 | [diff] [blame] | 108 | if (machine__is_host(machine)) |
| 109 | tgid = perf_event__get_comm_tgid(pid, event->comm.comm, |
| 110 | sizeof(event->comm.comm)); |
| 111 | else |
| 112 | tgid = machine->pid; |
| 113 | |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 114 | if (tgid < 0) |
| 115 | goto out; |
| 116 | |
| 117 | event->comm.pid = tgid; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 118 | event->comm.header.type = PERF_RECORD_COMM; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 119 | |
| 120 | size = strlen(event->comm.comm) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 121 | size = PERF_ALIGN(size, sizeof(u64)); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 122 | memset(event->comm.comm + size, 0, machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 123 | event->comm.header.size = (sizeof(event->comm) - |
| 124 | (sizeof(event->comm.comm) - size) + |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 125 | machine->id_hdr_size); |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 126 | event->comm.tid = pid; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 127 | |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 128 | if (process(tool, event, &synth_sample, machine) != 0) |
| 129 | return -1; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 130 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 131 | out: |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 132 | return tgid; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 133 | } |
| 134 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 135 | static int perf_event__synthesize_fork(struct perf_tool *tool, |
| 136 | union perf_event *event, pid_t pid, |
| 137 | pid_t tgid, perf_event__handler_t process, |
| 138 | struct machine *machine) |
| 139 | { |
| 140 | memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size); |
| 141 | |
| 142 | /* this is really a clone event but we use fork to synthesize it */ |
| 143 | event->fork.ppid = tgid; |
| 144 | event->fork.ptid = tgid; |
| 145 | event->fork.pid = tgid; |
| 146 | event->fork.tid = pid; |
| 147 | event->fork.header.type = PERF_RECORD_FORK; |
| 148 | |
| 149 | event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size); |
| 150 | |
| 151 | if (process(tool, event, &synth_sample, machine) != 0) |
| 152 | return -1; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Jiri Olsa | a18382b | 2014-01-07 13:47:20 +0100 | [diff] [blame] | 157 | int perf_event__synthesize_mmap_events(struct perf_tool *tool, |
| 158 | union perf_event *event, |
| 159 | pid_t pid, pid_t tgid, |
| 160 | perf_event__handler_t process, |
| 161 | struct machine *machine, |
| 162 | bool mmap_data) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 163 | { |
| 164 | char filename[PATH_MAX]; |
| 165 | FILE *fp; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 166 | int rc = 0; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 167 | |
Dongsheng Yang | c239c25a | 2013-12-20 15:53:00 -0500 | [diff] [blame] | 168 | if (machine__is_default_guest(machine)) |
| 169 | return 0; |
| 170 | |
Dongsheng Yang | 9956346 | 2013-12-20 15:52:57 -0500 | [diff] [blame] | 171 | snprintf(filename, sizeof(filename), "%s/proc/%d/maps", |
| 172 | machine->root_dir, pid); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 173 | |
| 174 | fp = fopen(filename, "r"); |
| 175 | if (fp == NULL) { |
| 176 | /* |
| 177 | * We raced with a task exiting - just return: |
| 178 | */ |
| 179 | pr_debug("couldn't open %s\n", filename); |
| 180 | return -1; |
| 181 | } |
| 182 | |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 183 | event->header.type = PERF_RECORD_MMAP2; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 184 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 185 | while (1) { |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 186 | char bf[BUFSIZ]; |
| 187 | char prot[5]; |
| 188 | char execname[PATH_MAX]; |
| 189 | char anonstr[] = "//anon"; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 190 | unsigned int ino; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 191 | size_t size; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 192 | ssize_t n; |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 193 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 194 | if (fgets(bf, sizeof(bf), fp) == NULL) |
| 195 | break; |
| 196 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 197 | /* ensure null termination since stack will be reused. */ |
| 198 | strcpy(execname, ""); |
| 199 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 200 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 201 | n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n", |
| 202 | &event->mmap2.start, &event->mmap2.len, prot, |
| 203 | &event->mmap2.pgoff, &event->mmap2.maj, |
| 204 | &event->mmap2.min, |
| 205 | &ino, execname); |
| 206 | |
Don Zickus | 9d4ecc8 | 2013-11-13 15:32:06 -0300 | [diff] [blame] | 207 | /* |
| 208 | * Anon maps don't have the execname. |
| 209 | */ |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 210 | if (n < 7) |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 211 | continue; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 212 | |
| 213 | event->mmap2.ino = (u64)ino; |
| 214 | |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 215 | /* |
| 216 | * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c |
| 217 | */ |
Dongsheng Yang | 73547aa | 2013-12-20 15:52:58 -0500 | [diff] [blame] | 218 | if (machine__is_host(machine)) |
| 219 | event->header.misc = PERF_RECORD_MISC_USER; |
| 220 | else |
| 221 | event->header.misc = PERF_RECORD_MISC_GUEST_USER; |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 222 | |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 223 | /* map protection and flags bits */ |
| 224 | event->mmap2.prot = 0; |
| 225 | event->mmap2.flags = 0; |
| 226 | if (prot[0] == 'r') |
| 227 | event->mmap2.prot |= PROT_READ; |
| 228 | if (prot[1] == 'w') |
| 229 | event->mmap2.prot |= PROT_WRITE; |
| 230 | if (prot[2] == 'x') |
| 231 | event->mmap2.prot |= PROT_EXEC; |
| 232 | |
| 233 | if (prot[3] == 's') |
| 234 | event->mmap2.flags |= MAP_SHARED; |
| 235 | else |
| 236 | event->mmap2.flags |= MAP_PRIVATE; |
| 237 | |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 238 | if (prot[2] != 'x') { |
| 239 | if (!mmap_data || prot[0] != 'r') |
| 240 | continue; |
| 241 | |
| 242 | event->header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 243 | } |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 244 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 245 | if (!strcmp(execname, "")) |
| 246 | strcpy(execname, anonstr); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 247 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 248 | size = strlen(execname) + 1; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 249 | memcpy(event->mmap2.filename, execname, size); |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 250 | size = PERF_ALIGN(size, sizeof(u64)); |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 251 | event->mmap2.len -= event->mmap.start; |
| 252 | event->mmap2.header.size = (sizeof(event->mmap2) - |
| 253 | (sizeof(event->mmap2.filename) - size)); |
| 254 | memset(event->mmap2.filename + size, 0, machine->id_hdr_size); |
| 255 | event->mmap2.header.size += machine->id_hdr_size; |
| 256 | event->mmap2.pid = tgid; |
| 257 | event->mmap2.tid = pid; |
Anton Blanchard | 6a0e55d | 2011-08-30 09:15:06 +1000 | [diff] [blame] | 258 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 259 | if (process(tool, event, &synth_sample, machine) != 0) { |
| 260 | rc = -1; |
| 261 | break; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | fclose(fp); |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 266 | return rc; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 267 | } |
| 268 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 269 | int perf_event__synthesize_modules(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 270 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 271 | struct machine *machine) |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 272 | { |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 273 | int rc = 0; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 274 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 275 | struct map_groups *kmaps = &machine->kmaps; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 276 | union perf_event *event = zalloc((sizeof(event->mmap) + |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 277 | machine->id_hdr_size)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 278 | if (event == NULL) { |
| 279 | pr_debug("Not enough memory synthesizing mmap event " |
| 280 | "for kernel modules\n"); |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | event->header.type = PERF_RECORD_MMAP; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 285 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 286 | /* |
| 287 | * kernel uses 0 for user space maps, see kernel/perf_event.c |
| 288 | * __perf_event_mmap |
| 289 | */ |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 290 | if (machine__is_host(machine)) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 291 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 292 | else |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 293 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 294 | |
| 295 | for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]); |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 296 | nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 297 | size_t size; |
| 298 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 299 | |
| 300 | if (pos->dso->kernel) |
| 301 | continue; |
| 302 | |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 303 | 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] | 304 | event->mmap.header.type = PERF_RECORD_MMAP; |
| 305 | event->mmap.header.size = (sizeof(event->mmap) - |
| 306 | (sizeof(event->mmap.filename) - size)); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 307 | memset(event->mmap.filename + size, 0, machine->id_hdr_size); |
| 308 | event->mmap.header.size += machine->id_hdr_size; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 309 | event->mmap.start = pos->start; |
| 310 | event->mmap.len = pos->end - pos->start; |
| 311 | event->mmap.pid = machine->pid; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 312 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 313 | memcpy(event->mmap.filename, pos->dso->long_name, |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 314 | pos->dso->long_name_len + 1); |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 315 | if (process(tool, event, &synth_sample, machine) != 0) { |
| 316 | rc = -1; |
| 317 | break; |
| 318 | } |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 319 | } |
| 320 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 321 | free(event); |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 322 | return rc; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 323 | } |
| 324 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 325 | static int __event__synthesize_thread(union perf_event *comm_event, |
| 326 | union perf_event *mmap_event, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 327 | union perf_event *fork_event, |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 328 | pid_t pid, int full, |
| 329 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 330 | struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 331 | struct machine *machine, bool mmap_data) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 332 | { |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 333 | char filename[PATH_MAX]; |
| 334 | DIR *tasks; |
| 335 | struct dirent dirent, *next; |
| 336 | pid_t tgid; |
| 337 | |
| 338 | /* special case: only send one comm event using passed in pid */ |
| 339 | if (!full) { |
| 340 | tgid = perf_event__synthesize_comm(tool, comm_event, pid, |
| 341 | process, machine); |
| 342 | |
| 343 | if (tgid == -1) |
| 344 | return -1; |
| 345 | |
| 346 | return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, |
| 347 | process, machine, mmap_data); |
| 348 | } |
| 349 | |
| 350 | if (machine__is_default_guest(machine)) |
| 351 | return 0; |
| 352 | |
| 353 | snprintf(filename, sizeof(filename), "%s/proc/%d/task", |
| 354 | machine->root_dir, pid); |
| 355 | |
| 356 | tasks = opendir(filename); |
| 357 | if (tasks == NULL) { |
| 358 | pr_debug("couldn't open %s\n", filename); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | while (!readdir_r(tasks, &dirent, &next) && next) { |
| 363 | char *end; |
| 364 | int rc = 0; |
| 365 | pid_t _pid; |
| 366 | |
| 367 | _pid = strtol(dirent.d_name, &end, 10); |
| 368 | if (*end) |
| 369 | continue; |
| 370 | |
| 371 | tgid = perf_event__synthesize_comm(tool, comm_event, _pid, |
| 372 | process, machine); |
| 373 | if (tgid == -1) |
| 374 | return -1; |
| 375 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 376 | if (_pid == pid) { |
| 377 | /* process the parent's maps too */ |
| 378 | rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, |
| 379 | process, machine, mmap_data); |
| 380 | } else { |
| 381 | /* only fork the tid's map, to save time */ |
| 382 | rc = perf_event__synthesize_fork(tool, fork_event, _pid, tgid, |
| 383 | process, machine); |
| 384 | } |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 385 | |
| 386 | if (rc) |
| 387 | return rc; |
| 388 | } |
| 389 | |
| 390 | closedir(tasks); |
| 391 | return 0; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 392 | } |
| 393 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 394 | int perf_event__synthesize_thread_map(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 395 | struct thread_map *threads, |
Arnaldo Carvalho de Melo | 7c940c1 | 2011-02-11 11:45:54 -0200 | [diff] [blame] | 396 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 397 | struct machine *machine, |
| 398 | bool mmap_data) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 399 | { |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 400 | union perf_event *comm_event, *mmap_event, *fork_event; |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 401 | int err = -1, thread, j; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 402 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 403 | 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] | 404 | if (comm_event == NULL) |
| 405 | goto out; |
| 406 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 407 | 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] | 408 | if (mmap_event == NULL) |
| 409 | goto out_free_comm; |
| 410 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 411 | fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); |
| 412 | if (fork_event == NULL) |
| 413 | goto out_free_mmap; |
| 414 | |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 415 | err = 0; |
| 416 | for (thread = 0; thread < threads->nr; ++thread) { |
| 417 | if (__event__synthesize_thread(comm_event, mmap_event, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 418 | fork_event, |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 419 | threads->map[thread], 0, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 420 | process, tool, machine, |
| 421 | mmap_data)) { |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 422 | err = -1; |
| 423 | break; |
| 424 | } |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 425 | |
| 426 | /* |
| 427 | * comm.pid is set to thread group id by |
| 428 | * perf_event__synthesize_comm |
| 429 | */ |
| 430 | if ((int) comm_event->comm.pid != threads->map[thread]) { |
| 431 | bool need_leader = true; |
| 432 | |
| 433 | /* is thread group leader in thread_map? */ |
| 434 | for (j = 0; j < threads->nr; ++j) { |
| 435 | if ((int) comm_event->comm.pid == threads->map[j]) { |
| 436 | need_leader = false; |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /* if not, generate events for it */ |
| 442 | if (need_leader && |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 443 | __event__synthesize_thread(comm_event, mmap_event, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 444 | fork_event, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 445 | comm_event->comm.pid, 0, |
| 446 | process, tool, machine, |
| 447 | mmap_data)) { |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 448 | err = -1; |
| 449 | break; |
| 450 | } |
| 451 | } |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 452 | } |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 453 | free(fork_event); |
| 454 | out_free_mmap: |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 455 | free(mmap_event); |
| 456 | out_free_comm: |
| 457 | free(comm_event); |
| 458 | out: |
| 459 | return err; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 460 | } |
| 461 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 462 | int perf_event__synthesize_threads(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 463 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 464 | struct machine *machine, bool mmap_data) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 465 | { |
| 466 | DIR *proc; |
Dongsheng Yang | 9956346 | 2013-12-20 15:52:57 -0500 | [diff] [blame] | 467 | char proc_path[PATH_MAX]; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 468 | struct dirent dirent, *next; |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 469 | union perf_event *comm_event, *mmap_event, *fork_event; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 470 | int err = -1; |
| 471 | |
Namhyung Kim | 574799b | 2014-03-17 10:45:49 +0900 | [diff] [blame] | 472 | if (machine__is_default_guest(machine)) |
| 473 | return 0; |
| 474 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 475 | 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] | 476 | if (comm_event == NULL) |
| 477 | goto out; |
| 478 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 479 | 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] | 480 | if (mmap_event == NULL) |
| 481 | goto out_free_comm; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 482 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 483 | fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); |
| 484 | if (fork_event == NULL) |
| 485 | goto out_free_mmap; |
| 486 | |
Dongsheng Yang | 9956346 | 2013-12-20 15:52:57 -0500 | [diff] [blame] | 487 | snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir); |
| 488 | proc = opendir(proc_path); |
| 489 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 490 | if (proc == NULL) |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 491 | goto out_free_fork; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 492 | |
| 493 | while (!readdir_r(proc, &dirent, &next) && next) { |
| 494 | char *end; |
| 495 | pid_t pid = strtol(dirent.d_name, &end, 10); |
| 496 | |
| 497 | if (*end) /* only interested in proper numerical dirents */ |
| 498 | continue; |
Arnaldo Carvalho de Melo | ba361c9 | 2012-10-17 18:50:13 -0300 | [diff] [blame] | 499 | /* |
| 500 | * We may race with exiting thread, so don't stop just because |
| 501 | * one thread couldn't be synthesized. |
| 502 | */ |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 503 | __event__synthesize_thread(comm_event, mmap_event, fork_event, pid, |
| 504 | 1, process, tool, machine, mmap_data); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 505 | } |
| 506 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 507 | err = 0; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 508 | closedir(proc); |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 509 | out_free_fork: |
| 510 | free(fork_event); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 511 | out_free_mmap: |
| 512 | free(mmap_event); |
| 513 | out_free_comm: |
| 514 | free(comm_event); |
| 515 | out: |
| 516 | return err; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 517 | } |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 518 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 519 | struct process_symbol_args { |
| 520 | const char *name; |
| 521 | u64 start; |
| 522 | }; |
| 523 | |
Arnaldo Carvalho de Melo | 3b01a41 | 2010-12-22 01:08:36 -0200 | [diff] [blame] | 524 | 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] | 525 | u64 start) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 526 | { |
| 527 | struct process_symbol_args *args = arg; |
| 528 | |
Arnaldo Carvalho de Melo | 881516e | 2010-01-15 18:08:27 -0200 | [diff] [blame] | 529 | /* |
| 530 | * Must be a function or at least an alias, as in PARISC64, where "_text" is |
| 531 | * an 'A' to the same address as "_stext". |
| 532 | */ |
| 533 | if (!(symbol_type__is_a(type, MAP__FUNCTION) || |
| 534 | type == 'A') || strcmp(name, args->name)) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 535 | return 0; |
| 536 | |
| 537 | args->start = start; |
| 538 | return 1; |
| 539 | } |
| 540 | |
Adrian Hunter | 29b596b | 2014-01-29 16:14:37 +0200 | [diff] [blame] | 541 | u64 kallsyms__get_function_start(const char *kallsyms_filename, |
| 542 | const char *symbol_name) |
| 543 | { |
| 544 | struct process_symbol_args args = { .name = symbol_name, }; |
| 545 | |
| 546 | if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0) |
| 547 | return 0; |
| 548 | |
| 549 | return args.start; |
| 550 | } |
| 551 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 552 | int perf_event__synthesize_kernel_mmap(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 553 | perf_event__handler_t process, |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 554 | struct machine *machine) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 555 | { |
| 556 | size_t size; |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 557 | const char *mmap_name; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 558 | char name_buff[PATH_MAX]; |
| 559 | struct map *map; |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 560 | struct kmap *kmap; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 561 | int err; |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 562 | union perf_event *event; |
| 563 | |
| 564 | if (machine->vmlinux_maps[0] == NULL) |
| 565 | return -1; |
| 566 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 567 | /* |
| 568 | * We should get this from /sys/kernel/sections/.text, but till that is |
| 569 | * available use this, and after it is use this as a fallback for older |
| 570 | * kernels. |
| 571 | */ |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 572 | event = zalloc((sizeof(event->mmap) + machine->id_hdr_size)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 573 | if (event == NULL) { |
| 574 | pr_debug("Not enough memory synthesizing mmap event " |
| 575 | "for kernel modules\n"); |
| 576 | return -1; |
| 577 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 578 | |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 579 | 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] | 580 | if (machine__is_host(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 581 | /* |
| 582 | * kernel uses PERF_RECORD_MISC_USER for user space maps, |
| 583 | * see kernel/perf_event.c __perf_event_mmap |
| 584 | */ |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 585 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 586 | } else { |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 587 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
Thomas Jarosch | 0b9e01a | 2013-01-25 11:20:47 +0100 | [diff] [blame] | 588 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 589 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 590 | map = machine->vmlinux_maps[MAP__FUNCTION]; |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 591 | kmap = map__kmap(map); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 592 | size = snprintf(event->mmap.filename, sizeof(event->mmap.filename), |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 593 | "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 594 | size = PERF_ALIGN(size, sizeof(u64)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 595 | event->mmap.header.type = PERF_RECORD_MMAP; |
| 596 | event->mmap.header.size = (sizeof(event->mmap) - |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 597 | (sizeof(event->mmap.filename) - size) + machine->id_hdr_size); |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 598 | event->mmap.pgoff = kmap->ref_reloc_sym->addr; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 599 | event->mmap.start = map->start; |
| 600 | event->mmap.len = map->end - event->mmap.start; |
| 601 | event->mmap.pid = machine->pid; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 602 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 603 | err = process(tool, event, &synth_sample, machine); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 604 | free(event); |
| 605 | |
| 606 | return err; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 607 | } |
| 608 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 609 | size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp) |
| 610 | { |
Adrian Hunter | 022c50d | 2014-07-14 13:02:27 +0300 | [diff] [blame] | 611 | const char *s; |
| 612 | |
| 613 | if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC) |
| 614 | s = " exec"; |
| 615 | else |
| 616 | s = ""; |
| 617 | |
Arnaldo Carvalho de Melo | 5067406 | 2015-02-24 17:20:31 -0300 | [diff] [blame^] | 618 | return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid); |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 619 | } |
| 620 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 621 | 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] | 622 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 623 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 624 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 625 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 626 | return machine__process_comm_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 627 | } |
| 628 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 629 | 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] | 630 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 631 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 632 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 633 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 634 | return machine__process_lost_event(machine, event, sample); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 635 | } |
| 636 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 637 | size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp) |
| 638 | { |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 639 | return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n", |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 640 | event->mmap.pid, event->mmap.tid, event->mmap.start, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 641 | event->mmap.len, event->mmap.pgoff, |
| 642 | (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x', |
| 643 | event->mmap.filename); |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 644 | } |
| 645 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 646 | size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp) |
| 647 | { |
| 648 | return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 649 | " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n", |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 650 | event->mmap2.pid, event->mmap2.tid, event->mmap2.start, |
| 651 | event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj, |
| 652 | event->mmap2.min, event->mmap2.ino, |
| 653 | event->mmap2.ino_generation, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 654 | (event->mmap2.prot & PROT_READ) ? 'r' : '-', |
| 655 | (event->mmap2.prot & PROT_WRITE) ? 'w' : '-', |
| 656 | (event->mmap2.prot & PROT_EXEC) ? 'x' : '-', |
| 657 | (event->mmap2.flags & MAP_SHARED) ? 's' : 'p', |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 658 | event->mmap2.filename); |
| 659 | } |
| 660 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 661 | 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] | 662 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 663 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 664 | struct machine *machine) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 665 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 666 | return machine__process_mmap_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 667 | } |
| 668 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 669 | int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused, |
| 670 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 671 | struct perf_sample *sample, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 672 | struct machine *machine) |
| 673 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 674 | return machine__process_mmap2_event(machine, event, sample); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 677 | size_t perf_event__fprintf_task(union perf_event *event, FILE *fp) |
| 678 | { |
| 679 | return fprintf(fp, "(%d:%d):(%d:%d)\n", |
| 680 | event->fork.pid, event->fork.tid, |
| 681 | event->fork.ppid, event->fork.ptid); |
| 682 | } |
| 683 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 684 | 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] | 685 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 686 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 687 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 688 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 689 | return machine__process_fork_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 690 | } |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 691 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 692 | int perf_event__process_exit(struct perf_tool *tool __maybe_unused, |
| 693 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 694 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 695 | struct machine *machine) |
| 696 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 697 | return machine__process_exit_event(machine, event, sample); |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 698 | } |
| 699 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 700 | size_t perf_event__fprintf(union perf_event *event, FILE *fp) |
| 701 | { |
| 702 | size_t ret = fprintf(fp, "PERF_RECORD_%s", |
| 703 | perf_event__name(event->header.type)); |
| 704 | |
| 705 | switch (event->header.type) { |
| 706 | case PERF_RECORD_COMM: |
| 707 | ret += perf_event__fprintf_comm(event, fp); |
| 708 | break; |
| 709 | case PERF_RECORD_FORK: |
| 710 | case PERF_RECORD_EXIT: |
| 711 | ret += perf_event__fprintf_task(event, fp); |
| 712 | break; |
| 713 | case PERF_RECORD_MMAP: |
| 714 | ret += perf_event__fprintf_mmap(event, fp); |
| 715 | break; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 716 | case PERF_RECORD_MMAP2: |
| 717 | ret += perf_event__fprintf_mmap2(event, fp); |
| 718 | break; |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 719 | default: |
| 720 | ret += fprintf(fp, "\n"); |
| 721 | } |
| 722 | |
| 723 | return ret; |
| 724 | } |
| 725 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 726 | int perf_event__process(struct perf_tool *tool __maybe_unused, |
| 727 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 728 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 729 | struct machine *machine) |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 730 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 731 | return machine__process_event(machine, event, sample); |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 732 | } |
| 733 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 734 | void thread__find_addr_map(struct thread *thread, u8 cpumode, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 735 | enum map_type type, u64 addr, |
Adrian Hunter | 326f59b | 2013-08-08 14:32:27 +0300 | [diff] [blame] | 736 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 737 | { |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 738 | struct map_groups *mg = thread->mg; |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 739 | struct machine *machine = mg->machine; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 740 | bool load_map = false; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 741 | |
Arnaldo Carvalho de Melo | cc22e57 | 2013-12-19 17:20:06 -0300 | [diff] [blame] | 742 | al->machine = machine; |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 743 | al->thread = thread; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 744 | al->addr = addr; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 745 | al->cpumode = cpumode; |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 746 | al->filtered = 0; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 747 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 748 | if (machine == NULL) { |
| 749 | al->map = NULL; |
| 750 | return; |
| 751 | } |
| 752 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 753 | if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 754 | al->level = 'k'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 755 | mg = &machine->kmaps; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 756 | load_map = true; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 757 | } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 758 | al->level = '.'; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 759 | } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { |
| 760 | al->level = 'g'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 761 | mg = &machine->kmaps; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 762 | load_map = true; |
Dongsheng Yang | fb50bb4 | 2013-12-20 15:52:56 -0500 | [diff] [blame] | 763 | } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) { |
| 764 | al->level = 'u'; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 765 | } else { |
Dongsheng Yang | fb50bb4 | 2013-12-20 15:52:56 -0500 | [diff] [blame] | 766 | al->level = 'H'; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 767 | al->map = NULL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 768 | |
| 769 | if ((cpumode == PERF_RECORD_MISC_GUEST_USER || |
| 770 | cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && |
| 771 | !perf_guest) |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 772 | al->filtered |= (1 << HIST_FILTER__GUEST); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 773 | if ((cpumode == PERF_RECORD_MISC_USER || |
| 774 | cpumode == PERF_RECORD_MISC_KERNEL) && |
| 775 | !perf_host) |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 776 | al->filtered |= (1 << HIST_FILTER__HOST); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 777 | |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 778 | return; |
| 779 | } |
| 780 | try_again: |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 781 | al->map = map_groups__find(mg, type, al->addr); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 782 | if (al->map == NULL) { |
| 783 | /* |
| 784 | * If this is outside of all known maps, and is a negative |
| 785 | * address, try to look it up in the kernel dso, as it might be |
| 786 | * a vsyscall or vdso (which executes in user-mode). |
| 787 | * |
| 788 | * XXX This is nasty, we should have a symbol list in the |
| 789 | * "[vdso]" dso, but for now lets use the old trick of looking |
| 790 | * in the whole kernel symbol list. |
| 791 | */ |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 792 | if (cpumode == PERF_RECORD_MISC_USER && machine && |
| 793 | mg != &machine->kmaps && |
| 794 | machine__kernel_ip(machine, al->addr)) { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 795 | mg = &machine->kmaps; |
Adrian Hunter | 1f2a706 | 2014-07-14 13:02:31 +0300 | [diff] [blame] | 796 | load_map = true; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 797 | goto try_again; |
| 798 | } |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 799 | } else { |
| 800 | /* |
| 801 | * Kernel maps might be changed when loading symbols so loading |
| 802 | * must be done prior to using kernel maps. |
| 803 | */ |
| 804 | if (load_map) |
Adrian Hunter | 326f59b | 2013-08-08 14:32:27 +0300 | [diff] [blame] | 805 | map__load(al->map, machine->symbol_filter); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 806 | al->addr = al->map->map_ip(al->map, al->addr); |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 807 | } |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 808 | } |
| 809 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 810 | void thread__find_addr_location(struct thread *thread, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 811 | u8 cpumode, enum map_type type, u64 addr, |
Adrian Hunter | 61710bd | 2013-08-08 14:32:26 +0300 | [diff] [blame] | 812 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 813 | { |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 814 | thread__find_addr_map(thread, cpumode, type, addr, al); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 815 | if (al->map != NULL) |
Adrian Hunter | 61710bd | 2013-08-08 14:32:26 +0300 | [diff] [blame] | 816 | al->sym = map__find_symbol(al->map, al->addr, |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 817 | thread->mg->machine->symbol_filter); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 818 | else |
| 819 | al->sym = NULL; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 820 | } |
| 821 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 822 | int perf_event__preprocess_sample(const union perf_event *event, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 823 | struct machine *machine, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 824 | struct addr_location *al, |
Adrian Hunter | e44baa3 | 2013-08-08 14:32:25 +0300 | [diff] [blame] | 825 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 826 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 827 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
Adrian Hunter | ef89325 | 2013-08-27 11:23:06 +0300 | [diff] [blame] | 828 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
Namhyung Kim | 13ce34d | 2014-05-12 09:56:42 +0900 | [diff] [blame] | 829 | sample->tid); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 830 | |
| 831 | if (thread == NULL) |
| 832 | return -1; |
| 833 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 834 | dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid); |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 835 | /* |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 836 | * Have we already created the kernel maps for this machine? |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 837 | * |
| 838 | * This should have happened earlier, when we processed the kernel MMAP |
| 839 | * events, but for older perf.data files there was no such thing, so do |
| 840 | * it now. |
| 841 | */ |
| 842 | if (cpumode == PERF_RECORD_MISC_KERNEL && |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 843 | machine->vmlinux_maps[MAP__FUNCTION] == NULL) |
| 844 | machine__create_kernel_maps(machine); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 845 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 846 | thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 847 | dump_printf(" ...... dso: %s\n", |
| 848 | al->map ? al->map->dso->long_name : |
| 849 | al->level == 'H' ? "[hypervisor]" : "<not found>"); |
Namhyung Kim | 466fa76 | 2014-03-17 17:12:27 -0300 | [diff] [blame] | 850 | |
| 851 | if (thread__is_filtered(thread)) |
| 852 | al->filtered |= (1 << HIST_FILTER__THREAD); |
| 853 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 854 | al->sym = NULL; |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 855 | al->cpu = sample->cpu; |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 856 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 857 | if (al->map) { |
Namhyung Kim | cb8f4e9 | 2011-12-13 00:16:55 +0900 | [diff] [blame] | 858 | struct dso *dso = al->map->dso; |
| 859 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 860 | if (symbol_conf.dso_list && |
Namhyung Kim | cb8f4e9 | 2011-12-13 00:16:55 +0900 | [diff] [blame] | 861 | (!dso || !(strlist__has_entry(symbol_conf.dso_list, |
| 862 | dso->short_name) || |
| 863 | (dso->short_name != dso->long_name && |
| 864 | strlist__has_entry(symbol_conf.dso_list, |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 865 | dso->long_name))))) { |
| 866 | al->filtered |= (1 << HIST_FILTER__DSO); |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 867 | } |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 868 | |
Adrian Hunter | e44baa3 | 2013-08-08 14:32:25 +0300 | [diff] [blame] | 869 | al->sym = map__find_symbol(al->map, al->addr, |
| 870 | machine->symbol_filter); |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 871 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 872 | |
Feng Tang | 1500b93 | 2012-09-07 16:42:23 +0800 | [diff] [blame] | 873 | if (symbol_conf.sym_list && |
| 874 | (!al->sym || !strlist__has_entry(symbol_conf.sym_list, |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 875 | al->sym->name))) { |
| 876 | al->filtered |= (1 << HIST_FILTER__SYMBOL); |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 877 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 878 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 879 | return 0; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 880 | } |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 881 | |
| 882 | bool is_bts_event(struct perf_event_attr *attr) |
| 883 | { |
| 884 | return attr->type == PERF_TYPE_HARDWARE && |
| 885 | (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && |
| 886 | attr->sample_period == 1; |
| 887 | } |
| 888 | |
| 889 | bool sample_addr_correlates_sym(struct perf_event_attr *attr) |
| 890 | { |
| 891 | if (attr->type == PERF_TYPE_SOFTWARE && |
| 892 | (attr->config == PERF_COUNT_SW_PAGE_FAULTS || |
| 893 | attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN || |
| 894 | attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)) |
| 895 | return true; |
| 896 | |
| 897 | if (is_bts_event(attr)) |
| 898 | return true; |
| 899 | |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | void perf_event__preprocess_sample_addr(union perf_event *event, |
| 904 | struct perf_sample *sample, |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 905 | struct thread *thread, |
| 906 | struct addr_location *al) |
| 907 | { |
| 908 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 909 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 910 | thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al); |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 911 | if (!al->map) |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 912 | thread__find_addr_map(thread, cpumode, MAP__VARIABLE, |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 913 | sample->addr, al); |
| 914 | |
| 915 | al->cpu = sample->cpu; |
| 916 | al->sym = NULL; |
| 917 | |
| 918 | if (al->map) |
| 919 | al->sym = map__find_symbol(al->map, al->addr, NULL); |
| 920 | } |