code-reading.c 15 KB

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