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