1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <linux/types.h>
10 #include <sys/param.h>
11 #include <perf/cpumap.h>
12 #include <perf/evlist.h>
17 #include "parse-events.h"
18 #include "trace-event.h"
21 #include "thread_map.h"
32 #include <linux/ctype.h>
42 static unsigned int hex(char c)
44 if (c >= '0' && c <= '9')
46 if (c >= 'a' && c <= 'f')
51 static size_t read_objdump_chunk(const char **line, unsigned char **buf,
54 size_t bytes_read = 0;
55 unsigned char *chunk_start = *buf;
58 while (*buf_len > 0) {
61 /* Get 2 hex digits */
69 /* Store byte and advance buf */
70 **buf = (hex(c1) << 4) | hex(c2);
81 * objdump will display raw insn as LE if code endian
82 * is LE and bytes_per_chunk > 1. In that case reverse
83 * the chunk we just read.
85 * see disassemble_bytes() at binutils/objdump.c for details
86 * how objdump chooses display endian)
88 if (bytes_read > 1 && !bigendian()) {
89 unsigned char *chunk_end = chunk_start + bytes_read - 1;
92 while (chunk_start < chunk_end) {
94 *chunk_start = *chunk_end;
104 static size_t read_objdump_line(const char *line, unsigned char *buf,
108 size_t ret, bytes_read = 0;
110 /* Skip to a colon */
111 p = strchr(line, ':');
116 /* Skip initial spaces */
124 ret = read_objdump_chunk(&p, &buf, &buf_len);
129 /* return number of successfully read bytes */
133 static int read_objdump_output(FILE *f, void *buf, size_t *len, u64 start_addr)
136 size_t line_len, off_last = 0;
139 u64 addr, last_addr = start_addr;
141 while (off_last < *len) {
142 size_t off, read_bytes, written_bytes;
143 unsigned char tmp[BUFSZ];
145 ret = getline(&line, &line_len, f);
149 pr_debug("getline failed\n");
154 /* read objdump data into temporary buffer */
155 read_bytes = read_objdump_line(line, tmp, sizeof(tmp));
159 if (sscanf(line, "%"PRIx64, &addr) != 1)
161 if (addr < last_addr) {
162 pr_debug("addr going backwards, read beyond section?\n");
167 /* copy it from temporary buffer to 'buf' according
168 * to address on current objdump line */
169 off = addr - start_addr;
172 written_bytes = MIN(read_bytes, *len - off);
173 memcpy(buf + off, tmp, written_bytes);
174 off_last = off + written_bytes;
177 /* len returns number of bytes that could not be read */
185 static int read_via_objdump(const char *filename, u64 addr, void *buf,
188 char cmd[PATH_MAX * 2];
193 fmt = "%s -z -d --start-address=0x%"PRIx64" --stop-address=0x%"PRIx64" %s";
194 ret = snprintf(cmd, sizeof(cmd), fmt, "objdump", addr, addr + len,
196 if (ret <= 0 || (size_t)ret >= sizeof(cmd))
199 pr_debug("Objdump command is: %s\n", cmd);
201 /* Ignore objdump errors */
202 strcat(cmd, " 2>/dev/null");
206 pr_debug("popen failed\n");
210 ret = read_objdump_output(f, buf, &len, addr);
212 pr_debug("objdump read too few bytes: %zd\n", len);
222 static void dump_buf(unsigned char *buf, size_t len)
226 for (i = 0; i < len; i++) {
227 pr_debug("0x%02x ", buf[i]);
234 static int read_object_code(u64 addr, size_t len, u8 cpumode,
235 struct thread *thread, struct state *state)
237 struct addr_location al;
238 unsigned char buf1[BUFSZ];
239 unsigned char buf2[BUFSZ];
242 const char *objdump_name;
243 char decomp_name[KMOD_DECOMP_LEN];
247 pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr);
249 if (!thread__find_map(thread, cpumode, addr, &al) || !al.map->dso) {
250 if (cpumode == PERF_RECORD_MISC_HYPERVISOR) {
251 pr_debug("Hypervisor address can not be resolved - skipping\n");
255 pr_debug("thread__find_map failed\n");
259 pr_debug("File is: %s\n", al.map->dso->long_name);
261 if (al.map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
262 !dso__is_kcore(al.map->dso)) {
263 pr_debug("Unexpected kernel address - skipping\n");
267 pr_debug("On file address is: %#"PRIx64"\n", al.addr);
272 /* Do not go off the map */
273 if (addr + len > al.map->end)
274 len = al.map->end - addr;
276 /* Read the object code using perf */
277 ret_len = dso__data_read_offset(al.map->dso, thread->mg->machine,
279 if (ret_len != len) {
280 pr_debug("dso__data_read_offset failed\n");
285 * Converting addresses for use by objdump requires more information.
286 * map__load() does that. See map__rip_2objdump() for details.
288 if (map__load(al.map))
291 /* objdump struggles with kcore - try each map only once */
292 if (dso__is_kcore(al.map->dso)) {
295 for (d = 0; d < state->done_cnt; d++) {
296 if (state->done[d] == al.map->start) {
297 pr_debug("kcore map tested already");
298 pr_debug(" - skipping\n");
302 if (state->done_cnt >= ARRAY_SIZE(state->done)) {
303 pr_debug("Too many kcore maps - skipping\n");
306 state->done[state->done_cnt++] = al.map->start;
309 objdump_name = al.map->dso->long_name;
310 if (dso__needs_decompress(al.map->dso)) {
311 if (dso__decompress_kmodule_path(al.map->dso, objdump_name,
313 sizeof(decomp_name)) < 0) {
314 pr_debug("decompression failed\n");
319 objdump_name = decomp_name;
322 /* Read the object code using objdump */
323 objdump_addr = map__rip_2objdump(al.map, al.addr);
324 ret = read_via_objdump(objdump_name, objdump_addr, buf2, len);
327 unlink(objdump_name);
331 * The kernel maps are inaccurate - assume objdump is right in
334 if (cpumode == PERF_RECORD_MISC_KERNEL ||
335 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) {
338 pr_debug("Reducing len to %zu\n", len);
339 } else if (dso__is_kcore(al.map->dso)) {
341 * objdump cannot handle very large segments
342 * that may be found in kcore.
344 pr_debug("objdump failed for kcore");
345 pr_debug(" - skipping\n");
353 pr_debug("read_via_objdump failed\n");
357 /* The results should be identical */
358 if (memcmp(buf1, buf2, len)) {
359 pr_debug("Bytes read differ from those read by objdump\n");
360 pr_debug("buf1 (dso):\n");
362 pr_debug("buf2 (objdump):\n");
366 pr_debug("Bytes read match those read by objdump\n");
371 static int process_sample_event(struct machine *machine,
372 struct evlist *evlist,
373 union perf_event *event, struct state *state)
375 struct perf_sample sample;
376 struct thread *thread;
379 if (perf_evlist__parse_sample(evlist, event, &sample)) {
380 pr_debug("perf_evlist__parse_sample failed\n");
384 thread = machine__findnew_thread(machine, sample.pid, sample.tid);
386 pr_debug("machine__findnew_thread failed\n");
390 ret = read_object_code(sample.ip, READLEN, sample.cpumode, thread, state);
395 static int process_event(struct machine *machine, struct evlist *evlist,
396 union perf_event *event, struct state *state)
398 if (event->header.type == PERF_RECORD_SAMPLE)
399 return process_sample_event(machine, evlist, event, state);
401 if (event->header.type == PERF_RECORD_THROTTLE ||
402 event->header.type == PERF_RECORD_UNTHROTTLE)
405 if (event->header.type < PERF_RECORD_MAX) {
408 ret = machine__process_event(machine, event, NULL);
410 pr_debug("machine__process_event failed, event type %u\n",
418 static int process_events(struct machine *machine, struct evlist *evlist,
421 union perf_event *event;
422 struct perf_mmap *md;
425 for (i = 0; i < evlist->nr_mmaps; i++) {
426 md = &evlist->mmap[i];
427 if (perf_mmap__read_init(md) < 0)
430 while ((event = perf_mmap__read_event(md)) != NULL) {
431 ret = process_event(machine, evlist, event, state);
432 perf_mmap__consume(md);
436 perf_mmap__read_done(md);
441 static int comp(const void *a, const void *b)
443 return *(int *)a - *(int *)b;
446 static void do_sort_something(void)
450 for (i = 0; i < (int)ARRAY_SIZE(buf); i++)
451 buf[i] = ARRAY_SIZE(buf) - i - 1;
453 qsort(buf, ARRAY_SIZE(buf), sizeof(int), comp);
455 for (i = 0; i < (int)ARRAY_SIZE(buf); i++) {
457 pr_debug("qsort failed\n");
463 static void sort_something(void)
467 for (i = 0; i < 10; i++)
471 static void syscall_something(void)
476 for (i = 0; i < 1000; i++) {
477 if (pipe(pipefd) < 0) {
478 pr_debug("pipe failed\n");
486 static void fs_something(void)
488 const char *test_file_name = "temp-perf-code-reading-test-file--";
492 for (i = 0; i < 1000; i++) {
493 f = fopen(test_file_name, "w+");
496 unlink(test_file_name);
502 #include "header.h" // for get_cpuid()
505 static const char *do_determine_event(bool excl_kernel)
507 const char *event = excl_kernel ? "cycles:u" : "cycles";
510 char cpuid[128], model[16], model_c[16], cpum_cf_v[16];
514 if (get_cpuid(cpuid, sizeof(cpuid)))
516 ret = sscanf(cpuid, "%*[^,],%u,%[^,],%[^,],%[^,],%x", &family, model_c,
517 model, cpum_cf_v, &cpum_cf_a);
518 if (ret != 5) /* Not available */
520 if (excl_kernel && (cpum_cf_a & 4))
522 if (!excl_kernel && (cpum_cf_a & 2))
525 /* Fall through: missing authorization */
527 event = excl_kernel ? "cpu-clock:u" : "cpu-clock";
533 static void do_something(void)
543 TEST_CODE_READING_OK,
544 TEST_CODE_READING_NO_VMLINUX,
545 TEST_CODE_READING_NO_KCORE,
546 TEST_CODE_READING_NO_ACCESS,
547 TEST_CODE_READING_NO_KERNEL_OBJ,
550 static int do_test_code_reading(bool try_kcore)
552 struct machine *machine;
553 struct thread *thread;
554 struct record_opts opts = {
555 .mmap_pages = UINT_MAX,
556 .user_freq = UINT_MAX,
557 .user_interval = ULLONG_MAX,
563 struct state state = {
566 struct perf_thread_map *threads = NULL;
567 struct perf_cpu_map *cpus = NULL;
568 struct evlist *evlist = NULL;
569 struct evsel *evsel = NULL;
573 bool have_vmlinux, have_kcore, excl_kernel = false;
577 machine = machine__new_host();
578 machine->env = &perf_env;
580 ret = machine__create_kernel_maps(machine);
582 pr_debug("machine__create_kernel_maps failed\n");
586 /* Force the use of kallsyms instead of vmlinux to try kcore */
588 symbol_conf.kallsyms_name = "/proc/kallsyms";
590 /* Load kernel map */
591 map = machine__kernel_map(machine);
592 ret = map__load(map);
594 pr_debug("map__load failed\n");
597 have_vmlinux = dso__is_vmlinux(map->dso);
598 have_kcore = dso__is_kcore(map->dso);
600 /* 2nd time through we just try kcore */
601 if (try_kcore && !have_kcore)
602 return TEST_CODE_READING_NO_KCORE;
604 /* No point getting kernel events if there is no kernel object */
605 if (!have_vmlinux && !have_kcore)
608 threads = thread_map__new_by_tid(pid);
610 pr_debug("thread_map__new_by_tid failed\n");
614 ret = perf_event__synthesize_thread_map(NULL, threads,
615 perf_event__process, machine, false);
617 pr_debug("perf_event__synthesize_thread_map failed\n");
621 thread = machine__findnew_thread(machine, pid, pid);
623 pr_debug("machine__findnew_thread failed\n");
627 cpus = perf_cpu_map__new(NULL);
629 pr_debug("perf_cpu_map__new failed\n");
636 evlist = evlist__new();
638 pr_debug("perf_evlist__new failed\n");
642 perf_evlist__set_maps(&evlist->core, cpus, threads);
644 str = do_determine_event(excl_kernel);
645 pr_debug("Parsing event '%s'\n", str);
646 ret = parse_events(evlist, str, NULL);
648 pr_debug("parse_events failed\n");
652 perf_evlist__config(evlist, &opts, NULL);
654 evsel = perf_evlist__first(evlist);
656 evsel->core.attr.comm = 1;
657 evsel->core.attr.disabled = 1;
658 evsel->core.attr.enable_on_exec = 0;
660 ret = evlist__open(evlist);
665 * Both cpus and threads are now owned by evlist
666 * and will be freed by following perf_evlist__set_maps
667 * call. Getting refference to keep them alive.
669 perf_cpu_map__get(cpus);
670 perf_thread_map__get(threads);
671 perf_evlist__set_maps(&evlist->core, NULL, NULL);
672 evlist__delete(evlist);
679 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
680 pr_debug("perf_evlist__open() failed!\n%s\n", errbuf);
688 ret = perf_evlist__mmap(evlist, UINT_MAX);
690 pr_debug("perf_evlist__mmap failed\n");
694 evlist__enable(evlist);
698 evlist__disable(evlist);
700 ret = process_events(machine, evlist, &state);
704 if (!have_vmlinux && !have_kcore && !try_kcore)
705 err = TEST_CODE_READING_NO_KERNEL_OBJ;
706 else if (!have_vmlinux && !try_kcore)
707 err = TEST_CODE_READING_NO_VMLINUX;
708 else if (excl_kernel)
709 err = TEST_CODE_READING_NO_ACCESS;
711 err = TEST_CODE_READING_OK;
717 evlist__delete(evlist);
719 perf_cpu_map__put(cpus);
720 perf_thread_map__put(threads);
722 machine__delete_threads(machine);
723 machine__delete(machine);
728 int test__code_reading(struct test *test __maybe_unused, int subtest __maybe_unused)
732 ret = do_test_code_reading(false);
734 ret = do_test_code_reading(true);
737 case TEST_CODE_READING_OK:
739 case TEST_CODE_READING_NO_VMLINUX:
740 pr_debug("no vmlinux\n");
742 case TEST_CODE_READING_NO_KCORE:
743 pr_debug("no kcore\n");
745 case TEST_CODE_READING_NO_ACCESS:
746 pr_debug("no access\n");
748 case TEST_CODE_READING_NO_KERNEL_OBJ:
749 pr_debug("no kernel obj\n");