code-reading.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 int read_object_code(u64 addr, size_t len, u8 cpumode,
  139. struct thread *thread, struct state *state)
  140. {
  141. struct addr_location al;
  142. unsigned char buf1[BUFSZ];
  143. unsigned char buf2[BUFSZ];
  144. size_t ret_len;
  145. u64 objdump_addr;
  146. int ret;
  147. pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr);
  148. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
  149. if (!al.map || !al.map->dso) {
  150. pr_debug("thread__find_addr_map failed\n");
  151. return -1;
  152. }
  153. pr_debug("File is: %s\n", al.map->dso->long_name);
  154. if (al.map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  155. !dso__is_kcore(al.map->dso)) {
  156. pr_debug("Unexpected kernel address - skipping\n");
  157. return 0;
  158. }
  159. pr_debug("On file address is: %#"PRIx64"\n", al.addr);
  160. if (len > BUFSZ)
  161. len = BUFSZ;
  162. /* Do not go off the map */
  163. if (addr + len > al.map->end)
  164. len = al.map->end - addr;
  165. /* Read the object code using perf */
  166. ret_len = dso__data_read_offset(al.map->dso, thread->mg->machine,
  167. al.addr, buf1, len);
  168. if (ret_len != len) {
  169. pr_debug("dso__data_read_offset failed\n");
  170. return -1;
  171. }
  172. /*
  173. * Converting addresses for use by objdump requires more information.
  174. * map__load() does that. See map__rip_2objdump() for details.
  175. */
  176. if (map__load(al.map, NULL))
  177. return -1;
  178. /* objdump struggles with kcore - try each map only once */
  179. if (dso__is_kcore(al.map->dso)) {
  180. size_t d;
  181. for (d = 0; d < state->done_cnt; d++) {
  182. if (state->done[d] == al.map->start) {
  183. pr_debug("kcore map tested already");
  184. pr_debug(" - skipping\n");
  185. return 0;
  186. }
  187. }
  188. if (state->done_cnt >= ARRAY_SIZE(state->done)) {
  189. pr_debug("Too many kcore maps - skipping\n");
  190. return 0;
  191. }
  192. state->done[state->done_cnt++] = al.map->start;
  193. }
  194. /* Read the object code using objdump */
  195. objdump_addr = map__rip_2objdump(al.map, al.addr);
  196. ret = read_via_objdump(al.map->dso->long_name, objdump_addr, buf2, len);
  197. if (ret > 0) {
  198. /*
  199. * The kernel maps are inaccurate - assume objdump is right in
  200. * that case.
  201. */
  202. if (cpumode == PERF_RECORD_MISC_KERNEL ||
  203. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) {
  204. len -= ret;
  205. if (len) {
  206. pr_debug("Reducing len to %zu\n", len);
  207. } else if (dso__is_kcore(al.map->dso)) {
  208. /*
  209. * objdump cannot handle very large segments
  210. * that may be found in kcore.
  211. */
  212. pr_debug("objdump failed for kcore");
  213. pr_debug(" - skipping\n");
  214. return 0;
  215. } else {
  216. return -1;
  217. }
  218. }
  219. }
  220. if (ret < 0) {
  221. pr_debug("read_via_objdump failed\n");
  222. return -1;
  223. }
  224. /* The results should be identical */
  225. if (memcmp(buf1, buf2, len)) {
  226. pr_debug("Bytes read differ from those read by objdump\n");
  227. return -1;
  228. }
  229. pr_debug("Bytes read match those read by objdump\n");
  230. return 0;
  231. }
  232. static int process_sample_event(struct machine *machine,
  233. struct perf_evlist *evlist,
  234. union perf_event *event, struct state *state)
  235. {
  236. struct perf_sample sample;
  237. struct thread *thread;
  238. u8 cpumode;
  239. int ret;
  240. if (perf_evlist__parse_sample(evlist, event, &sample)) {
  241. pr_debug("perf_evlist__parse_sample failed\n");
  242. return -1;
  243. }
  244. thread = machine__findnew_thread(machine, sample.pid, sample.tid);
  245. if (!thread) {
  246. pr_debug("machine__findnew_thread failed\n");
  247. return -1;
  248. }
  249. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  250. ret = read_object_code(sample.ip, READLEN, cpumode, thread, state);
  251. thread__put(thread);
  252. return ret;
  253. }
  254. static int process_event(struct machine *machine, struct perf_evlist *evlist,
  255. union perf_event *event, struct state *state)
  256. {
  257. if (event->header.type == PERF_RECORD_SAMPLE)
  258. return process_sample_event(machine, evlist, event, state);
  259. if (event->header.type == PERF_RECORD_THROTTLE ||
  260. event->header.type == PERF_RECORD_UNTHROTTLE)
  261. return 0;
  262. if (event->header.type < PERF_RECORD_MAX) {
  263. int ret;
  264. ret = machine__process_event(machine, event, NULL);
  265. if (ret < 0)
  266. pr_debug("machine__process_event failed, event type %u\n",
  267. event->header.type);
  268. return ret;
  269. }
  270. return 0;
  271. }
  272. static int process_events(struct machine *machine, struct perf_evlist *evlist,
  273. struct state *state)
  274. {
  275. union perf_event *event;
  276. int i, ret;
  277. for (i = 0; i < evlist->nr_mmaps; i++) {
  278. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  279. ret = process_event(machine, evlist, event, state);
  280. perf_evlist__mmap_consume(evlist, i);
  281. if (ret < 0)
  282. return ret;
  283. }
  284. }
  285. return 0;
  286. }
  287. static int comp(const void *a, const void *b)
  288. {
  289. return *(int *)a - *(int *)b;
  290. }
  291. static void do_sort_something(void)
  292. {
  293. int buf[40960], i;
  294. for (i = 0; i < (int)ARRAY_SIZE(buf); i++)
  295. buf[i] = ARRAY_SIZE(buf) - i - 1;
  296. qsort(buf, ARRAY_SIZE(buf), sizeof(int), comp);
  297. for (i = 0; i < (int)ARRAY_SIZE(buf); i++) {
  298. if (buf[i] != i) {
  299. pr_debug("qsort failed\n");
  300. break;
  301. }
  302. }
  303. }
  304. static void sort_something(void)
  305. {
  306. int i;
  307. for (i = 0; i < 10; i++)
  308. do_sort_something();
  309. }
  310. static void syscall_something(void)
  311. {
  312. int pipefd[2];
  313. int i;
  314. for (i = 0; i < 1000; i++) {
  315. if (pipe(pipefd) < 0) {
  316. pr_debug("pipe failed\n");
  317. break;
  318. }
  319. close(pipefd[1]);
  320. close(pipefd[0]);
  321. }
  322. }
  323. static void fs_something(void)
  324. {
  325. const char *test_file_name = "temp-perf-code-reading-test-file--";
  326. FILE *f;
  327. int i;
  328. for (i = 0; i < 1000; i++) {
  329. f = fopen(test_file_name, "w+");
  330. if (f) {
  331. fclose(f);
  332. unlink(test_file_name);
  333. }
  334. }
  335. }
  336. static void do_something(void)
  337. {
  338. fs_something();
  339. sort_something();
  340. syscall_something();
  341. }
  342. enum {
  343. TEST_CODE_READING_OK,
  344. TEST_CODE_READING_NO_VMLINUX,
  345. TEST_CODE_READING_NO_KCORE,
  346. TEST_CODE_READING_NO_ACCESS,
  347. TEST_CODE_READING_NO_KERNEL_OBJ,
  348. };
  349. static int do_test_code_reading(bool try_kcore)
  350. {
  351. struct machines machines;
  352. struct machine *machine;
  353. struct thread *thread;
  354. struct record_opts opts = {
  355. .mmap_pages = UINT_MAX,
  356. .user_freq = UINT_MAX,
  357. .user_interval = ULLONG_MAX,
  358. .freq = 4000,
  359. .target = {
  360. .uses_mmap = true,
  361. },
  362. };
  363. struct state state = {
  364. .done_cnt = 0,
  365. };
  366. struct thread_map *threads = NULL;
  367. struct cpu_map *cpus = NULL;
  368. struct perf_evlist *evlist = NULL;
  369. struct perf_evsel *evsel = NULL;
  370. int err = -1, ret;
  371. pid_t pid;
  372. struct map *map;
  373. bool have_vmlinux, have_kcore, excl_kernel = false;
  374. pid = getpid();
  375. machines__init(&machines);
  376. machine = &machines.host;
  377. ret = machine__create_kernel_maps(machine);
  378. if (ret < 0) {
  379. pr_debug("machine__create_kernel_maps failed\n");
  380. goto out_err;
  381. }
  382. /* Force the use of kallsyms instead of vmlinux to try kcore */
  383. if (try_kcore)
  384. symbol_conf.kallsyms_name = "/proc/kallsyms";
  385. /* Load kernel map */
  386. map = machine->vmlinux_maps[MAP__FUNCTION];
  387. ret = map__load(map, NULL);
  388. if (ret < 0) {
  389. pr_debug("map__load failed\n");
  390. goto out_err;
  391. }
  392. have_vmlinux = dso__is_vmlinux(map->dso);
  393. have_kcore = dso__is_kcore(map->dso);
  394. /* 2nd time through we just try kcore */
  395. if (try_kcore && !have_kcore)
  396. return TEST_CODE_READING_NO_KCORE;
  397. /* No point getting kernel events if there is no kernel object */
  398. if (!have_vmlinux && !have_kcore)
  399. excl_kernel = true;
  400. threads = thread_map__new_by_tid(pid);
  401. if (!threads) {
  402. pr_debug("thread_map__new_by_tid failed\n");
  403. goto out_err;
  404. }
  405. ret = perf_event__synthesize_thread_map(NULL, threads,
  406. perf_event__process, machine, false, 500);
  407. if (ret < 0) {
  408. pr_debug("perf_event__synthesize_thread_map failed\n");
  409. goto out_err;
  410. }
  411. thread = machine__findnew_thread(machine, pid, pid);
  412. if (!thread) {
  413. pr_debug("machine__findnew_thread failed\n");
  414. goto out_put;
  415. }
  416. cpus = cpu_map__new(NULL);
  417. if (!cpus) {
  418. pr_debug("cpu_map__new failed\n");
  419. goto out_put;
  420. }
  421. while (1) {
  422. const char *str;
  423. evlist = perf_evlist__new();
  424. if (!evlist) {
  425. pr_debug("perf_evlist__new failed\n");
  426. goto out_put;
  427. }
  428. perf_evlist__set_maps(evlist, cpus, threads);
  429. if (excl_kernel)
  430. str = "cycles:u";
  431. else
  432. str = "cycles";
  433. pr_debug("Parsing event '%s'\n", str);
  434. ret = parse_events(evlist, str, NULL);
  435. if (ret < 0) {
  436. pr_debug("parse_events failed\n");
  437. goto out_put;
  438. }
  439. perf_evlist__config(evlist, &opts);
  440. evsel = perf_evlist__first(evlist);
  441. evsel->attr.comm = 1;
  442. evsel->attr.disabled = 1;
  443. evsel->attr.enable_on_exec = 0;
  444. ret = perf_evlist__open(evlist);
  445. if (ret < 0) {
  446. if (!excl_kernel) {
  447. excl_kernel = true;
  448. perf_evlist__set_maps(evlist, NULL, NULL);
  449. perf_evlist__delete(evlist);
  450. evlist = NULL;
  451. continue;
  452. }
  453. pr_debug("perf_evlist__open failed\n");
  454. goto out_put;
  455. }
  456. break;
  457. }
  458. ret = perf_evlist__mmap(evlist, UINT_MAX, false);
  459. if (ret < 0) {
  460. pr_debug("perf_evlist__mmap failed\n");
  461. goto out_put;
  462. }
  463. perf_evlist__enable(evlist);
  464. do_something();
  465. perf_evlist__disable(evlist);
  466. ret = process_events(machine, evlist, &state);
  467. if (ret < 0)
  468. goto out_put;
  469. if (!have_vmlinux && !have_kcore && !try_kcore)
  470. err = TEST_CODE_READING_NO_KERNEL_OBJ;
  471. else if (!have_vmlinux && !try_kcore)
  472. err = TEST_CODE_READING_NO_VMLINUX;
  473. else if (excl_kernel)
  474. err = TEST_CODE_READING_NO_ACCESS;
  475. else
  476. err = TEST_CODE_READING_OK;
  477. out_put:
  478. thread__put(thread);
  479. out_err:
  480. if (evlist) {
  481. perf_evlist__delete(evlist);
  482. } else {
  483. cpu_map__put(cpus);
  484. thread_map__put(threads);
  485. }
  486. machines__destroy_kernel_maps(&machines);
  487. machine__delete_threads(machine);
  488. machines__exit(&machines);
  489. return err;
  490. }
  491. int test__code_reading(void)
  492. {
  493. int ret;
  494. ret = do_test_code_reading(false);
  495. if (!ret)
  496. ret = do_test_code_reading(true);
  497. switch (ret) {
  498. case TEST_CODE_READING_OK:
  499. return 0;
  500. case TEST_CODE_READING_NO_VMLINUX:
  501. fprintf(stderr, " (no vmlinux)");
  502. return 0;
  503. case TEST_CODE_READING_NO_KCORE:
  504. fprintf(stderr, " (no kcore)");
  505. return 0;
  506. case TEST_CODE_READING_NO_ACCESS:
  507. fprintf(stderr, " (no access)");
  508. return 0;
  509. case TEST_CODE_READING_NO_KERNEL_OBJ:
  510. fprintf(stderr, " (no kernel obj)");
  511. return 0;
  512. default:
  513. return -1;
  514. };
  515. }