code-reading.c 13 KB

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