code-reading.c 11.9 KB
Newer Older
B
Borislav Petkov 已提交
1
#include <linux/types.h>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include "parse-events.h"
#include "evlist.h"
#include "evsel.h"
#include "thread_map.h"
#include "cpumap.h"
#include "machine.h"
#include "event.h"
#include "thread.h"

#include "tests.h"

#define BUFSZ	1024
#define READLEN	128

22 23 24 25 26
struct state {
	u64 done[1024];
	size_t done_cnt;
};

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static unsigned int hex(char c)
{
	if (c >= '0' && c <= '9')
		return c - '0';
	if (c >= 'a' && c <= 'f')
		return c - 'a' + 10;
	return c - 'A' + 10;
}

static void read_objdump_line(const char *line, size_t line_len, void **buf,
			      size_t *len)
{
	const char *p;
	size_t i;

	/* Skip to a colon */
	p = strchr(line, ':');
	if (!p)
		return;
	i = p + 1 - line;

	/* Read bytes */
	while (*len) {
		char c1, c2;

		/* Skip spaces */
		for (; i < line_len; i++) {
			if (!isspace(line[i]))
				break;
		}
		/* Get 2 hex digits */
		if (i >= line_len || !isxdigit(line[i]))
			break;
		c1 = line[i++];
		if (i >= line_len || !isxdigit(line[i]))
			break;
		c2 = line[i++];
		/* Followed by a space */
		if (i < line_len && line[i] && !isspace(line[i]))
			break;
		/* Store byte */
		*(unsigned char *)*buf = (hex(c1) << 4) | hex(c2);
		*buf += 1;
		*len -= 1;
	}
}

static int read_objdump_output(FILE *f, void **buf, size_t *len)
{
	char *line = NULL;
	size_t line_len;
	ssize_t ret;
	int err = 0;

	while (1) {
		ret = getline(&line, &line_len, f);
		if (feof(f))
			break;
		if (ret < 0) {
			pr_debug("getline failed\n");
			err = -1;
			break;
		}
		read_objdump_line(line, ret, buf, len);
	}

	free(line);

	return err;
}

static int read_via_objdump(const char *filename, u64 addr, void *buf,
			    size_t len)
{
	char cmd[PATH_MAX * 2];
	const char *fmt;
	FILE *f;
	int ret;

	fmt = "%s -d --start-address=0x%"PRIx64" --stop-address=0x%"PRIx64" %s";
	ret = snprintf(cmd, sizeof(cmd), fmt, "objdump", addr, addr + len,
		       filename);
	if (ret <= 0 || (size_t)ret >= sizeof(cmd))
		return -1;

	pr_debug("Objdump command is: %s\n", cmd);

114 115 116
	/* Ignore objdump errors */
	strcat(cmd, " 2>/dev/null");

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	f = popen(cmd, "r");
	if (!f) {
		pr_debug("popen failed\n");
		return -1;
	}

	ret = read_objdump_output(f, &buf, &len);
	if (len) {
		pr_debug("objdump read too few bytes\n");
		if (!ret)
			ret = len;
	}

	pclose(f);

	return ret;
}

static int read_object_code(u64 addr, size_t len, u8 cpumode,
136
			    struct thread *thread, struct state *state)
137 138 139 140 141 142 143 144 145 146
{
	struct addr_location al;
	unsigned char buf1[BUFSZ];
	unsigned char buf2[BUFSZ];
	size_t ret_len;
	u64 objdump_addr;
	int ret;

	pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr);

147
	thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
148 149 150 151 152 153 154
	if (!al.map || !al.map->dso) {
		pr_debug("thread__find_addr_map failed\n");
		return -1;
	}

	pr_debug("File is: %s\n", al.map->dso->long_name);

155 156
	if (al.map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
	    !dso__is_kcore(al.map->dso)) {
157 158 159 160 161 162 163 164 165 166 167 168 169 170
		pr_debug("Unexpected kernel address - skipping\n");
		return 0;
	}

	pr_debug("On file address is: %#"PRIx64"\n", al.addr);

	if (len > BUFSZ)
		len = BUFSZ;

	/* Do not go off the map */
	if (addr + len > al.map->end)
		len = al.map->end - addr;

	/* Read the object code using perf */
171 172
	ret_len = dso__data_read_offset(al.map->dso, thread->mg->machine,
					al.addr, buf1, len);
173 174 175 176 177 178 179 180 181 182 183 184
	if (ret_len != len) {
		pr_debug("dso__data_read_offset failed\n");
		return -1;
	}

	/*
	 * Converting addresses for use by objdump requires more information.
	 * map__load() does that.  See map__rip_2objdump() for details.
	 */
	if (map__load(al.map, NULL))
		return -1;

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	/* objdump struggles with kcore - try each map only once */
	if (dso__is_kcore(al.map->dso)) {
		size_t d;

		for (d = 0; d < state->done_cnt; d++) {
			if (state->done[d] == al.map->start) {
				pr_debug("kcore map tested already");
				pr_debug(" - skipping\n");
				return 0;
			}
		}
		if (state->done_cnt >= ARRAY_SIZE(state->done)) {
			pr_debug("Too many kcore maps - skipping\n");
			return 0;
		}
		state->done[state->done_cnt++] = al.map->start;
	}

203 204 205 206 207 208 209 210 211 212 213
	/* Read the object code using objdump */
	objdump_addr = map__rip_2objdump(al.map, al.addr);
	ret = read_via_objdump(al.map->dso->long_name, objdump_addr, buf2, len);
	if (ret > 0) {
		/*
		 * The kernel maps are inaccurate - assume objdump is right in
		 * that case.
		 */
		if (cpumode == PERF_RECORD_MISC_KERNEL ||
		    cpumode == PERF_RECORD_MISC_GUEST_KERNEL) {
			len -= ret;
214
			if (len) {
215
				pr_debug("Reducing len to %zu\n", len);
216 217 218 219 220 221 222 223 224
			} else if (dso__is_kcore(al.map->dso)) {
				/*
				 * objdump cannot handle very large segments
				 * that may be found in kcore.
				 */
				pr_debug("objdump failed for kcore");
				pr_debug(" - skipping\n");
				return 0;
			} else {
225
				return -1;
226
			}
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		}
	}
	if (ret < 0) {
		pr_debug("read_via_objdump failed\n");
		return -1;
	}

	/* The results should be identical */
	if (memcmp(buf1, buf2, len)) {
		pr_debug("Bytes read differ from those read by objdump\n");
		return -1;
	}
	pr_debug("Bytes read match those read by objdump\n");

	return 0;
}

static int process_sample_event(struct machine *machine,
				struct perf_evlist *evlist,
246
				union perf_event *event, struct state *state)
247 248 249 250
{
	struct perf_sample sample;
	struct thread *thread;
	u8 cpumode;
251
	int ret;
252 253 254 255 256 257

	if (perf_evlist__parse_sample(evlist, event, &sample)) {
		pr_debug("perf_evlist__parse_sample failed\n");
		return -1;
	}

258
	thread = machine__findnew_thread(machine, sample.pid, sample.tid);
259 260 261 262 263 264 265
	if (!thread) {
		pr_debug("machine__findnew_thread failed\n");
		return -1;
	}

	cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;

266 267 268
	ret = read_object_code(sample.ip, READLEN, cpumode, thread, state);
	thread__put(thread);
	return ret;
269 270 271
}

static int process_event(struct machine *machine, struct perf_evlist *evlist,
272
			 union perf_event *event, struct state *state)
273 274
{
	if (event->header.type == PERF_RECORD_SAMPLE)
275
		return process_sample_event(machine, evlist, event, state);
276

277 278 279 280 281 282 283 284 285 286 287 288 289
	if (event->header.type == PERF_RECORD_THROTTLE ||
	    event->header.type == PERF_RECORD_UNTHROTTLE)
		return 0;

	if (event->header.type < PERF_RECORD_MAX) {
		int ret;

		ret = machine__process_event(machine, event, NULL);
		if (ret < 0)
			pr_debug("machine__process_event failed, event type %u\n",
				 event->header.type);
		return ret;
	}
290 291 292 293

	return 0;
}

294 295
static int process_events(struct machine *machine, struct perf_evlist *evlist,
			  struct state *state)
296 297 298 299 300 301
{
	union perf_event *event;
	int i, ret;

	for (i = 0; i < evlist->nr_mmaps; i++) {
		while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
302
			ret = process_event(machine, evlist, event, state);
303
			perf_evlist__mmap_consume(evlist, i);
304 305 306 307 308 309 310 311 312 313 314 315 316 317
			if (ret < 0)
				return ret;
		}
	}
	return 0;
}

static int comp(const void *a, const void *b)
{
	return *(int *)a - *(int *)b;
}

static void do_sort_something(void)
{
318
	int buf[40960], i;
319

320 321
	for (i = 0; i < (int)ARRAY_SIZE(buf); i++)
		buf[i] = ARRAY_SIZE(buf) - i - 1;
322

323
	qsort(buf, ARRAY_SIZE(buf), sizeof(int), comp);
324

325
	for (i = 0; i < (int)ARRAY_SIZE(buf); i++) {
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
		if (buf[i] != i) {
			pr_debug("qsort failed\n");
			break;
		}
	}
}

static void sort_something(void)
{
	int i;

	for (i = 0; i < 10; i++)
		do_sort_something();
}

static void syscall_something(void)
{
	int pipefd[2];
	int i;

	for (i = 0; i < 1000; i++) {
		if (pipe(pipefd) < 0) {
			pr_debug("pipe failed\n");
			break;
		}
		close(pipefd[1]);
		close(pipefd[0]);
	}
}

static void fs_something(void)
{
	const char *test_file_name = "temp-perf-code-reading-test-file--";
	FILE *f;
	int i;

	for (i = 0; i < 1000; i++) {
		f = fopen(test_file_name, "w+");
		if (f) {
			fclose(f);
			unlink(test_file_name);
		}
	}
}

static void do_something(void)
{
	fs_something();

	sort_something();

	syscall_something();
}

enum {
	TEST_CODE_READING_OK,
	TEST_CODE_READING_NO_VMLINUX,
383
	TEST_CODE_READING_NO_KCORE,
384
	TEST_CODE_READING_NO_ACCESS,
385
	TEST_CODE_READING_NO_KERNEL_OBJ,
386 387
};

388
static int do_test_code_reading(bool try_kcore)
389 390 391 392
{
	struct machines machines;
	struct machine *machine;
	struct thread *thread;
393
	struct record_opts opts = {
394 395 396 397 398 399 400 401
		.mmap_pages	     = UINT_MAX,
		.user_freq	     = UINT_MAX,
		.user_interval	     = ULLONG_MAX,
		.freq		     = 4000,
		.target		     = {
			.uses_mmap   = true,
		},
	};
402 403 404
	struct state state = {
		.done_cnt = 0,
	};
405 406 407 408 409 410 411
	struct thread_map *threads = NULL;
	struct cpu_map *cpus = NULL;
	struct perf_evlist *evlist = NULL;
	struct perf_evsel *evsel = NULL;
	int err = -1, ret;
	pid_t pid;
	struct map *map;
412
	bool have_vmlinux, have_kcore, excl_kernel = false;
413 414 415 416 417 418 419 420 421 422 423 424

	pid = getpid();

	machines__init(&machines);
	machine = &machines.host;

	ret = machine__create_kernel_maps(machine);
	if (ret < 0) {
		pr_debug("machine__create_kernel_maps failed\n");
		goto out_err;
	}

425 426 427 428
	/* Force the use of kallsyms instead of vmlinux to try kcore */
	if (try_kcore)
		symbol_conf.kallsyms_name = "/proc/kallsyms";

429 430 431 432 433 434 435
	/* Load kernel map */
	map = machine->vmlinux_maps[MAP__FUNCTION];
	ret = map__load(map, NULL);
	if (ret < 0) {
		pr_debug("map__load failed\n");
		goto out_err;
	}
436 437 438 439 440 441 442 443 444
	have_vmlinux = dso__is_vmlinux(map->dso);
	have_kcore = dso__is_kcore(map->dso);

	/* 2nd time through we just try kcore */
	if (try_kcore && !have_kcore)
		return TEST_CODE_READING_NO_KCORE;

	/* No point getting kernel events if there is no kernel object */
	if (!have_vmlinux && !have_kcore)
445 446 447 448 449 450 451 452 453
		excl_kernel = true;

	threads = thread_map__new_by_tid(pid);
	if (!threads) {
		pr_debug("thread_map__new_by_tid failed\n");
		goto out_err;
	}

	ret = perf_event__synthesize_thread_map(NULL, threads,
454
						perf_event__process, machine, false);
455 456 457 458 459
	if (ret < 0) {
		pr_debug("perf_event__synthesize_thread_map failed\n");
		goto out_err;
	}

460
	thread = machine__findnew_thread(machine, pid, pid);
461 462
	if (!thread) {
		pr_debug("machine__findnew_thread failed\n");
463
		goto out_put;
464 465 466 467 468
	}

	cpus = cpu_map__new(NULL);
	if (!cpus) {
		pr_debug("cpu_map__new failed\n");
469
		goto out_put;
470 471 472 473 474 475 476 477
	}

	while (1) {
		const char *str;

		evlist = perf_evlist__new();
		if (!evlist) {
			pr_debug("perf_evlist__new failed\n");
478
			goto out_put;
479 480 481 482 483 484 485 486 487
		}

		perf_evlist__set_maps(evlist, cpus, threads);

		if (excl_kernel)
			str = "cycles:u";
		else
			str = "cycles";
		pr_debug("Parsing event '%s'\n", str);
488
		ret = parse_events(evlist, str, NULL);
489 490
		if (ret < 0) {
			pr_debug("parse_events failed\n");
491
			goto out_put;
492 493 494 495 496 497 498 499 500 501 502 503 504 505
		}

		perf_evlist__config(evlist, &opts);

		evsel = perf_evlist__first(evlist);

		evsel->attr.comm = 1;
		evsel->attr.disabled = 1;
		evsel->attr.enable_on_exec = 0;

		ret = perf_evlist__open(evlist);
		if (ret < 0) {
			if (!excl_kernel) {
				excl_kernel = true;
506
				perf_evlist__set_maps(evlist, NULL, NULL);
507 508 509 510 511
				perf_evlist__delete(evlist);
				evlist = NULL;
				continue;
			}
			pr_debug("perf_evlist__open failed\n");
512
			goto out_put;
513 514 515 516 517 518 519
		}
		break;
	}

	ret = perf_evlist__mmap(evlist, UINT_MAX, false);
	if (ret < 0) {
		pr_debug("perf_evlist__mmap failed\n");
520
		goto out_put;
521 522 523 524 525 526 527 528
	}

	perf_evlist__enable(evlist);

	do_something();

	perf_evlist__disable(evlist);

529
	ret = process_events(machine, evlist, &state);
530
	if (ret < 0)
531
		goto out_put;
532

533 534 535
	if (!have_vmlinux && !have_kcore && !try_kcore)
		err = TEST_CODE_READING_NO_KERNEL_OBJ;
	else if (!have_vmlinux && !try_kcore)
536 537 538 539 540
		err = TEST_CODE_READING_NO_VMLINUX;
	else if (excl_kernel)
		err = TEST_CODE_READING_NO_ACCESS;
	else
		err = TEST_CODE_READING_OK;
541 542
out_put:
	thread__put(thread);
543
out_err:
544

545 546
	if (evlist) {
		perf_evlist__delete(evlist);
547
	} else {
548 549
		cpu_map__delete(cpus);
		thread_map__delete(threads);
550
	}
551 552 553 554 555 556 557 558 559 560 561
	machines__destroy_kernel_maps(&machines);
	machine__delete_threads(machine);
	machines__exit(&machines);

	return err;
}

int test__code_reading(void)
{
	int ret;

562 563 564
	ret = do_test_code_reading(false);
	if (!ret)
		ret = do_test_code_reading(true);
565 566 567 568 569 570 571

	switch (ret) {
	case TEST_CODE_READING_OK:
		return 0;
	case TEST_CODE_READING_NO_VMLINUX:
		fprintf(stderr, " (no vmlinux)");
		return 0;
572 573 574
	case TEST_CODE_READING_NO_KCORE:
		fprintf(stderr, " (no kcore)");
		return 0;
575 576 577
	case TEST_CODE_READING_NO_ACCESS:
		fprintf(stderr, " (no access)");
		return 0;
578 579 580
	case TEST_CODE_READING_NO_KERNEL_OBJ:
		fprintf(stderr, " (no kernel obj)");
		return 0;
581 582 583 584
	default:
		return -1;
	};
}