blob: 3a8bf15654939e9bd2cff8cd37f680aabb88e996 [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03002#include <linux/kernel.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02003#include <linux/types.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03004#include <inttypes.h>
Adrian Hunterb55ae0a2013-08-07 14:38:45 +03005#include <stdlib.h>
6#include <unistd.h>
7#include <stdio.h>
Adrian Hunterb55ae0a2013-08-07 14:38:45 +03008#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 Melo3d689ed2017-04-17 16:10:49 -030021#include "sane_ctype.h"
22
Adrian Hunterb55ae0a2013-08-07 14:38:45 +030023#define BUFSZ 1024
24#define READLEN 128
25
Adrian Hunter7a77bc22013-08-07 14:38:53 +030026struct state {
27 u64 done[1024];
28 size_t done_cnt;
29};
30
Adrian Hunterb55ae0a2013-08-07 14:38:45 +030031static 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 Stancekb2d0dbf2016-01-12 11:07:44 +010040static 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
93static size_t read_objdump_line(const char *line, unsigned char *buf,
94 size_t buf_len)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +030095{
96 const char *p;
Jan Stancekb2d0dbf2016-01-12 11:07:44 +010097 size_t ret, bytes_read = 0;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +030098
99 /* Skip to a colon */
100 p = strchr(line, ':');
101 if (!p)
Jan Stancek729a7ed2015-09-02 10:19:14 +0200102 return 0;
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100103 p++;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300104
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100105 /* Skip initial spaces */
106 while (*p) {
107 if (!isspace(*p))
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300108 break;
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100109 p++;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300110 }
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100111
112 do {
113 ret = read_objdump_chunk(&p, &buf, &buf_len);
114 bytes_read += ret;
115 p++;
116 } while (ret > 0);
117
Jan Stancek729a7ed2015-09-02 10:19:14 +0200118 /* return number of successfully read bytes */
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100119 return bytes_read;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300120}
121
Jan Stancek729a7ed2015-09-02 10:19:14 +0200122static int read_objdump_output(FILE *f, void *buf, size_t *len, u64 start_addr)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300123{
124 char *line = NULL;
Jan Stancek729a7ed2015-09-02 10:19:14 +0200125 size_t line_len, off_last = 0;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300126 ssize_t ret;
127 int err = 0;
Jan Stancekedfdb7e2015-09-02 10:19:16 +0200128 u64 addr, last_addr = start_addr;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300129
Jan Stancek729a7ed2015-09-02 10:19:14 +0200130 while (off_last < *len) {
131 size_t off, read_bytes, written_bytes;
132 unsigned char tmp[BUFSZ];
133
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300134 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 Stancek729a7ed2015-09-02 10:19:14 +0200142
143 /* read objdump data into temporary buffer */
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100144 read_bytes = read_objdump_line(line, tmp, sizeof(tmp));
Jan Stancek729a7ed2015-09-02 10:19:14 +0200145 if (!read_bytes)
146 continue;
147
148 if (sscanf(line, "%"PRIx64, &addr) != 1)
149 continue;
Jan Stancekedfdb7e2015-09-02 10:19:16 +0200150 if (addr < last_addr) {
151 pr_debug("addr going backwards, read beyond section?\n");
152 break;
153 }
154 last_addr = addr;
Jan Stancek729a7ed2015-09-02 10:19:14 +0200155
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 Hunterb55ae0a2013-08-07 14:38:45 +0300164 }
165
Jan Stancek729a7ed2015-09-02 10:19:14 +0200166 /* len returns number of bytes that could not be read */
167 *len -= off_last;
168
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300169 free(line);
170
171 return err;
172}
173
174static 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 Stancek06f679c2015-09-03 13:23:32 +0200182 fmt = "%s -z -d --start-address=0x%"PRIx64" --stop-address=0x%"PRIx64" %s";
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300183 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 Hunter7a77bc22013-08-07 14:38:53 +0300190 /* Ignore objdump errors */
191 strcat(cmd, " 2>/dev/null");
192
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300193 f = popen(cmd, "r");
194 if (!f) {
195 pr_debug("popen failed\n");
196 return -1;
197 }
198
Jan Stancek729a7ed2015-09-02 10:19:14 +0200199 ret = read_objdump_output(f, buf, &len, addr);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300200 if (len) {
Jan Stancekb2d0dbf2016-01-12 11:07:44 +0100201 pr_debug("objdump read too few bytes: %zd\n", len);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300202 if (!ret)
203 ret = len;
204 }
205
206 pclose(f);
207
208 return ret;
209}
210
Jan Stancekfd405cf2015-09-02 10:19:17 +0200211static 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 Hunterb55ae0a2013-08-07 14:38:45 +0300223static int read_object_code(u64 addr, size_t len, u8 cpumode,
Arnaldo Carvalho de Melo29f9e522014-10-23 17:21:54 -0300224 struct thread *thread, struct state *state)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300225{
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 Melobb871a92014-10-23 12:50:25 -0300235 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300236 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 Hunter7a77bc22013-08-07 14:38:53 +0300243 if (al.map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
244 !dso__is_kcore(al.map->dso)) {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300245 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 Melo29f9e522014-10-23 17:21:54 -0300259 ret_len = dso__data_read_offset(al.map->dso, thread->mg->machine,
260 al.addr, buf1, len);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300261 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 Melobe39db92016-09-01 19:25:52 -0300270 if (map__load(al.map))
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300271 return -1;
272
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300273 /* 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 Hunterb55ae0a2013-08-07 14:38:45 +0300291 /* 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 Hunter7a77bc22013-08-07 14:38:53 +0300302 if (len) {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300303 pr_debug("Reducing len to %zu\n", len);
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300304 } 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 Hunterb55ae0a2013-08-07 14:38:45 +0300313 return -1;
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300314 }
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300315 }
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 Stancekfd405cf2015-09-02 10:19:17 +0200325 pr_debug("buf1 (dso):\n");
326 dump_buf(buf1, len);
327 pr_debug("buf2 (objdump):\n");
328 dump_buf(buf2, len);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300329 return -1;
330 }
331 pr_debug("Bytes read match those read by objdump\n");
332
333 return 0;
334}
335
336static int process_sample_event(struct machine *machine,
337 struct perf_evlist *evlist,
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300338 union perf_event *event, struct state *state)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300339{
340 struct perf_sample sample;
341 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300342 int ret;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300343
344 if (perf_evlist__parse_sample(evlist, event, &sample)) {
345 pr_debug("perf_evlist__parse_sample failed\n");
346 return -1;
347 }
348
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900349 thread = machine__findnew_thread(machine, sample.pid, sample.tid);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300350 if (!thread) {
351 pr_debug("machine__findnew_thread failed\n");
352 return -1;
353 }
354
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300355 ret = read_object_code(sample.ip, READLEN, sample.cpumode, thread, state);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300356 thread__put(thread);
357 return ret;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300358}
359
360static int process_event(struct machine *machine, struct perf_evlist *evlist,
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300361 union perf_event *event, struct state *state)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300362{
363 if (event->header.type == PERF_RECORD_SAMPLE)
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300364 return process_sample_event(machine, evlist, event, state);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300365
Adrian Hunter48095b72013-11-12 09:59:33 +0200366 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 Hunterb55ae0a2013-08-07 14:38:45 +0300379
380 return 0;
381}
382
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300383static int process_events(struct machine *machine, struct perf_evlist *evlist,
384 struct state *state)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300385{
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 Hunter7a77bc22013-08-07 14:38:53 +0300391 ret = process_event(machine, evlist, event, state);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800392 perf_evlist__mmap_consume(evlist, i);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300393 if (ret < 0)
394 return ret;
395 }
396 }
397 return 0;
398}
399
400static int comp(const void *a, const void *b)
401{
402 return *(int *)a - *(int *)b;
403}
404
405static void do_sort_something(void)
406{
David Ahern309b5182013-08-13 22:32:12 -0600407 int buf[40960], i;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300408
David Ahern309b5182013-08-13 22:32:12 -0600409 for (i = 0; i < (int)ARRAY_SIZE(buf); i++)
410 buf[i] = ARRAY_SIZE(buf) - i - 1;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300411
David Ahern309b5182013-08-13 22:32:12 -0600412 qsort(buf, ARRAY_SIZE(buf), sizeof(int), comp);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300413
David Ahern309b5182013-08-13 22:32:12 -0600414 for (i = 0; i < (int)ARRAY_SIZE(buf); i++) {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300415 if (buf[i] != i) {
416 pr_debug("qsort failed\n");
417 break;
418 }
419 }
420}
421
422static void sort_something(void)
423{
424 int i;
425
426 for (i = 0; i < 10; i++)
427 do_sort_something();
428}
429
430static 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
445static 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
460static void do_something(void)
461{
462 fs_something();
463
464 sort_something();
465
466 syscall_something();
467}
468
469enum {
470 TEST_CODE_READING_OK,
471 TEST_CODE_READING_NO_VMLINUX,
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300472 TEST_CODE_READING_NO_KCORE,
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300473 TEST_CODE_READING_NO_ACCESS,
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300474 TEST_CODE_READING_NO_KERNEL_OBJ,
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300475};
476
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300477static int do_test_code_reading(bool try_kcore)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300478{
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300479 struct machine *machine;
480 struct thread *thread;
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300481 struct record_opts opts = {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300482 .mmap_pages = UINT_MAX,
483 .user_freq = UINT_MAX,
484 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo5243ba72016-02-18 13:45:25 -0300485 .freq = 500,
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300486 .target = {
487 .uses_mmap = true,
488 },
489 };
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300490 struct state state = {
491 .done_cnt = 0,
492 };
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300493 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 Hunter7a77bc22013-08-07 14:38:53 +0300500 bool have_vmlinux, have_kcore, excl_kernel = false;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300501
502 pid = getpid();
503
Jiri Olsa0fd40082015-12-03 09:34:14 +0100504 machine = machine__new_host();
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300505
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 Hunter7a77bc22013-08-07 14:38:53 +0300512 /* Force the use of kallsyms instead of vmlinux to try kcore */
513 if (try_kcore)
514 symbol_conf.kallsyms_name = "/proc/kallsyms";
515
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300516 /* Load kernel map */
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300517 map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300518 ret = map__load(map);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300519 if (ret < 0) {
520 pr_debug("map__load failed\n");
521 goto out_err;
522 }
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300523 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 Hunterb55ae0a2013-08-07 14:38:45 +0300532 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 Liang9d9cad72015-06-17 09:51:11 -0400541 perf_event__process, machine, false, 500);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300542 if (ret < 0) {
543 pr_debug("perf_event__synthesize_thread_map failed\n");
544 goto out_err;
545 }
546
Adrian Hunter314add62013-08-27 11:23:03 +0300547 thread = machine__findnew_thread(machine, pid, pid);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300548 if (!thread) {
549 pr_debug("machine__findnew_thread failed\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300550 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300551 }
552
553 cpus = cpu_map__new(NULL);
554 if (!cpus) {
555 pr_debug("cpu_map__new failed\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300556 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300557 }
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 Melob91fc392015-04-06 20:43:22 -0300565 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300566 }
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 Olsab39b8392015-04-22 21:10:16 +0200575 ret = parse_events(evlist, str, NULL);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300576 if (ret < 0) {
577 pr_debug("parse_events failed\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300578 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300579 }
580
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300581 perf_evlist__config(evlist, &opts, NULL);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300582
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 Olsa7320b1b2015-12-03 09:34:15 +0100593 /*
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 Hunterae450a72014-04-10 12:02:54 +0300600 perf_evlist__set_maps(evlist, NULL, NULL);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300601 perf_evlist__delete(evlist);
602 evlist = NULL;
603 continue;
604 }
Arnaldo Carvalho de Melo6880bbf2016-02-18 13:40:57 -0300605
Namhyung Kimbb963e12017-02-17 17:17:38 +0900606 if (verbose > 0) {
Arnaldo Carvalho de Melo6880bbf2016-02-18 13:40:57 -0300607 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 Melob91fc392015-04-06 20:43:22 -0300612 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300613 }
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 Melob91fc392015-04-06 20:43:22 -0300620 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300621 }
622
623 perf_evlist__enable(evlist);
624
625 do_something();
626
627 perf_evlist__disable(evlist);
628
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300629 ret = process_events(machine, evlist, &state);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300630 if (ret < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300631 goto out_put;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300632
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300633 if (!have_vmlinux && !have_kcore && !try_kcore)
634 err = TEST_CODE_READING_NO_KERNEL_OBJ;
635 else if (!have_vmlinux && !try_kcore)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300636 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 Melob91fc392015-04-06 20:43:22 -0300641out_put:
642 thread__put(thread);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300643out_err:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300644
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300645 if (evlist) {
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300646 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300647 } else {
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200648 cpu_map__put(cpus);
Jiri Olsa186fbb72015-06-23 00:36:05 +0200649 thread_map__put(threads);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300650 }
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300651 machine__delete_threads(machine);
Jiri Olsa0fd40082015-12-03 09:34:14 +0100652 machine__delete(machine);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300653
654 return err;
655}
656
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -0300657int test__code_reading(int subtest __maybe_unused)
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300658{
659 int ret;
660
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300661 ret = do_test_code_reading(false);
662 if (!ret)
663 ret = do_test_code_reading(true);
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300664
665 switch (ret) {
666 case TEST_CODE_READING_OK:
667 return 0;
668 case TEST_CODE_READING_NO_VMLINUX:
Wang Nan597bdeb2015-11-03 10:44:42 +0000669 pr_debug("no vmlinux\n");
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300670 return 0;
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300671 case TEST_CODE_READING_NO_KCORE:
Wang Nan597bdeb2015-11-03 10:44:42 +0000672 pr_debug("no kcore\n");
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300673 return 0;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300674 case TEST_CODE_READING_NO_ACCESS:
Wang Nan597bdeb2015-11-03 10:44:42 +0000675 pr_debug("no access\n");
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300676 return 0;
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300677 case TEST_CODE_READING_NO_KERNEL_OBJ:
Wang Nan597bdeb2015-11-03 10:44:42 +0000678 pr_debug("no kernel obj\n");
Adrian Hunter7a77bc22013-08-07 14:38:53 +0300679 return 0;
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300680 default:
681 return -1;
682 };
683}