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