code-reading.c 16 KB

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