blob: 6effcfb7e0c13e06fc9e83819d7b47141ae44bd1 [file] [log] [blame]
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03001#include <linux/kernel.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02002#include <linux/types.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Adrian Hunterb55ae0a2013-08-07 14:38:45 +03004#include <stdlib.h>
5#include <unistd.h>
6#include <stdio.h>
Adrian Hunterb55ae0a2013-08-07 14:38:45 +03007#include <ctype.h>
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
21#define BUFSZ 1024
22#define READLEN 128
23
Adrian Hunter7a77bc22013-08-07 14:38:53 +030024struct state {
25 u64 done[1024];
26 size_t done_cnt;
27};
28
Adrian Hunterb55ae0a2013-08-07 14:38:45 +030029static unsigned int hex(char c)
30{
31 if (c >= '0' && c <= '9')
32 return c - '0';
33 if (c >= 'a' && c <= 'f')
34 return c - 'a' + 10;
35 return c - 'A' + 10;
36}
37
Jan Stancekb2d0dbf2016-01-12 11:07:44 +010038static size_t read_objdump_chunk(const char **line, unsigned char **buf,
39 size_t *buf_len)
40{
41 size_t bytes_read = 0;
42 unsigned char *chunk_start = *buf;
43
44 /* Read bytes */
45 while (*buf_len > 0) {
46 char c1, c2;
47
48 /* Get 2 hex digits */
49 c1 = *(*line)++;
50 if (!isxdigit(c1))
51 break;
52 c2 = *(*line)++;
53 if (!isxdigit(c2))
54 break;
55
56 /* Store byte and advance buf */
57 **buf = (hex(c1) << 4) | hex(c2);
58 (*buf)++;
59 (*buf_len)--;
60 bytes_read++;
61
62 /* End of chunk? */
63 if (isspace(**line))
64 break;
65 }
66
67 /*
68 * objdump will display raw insn as LE if code endian
69 * is LE and bytes_per_chunk > 1. In that case reverse
70 * the chunk we just read.
71 *
72 * see disassemble_bytes() at binutils/objdump.c for details
73 * how objdump chooses display endian)
74 */
75 if (bytes_read > 1 && !bigendian()) {
76 unsigned char *chunk_end = chunk_start + bytes_read - 1;
77 unsigned char tmp;
78
79 while (chunk_start < chunk_end) {
80 tmp = *chunk_start;
81 *chunk_start = *chunk_end;
82 *chunk_end = tmp;
83 chunk_start++;
84 chunk_end--;
85 }
86 }
87
88 return bytes_read;
89}
90
91static size_t read_objdump_line(const char *line, unsigned char *buf,
92 size_t buf_len)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +030093{
94 const char *p;
Jan Stancekb2d0dbf2016-01-12 11:07:44 +010095 size_t ret, bytes_read = 0;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +030096
97 /* Skip to a colon */
98 p = strchr(line, ':');
99 if (!p)
Jan Stancek729a7ed2015-09-02 10:19:14 +0200100 return 0;
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100101 p++;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300102
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100103 /* Skip initial spaces */
104 while (*p) {
105 if (!isspace(*p))
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300106 break;
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100107 p++;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300108 }
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100109
110 do {
111 ret = read_objdump_chunk(&p, &buf, &buf_len);
112 bytes_read += ret;
113 p++;
114 } while (ret > 0);
115
Jan Stancek729a7ed2015-09-02 10:19:14 +0200116 /* return number of successfully read bytes */
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100117 return bytes_read;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300118}
119
Jan Stancek729a7ed2015-09-02 10:19:14 +0200120static int read_objdump_output(FILE *f, void *buf, size_t *len, u64 start_addr)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300121{
122 char *line = NULL;
Jan Stancek729a7ed2015-09-02 10:19:14 +0200123 size_t line_len, off_last = 0;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300124 ssize_t ret;
125 int err = 0;
Jan Stancekedfdb7e2015-09-02 10:19:16 +0200126 u64 addr, last_addr = start_addr;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300127
Jan Stancek729a7ed2015-09-02 10:19:14 +0200128 while (off_last < *len) {
129 size_t off, read_bytes, written_bytes;
130 unsigned char tmp[BUFSZ];
131
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300132 ret = getline(&line, &line_len, f);
133 if (feof(f))
134 break;
135 if (ret < 0) {
136 pr_debug("getline failed\n");
137 err = -1;
138 break;
139 }
Jan Stancek729a7ed2015-09-02 10:19:14 +0200140
141 /* read objdump data into temporary buffer */
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100142 read_bytes = read_objdump_line(line, tmp, sizeof(tmp));
Jan Stancek729a7ed2015-09-02 10:19:14 +0200143 if (!read_bytes)
144 continue;
145
146 if (sscanf(line, "%"PRIx64, &addr) != 1)
147 continue;
Jan Stancekedfdb7e2015-09-02 10:19:16 +0200148 if (addr < last_addr) {
149 pr_debug("addr going backwards, read beyond section?\n");
150 break;
151 }
152 last_addr = addr;
Jan Stancek729a7ed2015-09-02 10:19:14 +0200153
154 /* copy it from temporary buffer to 'buf' according
155 * to address on current objdump line */
156 off = addr - start_addr;
157 if (off >= *len)
158 break;
159 written_bytes = MIN(read_bytes, *len - off);
160 memcpy(buf + off, tmp, written_bytes);
161 off_last = off + written_bytes;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300162 }
163
Jan Stancek729a7ed2015-09-02 10:19:14 +0200164 /* len returns number of bytes that could not be read */
165 *len -= off_last;
166
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300167 free(line);
168
169 return err;
170}
171
172static int read_via_objdump(const char *filename, u64 addr, void *buf,
173 size_t len)
174{
175 char cmd[PATH_MAX * 2];
176 const char *fmt;
177 FILE *f;
178 int ret;
179
Jan Stancek06f679c2015-09-03 13:23:32 +0200180 fmt = "%s -z -d --start-address=0x%"PRIx64" --stop-address=0x%"PRIx64" %s";
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300181 ret = snprintf(cmd, sizeof(cmd), fmt, "objdump", addr, addr + len,
182 filename);
183 if (ret <= 0 || (size_t)ret >= sizeof(cmd))
184 return -1;
185
186 pr_debug("Objdump command is: %s\n", cmd);
187
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300188 /* Ignore objdump errors */
189 strcat(cmd, " 2>/dev/null");
190
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300191 f = popen(cmd, "r");
192 if (!f) {
193 pr_debug("popen failed\n");
194 return -1;
195 }
196
Jan Stancek729a7ed2015-09-02 10:19:14 +0200197 ret = read_objdump_output(f, buf, &len, addr);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300198 if (len) {
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100199 pr_debug("objdump read too few bytes: %zd\n", len);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300200 if (!ret)
201 ret = len;
202 }
203
204 pclose(f);
205
206 return ret;
207}
208
Jan Stancekfd405cf2015-09-02 10:19:17 +0200209static void dump_buf(unsigned char *buf, size_t len)
210{
211 size_t i;
212
213 for (i = 0; i < len; i++) {
214 pr_debug("0x%02x ", buf[i]);
215 if (i % 16 == 15)
216 pr_debug("\n");
217 }
218 pr_debug("\n");
219}
220
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300221static int read_object_code(u64 addr, size_t len, u8 cpumode,
Arnaldo Carvalho de Melo29f9e522014-10-23 17:21:54 -0300222 struct thread *thread, struct state *state)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300223{
224 struct addr_location al;
225 unsigned char buf1[BUFSZ];
226 unsigned char buf2[BUFSZ];
227 size_t ret_len;
228 u64 objdump_addr;
229 int ret;
230
231 pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr);
232
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300233 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300234 if (!al.map || !al.map->dso) {
235 pr_debug("thread__find_addr_map failed\n");
236 return -1;
237 }
238
239 pr_debug("File is: %s\n", al.map->dso->long_name);
240
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300241 if (al.map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
242 !dso__is_kcore(al.map->dso)) {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300243 pr_debug("Unexpected kernel address - skipping\n");
244 return 0;
245 }
246
247 pr_debug("On file address is: %#"PRIx64"\n", al.addr);
248
249 if (len > BUFSZ)
250 len = BUFSZ;
251
252 /* Do not go off the map */
253 if (addr + len > al.map->end)
254 len = al.map->end - addr;
255
256 /* Read the object code using perf */
Arnaldo Carvalho de Melo29f9e522014-10-23 17:21:54 -0300257 ret_len = dso__data_read_offset(al.map->dso, thread->mg->machine,
258 al.addr, buf1, len);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300259 if (ret_len != len) {
260 pr_debug("dso__data_read_offset failed\n");
261 return -1;
262 }
263
264 /*
265 * Converting addresses for use by objdump requires more information.
266 * map__load() does that. See map__rip_2objdump() for details.
267 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300268 if (map__load(al.map))
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300269 return -1;
270
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300271 /* objdump struggles with kcore - try each map only once */
272 if (dso__is_kcore(al.map->dso)) {
273 size_t d;
274
275 for (d = 0; d < state->done_cnt; d++) {
276 if (state->done[d] == al.map->start) {
277 pr_debug("kcore map tested already");
278 pr_debug(" - skipping\n");
279 return 0;
280 }
281 }
282 if (state->done_cnt >= ARRAY_SIZE(state->done)) {
283 pr_debug("Too many kcore maps - skipping\n");
284 return 0;
285 }
286 state->done[state->done_cnt++] = al.map->start;
287 }
288
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300289 /* Read the object code using objdump */
290 objdump_addr = map__rip_2objdump(al.map, al.addr);
291 ret = read_via_objdump(al.map->dso->long_name, objdump_addr, buf2, len);
292 if (ret > 0) {
293 /*
294 * The kernel maps are inaccurate - assume objdump is right in
295 * that case.
296 */
297 if (cpumode == PERF_RECORD_MISC_KERNEL ||
298 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) {
299 len -= ret;
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300300 if (len) {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300301 pr_debug("Reducing len to %zu\n", len);
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300302 } else if (dso__is_kcore(al.map->dso)) {
303 /*
304 * objdump cannot handle very large segments
305 * that may be found in kcore.
306 */
307 pr_debug("objdump failed for kcore");
308 pr_debug(" - skipping\n");
309 return 0;
310 } else {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300311 return -1;
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300312 }
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300313 }
314 }
315 if (ret < 0) {
316 pr_debug("read_via_objdump failed\n");
317 return -1;
318 }
319
320 /* The results should be identical */
321 if (memcmp(buf1, buf2, len)) {
322 pr_debug("Bytes read differ from those read by objdump\n");
Jan Stancekfd405cf2015-09-02 10:19:17 +0200323 pr_debug("buf1 (dso):\n");
324 dump_buf(buf1, len);
325 pr_debug("buf2 (objdump):\n");
326 dump_buf(buf2, len);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300327 return -1;
328 }
329 pr_debug("Bytes read match those read by objdump\n");
330
331 return 0;
332}
333
334static int process_sample_event(struct machine *machine,
335 struct perf_evlist *evlist,
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300336 union perf_event *event, struct state *state)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300337{
338 struct perf_sample sample;
339 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300340 int ret;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300341
342 if (perf_evlist__parse_sample(evlist, event, &sample)) {
343 pr_debug("perf_evlist__parse_sample failed\n");
344 return -1;
345 }
346
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900347 thread = machine__findnew_thread(machine, sample.pid, sample.tid);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300348 if (!thread) {
349 pr_debug("machine__findnew_thread failed\n");
350 return -1;
351 }
352
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300353 ret = read_object_code(sample.ip, READLEN, sample.cpumode, thread, state);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300354 thread__put(thread);
355 return ret;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300356}
357
358static int process_event(struct machine *machine, struct perf_evlist *evlist,
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300359 union perf_event *event, struct state *state)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300360{
361 if (event->header.type == PERF_RECORD_SAMPLE)
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300362 return process_sample_event(machine, evlist, event, state);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300363
Adrian Hunter48095b72013-11-12 09:59:33 +0200364 if (event->header.type == PERF_RECORD_THROTTLE ||
365 event->header.type == PERF_RECORD_UNTHROTTLE)
366 return 0;
367
368 if (event->header.type < PERF_RECORD_MAX) {
369 int ret;
370
371 ret = machine__process_event(machine, event, NULL);
372 if (ret < 0)
373 pr_debug("machine__process_event failed, event type %u\n",
374 event->header.type);
375 return ret;
376 }
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300377
378 return 0;
379}
380
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300381static int process_events(struct machine *machine, struct perf_evlist *evlist,
382 struct state *state)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300383{
384 union perf_event *event;
385 int i, ret;
386
387 for (i = 0; i < evlist->nr_mmaps; i++) {
388 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300389 ret = process_event(machine, evlist, event, state);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800390 perf_evlist__mmap_consume(evlist, i);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300391 if (ret < 0)
392 return ret;
393 }
394 }
395 return 0;
396}
397
398static int comp(const void *a, const void *b)
399{
400 return *(int *)a - *(int *)b;
401}
402
403static void do_sort_something(void)
404{
David Ahern309b5182013-08-13 22:32:12 -0600405 int buf[40960], i;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300406
David Ahern309b5182013-08-13 22:32:12 -0600407 for (i = 0; i < (int)ARRAY_SIZE(buf); i++)
408 buf[i] = ARRAY_SIZE(buf) - i - 1;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300409
David Ahern309b5182013-08-13 22:32:12 -0600410 qsort(buf, ARRAY_SIZE(buf), sizeof(int), comp);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300411
David Ahern309b5182013-08-13 22:32:12 -0600412 for (i = 0; i < (int)ARRAY_SIZE(buf); i++) {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300413 if (buf[i] != i) {
414 pr_debug("qsort failed\n");
415 break;
416 }
417 }
418}
419
420static void sort_something(void)
421{
422 int i;
423
424 for (i = 0; i < 10; i++)
425 do_sort_something();
426}
427
428static void syscall_something(void)
429{
430 int pipefd[2];
431 int i;
432
433 for (i = 0; i < 1000; i++) {
434 if (pipe(pipefd) < 0) {
435 pr_debug("pipe failed\n");
436 break;
437 }
438 close(pipefd[1]);
439 close(pipefd[0]);
440 }
441}
442
443static void fs_something(void)
444{
445 const char *test_file_name = "temp-perf-code-reading-test-file--";
446 FILE *f;
447 int i;
448
449 for (i = 0; i < 1000; i++) {
450 f = fopen(test_file_name, "w+");
451 if (f) {
452 fclose(f);
453 unlink(test_file_name);
454 }
455 }
456}
457
458static void do_something(void)
459{
460 fs_something();
461
462 sort_something();
463
464 syscall_something();
465}
466
467enum {
468 TEST_CODE_READING_OK,
469 TEST_CODE_READING_NO_VMLINUX,
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300470 TEST_CODE_READING_NO_KCORE,
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300471 TEST_CODE_READING_NO_ACCESS,
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300472 TEST_CODE_READING_NO_KERNEL_OBJ,
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300473};
474
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300475static int do_test_code_reading(bool try_kcore)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300476{
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300477 struct machine *machine;
478 struct thread *thread;
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300479 struct record_opts opts = {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300480 .mmap_pages = UINT_MAX,
481 .user_freq = UINT_MAX,
482 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo5243ba72016-02-18 13:45:25 -0300483 .freq = 500,
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300484 .target = {
485 .uses_mmap = true,
486 },
487 };
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300488 struct state state = {
489 .done_cnt = 0,
490 };
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300491 struct thread_map *threads = NULL;
492 struct cpu_map *cpus = NULL;
493 struct perf_evlist *evlist = NULL;
494 struct perf_evsel *evsel = NULL;
495 int err = -1, ret;
496 pid_t pid;
497 struct map *map;
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300498 bool have_vmlinux, have_kcore, excl_kernel = false;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300499
500 pid = getpid();
501
Jiri Olsa0fd40082015-12-03 09:34:14 +0100502 machine = machine__new_host();
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300503
504 ret = machine__create_kernel_maps(machine);
505 if (ret < 0) {
506 pr_debug("machine__create_kernel_maps failed\n");
507 goto out_err;
508 }
509
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300510 /* Force the use of kallsyms instead of vmlinux to try kcore */
511 if (try_kcore)
512 symbol_conf.kallsyms_name = "/proc/kallsyms";
513
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300514 /* Load kernel map */
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300515 map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300516 ret = map__load(map);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300517 if (ret < 0) {
518 pr_debug("map__load failed\n");
519 goto out_err;
520 }
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300521 have_vmlinux = dso__is_vmlinux(map->dso);
522 have_kcore = dso__is_kcore(map->dso);
523
524 /* 2nd time through we just try kcore */
525 if (try_kcore && !have_kcore)
526 return TEST_CODE_READING_NO_KCORE;
527
528 /* No point getting kernel events if there is no kernel object */
529 if (!have_vmlinux && !have_kcore)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300530 excl_kernel = true;
531
532 threads = thread_map__new_by_tid(pid);
533 if (!threads) {
534 pr_debug("thread_map__new_by_tid failed\n");
535 goto out_err;
536 }
537
538 ret = perf_event__synthesize_thread_map(NULL, threads,
Kan Liang9d9cad72015-06-17 09:51:11 -0400539 perf_event__process, machine, false, 500);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300540 if (ret < 0) {
541 pr_debug("perf_event__synthesize_thread_map failed\n");
542 goto out_err;
543 }
544
Adrian Hunter314add62013-08-27 11:23:03 +0300545 thread = machine__findnew_thread(machine, pid, pid);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300546 if (!thread) {
547 pr_debug("machine__findnew_thread failed\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300548 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300549 }
550
551 cpus = cpu_map__new(NULL);
552 if (!cpus) {
553 pr_debug("cpu_map__new failed\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300554 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300555 }
556
557 while (1) {
558 const char *str;
559
560 evlist = perf_evlist__new();
561 if (!evlist) {
562 pr_debug("perf_evlist__new failed\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300563 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300564 }
565
566 perf_evlist__set_maps(evlist, cpus, threads);
567
568 if (excl_kernel)
569 str = "cycles:u";
570 else
571 str = "cycles";
572 pr_debug("Parsing event '%s'\n", str);
Jiri Olsab39b8392015-04-22 21:10:16 +0200573 ret = parse_events(evlist, str, NULL);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300574 if (ret < 0) {
575 pr_debug("parse_events failed\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300576 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300577 }
578
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300579 perf_evlist__config(evlist, &opts, NULL);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300580
581 evsel = perf_evlist__first(evlist);
582
583 evsel->attr.comm = 1;
584 evsel->attr.disabled = 1;
585 evsel->attr.enable_on_exec = 0;
586
587 ret = perf_evlist__open(evlist);
588 if (ret < 0) {
589 if (!excl_kernel) {
590 excl_kernel = true;
Jiri Olsa7320b1b2015-12-03 09:34:15 +0100591 /*
592 * Both cpus and threads are now owned by evlist
593 * and will be freed by following perf_evlist__set_maps
594 * call. Getting refference to keep them alive.
595 */
596 cpu_map__get(cpus);
597 thread_map__get(threads);
Adrian Hunterae450a72014-04-10 12:02:54 +0300598 perf_evlist__set_maps(evlist, NULL, NULL);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300599 perf_evlist__delete(evlist);
600 evlist = NULL;
601 continue;
602 }
Arnaldo Carvalho de Melo6880bbf2016-02-18 13:40:57 -0300603
Namhyung Kimbb963e12017-02-17 17:17:38 +0900604 if (verbose > 0) {
Arnaldo Carvalho de Melo6880bbf2016-02-18 13:40:57 -0300605 char errbuf[512];
606 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
607 pr_debug("perf_evlist__open() failed!\n%s\n", errbuf);
608 }
609
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300610 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300611 }
612 break;
613 }
614
615 ret = perf_evlist__mmap(evlist, UINT_MAX, false);
616 if (ret < 0) {
617 pr_debug("perf_evlist__mmap failed\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300618 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300619 }
620
621 perf_evlist__enable(evlist);
622
623 do_something();
624
625 perf_evlist__disable(evlist);
626
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300627 ret = process_events(machine, evlist, &state);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300628 if (ret < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300629 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300630
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300631 if (!have_vmlinux && !have_kcore && !try_kcore)
632 err = TEST_CODE_READING_NO_KERNEL_OBJ;
633 else if (!have_vmlinux && !try_kcore)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300634 err = TEST_CODE_READING_NO_VMLINUX;
635 else if (excl_kernel)
636 err = TEST_CODE_READING_NO_ACCESS;
637 else
638 err = TEST_CODE_READING_OK;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300639out_put:
640 thread__put(thread);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300641out_err:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300642
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300643 if (evlist) {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300644 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300645 } else {
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200646 cpu_map__put(cpus);
Jiri Olsa186fbb72015-06-23 00:36:05 +0200647 thread_map__put(threads);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300648 }
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300649 machine__delete_threads(machine);
Jiri Olsa0fd40082015-12-03 09:34:14 +0100650 machine__delete(machine);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300651
652 return err;
653}
654
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -0300655int test__code_reading(int subtest __maybe_unused)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300656{
657 int ret;
658
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300659 ret = do_test_code_reading(false);
660 if (!ret)
661 ret = do_test_code_reading(true);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300662
663 switch (ret) {
664 case TEST_CODE_READING_OK:
665 return 0;
666 case TEST_CODE_READING_NO_VMLINUX:
Wang Nan597bdeb2015-11-03 10:44:42 +0000667 pr_debug("no vmlinux\n");
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300668 return 0;
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300669 case TEST_CODE_READING_NO_KCORE:
Wang Nan597bdeb2015-11-03 10:44:42 +0000670 pr_debug("no kcore\n");
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300671 return 0;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300672 case TEST_CODE_READING_NO_ACCESS:
Wang Nan597bdeb2015-11-03 10:44:42 +0000673 pr_debug("no access\n");
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300674 return 0;
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300675 case TEST_CODE_READING_NO_KERNEL_OBJ:
Wang Nan597bdeb2015-11-03 10:44:42 +0000676 pr_debug("no kernel obj\n");
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300677 return 0;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300678 default:
679 return -1;
680 };
681}