code-reading.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. u8 cpumode;
  253. int ret;
  254. if (perf_evlist__parse_sample(evlist, event, &sample)) {
  255. pr_debug("perf_evlist__parse_sample failed\n");
  256. return -1;
  257. }
  258. thread = machine__findnew_thread(machine, sample.pid, sample.tid);
  259. if (!thread) {
  260. pr_debug("machine__findnew_thread failed\n");
  261. return -1;
  262. }
  263. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  264. ret = read_object_code(sample.ip, READLEN, cpumode, thread, state);
  265. thread__put(thread);
  266. return ret;
  267. }
  268. static int process_event(struct machine *machine, struct perf_evlist *evlist,
  269. union perf_event *event, struct state *state)
  270. {
  271. if (event->header.type == PERF_RECORD_SAMPLE)
  272. return process_sample_event(machine, evlist, event, state);
  273. if (event->header.type == PERF_RECORD_THROTTLE ||
  274. event->header.type == PERF_RECORD_UNTHROTTLE)
  275. return 0;
  276. if (event->header.type < PERF_RECORD_MAX) {
  277. int ret;
  278. ret = machine__process_event(machine, event, NULL);
  279. if (ret < 0)
  280. pr_debug("machine__process_event failed, event type %u\n",
  281. event->header.type);
  282. return ret;
  283. }
  284. return 0;
  285. }
  286. static int process_events(struct machine *machine, struct perf_evlist *evlist,
  287. struct state *state)
  288. {
  289. union perf_event *event;
  290. int i, ret;
  291. for (i = 0; i < evlist->nr_mmaps; i++) {
  292. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  293. ret = process_event(machine, evlist, event, state);
  294. perf_evlist__mmap_consume(evlist, i);
  295. if (ret < 0)
  296. return ret;
  297. }
  298. }
  299. return 0;
  300. }
  301. static int comp(const void *a, const void *b)
  302. {
  303. return *(int *)a - *(int *)b;
  304. }
  305. static void do_sort_something(void)
  306. {
  307. int buf[40960], i;
  308. for (i = 0; i < (int)ARRAY_SIZE(buf); i++)
  309. buf[i] = ARRAY_SIZE(buf) - i - 1;
  310. qsort(buf, ARRAY_SIZE(buf), sizeof(int), comp);
  311. for (i = 0; i < (int)ARRAY_SIZE(buf); i++) {
  312. if (buf[i] != i) {
  313. pr_debug("qsort failed\n");
  314. break;
  315. }
  316. }
  317. }
  318. static void sort_something(void)
  319. {
  320. int i;
  321. for (i = 0; i < 10; i++)
  322. do_sort_something();
  323. }
  324. static void syscall_something(void)
  325. {
  326. int pipefd[2];
  327. int i;
  328. for (i = 0; i < 1000; i++) {
  329. if (pipe(pipefd) < 0) {
  330. pr_debug("pipe failed\n");
  331. break;
  332. }
  333. close(pipefd[1]);
  334. close(pipefd[0]);
  335. }
  336. }
  337. static void fs_something(void)
  338. {
  339. const char *test_file_name = "temp-perf-code-reading-test-file--";
  340. FILE *f;
  341. int i;
  342. for (i = 0; i < 1000; i++) {
  343. f = fopen(test_file_name, "w+");
  344. if (f) {
  345. fclose(f);
  346. unlink(test_file_name);
  347. }
  348. }
  349. }
  350. static void do_something(void)
  351. {
  352. fs_something();
  353. sort_something();
  354. syscall_something();
  355. }
  356. enum {
  357. TEST_CODE_READING_OK,
  358. TEST_CODE_READING_NO_VMLINUX,
  359. TEST_CODE_READING_NO_KCORE,
  360. TEST_CODE_READING_NO_ACCESS,
  361. TEST_CODE_READING_NO_KERNEL_OBJ,
  362. };
  363. static int do_test_code_reading(bool try_kcore)
  364. {
  365. struct machines machines;
  366. struct machine *machine;
  367. struct thread *thread;
  368. struct record_opts opts = {
  369. .mmap_pages = UINT_MAX,
  370. .user_freq = UINT_MAX,
  371. .user_interval = ULLONG_MAX,
  372. .freq = 4000,
  373. .target = {
  374. .uses_mmap = true,
  375. },
  376. };
  377. struct state state = {
  378. .done_cnt = 0,
  379. };
  380. struct thread_map *threads = NULL;
  381. struct cpu_map *cpus = NULL;
  382. struct perf_evlist *evlist = NULL;
  383. struct perf_evsel *evsel = NULL;
  384. int err = -1, ret;
  385. pid_t pid;
  386. struct map *map;
  387. bool have_vmlinux, have_kcore, excl_kernel = false;
  388. pid = getpid();
  389. machines__init(&machines);
  390. machine = &machines.host;
  391. ret = machine__create_kernel_maps(machine);
  392. if (ret < 0) {
  393. pr_debug("machine__create_kernel_maps failed\n");
  394. goto out_err;
  395. }
  396. /* Force the use of kallsyms instead of vmlinux to try kcore */
  397. if (try_kcore)
  398. symbol_conf.kallsyms_name = "/proc/kallsyms";
  399. /* Load kernel map */
  400. map = machine__kernel_map(machine);
  401. ret = map__load(map, NULL);
  402. if (ret < 0) {
  403. pr_debug("map__load failed\n");
  404. goto out_err;
  405. }
  406. have_vmlinux = dso__is_vmlinux(map->dso);
  407. have_kcore = dso__is_kcore(map->dso);
  408. /* 2nd time through we just try kcore */
  409. if (try_kcore && !have_kcore)
  410. return TEST_CODE_READING_NO_KCORE;
  411. /* No point getting kernel events if there is no kernel object */
  412. if (!have_vmlinux && !have_kcore)
  413. excl_kernel = true;
  414. threads = thread_map__new_by_tid(pid);
  415. if (!threads) {
  416. pr_debug("thread_map__new_by_tid failed\n");
  417. goto out_err;
  418. }
  419. ret = perf_event__synthesize_thread_map(NULL, threads,
  420. perf_event__process, machine, false, 500);
  421. if (ret < 0) {
  422. pr_debug("perf_event__synthesize_thread_map failed\n");
  423. goto out_err;
  424. }
  425. thread = machine__findnew_thread(machine, pid, pid);
  426. if (!thread) {
  427. pr_debug("machine__findnew_thread failed\n");
  428. goto out_put;
  429. }
  430. cpus = cpu_map__new(NULL);
  431. if (!cpus) {
  432. pr_debug("cpu_map__new failed\n");
  433. goto out_put;
  434. }
  435. while (1) {
  436. const char *str;
  437. evlist = perf_evlist__new();
  438. if (!evlist) {
  439. pr_debug("perf_evlist__new failed\n");
  440. goto out_put;
  441. }
  442. perf_evlist__set_maps(evlist, cpus, threads);
  443. if (excl_kernel)
  444. str = "cycles:u";
  445. else
  446. str = "cycles";
  447. pr_debug("Parsing event '%s'\n", str);
  448. ret = parse_events(evlist, str, NULL);
  449. if (ret < 0) {
  450. pr_debug("parse_events failed\n");
  451. goto out_put;
  452. }
  453. perf_evlist__config(evlist, &opts);
  454. evsel = perf_evlist__first(evlist);
  455. evsel->attr.comm = 1;
  456. evsel->attr.disabled = 1;
  457. evsel->attr.enable_on_exec = 0;
  458. ret = perf_evlist__open(evlist);
  459. if (ret < 0) {
  460. if (!excl_kernel) {
  461. excl_kernel = true;
  462. perf_evlist__set_maps(evlist, NULL, NULL);
  463. perf_evlist__delete(evlist);
  464. evlist = NULL;
  465. continue;
  466. }
  467. pr_debug("perf_evlist__open failed\n");
  468. goto out_put;
  469. }
  470. break;
  471. }
  472. ret = perf_evlist__mmap(evlist, UINT_MAX, false);
  473. if (ret < 0) {
  474. pr_debug("perf_evlist__mmap failed\n");
  475. goto out_put;
  476. }
  477. perf_evlist__enable(evlist);
  478. do_something();
  479. perf_evlist__disable(evlist);
  480. ret = process_events(machine, evlist, &state);
  481. if (ret < 0)
  482. goto out_put;
  483. if (!have_vmlinux && !have_kcore && !try_kcore)
  484. err = TEST_CODE_READING_NO_KERNEL_OBJ;
  485. else if (!have_vmlinux && !try_kcore)
  486. err = TEST_CODE_READING_NO_VMLINUX;
  487. else if (excl_kernel)
  488. err = TEST_CODE_READING_NO_ACCESS;
  489. else
  490. err = TEST_CODE_READING_OK;
  491. out_put:
  492. thread__put(thread);
  493. out_err:
  494. if (evlist) {
  495. perf_evlist__delete(evlist);
  496. } else {
  497. cpu_map__put(cpus);
  498. thread_map__put(threads);
  499. }
  500. machines__destroy_kernel_maps(&machines);
  501. machine__delete_threads(machine);
  502. machines__exit(&machines);
  503. return err;
  504. }
  505. int test__code_reading(void)
  506. {
  507. int ret;
  508. ret = do_test_code_reading(false);
  509. if (!ret)
  510. ret = do_test_code_reading(true);
  511. switch (ret) {
  512. case TEST_CODE_READING_OK:
  513. return 0;
  514. case TEST_CODE_READING_NO_VMLINUX:
  515. pr_debug("no vmlinux\n");
  516. return 0;
  517. case TEST_CODE_READING_NO_KCORE:
  518. pr_debug("no kcore\n");
  519. return 0;
  520. case TEST_CODE_READING_NO_ACCESS:
  521. pr_debug("no access\n");
  522. return 0;
  523. case TEST_CODE_READING_NO_KERNEL_OBJ:
  524. pr_debug("no kernel obj\n");
  525. return 0;
  526. default:
  527. return -1;
  528. };
  529. }