code-reading.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. if (!thread__find_map(thread, cpumode, addr, &al) || !al.map->dso) {
  199. if (cpumode == PERF_RECORD_MISC_HYPERVISOR) {
  200. pr_debug("Hypervisor address can not be resolved - skipping\n");
  201. return 0;
  202. }
  203. pr_debug("thread__find_map failed\n");
  204. return -1;
  205. }
  206. pr_debug("File is: %s\n", al.map->dso->long_name);
  207. if (al.map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  208. !dso__is_kcore(al.map->dso)) {
  209. pr_debug("Unexpected kernel address - skipping\n");
  210. return 0;
  211. }
  212. pr_debug("On file address is: %#"PRIx64"\n", al.addr);
  213. if (len > BUFSZ)
  214. len = BUFSZ;
  215. /* Do not go off the map */
  216. if (addr + len > al.map->end)
  217. len = al.map->end - addr;
  218. /* Read the object code using perf */
  219. ret_len = dso__data_read_offset(al.map->dso, thread->mg->machine,
  220. al.addr, buf1, len);
  221. if (ret_len != len) {
  222. pr_debug("dso__data_read_offset failed\n");
  223. return -1;
  224. }
  225. /*
  226. * Converting addresses for use by objdump requires more information.
  227. * map__load() does that. See map__rip_2objdump() for details.
  228. */
  229. if (map__load(al.map))
  230. return -1;
  231. /* objdump struggles with kcore - try each map only once */
  232. if (dso__is_kcore(al.map->dso)) {
  233. size_t d;
  234. for (d = 0; d < state->done_cnt; d++) {
  235. if (state->done[d] == al.map->start) {
  236. pr_debug("kcore map tested already");
  237. pr_debug(" - skipping\n");
  238. return 0;
  239. }
  240. }
  241. if (state->done_cnt >= ARRAY_SIZE(state->done)) {
  242. pr_debug("Too many kcore maps - skipping\n");
  243. return 0;
  244. }
  245. state->done[state->done_cnt++] = al.map->start;
  246. }
  247. objdump_name = al.map->dso->long_name;
  248. if (dso__needs_decompress(al.map->dso)) {
  249. if (dso__decompress_kmodule_path(al.map->dso, objdump_name,
  250. decomp_name,
  251. sizeof(decomp_name)) < 0) {
  252. pr_debug("decompression failed\n");
  253. return -1;
  254. }
  255. objdump_name = decomp_name;
  256. }
  257. /* Read the object code using objdump */
  258. objdump_addr = map__rip_2objdump(al.map, al.addr);
  259. ret = read_via_objdump(objdump_name, objdump_addr, buf2, len);
  260. if (dso__needs_decompress(al.map->dso))
  261. unlink(objdump_name);
  262. if (ret > 0) {
  263. /*
  264. * The kernel maps are inaccurate - assume objdump is right in
  265. * that case.
  266. */
  267. if (cpumode == PERF_RECORD_MISC_KERNEL ||
  268. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) {
  269. len -= ret;
  270. if (len) {
  271. pr_debug("Reducing len to %zu\n", len);
  272. } else if (dso__is_kcore(al.map->dso)) {
  273. /*
  274. * objdump cannot handle very large segments
  275. * that may be found in kcore.
  276. */
  277. pr_debug("objdump failed for kcore");
  278. pr_debug(" - skipping\n");
  279. return 0;
  280. } else {
  281. return -1;
  282. }
  283. }
  284. }
  285. if (ret < 0) {
  286. pr_debug("read_via_objdump failed\n");
  287. return -1;
  288. }
  289. /* The results should be identical */
  290. if (memcmp(buf1, buf2, len)) {
  291. pr_debug("Bytes read differ from those read by objdump\n");
  292. pr_debug("buf1 (dso):\n");
  293. dump_buf(buf1, len);
  294. pr_debug("buf2 (objdump):\n");
  295. dump_buf(buf2, len);
  296. return -1;
  297. }
  298. pr_debug("Bytes read match those read by objdump\n");
  299. return 0;
  300. }
  301. static int process_sample_event(struct machine *machine,
  302. struct perf_evlist *evlist,
  303. union perf_event *event, struct state *state)
  304. {
  305. struct perf_sample sample;
  306. struct thread *thread;
  307. int ret;
  308. if (perf_evlist__parse_sample(evlist, event, &sample)) {
  309. pr_debug("perf_evlist__parse_sample failed\n");
  310. return -1;
  311. }
  312. thread = machine__findnew_thread(machine, sample.pid, sample.tid);
  313. if (!thread) {
  314. pr_debug("machine__findnew_thread failed\n");
  315. return -1;
  316. }
  317. ret = read_object_code(sample.ip, READLEN, sample.cpumode, thread, state);
  318. thread__put(thread);
  319. return ret;
  320. }
  321. static int process_event(struct machine *machine, struct perf_evlist *evlist,
  322. union perf_event *event, struct state *state)
  323. {
  324. if (event->header.type == PERF_RECORD_SAMPLE)
  325. return process_sample_event(machine, evlist, event, state);
  326. if (event->header.type == PERF_RECORD_THROTTLE ||
  327. event->header.type == PERF_RECORD_UNTHROTTLE)
  328. return 0;
  329. if (event->header.type < PERF_RECORD_MAX) {
  330. int ret;
  331. ret = machine__process_event(machine, event, NULL);
  332. if (ret < 0)
  333. pr_debug("machine__process_event failed, event type %u\n",
  334. event->header.type);
  335. return ret;
  336. }
  337. return 0;
  338. }
  339. static int process_events(struct machine *machine, struct perf_evlist *evlist,
  340. struct state *state)
  341. {
  342. union perf_event *event;
  343. struct perf_mmap *md;
  344. int i, ret;
  345. for (i = 0; i < evlist->nr_mmaps; i++) {
  346. md = &evlist->mmap[i];
  347. if (perf_mmap__read_init(md) < 0)
  348. continue;
  349. while ((event = perf_mmap__read_event(md)) != NULL) {
  350. ret = process_event(machine, evlist, event, state);
  351. perf_mmap__consume(md);
  352. if (ret < 0)
  353. return ret;
  354. }
  355. perf_mmap__read_done(md);
  356. }
  357. return 0;
  358. }
  359. static int comp(const void *a, const void *b)
  360. {
  361. return *(int *)a - *(int *)b;
  362. }
  363. static void do_sort_something(void)
  364. {
  365. int buf[40960], i;
  366. for (i = 0; i < (int)ARRAY_SIZE(buf); i++)
  367. buf[i] = ARRAY_SIZE(buf) - i - 1;
  368. qsort(buf, ARRAY_SIZE(buf), sizeof(int), comp);
  369. for (i = 0; i < (int)ARRAY_SIZE(buf); i++) {
  370. if (buf[i] != i) {
  371. pr_debug("qsort failed\n");
  372. break;
  373. }
  374. }
  375. }
  376. static void sort_something(void)
  377. {
  378. int i;
  379. for (i = 0; i < 10; i++)
  380. do_sort_something();
  381. }
  382. static void syscall_something(void)
  383. {
  384. int pipefd[2];
  385. int i;
  386. for (i = 0; i < 1000; i++) {
  387. if (pipe(pipefd) < 0) {
  388. pr_debug("pipe failed\n");
  389. break;
  390. }
  391. close(pipefd[1]);
  392. close(pipefd[0]);
  393. }
  394. }
  395. static void fs_something(void)
  396. {
  397. const char *test_file_name = "temp-perf-code-reading-test-file--";
  398. FILE *f;
  399. int i;
  400. for (i = 0; i < 1000; i++) {
  401. f = fopen(test_file_name, "w+");
  402. if (f) {
  403. fclose(f);
  404. unlink(test_file_name);
  405. }
  406. }
  407. }
  408. static const char *do_determine_event(bool excl_kernel)
  409. {
  410. const char *event = excl_kernel ? "cycles:u" : "cycles";
  411. #ifdef __s390x__
  412. char cpuid[128], model[16], model_c[16], cpum_cf_v[16];
  413. unsigned int family;
  414. int ret, cpum_cf_a;
  415. if (get_cpuid(cpuid, sizeof(cpuid)))
  416. goto out_clocks;
  417. ret = sscanf(cpuid, "%*[^,],%u,%[^,],%[^,],%[^,],%x", &family, model_c,
  418. model, cpum_cf_v, &cpum_cf_a);
  419. if (ret != 5) /* Not available */
  420. goto out_clocks;
  421. if (excl_kernel && (cpum_cf_a & 4))
  422. return event;
  423. if (!excl_kernel && (cpum_cf_a & 2))
  424. return event;
  425. /* Fall through: missing authorization */
  426. out_clocks:
  427. event = excl_kernel ? "cpu-clock:u" : "cpu-clock";
  428. #endif
  429. return event;
  430. }
  431. static void do_something(void)
  432. {
  433. fs_something();
  434. sort_something();
  435. syscall_something();
  436. }
  437. enum {
  438. TEST_CODE_READING_OK,
  439. TEST_CODE_READING_NO_VMLINUX,
  440. TEST_CODE_READING_NO_KCORE,
  441. TEST_CODE_READING_NO_ACCESS,
  442. TEST_CODE_READING_NO_KERNEL_OBJ,
  443. };
  444. static int do_test_code_reading(bool try_kcore)
  445. {
  446. struct machine *machine;
  447. struct thread *thread;
  448. struct record_opts opts = {
  449. .mmap_pages = UINT_MAX,
  450. .user_freq = UINT_MAX,
  451. .user_interval = ULLONG_MAX,
  452. .freq = 500,
  453. .target = {
  454. .uses_mmap = true,
  455. },
  456. };
  457. struct state state = {
  458. .done_cnt = 0,
  459. };
  460. struct thread_map *threads = NULL;
  461. struct cpu_map *cpus = NULL;
  462. struct perf_evlist *evlist = NULL;
  463. struct perf_evsel *evsel = NULL;
  464. int err = -1, ret;
  465. pid_t pid;
  466. struct map *map;
  467. bool have_vmlinux, have_kcore, excl_kernel = false;
  468. pid = getpid();
  469. machine = machine__new_host();
  470. ret = machine__create_kernel_maps(machine);
  471. if (ret < 0) {
  472. pr_debug("machine__create_kernel_maps failed\n");
  473. goto out_err;
  474. }
  475. /* Force the use of kallsyms instead of vmlinux to try kcore */
  476. if (try_kcore)
  477. symbol_conf.kallsyms_name = "/proc/kallsyms";
  478. /* Load kernel map */
  479. map = machine__kernel_map(machine);
  480. ret = map__load(map);
  481. if (ret < 0) {
  482. pr_debug("map__load failed\n");
  483. goto out_err;
  484. }
  485. have_vmlinux = dso__is_vmlinux(map->dso);
  486. have_kcore = dso__is_kcore(map->dso);
  487. /* 2nd time through we just try kcore */
  488. if (try_kcore && !have_kcore)
  489. return TEST_CODE_READING_NO_KCORE;
  490. /* No point getting kernel events if there is no kernel object */
  491. if (!have_vmlinux && !have_kcore)
  492. excl_kernel = true;
  493. threads = thread_map__new_by_tid(pid);
  494. if (!threads) {
  495. pr_debug("thread_map__new_by_tid failed\n");
  496. goto out_err;
  497. }
  498. ret = perf_event__synthesize_thread_map(NULL, threads,
  499. perf_event__process, machine, false, 500);
  500. if (ret < 0) {
  501. pr_debug("perf_event__synthesize_thread_map failed\n");
  502. goto out_err;
  503. }
  504. thread = machine__findnew_thread(machine, pid, pid);
  505. if (!thread) {
  506. pr_debug("machine__findnew_thread failed\n");
  507. goto out_put;
  508. }
  509. cpus = cpu_map__new(NULL);
  510. if (!cpus) {
  511. pr_debug("cpu_map__new failed\n");
  512. goto out_put;
  513. }
  514. while (1) {
  515. const char *str;
  516. evlist = perf_evlist__new();
  517. if (!evlist) {
  518. pr_debug("perf_evlist__new failed\n");
  519. goto out_put;
  520. }
  521. perf_evlist__set_maps(evlist, cpus, threads);
  522. str = do_determine_event(excl_kernel);
  523. pr_debug("Parsing event '%s'\n", str);
  524. ret = parse_events(evlist, str, NULL);
  525. if (ret < 0) {
  526. pr_debug("parse_events failed\n");
  527. goto out_put;
  528. }
  529. perf_evlist__config(evlist, &opts, NULL);
  530. evsel = perf_evlist__first(evlist);
  531. evsel->attr.comm = 1;
  532. evsel->attr.disabled = 1;
  533. evsel->attr.enable_on_exec = 0;
  534. ret = perf_evlist__open(evlist);
  535. if (ret < 0) {
  536. if (!excl_kernel) {
  537. excl_kernel = true;
  538. /*
  539. * Both cpus and threads are now owned by evlist
  540. * and will be freed by following perf_evlist__set_maps
  541. * call. Getting refference to keep them alive.
  542. */
  543. cpu_map__get(cpus);
  544. thread_map__get(threads);
  545. perf_evlist__set_maps(evlist, NULL, NULL);
  546. perf_evlist__delete(evlist);
  547. evlist = NULL;
  548. continue;
  549. }
  550. if (verbose > 0) {
  551. char errbuf[512];
  552. perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
  553. pr_debug("perf_evlist__open() failed!\n%s\n", errbuf);
  554. }
  555. goto out_put;
  556. }
  557. break;
  558. }
  559. ret = perf_evlist__mmap(evlist, UINT_MAX);
  560. if (ret < 0) {
  561. pr_debug("perf_evlist__mmap failed\n");
  562. goto out_put;
  563. }
  564. perf_evlist__enable(evlist);
  565. do_something();
  566. perf_evlist__disable(evlist);
  567. ret = process_events(machine, evlist, &state);
  568. if (ret < 0)
  569. goto out_put;
  570. if (!have_vmlinux && !have_kcore && !try_kcore)
  571. err = TEST_CODE_READING_NO_KERNEL_OBJ;
  572. else if (!have_vmlinux && !try_kcore)
  573. err = TEST_CODE_READING_NO_VMLINUX;
  574. else if (excl_kernel)
  575. err = TEST_CODE_READING_NO_ACCESS;
  576. else
  577. err = TEST_CODE_READING_OK;
  578. out_put:
  579. thread__put(thread);
  580. out_err:
  581. if (evlist) {
  582. perf_evlist__delete(evlist);
  583. } else {
  584. cpu_map__put(cpus);
  585. thread_map__put(threads);
  586. }
  587. machine__delete_threads(machine);
  588. machine__delete(machine);
  589. return err;
  590. }
  591. int test__code_reading(struct test *test __maybe_unused, int subtest __maybe_unused)
  592. {
  593. int ret;
  594. ret = do_test_code_reading(false);
  595. if (!ret)
  596. ret = do_test_code_reading(true);
  597. switch (ret) {
  598. case TEST_CODE_READING_OK:
  599. return 0;
  600. case TEST_CODE_READING_NO_VMLINUX:
  601. pr_debug("no vmlinux\n");
  602. return 0;
  603. case TEST_CODE_READING_NO_KCORE:
  604. pr_debug("no kcore\n");
  605. return 0;
  606. case TEST_CODE_READING_NO_ACCESS:
  607. pr_debug("no access\n");
  608. return 0;
  609. case TEST_CODE_READING_NO_KERNEL_OBJ:
  610. pr_debug("no kernel obj\n");
  611. return 0;
  612. default:
  613. return -1;
  614. };
  615. }