code-reading.c 14 KB

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