code-reading.c 12 KB

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