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