Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 1 | #include <errno.h> |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 2 | #include <linux/kernel.h> |
Borislav Petkov | d944c4e | 2014-04-25 21:31:02 +0200 | [diff] [blame] | 3 | #include <linux/types.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 4 | #include <inttypes.h> |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <unistd.h> |
| 7 | #include <stdio.h> |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
| 10 | #include "parse-events.h" |
| 11 | #include "evlist.h" |
| 12 | #include "evsel.h" |
| 13 | #include "thread_map.h" |
| 14 | #include "cpumap.h" |
| 15 | #include "machine.h" |
| 16 | #include "event.h" |
| 17 | #include "thread.h" |
| 18 | |
| 19 | #include "tests.h" |
| 20 | |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 21 | #include "sane_ctype.h" |
| 22 | |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 23 | #define BUFSZ 1024 |
| 24 | #define READLEN 128 |
| 25 | |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 26 | struct state { |
| 27 | u64 done[1024]; |
| 28 | size_t done_cnt; |
| 29 | }; |
| 30 | |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 31 | static unsigned int hex(char c) |
| 32 | { |
| 33 | if (c >= '0' && c <= '9') |
| 34 | return c - '0'; |
| 35 | if (c >= 'a' && c <= 'f') |
| 36 | return c - 'a' + 10; |
| 37 | return c - 'A' + 10; |
| 38 | } |
| 39 | |
Jan Stancek | b2d0dbf | 2016-01-12 11:07:44 +0100 | [diff] [blame] | 40 | static size_t read_objdump_chunk(const char **line, unsigned char **buf, |
| 41 | size_t *buf_len) |
| 42 | { |
| 43 | size_t bytes_read = 0; |
| 44 | unsigned char *chunk_start = *buf; |
| 45 | |
| 46 | /* Read bytes */ |
| 47 | while (*buf_len > 0) { |
| 48 | char c1, c2; |
| 49 | |
| 50 | /* Get 2 hex digits */ |
| 51 | c1 = *(*line)++; |
| 52 | if (!isxdigit(c1)) |
| 53 | break; |
| 54 | c2 = *(*line)++; |
| 55 | if (!isxdigit(c2)) |
| 56 | break; |
| 57 | |
| 58 | /* Store byte and advance buf */ |
| 59 | **buf = (hex(c1) << 4) | hex(c2); |
| 60 | (*buf)++; |
| 61 | (*buf_len)--; |
| 62 | bytes_read++; |
| 63 | |
| 64 | /* End of chunk? */ |
| 65 | if (isspace(**line)) |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * objdump will display raw insn as LE if code endian |
| 71 | * is LE and bytes_per_chunk > 1. In that case reverse |
| 72 | * the chunk we just read. |
| 73 | * |
| 74 | * see disassemble_bytes() at binutils/objdump.c for details |
| 75 | * how objdump chooses display endian) |
| 76 | */ |
| 77 | if (bytes_read > 1 && !bigendian()) { |
| 78 | unsigned char *chunk_end = chunk_start + bytes_read - 1; |
| 79 | unsigned char tmp; |
| 80 | |
| 81 | while (chunk_start < chunk_end) { |
| 82 | tmp = *chunk_start; |
| 83 | *chunk_start = *chunk_end; |
| 84 | *chunk_end = tmp; |
| 85 | chunk_start++; |
| 86 | chunk_end--; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return bytes_read; |
| 91 | } |
| 92 | |
| 93 | static size_t read_objdump_line(const char *line, unsigned char *buf, |
| 94 | size_t buf_len) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 95 | { |
| 96 | const char *p; |
Jan Stancek | b2d0dbf | 2016-01-12 11:07:44 +0100 | [diff] [blame] | 97 | size_t ret, bytes_read = 0; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 98 | |
| 99 | /* Skip to a colon */ |
| 100 | p = strchr(line, ':'); |
| 101 | if (!p) |
Jan Stancek | 729a7ed | 2015-09-02 10:19:14 +0200 | [diff] [blame] | 102 | return 0; |
Jan Stancek | b2d0dbf | 2016-01-12 11:07:44 +0100 | [diff] [blame] | 103 | p++; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 104 | |
Jan Stancek | b2d0dbf | 2016-01-12 11:07:44 +0100 | [diff] [blame] | 105 | /* Skip initial spaces */ |
| 106 | while (*p) { |
| 107 | if (!isspace(*p)) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 108 | break; |
Jan Stancek | b2d0dbf | 2016-01-12 11:07:44 +0100 | [diff] [blame] | 109 | p++; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 110 | } |
Jan Stancek | b2d0dbf | 2016-01-12 11:07:44 +0100 | [diff] [blame] | 111 | |
| 112 | do { |
| 113 | ret = read_objdump_chunk(&p, &buf, &buf_len); |
| 114 | bytes_read += ret; |
| 115 | p++; |
| 116 | } while (ret > 0); |
| 117 | |
Jan Stancek | 729a7ed | 2015-09-02 10:19:14 +0200 | [diff] [blame] | 118 | /* return number of successfully read bytes */ |
Jan Stancek | b2d0dbf | 2016-01-12 11:07:44 +0100 | [diff] [blame] | 119 | return bytes_read; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 120 | } |
| 121 | |
Jan Stancek | 729a7ed | 2015-09-02 10:19:14 +0200 | [diff] [blame] | 122 | static int read_objdump_output(FILE *f, void *buf, size_t *len, u64 start_addr) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 123 | { |
| 124 | char *line = NULL; |
Jan Stancek | 729a7ed | 2015-09-02 10:19:14 +0200 | [diff] [blame] | 125 | size_t line_len, off_last = 0; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 126 | ssize_t ret; |
| 127 | int err = 0; |
Jan Stancek | edfdb7e | 2015-09-02 10:19:16 +0200 | [diff] [blame] | 128 | u64 addr, last_addr = start_addr; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 129 | |
Jan Stancek | 729a7ed | 2015-09-02 10:19:14 +0200 | [diff] [blame] | 130 | while (off_last < *len) { |
| 131 | size_t off, read_bytes, written_bytes; |
| 132 | unsigned char tmp[BUFSZ]; |
| 133 | |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 134 | ret = getline(&line, &line_len, f); |
| 135 | if (feof(f)) |
| 136 | break; |
| 137 | if (ret < 0) { |
| 138 | pr_debug("getline failed\n"); |
| 139 | err = -1; |
| 140 | break; |
| 141 | } |
Jan Stancek | 729a7ed | 2015-09-02 10:19:14 +0200 | [diff] [blame] | 142 | |
| 143 | /* read objdump data into temporary buffer */ |
Jan Stancek | b2d0dbf | 2016-01-12 11:07:44 +0100 | [diff] [blame] | 144 | read_bytes = read_objdump_line(line, tmp, sizeof(tmp)); |
Jan Stancek | 729a7ed | 2015-09-02 10:19:14 +0200 | [diff] [blame] | 145 | if (!read_bytes) |
| 146 | continue; |
| 147 | |
| 148 | if (sscanf(line, "%"PRIx64, &addr) != 1) |
| 149 | continue; |
Jan Stancek | edfdb7e | 2015-09-02 10:19:16 +0200 | [diff] [blame] | 150 | if (addr < last_addr) { |
| 151 | pr_debug("addr going backwards, read beyond section?\n"); |
| 152 | break; |
| 153 | } |
| 154 | last_addr = addr; |
Jan Stancek | 729a7ed | 2015-09-02 10:19:14 +0200 | [diff] [blame] | 155 | |
| 156 | /* copy it from temporary buffer to 'buf' according |
| 157 | * to address on current objdump line */ |
| 158 | off = addr - start_addr; |
| 159 | if (off >= *len) |
| 160 | break; |
| 161 | written_bytes = MIN(read_bytes, *len - off); |
| 162 | memcpy(buf + off, tmp, written_bytes); |
| 163 | off_last = off + written_bytes; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 164 | } |
| 165 | |
Jan Stancek | 729a7ed | 2015-09-02 10:19:14 +0200 | [diff] [blame] | 166 | /* len returns number of bytes that could not be read */ |
| 167 | *len -= off_last; |
| 168 | |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 169 | free(line); |
| 170 | |
| 171 | return err; |
| 172 | } |
| 173 | |
| 174 | static int read_via_objdump(const char *filename, u64 addr, void *buf, |
| 175 | size_t len) |
| 176 | { |
| 177 | char cmd[PATH_MAX * 2]; |
| 178 | const char *fmt; |
| 179 | FILE *f; |
| 180 | int ret; |
| 181 | |
Jan Stancek | 06f679c | 2015-09-03 13:23:32 +0200 | [diff] [blame] | 182 | fmt = "%s -z -d --start-address=0x%"PRIx64" --stop-address=0x%"PRIx64" %s"; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 183 | ret = snprintf(cmd, sizeof(cmd), fmt, "objdump", addr, addr + len, |
| 184 | filename); |
| 185 | if (ret <= 0 || (size_t)ret >= sizeof(cmd)) |
| 186 | return -1; |
| 187 | |
| 188 | pr_debug("Objdump command is: %s\n", cmd); |
| 189 | |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 190 | /* Ignore objdump errors */ |
| 191 | strcat(cmd, " 2>/dev/null"); |
| 192 | |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 193 | f = popen(cmd, "r"); |
| 194 | if (!f) { |
| 195 | pr_debug("popen failed\n"); |
| 196 | return -1; |
| 197 | } |
| 198 | |
Jan Stancek | 729a7ed | 2015-09-02 10:19:14 +0200 | [diff] [blame] | 199 | ret = read_objdump_output(f, buf, &len, addr); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 200 | if (len) { |
Jan Stancek | b2d0dbf | 2016-01-12 11:07:44 +0100 | [diff] [blame] | 201 | pr_debug("objdump read too few bytes: %zd\n", len); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 202 | if (!ret) |
| 203 | ret = len; |
| 204 | } |
| 205 | |
| 206 | pclose(f); |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
Jan Stancek | fd405cf | 2015-09-02 10:19:17 +0200 | [diff] [blame] | 211 | static void dump_buf(unsigned char *buf, size_t len) |
| 212 | { |
| 213 | size_t i; |
| 214 | |
| 215 | for (i = 0; i < len; i++) { |
| 216 | pr_debug("0x%02x ", buf[i]); |
| 217 | if (i % 16 == 15) |
| 218 | pr_debug("\n"); |
| 219 | } |
| 220 | pr_debug("\n"); |
| 221 | } |
| 222 | |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 223 | static int read_object_code(u64 addr, size_t len, u8 cpumode, |
Arnaldo Carvalho de Melo | 29f9e52 | 2014-10-23 17:21:54 -0300 | [diff] [blame] | 224 | struct thread *thread, struct state *state) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 225 | { |
| 226 | struct addr_location al; |
| 227 | unsigned char buf1[BUFSZ]; |
| 228 | unsigned char buf2[BUFSZ]; |
| 229 | size_t ret_len; |
| 230 | u64 objdump_addr; |
| 231 | int ret; |
| 232 | |
| 233 | pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr); |
| 234 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 235 | thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 236 | if (!al.map || !al.map->dso) { |
| 237 | pr_debug("thread__find_addr_map failed\n"); |
| 238 | return -1; |
| 239 | } |
| 240 | |
| 241 | pr_debug("File is: %s\n", al.map->dso->long_name); |
| 242 | |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 243 | if (al.map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && |
| 244 | !dso__is_kcore(al.map->dso)) { |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 245 | pr_debug("Unexpected kernel address - skipping\n"); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | pr_debug("On file address is: %#"PRIx64"\n", al.addr); |
| 250 | |
| 251 | if (len > BUFSZ) |
| 252 | len = BUFSZ; |
| 253 | |
| 254 | /* Do not go off the map */ |
| 255 | if (addr + len > al.map->end) |
| 256 | len = al.map->end - addr; |
| 257 | |
| 258 | /* Read the object code using perf */ |
Arnaldo Carvalho de Melo | 29f9e52 | 2014-10-23 17:21:54 -0300 | [diff] [blame] | 259 | ret_len = dso__data_read_offset(al.map->dso, thread->mg->machine, |
| 260 | al.addr, buf1, len); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 261 | if (ret_len != len) { |
| 262 | pr_debug("dso__data_read_offset failed\n"); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Converting addresses for use by objdump requires more information. |
| 268 | * map__load() does that. See map__rip_2objdump() for details. |
| 269 | */ |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 270 | if (map__load(al.map)) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 271 | return -1; |
| 272 | |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 273 | /* objdump struggles with kcore - try each map only once */ |
| 274 | if (dso__is_kcore(al.map->dso)) { |
| 275 | size_t d; |
| 276 | |
| 277 | for (d = 0; d < state->done_cnt; d++) { |
| 278 | if (state->done[d] == al.map->start) { |
| 279 | pr_debug("kcore map tested already"); |
| 280 | pr_debug(" - skipping\n"); |
| 281 | return 0; |
| 282 | } |
| 283 | } |
| 284 | if (state->done_cnt >= ARRAY_SIZE(state->done)) { |
| 285 | pr_debug("Too many kcore maps - skipping\n"); |
| 286 | return 0; |
| 287 | } |
| 288 | state->done[state->done_cnt++] = al.map->start; |
| 289 | } |
| 290 | |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 291 | /* Read the object code using objdump */ |
| 292 | objdump_addr = map__rip_2objdump(al.map, al.addr); |
| 293 | ret = read_via_objdump(al.map->dso->long_name, objdump_addr, buf2, len); |
| 294 | if (ret > 0) { |
| 295 | /* |
| 296 | * The kernel maps are inaccurate - assume objdump is right in |
| 297 | * that case. |
| 298 | */ |
| 299 | if (cpumode == PERF_RECORD_MISC_KERNEL || |
| 300 | cpumode == PERF_RECORD_MISC_GUEST_KERNEL) { |
| 301 | len -= ret; |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 302 | if (len) { |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 303 | pr_debug("Reducing len to %zu\n", len); |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 304 | } else if (dso__is_kcore(al.map->dso)) { |
| 305 | /* |
| 306 | * objdump cannot handle very large segments |
| 307 | * that may be found in kcore. |
| 308 | */ |
| 309 | pr_debug("objdump failed for kcore"); |
| 310 | pr_debug(" - skipping\n"); |
| 311 | return 0; |
| 312 | } else { |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 313 | return -1; |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 314 | } |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | if (ret < 0) { |
| 318 | pr_debug("read_via_objdump failed\n"); |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | /* The results should be identical */ |
| 323 | if (memcmp(buf1, buf2, len)) { |
| 324 | pr_debug("Bytes read differ from those read by objdump\n"); |
Jan Stancek | fd405cf | 2015-09-02 10:19:17 +0200 | [diff] [blame] | 325 | pr_debug("buf1 (dso):\n"); |
| 326 | dump_buf(buf1, len); |
| 327 | pr_debug("buf2 (objdump):\n"); |
| 328 | dump_buf(buf2, len); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 329 | return -1; |
| 330 | } |
| 331 | pr_debug("Bytes read match those read by objdump\n"); |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static int process_sample_event(struct machine *machine, |
| 337 | struct perf_evlist *evlist, |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 338 | union perf_event *event, struct state *state) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 339 | { |
| 340 | struct perf_sample sample; |
| 341 | struct thread *thread; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 342 | int ret; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 343 | |
| 344 | if (perf_evlist__parse_sample(evlist, event, &sample)) { |
| 345 | pr_debug("perf_evlist__parse_sample failed\n"); |
| 346 | return -1; |
| 347 | } |
| 348 | |
Namhyung Kim | 13ce34d | 2014-05-12 09:56:42 +0900 | [diff] [blame] | 349 | thread = machine__findnew_thread(machine, sample.pid, sample.tid); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 350 | if (!thread) { |
| 351 | pr_debug("machine__findnew_thread failed\n"); |
| 352 | return -1; |
| 353 | } |
| 354 | |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 355 | ret = read_object_code(sample.ip, READLEN, sample.cpumode, thread, state); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 356 | thread__put(thread); |
| 357 | return ret; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static int process_event(struct machine *machine, struct perf_evlist *evlist, |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 361 | union perf_event *event, struct state *state) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 362 | { |
| 363 | if (event->header.type == PERF_RECORD_SAMPLE) |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 364 | return process_sample_event(machine, evlist, event, state); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 365 | |
Adrian Hunter | 48095b7 | 2013-11-12 09:59:33 +0200 | [diff] [blame] | 366 | if (event->header.type == PERF_RECORD_THROTTLE || |
| 367 | event->header.type == PERF_RECORD_UNTHROTTLE) |
| 368 | return 0; |
| 369 | |
| 370 | if (event->header.type < PERF_RECORD_MAX) { |
| 371 | int ret; |
| 372 | |
| 373 | ret = machine__process_event(machine, event, NULL); |
| 374 | if (ret < 0) |
| 375 | pr_debug("machine__process_event failed, event type %u\n", |
| 376 | event->header.type); |
| 377 | return ret; |
| 378 | } |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 383 | static int process_events(struct machine *machine, struct perf_evlist *evlist, |
| 384 | struct state *state) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 385 | { |
| 386 | union perf_event *event; |
| 387 | int i, ret; |
| 388 | |
| 389 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 390 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 391 | ret = process_event(machine, evlist, event, state); |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 392 | perf_evlist__mmap_consume(evlist, i); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 393 | if (ret < 0) |
| 394 | return ret; |
| 395 | } |
| 396 | } |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static int comp(const void *a, const void *b) |
| 401 | { |
| 402 | return *(int *)a - *(int *)b; |
| 403 | } |
| 404 | |
| 405 | static void do_sort_something(void) |
| 406 | { |
David Ahern | 309b518 | 2013-08-13 22:32:12 -0600 | [diff] [blame] | 407 | int buf[40960], i; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 408 | |
David Ahern | 309b518 | 2013-08-13 22:32:12 -0600 | [diff] [blame] | 409 | for (i = 0; i < (int)ARRAY_SIZE(buf); i++) |
| 410 | buf[i] = ARRAY_SIZE(buf) - i - 1; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 411 | |
David Ahern | 309b518 | 2013-08-13 22:32:12 -0600 | [diff] [blame] | 412 | qsort(buf, ARRAY_SIZE(buf), sizeof(int), comp); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 413 | |
David Ahern | 309b518 | 2013-08-13 22:32:12 -0600 | [diff] [blame] | 414 | for (i = 0; i < (int)ARRAY_SIZE(buf); i++) { |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 415 | if (buf[i] != i) { |
| 416 | pr_debug("qsort failed\n"); |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | static void sort_something(void) |
| 423 | { |
| 424 | int i; |
| 425 | |
| 426 | for (i = 0; i < 10; i++) |
| 427 | do_sort_something(); |
| 428 | } |
| 429 | |
| 430 | static void syscall_something(void) |
| 431 | { |
| 432 | int pipefd[2]; |
| 433 | int i; |
| 434 | |
| 435 | for (i = 0; i < 1000; i++) { |
| 436 | if (pipe(pipefd) < 0) { |
| 437 | pr_debug("pipe failed\n"); |
| 438 | break; |
| 439 | } |
| 440 | close(pipefd[1]); |
| 441 | close(pipefd[0]); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | static void fs_something(void) |
| 446 | { |
| 447 | const char *test_file_name = "temp-perf-code-reading-test-file--"; |
| 448 | FILE *f; |
| 449 | int i; |
| 450 | |
| 451 | for (i = 0; i < 1000; i++) { |
| 452 | f = fopen(test_file_name, "w+"); |
| 453 | if (f) { |
| 454 | fclose(f); |
| 455 | unlink(test_file_name); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | static void do_something(void) |
| 461 | { |
| 462 | fs_something(); |
| 463 | |
| 464 | sort_something(); |
| 465 | |
| 466 | syscall_something(); |
| 467 | } |
| 468 | |
| 469 | enum { |
| 470 | TEST_CODE_READING_OK, |
| 471 | TEST_CODE_READING_NO_VMLINUX, |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 472 | TEST_CODE_READING_NO_KCORE, |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 473 | TEST_CODE_READING_NO_ACCESS, |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 474 | TEST_CODE_READING_NO_KERNEL_OBJ, |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 475 | }; |
| 476 | |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 477 | static int do_test_code_reading(bool try_kcore) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 478 | { |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 479 | struct machine *machine; |
| 480 | struct thread *thread; |
Arnaldo Carvalho de Melo | b400679 | 2013-12-19 14:43:45 -0300 | [diff] [blame] | 481 | struct record_opts opts = { |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 482 | .mmap_pages = UINT_MAX, |
| 483 | .user_freq = UINT_MAX, |
| 484 | .user_interval = ULLONG_MAX, |
Arnaldo Carvalho de Melo | 5243ba7 | 2016-02-18 13:45:25 -0300 | [diff] [blame] | 485 | .freq = 500, |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 486 | .target = { |
| 487 | .uses_mmap = true, |
| 488 | }, |
| 489 | }; |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 490 | struct state state = { |
| 491 | .done_cnt = 0, |
| 492 | }; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 493 | struct thread_map *threads = NULL; |
| 494 | struct cpu_map *cpus = NULL; |
| 495 | struct perf_evlist *evlist = NULL; |
| 496 | struct perf_evsel *evsel = NULL; |
| 497 | int err = -1, ret; |
| 498 | pid_t pid; |
| 499 | struct map *map; |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 500 | bool have_vmlinux, have_kcore, excl_kernel = false; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 501 | |
| 502 | pid = getpid(); |
| 503 | |
Jiri Olsa | 0fd4008 | 2015-12-03 09:34:14 +0100 | [diff] [blame] | 504 | machine = machine__new_host(); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 505 | |
| 506 | ret = machine__create_kernel_maps(machine); |
| 507 | if (ret < 0) { |
| 508 | pr_debug("machine__create_kernel_maps failed\n"); |
| 509 | goto out_err; |
| 510 | } |
| 511 | |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 512 | /* Force the use of kallsyms instead of vmlinux to try kcore */ |
| 513 | if (try_kcore) |
| 514 | symbol_conf.kallsyms_name = "/proc/kallsyms"; |
| 515 | |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 516 | /* Load kernel map */ |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 517 | map = machine__kernel_map(machine); |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 518 | ret = map__load(map); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 519 | if (ret < 0) { |
| 520 | pr_debug("map__load failed\n"); |
| 521 | goto out_err; |
| 522 | } |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 523 | have_vmlinux = dso__is_vmlinux(map->dso); |
| 524 | have_kcore = dso__is_kcore(map->dso); |
| 525 | |
| 526 | /* 2nd time through we just try kcore */ |
| 527 | if (try_kcore && !have_kcore) |
| 528 | return TEST_CODE_READING_NO_KCORE; |
| 529 | |
| 530 | /* No point getting kernel events if there is no kernel object */ |
| 531 | if (!have_vmlinux && !have_kcore) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 532 | excl_kernel = true; |
| 533 | |
| 534 | threads = thread_map__new_by_tid(pid); |
| 535 | if (!threads) { |
| 536 | pr_debug("thread_map__new_by_tid failed\n"); |
| 537 | goto out_err; |
| 538 | } |
| 539 | |
| 540 | ret = perf_event__synthesize_thread_map(NULL, threads, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 541 | perf_event__process, machine, false, 500); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 542 | if (ret < 0) { |
| 543 | pr_debug("perf_event__synthesize_thread_map failed\n"); |
| 544 | goto out_err; |
| 545 | } |
| 546 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 547 | thread = machine__findnew_thread(machine, pid, pid); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 548 | if (!thread) { |
| 549 | pr_debug("machine__findnew_thread failed\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 550 | goto out_put; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | cpus = cpu_map__new(NULL); |
| 554 | if (!cpus) { |
| 555 | pr_debug("cpu_map__new failed\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 556 | goto out_put; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | while (1) { |
| 560 | const char *str; |
| 561 | |
| 562 | evlist = perf_evlist__new(); |
| 563 | if (!evlist) { |
| 564 | pr_debug("perf_evlist__new failed\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 565 | goto out_put; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | perf_evlist__set_maps(evlist, cpus, threads); |
| 569 | |
| 570 | if (excl_kernel) |
| 571 | str = "cycles:u"; |
| 572 | else |
| 573 | str = "cycles"; |
| 574 | pr_debug("Parsing event '%s'\n", str); |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 575 | ret = parse_events(evlist, str, NULL); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 576 | if (ret < 0) { |
| 577 | pr_debug("parse_events failed\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 578 | goto out_put; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 579 | } |
| 580 | |
Arnaldo Carvalho de Melo | e68ae9c | 2016-04-11 18:15:29 -0300 | [diff] [blame] | 581 | perf_evlist__config(evlist, &opts, NULL); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 582 | |
| 583 | evsel = perf_evlist__first(evlist); |
| 584 | |
| 585 | evsel->attr.comm = 1; |
| 586 | evsel->attr.disabled = 1; |
| 587 | evsel->attr.enable_on_exec = 0; |
| 588 | |
| 589 | ret = perf_evlist__open(evlist); |
| 590 | if (ret < 0) { |
| 591 | if (!excl_kernel) { |
| 592 | excl_kernel = true; |
Jiri Olsa | 7320b1b | 2015-12-03 09:34:15 +0100 | [diff] [blame] | 593 | /* |
| 594 | * Both cpus and threads are now owned by evlist |
| 595 | * and will be freed by following perf_evlist__set_maps |
| 596 | * call. Getting refference to keep them alive. |
| 597 | */ |
| 598 | cpu_map__get(cpus); |
| 599 | thread_map__get(threads); |
Adrian Hunter | ae450a7 | 2014-04-10 12:02:54 +0300 | [diff] [blame] | 600 | perf_evlist__set_maps(evlist, NULL, NULL); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 601 | perf_evlist__delete(evlist); |
| 602 | evlist = NULL; |
| 603 | continue; |
| 604 | } |
Arnaldo Carvalho de Melo | 6880bbf | 2016-02-18 13:40:57 -0300 | [diff] [blame] | 605 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 606 | if (verbose > 0) { |
Arnaldo Carvalho de Melo | 6880bbf | 2016-02-18 13:40:57 -0300 | [diff] [blame] | 607 | char errbuf[512]; |
| 608 | perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf)); |
| 609 | pr_debug("perf_evlist__open() failed!\n%s\n", errbuf); |
| 610 | } |
| 611 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 612 | goto out_put; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 613 | } |
| 614 | break; |
| 615 | } |
| 616 | |
| 617 | ret = perf_evlist__mmap(evlist, UINT_MAX, false); |
| 618 | if (ret < 0) { |
| 619 | pr_debug("perf_evlist__mmap failed\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 620 | goto out_put; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | perf_evlist__enable(evlist); |
| 624 | |
| 625 | do_something(); |
| 626 | |
| 627 | perf_evlist__disable(evlist); |
| 628 | |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 629 | ret = process_events(machine, evlist, &state); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 630 | if (ret < 0) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 631 | goto out_put; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 632 | |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 633 | if (!have_vmlinux && !have_kcore && !try_kcore) |
| 634 | err = TEST_CODE_READING_NO_KERNEL_OBJ; |
| 635 | else if (!have_vmlinux && !try_kcore) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 636 | err = TEST_CODE_READING_NO_VMLINUX; |
| 637 | else if (excl_kernel) |
| 638 | err = TEST_CODE_READING_NO_ACCESS; |
| 639 | else |
| 640 | err = TEST_CODE_READING_OK; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 641 | out_put: |
| 642 | thread__put(thread); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 643 | out_err: |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 644 | |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 645 | if (evlist) { |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 646 | perf_evlist__delete(evlist); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 647 | } else { |
Jiri Olsa | f30a79b | 2015-06-23 00:36:04 +0200 | [diff] [blame] | 648 | cpu_map__put(cpus); |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 649 | thread_map__put(threads); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 650 | } |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 651 | machine__delete_threads(machine); |
Jiri Olsa | 0fd4008 | 2015-12-03 09:34:14 +0100 | [diff] [blame] | 652 | machine__delete(machine); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 653 | |
| 654 | return err; |
| 655 | } |
| 656 | |
Arnaldo Carvalho de Melo | 721a1f5 | 2015-11-19 12:01:48 -0300 | [diff] [blame] | 657 | int test__code_reading(int subtest __maybe_unused) |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 658 | { |
| 659 | int ret; |
| 660 | |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 661 | ret = do_test_code_reading(false); |
| 662 | if (!ret) |
| 663 | ret = do_test_code_reading(true); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 664 | |
| 665 | switch (ret) { |
| 666 | case TEST_CODE_READING_OK: |
| 667 | return 0; |
| 668 | case TEST_CODE_READING_NO_VMLINUX: |
Wang Nan | 597bdeb | 2015-11-03 10:44:42 +0000 | [diff] [blame] | 669 | pr_debug("no vmlinux\n"); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 670 | return 0; |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 671 | case TEST_CODE_READING_NO_KCORE: |
Wang Nan | 597bdeb | 2015-11-03 10:44:42 +0000 | [diff] [blame] | 672 | pr_debug("no kcore\n"); |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 673 | return 0; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 674 | case TEST_CODE_READING_NO_ACCESS: |
Wang Nan | 597bdeb | 2015-11-03 10:44:42 +0000 | [diff] [blame] | 675 | pr_debug("no access\n"); |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 676 | return 0; |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 677 | case TEST_CODE_READING_NO_KERNEL_OBJ: |
Wang Nan | 597bdeb | 2015-11-03 10:44:42 +0000 | [diff] [blame] | 678 | pr_debug("no kernel obj\n"); |
Adrian Hunter | 7a77bc2 | 2013-08-07 14:38:53 +0300 | [diff] [blame] | 679 | return 0; |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 680 | default: |
| 681 | return -1; |
| 682 | }; |
| 683 | } |