code-reading.c 15 KB

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