code-reading.c 14 KB

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