Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 1 | #include <linux/types.h> |
David Carrillo-Cisneros | 16eb813 | 2017-04-11 23:49:19 -0700 | [diff] [blame] | 2 | #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */ |
Wang Nan | d7e404a | 2016-09-06 04:58:29 +0000 | [diff] [blame] | 3 | #include <api/fs/fs.h> |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 4 | #include "event.h" |
| 5 | #include "debug.h" |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 6 | #include "hist.h" |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 7 | #include "machine.h" |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 8 | #include "sort.h" |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 9 | #include "string.h" |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 10 | #include "strlist.h" |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 11 | #include "thread.h" |
Arnaldo Carvalho de Melo | 7c940c1 | 2011-02-11 11:45:54 -0200 | [diff] [blame] | 12 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | c506c96 | 2013-12-11 09:15:00 -0300 | [diff] [blame] | 13 | #include "symbol/kallsyms.h" |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 14 | #include "asm/bug.h" |
| 15 | #include "stat.h" |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 17 | static const char *perf_event__names[] = { |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 18 | [0] = "TOTAL", |
| 19 | [PERF_RECORD_MMAP] = "MMAP", |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 20 | [PERF_RECORD_MMAP2] = "MMAP2", |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 21 | [PERF_RECORD_LOST] = "LOST", |
| 22 | [PERF_RECORD_COMM] = "COMM", |
| 23 | [PERF_RECORD_EXIT] = "EXIT", |
| 24 | [PERF_RECORD_THROTTLE] = "THROTTLE", |
| 25 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", |
| 26 | [PERF_RECORD_FORK] = "FORK", |
| 27 | [PERF_RECORD_READ] = "READ", |
| 28 | [PERF_RECORD_SAMPLE] = "SAMPLE", |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 29 | [PERF_RECORD_AUX] = "AUX", |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 30 | [PERF_RECORD_ITRACE_START] = "ITRACE_START", |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 31 | [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES", |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 32 | [PERF_RECORD_SWITCH] = "SWITCH", |
| 33 | [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE", |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 34 | [PERF_RECORD_NAMESPACES] = "NAMESPACES", |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 35 | [PERF_RECORD_HEADER_ATTR] = "ATTR", |
| 36 | [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", |
| 37 | [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", |
| 38 | [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", |
| 39 | [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND", |
Adrian Hunter | 3c659ee | 2014-10-27 15:49:22 +0200 | [diff] [blame] | 40 | [PERF_RECORD_ID_INDEX] = "ID_INDEX", |
Adrian Hunter | a16ac02 | 2015-04-09 18:53:43 +0300 | [diff] [blame] | 41 | [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO", |
| 42 | [PERF_RECORD_AUXTRACE] = "AUXTRACE", |
Adrian Hunter | e9bf54d | 2015-04-09 18:53:47 +0300 | [diff] [blame] | 43 | [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR", |
Jiri Olsa | 5f3339d | 2015-10-25 15:51:19 +0100 | [diff] [blame] | 44 | [PERF_RECORD_THREAD_MAP] = "THREAD_MAP", |
Jiri Olsa | 6640b6c | 2015-10-25 15:51:23 +0100 | [diff] [blame] | 45 | [PERF_RECORD_CPU_MAP] = "CPU_MAP", |
Jiri Olsa | 374fb9e | 2015-10-25 15:51:27 +0100 | [diff] [blame] | 46 | [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG", |
Jiri Olsa | d80518c | 2015-10-25 15:51:30 +0100 | [diff] [blame] | 47 | [PERF_RECORD_STAT] = "STAT", |
Jiri Olsa | 2d8f0f1 | 2015-10-25 15:51:33 +0100 | [diff] [blame] | 48 | [PERF_RECORD_STAT_ROUND] = "STAT_ROUND", |
Jiri Olsa | ffe77725 | 2015-10-25 15:51:36 +0100 | [diff] [blame] | 49 | [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE", |
Adrian Hunter | 46bc29b | 2016-03-08 10:38:44 +0200 | [diff] [blame] | 50 | [PERF_RECORD_TIME_CONV] = "TIME_CONV", |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 51 | }; |
| 52 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 53 | static const char *perf_ns__names[] = { |
| 54 | [NET_NS_INDEX] = "net", |
| 55 | [UTS_NS_INDEX] = "uts", |
| 56 | [IPC_NS_INDEX] = "ipc", |
| 57 | [PID_NS_INDEX] = "pid", |
| 58 | [USER_NS_INDEX] = "user", |
| 59 | [MNT_NS_INDEX] = "mnt", |
| 60 | [CGROUP_NS_INDEX] = "cgroup", |
| 61 | }; |
| 62 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 63 | const char *perf_event__name(unsigned int id) |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 64 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 65 | if (id >= ARRAY_SIZE(perf_event__names)) |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 66 | return "INVALID"; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 67 | if (!perf_event__names[id]) |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 68 | return "UNKNOWN"; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 69 | return perf_event__names[id]; |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 72 | static const char *perf_ns__name(unsigned int id) |
| 73 | { |
| 74 | if (id >= ARRAY_SIZE(perf_ns__names)) |
| 75 | return "UNKNOWN"; |
| 76 | return perf_ns__names[id]; |
| 77 | } |
| 78 | |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 79 | static int perf_tool__process_synth_event(struct perf_tool *tool, |
| 80 | union perf_event *event, |
| 81 | struct machine *machine, |
| 82 | perf_event__handler_t process) |
| 83 | { |
| 84 | struct perf_sample synth_sample = { |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 85 | .pid = -1, |
| 86 | .tid = -1, |
| 87 | .time = -1, |
| 88 | .stream_id = -1, |
| 89 | .cpu = -1, |
| 90 | .period = 1, |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 91 | .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK, |
| 92 | }; |
| 93 | |
| 94 | return process(tool, event, &synth_sample, machine); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 95 | }; |
| 96 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 97 | /* |
| 98 | * Assumes that the first 4095 bytes of /proc/pid/stat contains |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 99 | * the comm, tgid and ppid. |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 100 | */ |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 101 | static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len, |
| 102 | pid_t *tgid, pid_t *ppid) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 103 | { |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 104 | char filename[PATH_MAX]; |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 105 | char bf[4096]; |
| 106 | int fd; |
Andrzej Hajda | 3834966 | 2015-10-06 11:00:17 +0200 | [diff] [blame] | 107 | size_t size = 0; |
| 108 | ssize_t n; |
Taeung Song | bdd97ca | 2017-04-07 23:24:21 +0900 | [diff] [blame] | 109 | char *name, *tgids, *ppids; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 110 | |
| 111 | *tgid = -1; |
| 112 | *ppid = -1; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 113 | |
| 114 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); |
| 115 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 116 | fd = open(filename, O_RDONLY); |
| 117 | if (fd < 0) { |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 118 | pr_debug("couldn't open %s\n", filename); |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 119 | return -1; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 120 | } |
| 121 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 122 | n = read(fd, bf, sizeof(bf) - 1); |
| 123 | close(fd); |
| 124 | if (n <= 0) { |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 125 | 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] | 126 | pid); |
| 127 | return -1; |
| 128 | } |
| 129 | bf[n] = '\0'; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 130 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 131 | name = strstr(bf, "Name:"); |
| 132 | tgids = strstr(bf, "Tgid:"); |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 133 | ppids = strstr(bf, "PPid:"); |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 134 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 135 | if (name) { |
| 136 | name += 5; /* strlen("Name:") */ |
Taeung Song | bdd97ca | 2017-04-07 23:24:21 +0900 | [diff] [blame] | 137 | name = rtrim(ltrim(name)); |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 138 | size = strlen(name); |
| 139 | if (size >= len) |
| 140 | size = len - 1; |
| 141 | memcpy(comm, name, size); |
| 142 | comm[size] = '\0'; |
| 143 | } else { |
| 144 | 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] | 145 | } |
| 146 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 147 | if (tgids) { |
| 148 | tgids += 5; /* strlen("Tgid:") */ |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 149 | *tgid = atoi(tgids); |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 150 | } else { |
| 151 | pr_debug("Tgid: string not found for pid %d\n", pid); |
| 152 | } |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 153 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 154 | if (ppids) { |
| 155 | ppids += 5; /* strlen("PPid:") */ |
| 156 | *ppid = atoi(ppids); |
| 157 | } else { |
| 158 | pr_debug("PPid: string not found for pid %d\n", pid); |
| 159 | } |
| 160 | |
| 161 | return 0; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 162 | } |
| 163 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 164 | static int perf_event__prepare_comm(union perf_event *event, pid_t pid, |
| 165 | struct machine *machine, |
| 166 | pid_t *tgid, pid_t *ppid) |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 167 | { |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 168 | size_t size; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 169 | |
| 170 | *ppid = -1; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 171 | |
| 172 | memset(&event->comm, 0, sizeof(event->comm)); |
| 173 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 174 | if (machine__is_host(machine)) { |
| 175 | if (perf_event__get_comm_ids(pid, event->comm.comm, |
| 176 | sizeof(event->comm.comm), |
| 177 | tgid, ppid) != 0) { |
| 178 | return -1; |
| 179 | } |
| 180 | } else { |
| 181 | *tgid = machine->pid; |
| 182 | } |
Dongsheng Yang | f5db57c4 | 2013-12-20 15:52:59 -0500 | [diff] [blame] | 183 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 184 | if (*tgid < 0) |
| 185 | return -1; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 186 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 187 | event->comm.pid = *tgid; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 188 | event->comm.header.type = PERF_RECORD_COMM; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 189 | |
| 190 | size = strlen(event->comm.comm) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 191 | size = PERF_ALIGN(size, sizeof(u64)); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 192 | memset(event->comm.comm + size, 0, machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 193 | event->comm.header.size = (sizeof(event->comm) - |
| 194 | (sizeof(event->comm.comm) - size) + |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 195 | machine->id_hdr_size); |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 196 | event->comm.tid = pid; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 197 | |
| 198 | return 0; |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 199 | } |
| 200 | |
Namhyung Kim | e803cf9 | 2015-09-22 09:24:55 +0900 | [diff] [blame] | 201 | pid_t perf_event__synthesize_comm(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 202 | union perf_event *event, pid_t pid, |
| 203 | perf_event__handler_t process, |
| 204 | struct machine *machine) |
| 205 | { |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 206 | pid_t tgid, ppid; |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 207 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 208 | if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0) |
| 209 | return -1; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 210 | |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 211 | if (perf_tool__process_synth_event(tool, event, machine, process) != 0) |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 212 | return -1; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 213 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 214 | return tgid; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 215 | } |
| 216 | |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 217 | static void perf_event__get_ns_link_info(pid_t pid, const char *ns, |
| 218 | struct perf_ns_link_info *ns_link_info) |
| 219 | { |
| 220 | struct stat64 st; |
| 221 | char proc_ns[128]; |
| 222 | |
| 223 | sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns); |
| 224 | if (stat64(proc_ns, &st) == 0) { |
| 225 | ns_link_info->dev = st.st_dev; |
| 226 | ns_link_info->ino = st.st_ino; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | int perf_event__synthesize_namespaces(struct perf_tool *tool, |
| 231 | union perf_event *event, |
| 232 | pid_t pid, pid_t tgid, |
| 233 | perf_event__handler_t process, |
| 234 | struct machine *machine) |
| 235 | { |
| 236 | u32 idx; |
| 237 | struct perf_ns_link_info *ns_link_info; |
| 238 | |
| 239 | if (!tool || !tool->namespace_events) |
| 240 | return 0; |
| 241 | |
| 242 | memset(&event->namespaces, 0, (sizeof(event->namespaces) + |
| 243 | (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + |
| 244 | machine->id_hdr_size)); |
| 245 | |
| 246 | event->namespaces.pid = tgid; |
| 247 | event->namespaces.tid = pid; |
| 248 | |
| 249 | event->namespaces.nr_namespaces = NR_NAMESPACES; |
| 250 | |
| 251 | ns_link_info = event->namespaces.link_info; |
| 252 | |
| 253 | for (idx = 0; idx < event->namespaces.nr_namespaces; idx++) |
| 254 | perf_event__get_ns_link_info(pid, perf_ns__name(idx), |
| 255 | &ns_link_info[idx]); |
| 256 | |
| 257 | event->namespaces.header.type = PERF_RECORD_NAMESPACES; |
| 258 | |
| 259 | event->namespaces.header.size = (sizeof(event->namespaces) + |
| 260 | (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + |
| 261 | machine->id_hdr_size); |
| 262 | |
| 263 | if (perf_tool__process_synth_event(tool, event, machine, process) != 0) |
| 264 | return -1; |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 269 | static int perf_event__synthesize_fork(struct perf_tool *tool, |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 270 | union perf_event *event, |
| 271 | pid_t pid, pid_t tgid, pid_t ppid, |
| 272 | perf_event__handler_t process, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 273 | struct machine *machine) |
| 274 | { |
| 275 | memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size); |
| 276 | |
David Ahern | 7764a38 | 2015-04-09 12:48:27 -0400 | [diff] [blame] | 277 | /* |
| 278 | * for main thread set parent to ppid from status file. For other |
| 279 | * threads set parent pid to main thread. ie., assume main thread |
| 280 | * spawns all threads in a process |
| 281 | */ |
| 282 | if (tgid == pid) { |
| 283 | event->fork.ppid = ppid; |
| 284 | event->fork.ptid = ppid; |
| 285 | } else { |
| 286 | event->fork.ppid = tgid; |
| 287 | event->fork.ptid = tgid; |
| 288 | } |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 289 | event->fork.pid = tgid; |
| 290 | event->fork.tid = pid; |
| 291 | event->fork.header.type = PERF_RECORD_FORK; |
| 292 | |
| 293 | event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size); |
| 294 | |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 295 | if (perf_tool__process_synth_event(tool, event, machine, process) != 0) |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 296 | return -1; |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Jiri Olsa | a18382b | 2014-01-07 13:47:20 +0100 | [diff] [blame] | 301 | int perf_event__synthesize_mmap_events(struct perf_tool *tool, |
| 302 | union perf_event *event, |
| 303 | pid_t pid, pid_t tgid, |
| 304 | perf_event__handler_t process, |
| 305 | struct machine *machine, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 306 | bool mmap_data, |
| 307 | unsigned int proc_map_timeout) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 308 | { |
| 309 | char filename[PATH_MAX]; |
| 310 | FILE *fp; |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 311 | unsigned long long t; |
| 312 | bool truncation = false; |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 313 | unsigned long long timeout = proc_map_timeout * 1000000ULL; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 314 | int rc = 0; |
Wang Nan | d7e404a | 2016-09-06 04:58:29 +0000 | [diff] [blame] | 315 | const char *hugetlbfs_mnt = hugetlbfs__mountpoint(); |
| 316 | int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 317 | |
Dongsheng Yang | c239c25a | 2013-12-20 15:53:00 -0500 | [diff] [blame] | 318 | if (machine__is_default_guest(machine)) |
| 319 | return 0; |
| 320 | |
Stephane Eranian | 88b897a | 2017-03-15 10:17:13 -0700 | [diff] [blame] | 321 | snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps", |
| 322 | machine->root_dir, pid, pid); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 323 | |
| 324 | fp = fopen(filename, "r"); |
| 325 | if (fp == NULL) { |
| 326 | /* |
| 327 | * We raced with a task exiting - just return: |
| 328 | */ |
| 329 | pr_debug("couldn't open %s\n", filename); |
| 330 | return -1; |
| 331 | } |
| 332 | |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 333 | event->header.type = PERF_RECORD_MMAP2; |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 334 | t = rdclock(); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 335 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 336 | while (1) { |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 337 | char bf[BUFSIZ]; |
| 338 | char prot[5]; |
| 339 | char execname[PATH_MAX]; |
| 340 | char anonstr[] = "//anon"; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 341 | unsigned int ino; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 342 | size_t size; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 343 | ssize_t n; |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 344 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 345 | if (fgets(bf, sizeof(bf), fp) == NULL) |
| 346 | break; |
| 347 | |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 348 | if ((rdclock() - t) > timeout) { |
| 349 | pr_warning("Reading %s time out. " |
| 350 | "You may want to increase " |
| 351 | "the time limit by --proc-map-timeout\n", |
| 352 | filename); |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 353 | truncation = true; |
| 354 | goto out; |
| 355 | } |
| 356 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 357 | /* ensure null termination since stack will be reused. */ |
| 358 | strcpy(execname, ""); |
| 359 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 360 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ |
Marcin Åšlusarz | 89fee59 | 2016-01-19 20:03:03 +0100 | [diff] [blame] | 361 | n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n", |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 362 | &event->mmap2.start, &event->mmap2.len, prot, |
| 363 | &event->mmap2.pgoff, &event->mmap2.maj, |
| 364 | &event->mmap2.min, |
| 365 | &ino, execname); |
| 366 | |
Don Zickus | 9d4ecc8 | 2013-11-13 15:32:06 -0300 | [diff] [blame] | 367 | /* |
| 368 | * Anon maps don't have the execname. |
| 369 | */ |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 370 | if (n < 7) |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 371 | continue; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 372 | |
| 373 | event->mmap2.ino = (u64)ino; |
| 374 | |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 375 | /* |
| 376 | * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c |
| 377 | */ |
Dongsheng Yang | 73547aa | 2013-12-20 15:52:58 -0500 | [diff] [blame] | 378 | if (machine__is_host(machine)) |
| 379 | event->header.misc = PERF_RECORD_MISC_USER; |
| 380 | else |
| 381 | event->header.misc = PERF_RECORD_MISC_GUEST_USER; |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 382 | |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 383 | /* map protection and flags bits */ |
| 384 | event->mmap2.prot = 0; |
| 385 | event->mmap2.flags = 0; |
| 386 | if (prot[0] == 'r') |
| 387 | event->mmap2.prot |= PROT_READ; |
| 388 | if (prot[1] == 'w') |
| 389 | event->mmap2.prot |= PROT_WRITE; |
| 390 | if (prot[2] == 'x') |
| 391 | event->mmap2.prot |= PROT_EXEC; |
| 392 | |
| 393 | if (prot[3] == 's') |
| 394 | event->mmap2.flags |= MAP_SHARED; |
| 395 | else |
| 396 | event->mmap2.flags |= MAP_PRIVATE; |
| 397 | |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 398 | if (prot[2] != 'x') { |
| 399 | if (!mmap_data || prot[0] != 'r') |
| 400 | continue; |
| 401 | |
| 402 | event->header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 403 | } |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 404 | |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 405 | out: |
| 406 | if (truncation) |
| 407 | event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT; |
| 408 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 409 | if (!strcmp(execname, "")) |
| 410 | strcpy(execname, anonstr); |
Arnaldo Carvalho de Melo | fbef103 | 2016-09-12 16:47:57 -0300 | [diff] [blame] | 411 | |
Adrian Hunter | 973186c | 2016-09-23 17:38:34 +0300 | [diff] [blame] | 412 | if (hugetlbfs_mnt_len && |
| 413 | !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) { |
Wang Nan | d7e404a | 2016-09-06 04:58:29 +0000 | [diff] [blame] | 414 | strcpy(execname, anonstr); |
| 415 | event->mmap2.flags |= MAP_HUGETLB; |
| 416 | } |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 417 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 418 | size = strlen(execname) + 1; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 419 | memcpy(event->mmap2.filename, execname, size); |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 420 | size = PERF_ALIGN(size, sizeof(u64)); |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 421 | event->mmap2.len -= event->mmap.start; |
| 422 | event->mmap2.header.size = (sizeof(event->mmap2) - |
| 423 | (sizeof(event->mmap2.filename) - size)); |
| 424 | memset(event->mmap2.filename + size, 0, machine->id_hdr_size); |
| 425 | event->mmap2.header.size += machine->id_hdr_size; |
| 426 | event->mmap2.pid = tgid; |
| 427 | event->mmap2.tid = pid; |
Anton Blanchard | 6a0e55d | 2011-08-30 09:15:06 +1000 | [diff] [blame] | 428 | |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 429 | if (perf_tool__process_synth_event(tool, event, machine, process) != 0) { |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 430 | rc = -1; |
| 431 | break; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 432 | } |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 433 | |
| 434 | if (truncation) |
| 435 | break; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | fclose(fp); |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 439 | return rc; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 440 | } |
| 441 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 442 | int perf_event__synthesize_modules(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 443 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 444 | struct machine *machine) |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 445 | { |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 446 | int rc = 0; |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 447 | struct map *pos; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 448 | struct map_groups *kmaps = &machine->kmaps; |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 449 | struct maps *maps = &kmaps->maps[MAP__FUNCTION]; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 450 | union perf_event *event = zalloc((sizeof(event->mmap) + |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 451 | machine->id_hdr_size)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 452 | if (event == NULL) { |
| 453 | pr_debug("Not enough memory synthesizing mmap event " |
| 454 | "for kernel modules\n"); |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | event->header.type = PERF_RECORD_MMAP; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 459 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 460 | /* |
| 461 | * kernel uses 0 for user space maps, see kernel/perf_event.c |
| 462 | * __perf_event_mmap |
| 463 | */ |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 464 | if (machine__is_host(machine)) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 465 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 466 | else |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 467 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 468 | |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 469 | for (pos = maps__first(maps); pos; pos = map__next(pos)) { |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 470 | size_t size; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 471 | |
Arnaldo Carvalho de Melo | ab9c2bd | 2015-09-23 15:45:20 -0300 | [diff] [blame] | 472 | if (__map__is_kernel(pos)) |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 473 | continue; |
| 474 | |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 475 | 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] | 476 | event->mmap.header.type = PERF_RECORD_MMAP; |
| 477 | event->mmap.header.size = (sizeof(event->mmap) - |
| 478 | (sizeof(event->mmap.filename) - size)); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 479 | memset(event->mmap.filename + size, 0, machine->id_hdr_size); |
| 480 | event->mmap.header.size += machine->id_hdr_size; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 481 | event->mmap.start = pos->start; |
| 482 | event->mmap.len = pos->end - pos->start; |
| 483 | event->mmap.pid = machine->pid; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 484 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 485 | memcpy(event->mmap.filename, pos->dso->long_name, |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 486 | pos->dso->long_name_len + 1); |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 487 | if (perf_tool__process_synth_event(tool, event, machine, process) != 0) { |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 488 | rc = -1; |
| 489 | break; |
| 490 | } |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 491 | } |
| 492 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 493 | free(event); |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 494 | return rc; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 495 | } |
| 496 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 497 | static int __event__synthesize_thread(union perf_event *comm_event, |
| 498 | union perf_event *mmap_event, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 499 | union perf_event *fork_event, |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 500 | union perf_event *namespaces_event, |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 501 | pid_t pid, int full, |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 502 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 503 | struct perf_tool *tool, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 504 | struct machine *machine, |
| 505 | bool mmap_data, |
| 506 | unsigned int proc_map_timeout) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 507 | { |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 508 | char filename[PATH_MAX]; |
| 509 | DIR *tasks; |
Arnaldo Carvalho de Melo | 7093b4c | 2016-04-08 11:32:15 -0300 | [diff] [blame] | 510 | struct dirent *dirent; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 511 | pid_t tgid, ppid; |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 512 | int rc = 0; |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 513 | |
| 514 | /* special case: only send one comm event using passed in pid */ |
| 515 | if (!full) { |
| 516 | tgid = perf_event__synthesize_comm(tool, comm_event, pid, |
| 517 | process, machine); |
| 518 | |
| 519 | if (tgid == -1) |
| 520 | return -1; |
| 521 | |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 522 | if (perf_event__synthesize_namespaces(tool, namespaces_event, pid, |
| 523 | tgid, process, machine) < 0) |
| 524 | return -1; |
| 525 | |
| 526 | |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 527 | return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 528 | process, machine, mmap_data, |
| 529 | proc_map_timeout); |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | if (machine__is_default_guest(machine)) |
| 533 | return 0; |
| 534 | |
| 535 | snprintf(filename, sizeof(filename), "%s/proc/%d/task", |
| 536 | machine->root_dir, pid); |
| 537 | |
| 538 | tasks = opendir(filename); |
| 539 | if (tasks == NULL) { |
| 540 | pr_debug("couldn't open %s\n", filename); |
| 541 | return 0; |
| 542 | } |
| 543 | |
Arnaldo Carvalho de Melo | 7093b4c | 2016-04-08 11:32:15 -0300 | [diff] [blame] | 544 | while ((dirent = readdir(tasks)) != NULL) { |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 545 | char *end; |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 546 | pid_t _pid; |
| 547 | |
Arnaldo Carvalho de Melo | 7093b4c | 2016-04-08 11:32:15 -0300 | [diff] [blame] | 548 | _pid = strtol(dirent->d_name, &end, 10); |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 549 | if (*end) |
| 550 | continue; |
| 551 | |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 552 | rc = -1; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 553 | if (perf_event__prepare_comm(comm_event, _pid, machine, |
| 554 | &tgid, &ppid) != 0) |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 555 | break; |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 556 | |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 557 | if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid, |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 558 | ppid, process, machine) < 0) |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 559 | break; |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 560 | |
| 561 | if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid, |
| 562 | tgid, process, machine) < 0) |
| 563 | break; |
| 564 | |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 565 | /* |
| 566 | * Send the prepared comm event |
| 567 | */ |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 568 | if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0) |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 569 | break; |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 570 | |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 571 | rc = 0; |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 572 | if (_pid == pid) { |
| 573 | /* process the parent's maps too */ |
| 574 | rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 575 | process, machine, mmap_data, proc_map_timeout); |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 576 | if (rc) |
| 577 | break; |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 578 | } |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | closedir(tasks); |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 582 | return rc; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 583 | } |
| 584 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 585 | int perf_event__synthesize_thread_map(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 586 | struct thread_map *threads, |
Arnaldo Carvalho de Melo | 7c940c1 | 2011-02-11 11:45:54 -0200 | [diff] [blame] | 587 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 588 | struct machine *machine, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 589 | bool mmap_data, |
| 590 | unsigned int proc_map_timeout) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 591 | { |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 592 | union perf_event *comm_event, *mmap_event, *fork_event; |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 593 | union perf_event *namespaces_event; |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 594 | int err = -1, thread, j; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 595 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 596 | 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] | 597 | if (comm_event == NULL) |
| 598 | goto out; |
| 599 | |
Wang Nan | b0fb978 | 2016-01-12 10:12:04 +0000 | [diff] [blame] | 600 | mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 601 | if (mmap_event == NULL) |
| 602 | goto out_free_comm; |
| 603 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 604 | fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); |
| 605 | if (fork_event == NULL) |
| 606 | goto out_free_mmap; |
| 607 | |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 608 | namespaces_event = malloc(sizeof(namespaces_event->namespaces) + |
| 609 | (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + |
| 610 | machine->id_hdr_size); |
| 611 | if (namespaces_event == NULL) |
| 612 | goto out_free_fork; |
| 613 | |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 614 | err = 0; |
| 615 | for (thread = 0; thread < threads->nr; ++thread) { |
| 616 | if (__event__synthesize_thread(comm_event, mmap_event, |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 617 | fork_event, namespaces_event, |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 618 | thread_map__pid(threads, thread), 0, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 619 | process, tool, machine, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 620 | mmap_data, proc_map_timeout)) { |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 621 | err = -1; |
| 622 | break; |
| 623 | } |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * comm.pid is set to thread group id by |
| 627 | * perf_event__synthesize_comm |
| 628 | */ |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 629 | if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) { |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 630 | bool need_leader = true; |
| 631 | |
| 632 | /* is thread group leader in thread_map? */ |
| 633 | for (j = 0; j < threads->nr; ++j) { |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 634 | if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) { |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 635 | need_leader = false; |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* if not, generate events for it */ |
| 641 | if (need_leader && |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 642 | __event__synthesize_thread(comm_event, mmap_event, |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 643 | fork_event, namespaces_event, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 644 | comm_event->comm.pid, 0, |
| 645 | process, tool, machine, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 646 | mmap_data, proc_map_timeout)) { |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 647 | err = -1; |
| 648 | break; |
| 649 | } |
| 650 | } |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 651 | } |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 652 | free(namespaces_event); |
| 653 | out_free_fork: |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 654 | free(fork_event); |
| 655 | out_free_mmap: |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 656 | free(mmap_event); |
| 657 | out_free_comm: |
| 658 | free(comm_event); |
| 659 | out: |
| 660 | return err; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 661 | } |
| 662 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 663 | int perf_event__synthesize_threads(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 664 | perf_event__handler_t process, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 665 | struct machine *machine, |
| 666 | bool mmap_data, |
| 667 | unsigned int proc_map_timeout) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 668 | { |
| 669 | DIR *proc; |
Dongsheng Yang | 9956346 | 2013-12-20 15:52:57 -0500 | [diff] [blame] | 670 | char proc_path[PATH_MAX]; |
Arnaldo Carvalho de Melo | 7093b4c | 2016-04-08 11:32:15 -0300 | [diff] [blame] | 671 | struct dirent *dirent; |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 672 | union perf_event *comm_event, *mmap_event, *fork_event; |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 673 | union perf_event *namespaces_event; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 674 | int err = -1; |
| 675 | |
Namhyung Kim | 574799b | 2014-03-17 10:45:49 +0900 | [diff] [blame] | 676 | if (machine__is_default_guest(machine)) |
| 677 | return 0; |
| 678 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 679 | 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] | 680 | if (comm_event == NULL) |
| 681 | goto out; |
| 682 | |
Wang Nan | b0fb978 | 2016-01-12 10:12:04 +0000 | [diff] [blame] | 683 | mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 684 | if (mmap_event == NULL) |
| 685 | goto out_free_comm; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 686 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 687 | fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); |
| 688 | if (fork_event == NULL) |
| 689 | goto out_free_mmap; |
| 690 | |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 691 | namespaces_event = malloc(sizeof(namespaces_event->namespaces) + |
| 692 | (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + |
| 693 | machine->id_hdr_size); |
| 694 | if (namespaces_event == NULL) |
| 695 | goto out_free_fork; |
| 696 | |
Dongsheng Yang | 9956346 | 2013-12-20 15:52:57 -0500 | [diff] [blame] | 697 | snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir); |
| 698 | proc = opendir(proc_path); |
| 699 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 700 | if (proc == NULL) |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 701 | goto out_free_namespaces; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 702 | |
Arnaldo Carvalho de Melo | 7093b4c | 2016-04-08 11:32:15 -0300 | [diff] [blame] | 703 | while ((dirent = readdir(proc)) != NULL) { |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 704 | char *end; |
Arnaldo Carvalho de Melo | 7093b4c | 2016-04-08 11:32:15 -0300 | [diff] [blame] | 705 | pid_t pid = strtol(dirent->d_name, &end, 10); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 706 | |
| 707 | if (*end) /* only interested in proper numerical dirents */ |
| 708 | continue; |
Arnaldo Carvalho de Melo | ba361c9 | 2012-10-17 18:50:13 -0300 | [diff] [blame] | 709 | /* |
| 710 | * We may race with exiting thread, so don't stop just because |
| 711 | * one thread couldn't be synthesized. |
| 712 | */ |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 713 | __event__synthesize_thread(comm_event, mmap_event, fork_event, |
| 714 | namespaces_event, pid, 1, process, |
| 715 | tool, machine, mmap_data, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 716 | proc_map_timeout); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 717 | } |
| 718 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 719 | err = 0; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 720 | closedir(proc); |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 721 | out_free_namespaces: |
| 722 | free(namespaces_event); |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 723 | out_free_fork: |
| 724 | free(fork_event); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 725 | out_free_mmap: |
| 726 | free(mmap_event); |
| 727 | out_free_comm: |
| 728 | free(comm_event); |
| 729 | out: |
| 730 | return err; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 731 | } |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 732 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 733 | struct process_symbol_args { |
| 734 | const char *name; |
| 735 | u64 start; |
| 736 | }; |
| 737 | |
Arnaldo Carvalho de Melo | 3b01a41 | 2010-12-22 01:08:36 -0200 | [diff] [blame] | 738 | 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] | 739 | u64 start) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 740 | { |
| 741 | struct process_symbol_args *args = arg; |
| 742 | |
Arnaldo Carvalho de Melo | 881516e | 2010-01-15 18:08:27 -0200 | [diff] [blame] | 743 | /* |
| 744 | * Must be a function or at least an alias, as in PARISC64, where "_text" is |
| 745 | * an 'A' to the same address as "_stext". |
| 746 | */ |
| 747 | if (!(symbol_type__is_a(type, MAP__FUNCTION) || |
| 748 | type == 'A') || strcmp(name, args->name)) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 749 | return 0; |
| 750 | |
| 751 | args->start = start; |
| 752 | return 1; |
| 753 | } |
| 754 | |
Adrian Hunter | 29b596b | 2014-01-29 16:14:37 +0200 | [diff] [blame] | 755 | u64 kallsyms__get_function_start(const char *kallsyms_filename, |
| 756 | const char *symbol_name) |
| 757 | { |
| 758 | struct process_symbol_args args = { .name = symbol_name, }; |
| 759 | |
| 760 | if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0) |
| 761 | return 0; |
| 762 | |
| 763 | return args.start; |
| 764 | } |
| 765 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 766 | int perf_event__synthesize_kernel_mmap(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 767 | perf_event__handler_t process, |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 768 | struct machine *machine) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 769 | { |
| 770 | size_t size; |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 771 | const char *mmap_name; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 772 | char name_buff[PATH_MAX]; |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 773 | struct map *map = machine__kernel_map(machine); |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 774 | struct kmap *kmap; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 775 | int err; |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 776 | union perf_event *event; |
| 777 | |
Wang Nan | 3dc6c1d | 2016-05-24 09:21:28 +0000 | [diff] [blame] | 778 | if (symbol_conf.kptr_restrict) |
| 779 | return -1; |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 780 | if (map == NULL) |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 781 | return -1; |
| 782 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 783 | /* |
| 784 | * We should get this from /sys/kernel/sections/.text, but till that is |
| 785 | * available use this, and after it is use this as a fallback for older |
| 786 | * kernels. |
| 787 | */ |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 788 | event = zalloc((sizeof(event->mmap) + machine->id_hdr_size)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 789 | if (event == NULL) { |
| 790 | pr_debug("Not enough memory synthesizing mmap event " |
| 791 | "for kernel modules\n"); |
| 792 | return -1; |
| 793 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 794 | |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 795 | 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] | 796 | if (machine__is_host(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 797 | /* |
| 798 | * kernel uses PERF_RECORD_MISC_USER for user space maps, |
| 799 | * see kernel/perf_event.c __perf_event_mmap |
| 800 | */ |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 801 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 802 | } else { |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 803 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
Thomas Jarosch | 0b9e01a | 2013-01-25 11:20:47 +0100 | [diff] [blame] | 804 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 805 | |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 806 | kmap = map__kmap(map); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 807 | size = snprintf(event->mmap.filename, sizeof(event->mmap.filename), |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 808 | "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 809 | size = PERF_ALIGN(size, sizeof(u64)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 810 | event->mmap.header.type = PERF_RECORD_MMAP; |
| 811 | event->mmap.header.size = (sizeof(event->mmap) - |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 812 | (sizeof(event->mmap.filename) - size) + machine->id_hdr_size); |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 813 | event->mmap.pgoff = kmap->ref_reloc_sym->addr; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 814 | event->mmap.start = map->start; |
| 815 | event->mmap.len = map->end - event->mmap.start; |
| 816 | event->mmap.pid = machine->pid; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 817 | |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 818 | err = perf_tool__process_synth_event(tool, event, machine, process); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 819 | free(event); |
| 820 | |
| 821 | return err; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 822 | } |
| 823 | |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 824 | int perf_event__synthesize_thread_map2(struct perf_tool *tool, |
| 825 | struct thread_map *threads, |
| 826 | perf_event__handler_t process, |
| 827 | struct machine *machine) |
| 828 | { |
| 829 | union perf_event *event; |
| 830 | int i, err, size; |
| 831 | |
| 832 | size = sizeof(event->thread_map); |
| 833 | size += threads->nr * sizeof(event->thread_map.entries[0]); |
| 834 | |
| 835 | event = zalloc(size); |
| 836 | if (!event) |
| 837 | return -ENOMEM; |
| 838 | |
| 839 | event->header.type = PERF_RECORD_THREAD_MAP; |
| 840 | event->header.size = size; |
| 841 | event->thread_map.nr = threads->nr; |
| 842 | |
| 843 | for (i = 0; i < threads->nr; i++) { |
| 844 | struct thread_map_event_entry *entry = &event->thread_map.entries[i]; |
| 845 | char *comm = thread_map__comm(threads, i); |
| 846 | |
| 847 | if (!comm) |
| 848 | comm = (char *) ""; |
| 849 | |
| 850 | entry->pid = thread_map__pid(threads, i); |
| 851 | strncpy((char *) &entry->comm, comm, sizeof(entry->comm)); |
| 852 | } |
| 853 | |
| 854 | err = process(tool, event, NULL, machine); |
| 855 | |
| 856 | free(event); |
| 857 | return err; |
| 858 | } |
| 859 | |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 860 | static void synthesize_cpus(struct cpu_map_entries *cpus, |
| 861 | struct cpu_map *map) |
| 862 | { |
| 863 | int i; |
| 864 | |
| 865 | cpus->nr = map->nr; |
| 866 | |
| 867 | for (i = 0; i < map->nr; i++) |
| 868 | cpus->cpu[i] = map->map[i]; |
| 869 | } |
| 870 | |
| 871 | static void synthesize_mask(struct cpu_map_mask *mask, |
| 872 | struct cpu_map *map, int max) |
| 873 | { |
| 874 | int i; |
| 875 | |
| 876 | mask->nr = BITS_TO_LONGS(max); |
| 877 | mask->long_size = sizeof(long); |
| 878 | |
| 879 | for (i = 0; i < map->nr; i++) |
| 880 | set_bit(map->map[i], mask->mask); |
| 881 | } |
| 882 | |
| 883 | static size_t cpus_size(struct cpu_map *map) |
| 884 | { |
| 885 | return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16); |
| 886 | } |
| 887 | |
| 888 | static size_t mask_size(struct cpu_map *map, int *max) |
| 889 | { |
| 890 | int i; |
| 891 | |
| 892 | *max = 0; |
| 893 | |
| 894 | for (i = 0; i < map->nr; i++) { |
| 895 | /* bit possition of the cpu is + 1 */ |
| 896 | int bit = map->map[i] + 1; |
| 897 | |
| 898 | if (bit > *max) |
| 899 | *max = bit; |
| 900 | } |
| 901 | |
| 902 | return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long); |
| 903 | } |
| 904 | |
| 905 | void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max) |
| 906 | { |
| 907 | size_t size_cpus, size_mask; |
| 908 | bool is_dummy = cpu_map__empty(map); |
| 909 | |
| 910 | /* |
| 911 | * Both array and mask data have variable size based |
| 912 | * on the number of cpus and their actual values. |
| 913 | * The size of the 'struct cpu_map_data' is: |
| 914 | * |
| 915 | * array = size of 'struct cpu_map_entries' + |
| 916 | * number of cpus * sizeof(u64) |
| 917 | * |
| 918 | * mask = size of 'struct cpu_map_mask' + |
| 919 | * maximum cpu bit converted to size of longs |
| 920 | * |
| 921 | * and finaly + the size of 'struct cpu_map_data'. |
| 922 | */ |
| 923 | size_cpus = cpus_size(map); |
| 924 | size_mask = mask_size(map, max); |
| 925 | |
| 926 | if (is_dummy || (size_cpus < size_mask)) { |
| 927 | *size += size_cpus; |
| 928 | *type = PERF_CPU_MAP__CPUS; |
| 929 | } else { |
| 930 | *size += size_mask; |
| 931 | *type = PERF_CPU_MAP__MASK; |
| 932 | } |
| 933 | |
| 934 | *size += sizeof(struct cpu_map_data); |
| 935 | return zalloc(*size); |
| 936 | } |
| 937 | |
| 938 | void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map, |
| 939 | u16 type, int max) |
| 940 | { |
| 941 | data->type = type; |
| 942 | |
| 943 | switch (type) { |
| 944 | case PERF_CPU_MAP__CPUS: |
| 945 | synthesize_cpus((struct cpu_map_entries *) data->data, map); |
| 946 | break; |
| 947 | case PERF_CPU_MAP__MASK: |
| 948 | synthesize_mask((struct cpu_map_mask *) data->data, map, max); |
| 949 | default: |
| 950 | break; |
| 951 | }; |
| 952 | } |
| 953 | |
| 954 | static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map) |
| 955 | { |
| 956 | size_t size = sizeof(struct cpu_map_event); |
| 957 | struct cpu_map_event *event; |
| 958 | int max; |
| 959 | u16 type; |
| 960 | |
| 961 | event = cpu_map_data__alloc(map, &size, &type, &max); |
| 962 | if (!event) |
| 963 | return NULL; |
| 964 | |
| 965 | event->header.type = PERF_RECORD_CPU_MAP; |
| 966 | event->header.size = size; |
| 967 | event->data.type = type; |
| 968 | |
| 969 | cpu_map_data__synthesize(&event->data, map, type, max); |
| 970 | return event; |
| 971 | } |
| 972 | |
| 973 | int perf_event__synthesize_cpu_map(struct perf_tool *tool, |
| 974 | struct cpu_map *map, |
| 975 | perf_event__handler_t process, |
| 976 | struct machine *machine) |
| 977 | { |
| 978 | struct cpu_map_event *event; |
| 979 | int err; |
| 980 | |
| 981 | event = cpu_map_event__new(map); |
| 982 | if (!event) |
| 983 | return -ENOMEM; |
| 984 | |
| 985 | err = process(tool, (union perf_event *) event, NULL, machine); |
| 986 | |
| 987 | free(event); |
| 988 | return err; |
| 989 | } |
| 990 | |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 991 | int perf_event__synthesize_stat_config(struct perf_tool *tool, |
| 992 | struct perf_stat_config *config, |
| 993 | perf_event__handler_t process, |
| 994 | struct machine *machine) |
| 995 | { |
| 996 | struct stat_config_event *event; |
| 997 | int size, i = 0, err; |
| 998 | |
| 999 | size = sizeof(*event); |
| 1000 | size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0])); |
| 1001 | |
| 1002 | event = zalloc(size); |
| 1003 | if (!event) |
| 1004 | return -ENOMEM; |
| 1005 | |
| 1006 | event->header.type = PERF_RECORD_STAT_CONFIG; |
| 1007 | event->header.size = size; |
| 1008 | event->nr = PERF_STAT_CONFIG_TERM__MAX; |
| 1009 | |
| 1010 | #define ADD(__term, __val) \ |
| 1011 | event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \ |
| 1012 | event->data[i].val = __val; \ |
| 1013 | i++; |
| 1014 | |
| 1015 | ADD(AGGR_MODE, config->aggr_mode) |
| 1016 | ADD(INTERVAL, config->interval) |
| 1017 | ADD(SCALE, config->scale) |
| 1018 | |
| 1019 | WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX, |
| 1020 | "stat config terms unbalanced\n"); |
| 1021 | #undef ADD |
| 1022 | |
| 1023 | err = process(tool, (union perf_event *) event, NULL, machine); |
| 1024 | |
| 1025 | free(event); |
| 1026 | return err; |
| 1027 | } |
| 1028 | |
Jiri Olsa | 5796f8f | 2015-10-25 15:51:31 +0100 | [diff] [blame] | 1029 | int perf_event__synthesize_stat(struct perf_tool *tool, |
| 1030 | u32 cpu, u32 thread, u64 id, |
| 1031 | struct perf_counts_values *count, |
| 1032 | perf_event__handler_t process, |
| 1033 | struct machine *machine) |
| 1034 | { |
| 1035 | struct stat_event event; |
| 1036 | |
| 1037 | event.header.type = PERF_RECORD_STAT; |
| 1038 | event.header.size = sizeof(event); |
| 1039 | event.header.misc = 0; |
| 1040 | |
| 1041 | event.id = id; |
| 1042 | event.cpu = cpu; |
| 1043 | event.thread = thread; |
| 1044 | event.val = count->val; |
| 1045 | event.ena = count->ena; |
| 1046 | event.run = count->run; |
| 1047 | |
| 1048 | return process(tool, (union perf_event *) &event, NULL, machine); |
| 1049 | } |
| 1050 | |
Jiri Olsa | d4c2259 | 2015-10-25 15:51:34 +0100 | [diff] [blame] | 1051 | int perf_event__synthesize_stat_round(struct perf_tool *tool, |
| 1052 | u64 evtime, u64 type, |
| 1053 | perf_event__handler_t process, |
| 1054 | struct machine *machine) |
| 1055 | { |
| 1056 | struct stat_round_event event; |
| 1057 | |
| 1058 | event.header.type = PERF_RECORD_STAT_ROUND; |
| 1059 | event.header.size = sizeof(event); |
| 1060 | event.header.misc = 0; |
| 1061 | |
| 1062 | event.time = evtime; |
| 1063 | event.type = type; |
| 1064 | |
| 1065 | return process(tool, (union perf_event *) &event, NULL, machine); |
| 1066 | } |
| 1067 | |
Jiri Olsa | 8e38159 | 2015-10-25 15:51:29 +0100 | [diff] [blame] | 1068 | void perf_event__read_stat_config(struct perf_stat_config *config, |
| 1069 | struct stat_config_event *event) |
| 1070 | { |
| 1071 | unsigned i; |
| 1072 | |
| 1073 | for (i = 0; i < event->nr; i++) { |
| 1074 | |
| 1075 | switch (event->data[i].tag) { |
| 1076 | #define CASE(__term, __val) \ |
| 1077 | case PERF_STAT_CONFIG_TERM__##__term: \ |
| 1078 | config->__val = event->data[i].val; \ |
| 1079 | break; |
| 1080 | |
| 1081 | CASE(AGGR_MODE, aggr_mode) |
| 1082 | CASE(SCALE, scale) |
| 1083 | CASE(INTERVAL, interval) |
| 1084 | #undef CASE |
| 1085 | default: |
| 1086 | pr_warning("unknown stat config term %" PRIu64 "\n", |
| 1087 | event->data[i].tag); |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1092 | size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp) |
| 1093 | { |
Adrian Hunter | 022c50d | 2014-07-14 13:02:27 +0300 | [diff] [blame] | 1094 | const char *s; |
| 1095 | |
| 1096 | if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC) |
| 1097 | s = " exec"; |
| 1098 | else |
| 1099 | s = ""; |
| 1100 | |
Arnaldo Carvalho de Melo | 5067406 | 2015-02-24 17:20:31 -0300 | [diff] [blame] | 1101 | 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] | 1102 | } |
| 1103 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 1104 | size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp) |
| 1105 | { |
| 1106 | size_t ret = 0; |
| 1107 | struct perf_ns_link_info *ns_link_info; |
| 1108 | u32 nr_namespaces, idx; |
| 1109 | |
| 1110 | ns_link_info = event->namespaces.link_info; |
| 1111 | nr_namespaces = event->namespaces.nr_namespaces; |
| 1112 | |
| 1113 | ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[", |
| 1114 | event->namespaces.pid, |
| 1115 | event->namespaces.tid, |
| 1116 | nr_namespaces); |
| 1117 | |
| 1118 | for (idx = 0; idx < nr_namespaces; idx++) { |
| 1119 | if (idx && (idx % 4 == 0)) |
| 1120 | ret += fprintf(fp, "\n\t\t "); |
| 1121 | |
| 1122 | ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx, |
| 1123 | perf_ns__name(idx), (u64)ns_link_info[idx].dev, |
| 1124 | (u64)ns_link_info[idx].ino, |
| 1125 | ((idx + 1) != nr_namespaces) ? ", " : "]\n"); |
| 1126 | } |
| 1127 | |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1131 | 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] | 1132 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1133 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1134 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1135 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1136 | return machine__process_comm_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1137 | } |
| 1138 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 1139 | int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused, |
| 1140 | union perf_event *event, |
| 1141 | struct perf_sample *sample, |
| 1142 | struct machine *machine) |
| 1143 | { |
| 1144 | return machine__process_namespaces_event(machine, event, sample); |
| 1145 | } |
| 1146 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1147 | 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] | 1148 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1149 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1150 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1151 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1152 | return machine__process_lost_event(machine, event, sample); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1153 | } |
| 1154 | |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1155 | int perf_event__process_aux(struct perf_tool *tool __maybe_unused, |
| 1156 | union perf_event *event, |
| 1157 | struct perf_sample *sample __maybe_unused, |
| 1158 | struct machine *machine) |
| 1159 | { |
| 1160 | return machine__process_aux_event(machine, event); |
| 1161 | } |
| 1162 | |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1163 | int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused, |
| 1164 | union perf_event *event, |
| 1165 | struct perf_sample *sample __maybe_unused, |
| 1166 | struct machine *machine) |
| 1167 | { |
| 1168 | return machine__process_itrace_start_event(machine, event); |
| 1169 | } |
| 1170 | |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 1171 | int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused, |
| 1172 | union perf_event *event, |
| 1173 | struct perf_sample *sample, |
| 1174 | struct machine *machine) |
| 1175 | { |
| 1176 | return machine__process_lost_samples_event(machine, event, sample); |
| 1177 | } |
| 1178 | |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1179 | int perf_event__process_switch(struct perf_tool *tool __maybe_unused, |
| 1180 | union perf_event *event, |
| 1181 | struct perf_sample *sample __maybe_unused, |
| 1182 | struct machine *machine) |
| 1183 | { |
| 1184 | return machine__process_switch_event(machine, event); |
| 1185 | } |
| 1186 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1187 | size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp) |
| 1188 | { |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 1189 | 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] | 1190 | event->mmap.pid, event->mmap.tid, event->mmap.start, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 1191 | event->mmap.len, event->mmap.pgoff, |
| 1192 | (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x', |
| 1193 | event->mmap.filename); |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1194 | } |
| 1195 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1196 | size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp) |
| 1197 | { |
| 1198 | return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1199 | " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n", |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1200 | event->mmap2.pid, event->mmap2.tid, event->mmap2.start, |
| 1201 | event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj, |
| 1202 | event->mmap2.min, event->mmap2.ino, |
| 1203 | event->mmap2.ino_generation, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1204 | (event->mmap2.prot & PROT_READ) ? 'r' : '-', |
| 1205 | (event->mmap2.prot & PROT_WRITE) ? 'w' : '-', |
| 1206 | (event->mmap2.prot & PROT_EXEC) ? 'x' : '-', |
| 1207 | (event->mmap2.flags & MAP_SHARED) ? 's' : 'p', |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1208 | event->mmap2.filename); |
| 1209 | } |
| 1210 | |
Jiri Olsa | ec7fa596 | 2015-10-25 15:51:22 +0100 | [diff] [blame] | 1211 | size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp) |
| 1212 | { |
| 1213 | struct thread_map *threads = thread_map__new_event(&event->thread_map); |
| 1214 | size_t ret; |
| 1215 | |
| 1216 | ret = fprintf(fp, " nr: "); |
| 1217 | |
| 1218 | if (threads) |
| 1219 | ret += thread_map__fprintf(threads, fp); |
| 1220 | else |
| 1221 | ret += fprintf(fp, "failed to get threads from event\n"); |
| 1222 | |
| 1223 | thread_map__put(threads); |
| 1224 | return ret; |
| 1225 | } |
| 1226 | |
Jiri Olsa | eb12a1a | 2015-10-25 15:51:26 +0100 | [diff] [blame] | 1227 | size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp) |
| 1228 | { |
| 1229 | struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data); |
| 1230 | size_t ret; |
| 1231 | |
Jiri Olsa | a24020e | 2016-06-28 13:29:04 +0200 | [diff] [blame] | 1232 | ret = fprintf(fp, ": "); |
Jiri Olsa | eb12a1a | 2015-10-25 15:51:26 +0100 | [diff] [blame] | 1233 | |
| 1234 | if (cpus) |
| 1235 | ret += cpu_map__fprintf(cpus, fp); |
| 1236 | else |
| 1237 | ret += fprintf(fp, "failed to get cpumap from event\n"); |
| 1238 | |
| 1239 | cpu_map__put(cpus); |
| 1240 | return ret; |
| 1241 | } |
| 1242 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1243 | 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] | 1244 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1245 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1246 | struct machine *machine) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1247 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1248 | return machine__process_mmap_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1249 | } |
| 1250 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1251 | int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused, |
| 1252 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1253 | struct perf_sample *sample, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1254 | struct machine *machine) |
| 1255 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1256 | return machine__process_mmap2_event(machine, event, sample); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1257 | } |
| 1258 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1259 | size_t perf_event__fprintf_task(union perf_event *event, FILE *fp) |
| 1260 | { |
| 1261 | return fprintf(fp, "(%d:%d):(%d:%d)\n", |
| 1262 | event->fork.pid, event->fork.tid, |
| 1263 | event->fork.ppid, event->fork.ptid); |
| 1264 | } |
| 1265 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1266 | 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] | 1267 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1268 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1269 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1270 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1271 | return machine__process_fork_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1272 | } |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1273 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1274 | int perf_event__process_exit(struct perf_tool *tool __maybe_unused, |
| 1275 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1276 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1277 | struct machine *machine) |
| 1278 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1279 | return machine__process_exit_event(machine, event, sample); |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1280 | } |
| 1281 | |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1282 | size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp) |
| 1283 | { |
Alexander Shishkin | 05a1f47 | 2017-03-16 18:41:59 +0200 | [diff] [blame] | 1284 | return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s%s]\n", |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1285 | event->aux.aux_offset, event->aux.aux_size, |
| 1286 | event->aux.flags, |
| 1287 | event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "", |
Alexander Shishkin | 05a1f47 | 2017-03-16 18:41:59 +0200 | [diff] [blame] | 1288 | event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "", |
| 1289 | event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : ""); |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1290 | } |
| 1291 | |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1292 | size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp) |
| 1293 | { |
| 1294 | return fprintf(fp, " pid: %u tid: %u\n", |
| 1295 | event->itrace_start.pid, event->itrace_start.tid); |
| 1296 | } |
| 1297 | |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1298 | size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp) |
| 1299 | { |
| 1300 | bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; |
| 1301 | const char *in_out = out ? "OUT" : "IN "; |
| 1302 | |
| 1303 | if (event->header.type == PERF_RECORD_SWITCH) |
| 1304 | return fprintf(fp, " %s\n", in_out); |
| 1305 | |
| 1306 | return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n", |
| 1307 | in_out, out ? "next" : "prev", |
| 1308 | event->context_switch.next_prev_pid, |
| 1309 | event->context_switch.next_prev_tid); |
| 1310 | } |
| 1311 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1312 | size_t perf_event__fprintf(union perf_event *event, FILE *fp) |
| 1313 | { |
| 1314 | size_t ret = fprintf(fp, "PERF_RECORD_%s", |
| 1315 | perf_event__name(event->header.type)); |
| 1316 | |
| 1317 | switch (event->header.type) { |
| 1318 | case PERF_RECORD_COMM: |
| 1319 | ret += perf_event__fprintf_comm(event, fp); |
| 1320 | break; |
| 1321 | case PERF_RECORD_FORK: |
| 1322 | case PERF_RECORD_EXIT: |
| 1323 | ret += perf_event__fprintf_task(event, fp); |
| 1324 | break; |
| 1325 | case PERF_RECORD_MMAP: |
| 1326 | ret += perf_event__fprintf_mmap(event, fp); |
| 1327 | break; |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 1328 | case PERF_RECORD_NAMESPACES: |
| 1329 | ret += perf_event__fprintf_namespaces(event, fp); |
| 1330 | break; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1331 | case PERF_RECORD_MMAP2: |
| 1332 | ret += perf_event__fprintf_mmap2(event, fp); |
| 1333 | break; |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1334 | case PERF_RECORD_AUX: |
| 1335 | ret += perf_event__fprintf_aux(event, fp); |
| 1336 | break; |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1337 | case PERF_RECORD_ITRACE_START: |
| 1338 | ret += perf_event__fprintf_itrace_start(event, fp); |
| 1339 | break; |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1340 | case PERF_RECORD_SWITCH: |
| 1341 | case PERF_RECORD_SWITCH_CPU_WIDE: |
| 1342 | ret += perf_event__fprintf_switch(event, fp); |
| 1343 | break; |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1344 | default: |
| 1345 | ret += fprintf(fp, "\n"); |
| 1346 | } |
| 1347 | |
| 1348 | return ret; |
| 1349 | } |
| 1350 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1351 | int perf_event__process(struct perf_tool *tool __maybe_unused, |
| 1352 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1353 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1354 | struct machine *machine) |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 1355 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1356 | return machine__process_event(machine, event, sample); |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 1357 | } |
| 1358 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1359 | void thread__find_addr_map(struct thread *thread, u8 cpumode, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1360 | enum map_type type, u64 addr, |
Adrian Hunter | 326f59b | 2013-08-08 14:32:27 +0300 | [diff] [blame] | 1361 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1362 | { |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 1363 | struct map_groups *mg = thread->mg; |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1364 | struct machine *machine = mg->machine; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1365 | bool load_map = false; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1366 | |
Arnaldo Carvalho de Melo | cc22e57 | 2013-12-19 17:20:06 -0300 | [diff] [blame] | 1367 | al->machine = machine; |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1368 | al->thread = thread; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1369 | al->addr = addr; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1370 | al->cpumode = cpumode; |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1371 | al->filtered = 0; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1372 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1373 | if (machine == NULL) { |
| 1374 | al->map = NULL; |
| 1375 | return; |
| 1376 | } |
| 1377 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1378 | if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1379 | al->level = 'k'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1380 | mg = &machine->kmaps; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1381 | load_map = true; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1382 | } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1383 | al->level = '.'; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1384 | } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { |
| 1385 | al->level = 'g'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1386 | mg = &machine->kmaps; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1387 | load_map = true; |
Dongsheng Yang | fb50bb4 | 2013-12-20 15:52:56 -0500 | [diff] [blame] | 1388 | } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) { |
| 1389 | al->level = 'u'; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1390 | } else { |
Dongsheng Yang | fb50bb4 | 2013-12-20 15:52:56 -0500 | [diff] [blame] | 1391 | al->level = 'H'; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1392 | al->map = NULL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1393 | |
| 1394 | if ((cpumode == PERF_RECORD_MISC_GUEST_USER || |
| 1395 | cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && |
| 1396 | !perf_guest) |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1397 | al->filtered |= (1 << HIST_FILTER__GUEST); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1398 | if ((cpumode == PERF_RECORD_MISC_USER || |
| 1399 | cpumode == PERF_RECORD_MISC_KERNEL) && |
| 1400 | !perf_host) |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1401 | al->filtered |= (1 << HIST_FILTER__HOST); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1402 | |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1403 | return; |
| 1404 | } |
| 1405 | try_again: |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 1406 | al->map = map_groups__find(mg, type, al->addr); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1407 | if (al->map == NULL) { |
| 1408 | /* |
| 1409 | * If this is outside of all known maps, and is a negative |
| 1410 | * address, try to look it up in the kernel dso, as it might be |
| 1411 | * a vsyscall or vdso (which executes in user-mode). |
| 1412 | * |
| 1413 | * XXX This is nasty, we should have a symbol list in the |
| 1414 | * "[vdso]" dso, but for now lets use the old trick of looking |
| 1415 | * in the whole kernel symbol list. |
| 1416 | */ |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 1417 | if (cpumode == PERF_RECORD_MISC_USER && machine && |
| 1418 | mg != &machine->kmaps && |
| 1419 | machine__kernel_ip(machine, al->addr)) { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1420 | mg = &machine->kmaps; |
Adrian Hunter | 1f2a706 | 2014-07-14 13:02:31 +0300 | [diff] [blame] | 1421 | load_map = true; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1422 | goto try_again; |
| 1423 | } |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1424 | } else { |
| 1425 | /* |
| 1426 | * Kernel maps might be changed when loading symbols so loading |
| 1427 | * must be done prior to using kernel maps. |
| 1428 | */ |
| 1429 | if (load_map) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1430 | map__load(al->map); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1431 | al->addr = al->map->map_ip(al->map, al->addr); |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1432 | } |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 1433 | } |
| 1434 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1435 | void thread__find_addr_location(struct thread *thread, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1436 | u8 cpumode, enum map_type type, u64 addr, |
Adrian Hunter | 61710bd | 2013-08-08 14:32:26 +0300 | [diff] [blame] | 1437 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 1438 | { |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1439 | thread__find_addr_map(thread, cpumode, type, addr, al); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 1440 | if (al->map != NULL) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1441 | al->sym = map__find_symbol(al->map, al->addr); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 1442 | else |
| 1443 | al->sym = NULL; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1444 | } |
| 1445 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1446 | /* |
| 1447 | * Callers need to drop the reference to al->thread, obtained in |
| 1448 | * machine__findnew_thread() |
| 1449 | */ |
Arnaldo Carvalho de Melo | bb3eb56 | 2016-03-22 18:39:09 -0300 | [diff] [blame] | 1450 | int machine__resolve(struct machine *machine, struct addr_location *al, |
| 1451 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1452 | { |
Adrian Hunter | ef89325 | 2013-08-27 11:23:06 +0300 | [diff] [blame] | 1453 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
Namhyung Kim | 13ce34d | 2014-05-12 09:56:42 +0900 | [diff] [blame] | 1454 | sample->tid); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1455 | |
| 1456 | if (thread == NULL) |
| 1457 | return -1; |
| 1458 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 1459 | 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] | 1460 | /* |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1461 | * Have we already created the kernel maps for this machine? |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 1462 | * |
| 1463 | * This should have happened earlier, when we processed the kernel MMAP |
| 1464 | * events, but for older perf.data files there was no such thing, so do |
| 1465 | * it now. |
| 1466 | */ |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1467 | if (sample->cpumode == PERF_RECORD_MISC_KERNEL && |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 1468 | machine__kernel_map(machine) == NULL) |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1469 | machine__create_kernel_maps(machine); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1470 | |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1471 | thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1472 | dump_printf(" ...... dso: %s\n", |
| 1473 | al->map ? al->map->dso->long_name : |
| 1474 | al->level == 'H' ? "[hypervisor]" : "<not found>"); |
Namhyung Kim | 466fa76 | 2014-03-17 17:12:27 -0300 | [diff] [blame] | 1475 | |
| 1476 | if (thread__is_filtered(thread)) |
| 1477 | al->filtered |= (1 << HIST_FILTER__THREAD); |
| 1478 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1479 | al->sym = NULL; |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 1480 | al->cpu = sample->cpu; |
Kan Liang | 0c4c4deb | 2015-09-04 10:45:42 -0400 | [diff] [blame] | 1481 | al->socket = -1; |
| 1482 | |
| 1483 | if (al->cpu >= 0) { |
| 1484 | struct perf_env *env = machine->env; |
| 1485 | |
| 1486 | if (env && env->cpu) |
| 1487 | al->socket = env->cpu[al->cpu].socket_id; |
| 1488 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1489 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1490 | if (al->map) { |
Namhyung Kim | cb8f4e9 | 2011-12-13 00:16:55 +0900 | [diff] [blame] | 1491 | struct dso *dso = al->map->dso; |
| 1492 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1493 | if (symbol_conf.dso_list && |
Namhyung Kim | cb8f4e9 | 2011-12-13 00:16:55 +0900 | [diff] [blame] | 1494 | (!dso || !(strlist__has_entry(symbol_conf.dso_list, |
| 1495 | dso->short_name) || |
| 1496 | (dso->short_name != dso->long_name && |
| 1497 | strlist__has_entry(symbol_conf.dso_list, |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1498 | dso->long_name))))) { |
| 1499 | al->filtered |= (1 << HIST_FILTER__DSO); |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1500 | } |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1501 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1502 | al->sym = map__find_symbol(al->map, al->addr); |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1503 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1504 | |
Feng Tang | 1500b93 | 2012-09-07 16:42:23 +0800 | [diff] [blame] | 1505 | if (symbol_conf.sym_list && |
| 1506 | (!al->sym || !strlist__has_entry(symbol_conf.sym_list, |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1507 | al->sym->name))) { |
| 1508 | al->filtered |= (1 << HIST_FILTER__SYMBOL); |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1509 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1510 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1511 | return 0; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1512 | } |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1513 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1514 | /* |
| 1515 | * The preprocess_sample method will return with reference counts for the |
| 1516 | * in it, when done using (and perhaps getting ref counts if needing to |
| 1517 | * keep a pointer to one of those entries) it must be paired with |
| 1518 | * addr_location__put(), so that the refcounts can be decremented. |
| 1519 | */ |
| 1520 | void addr_location__put(struct addr_location *al) |
| 1521 | { |
| 1522 | thread__zput(al->thread); |
| 1523 | } |
| 1524 | |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1525 | bool is_bts_event(struct perf_event_attr *attr) |
| 1526 | { |
| 1527 | return attr->type == PERF_TYPE_HARDWARE && |
| 1528 | (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && |
| 1529 | attr->sample_period == 1; |
| 1530 | } |
| 1531 | |
| 1532 | bool sample_addr_correlates_sym(struct perf_event_attr *attr) |
| 1533 | { |
| 1534 | if (attr->type == PERF_TYPE_SOFTWARE && |
| 1535 | (attr->config == PERF_COUNT_SW_PAGE_FAULTS || |
| 1536 | attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN || |
| 1537 | attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)) |
| 1538 | return true; |
| 1539 | |
| 1540 | if (is_bts_event(attr)) |
| 1541 | return true; |
| 1542 | |
| 1543 | return false; |
| 1544 | } |
| 1545 | |
Arnaldo Carvalho de Melo | c2740a8 | 2016-03-22 18:44:46 -0300 | [diff] [blame] | 1546 | void thread__resolve(struct thread *thread, struct addr_location *al, |
| 1547 | struct perf_sample *sample) |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1548 | { |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1549 | thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al); |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1550 | if (!al->map) |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1551 | thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE, |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1552 | sample->addr, al); |
| 1553 | |
| 1554 | al->cpu = sample->cpu; |
| 1555 | al->sym = NULL; |
| 1556 | |
| 1557 | if (al->map) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1558 | al->sym = map__find_symbol(al->map, al->addr); |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1559 | } |