code-reading.c 15 KB

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