code-reading.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <linux/kconfig.h>
  4. #include <linux/kernel.h>
  5. #include <linux/rbtree.h>
  6. #include <linux/types.h>
  7. #include <inttypes.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <sys/param.h>
  13. #include <sys/utsname.h>
  14. #include <perf/cpumap.h>
  15. #include <perf/evlist.h>
  16. #include <perf/mmap.h>
  17. #include "debug.h"
  18. #include "dso.h"
  19. #include "env.h"
  20. #include "parse-events.h"
  21. #include "evlist.h"
  22. #include "evsel.h"
  23. #include "thread_map.h"
  24. #include "machine.h"
  25. #include "map.h"
  26. #include "symbol.h"
  27. #include "event.h"
  28. #include "record.h"
  29. #include "util/mmap.h"
  30. #include "util/string2.h"
  31. #include "util/synthetic-events.h"
  32. #include "util/util.h"
  33. #include "thread.h"
  34. #include "tests.h"
  35. #include <linux/ctype.h>
  36. #define BUFSZ 1024
  37. #define READLEN 128
  38. struct tested_section {
  39. struct rb_node rb_node;
  40. u64 addr;
  41. char *path;
  42. };
  43. static bool tested_code_insert_or_exists(const char *path, u64 addr,
  44. struct rb_root *tested_sections)
  45. {
  46. struct rb_node **node = &tested_sections->rb_node;
  47. struct rb_node *parent = NULL;
  48. struct tested_section *data;
  49. while (*node) {
  50. int cmp;
  51. parent = *node;
  52. data = rb_entry(*node, struct tested_section, rb_node);
  53. cmp = strcmp(path, data->path);
  54. if (!cmp) {
  55. if (addr < data->addr)
  56. cmp = -1;
  57. else if (addr > data->addr)
  58. cmp = 1;
  59. else
  60. return true; /* already tested */
  61. }
  62. if (cmp < 0)
  63. node = &(*node)->rb_left;
  64. else
  65. node = &(*node)->rb_right;
  66. }
  67. data = zalloc(sizeof(*data));
  68. if (!data)
  69. return true;
  70. data->addr = addr;
  71. data->path = strdup(path);
  72. if (!data->path) {
  73. free(data);
  74. return true;
  75. }
  76. rb_link_node(&data->rb_node, parent, node);
  77. rb_insert_color(&data->rb_node, tested_sections);
  78. return false;
  79. }
  80. static void tested_sections__free(struct rb_root *root)
  81. {
  82. while (!RB_EMPTY_ROOT(root)) {
  83. struct rb_node *node = rb_first(root);
  84. struct tested_section *ts = rb_entry(node,
  85. struct tested_section,
  86. rb_node);
  87. rb_erase(node, root);
  88. free(ts->path);
  89. free(ts);
  90. }
  91. }
  92. static size_t read_objdump_chunk(const char **line, unsigned char **buf,
  93. size_t *buf_len)
  94. {
  95. size_t bytes_read = 0;
  96. unsigned char *chunk_start = *buf;
  97. /* Read bytes */
  98. while (*buf_len > 0) {
  99. char c1, c2;
  100. /* Get 2 hex digits */
  101. c1 = *(*line)++;
  102. if (!isxdigit(c1))
  103. break;
  104. c2 = *(*line)++;
  105. if (!isxdigit(c2))
  106. break;
  107. /* Store byte and advance buf */
  108. **buf = (hex(c1) << 4) | hex(c2);
  109. (*buf)++;
  110. (*buf_len)--;
  111. bytes_read++;
  112. /* End of chunk? */
  113. if (isspace(**line))
  114. break;
  115. }
  116. /*
  117. * objdump will display raw insn as LE if code endian
  118. * is LE and bytes_per_chunk > 1. In that case reverse
  119. * the chunk we just read.
  120. *
  121. * see disassemble_bytes() at binutils/objdump.c for details
  122. * how objdump chooses display endian)
  123. */
  124. if (bytes_read > 1 && !host_is_bigendian()) {
  125. unsigned char *chunk_end = chunk_start + bytes_read - 1;
  126. unsigned char tmp;
  127. while (chunk_start < chunk_end) {
  128. tmp = *chunk_start;
  129. *chunk_start = *chunk_end;
  130. *chunk_end = tmp;
  131. chunk_start++;
  132. chunk_end--;
  133. }
  134. }
  135. return bytes_read;
  136. }
  137. static size_t read_objdump_line(const char *line, unsigned char *buf,
  138. size_t buf_len)
  139. {
  140. const char *p;
  141. size_t ret, bytes_read = 0;
  142. /* Skip to a colon */
  143. p = strchr(line, ':');
  144. if (!p)
  145. return 0;
  146. p++;
  147. /* Skip initial spaces */
  148. while (*p) {
  149. if (!isspace(*p))
  150. break;
  151. p++;
  152. }
  153. do {
  154. ret = read_objdump_chunk(&p, &buf, &buf_len);
  155. bytes_read += ret;
  156. p++;
  157. } while (ret > 0);
  158. /* return number of successfully read bytes */
  159. return bytes_read;
  160. }
  161. static int read_objdump_output(FILE *f, void *buf, size_t *len, u64 start_addr)
  162. {
  163. char *line = NULL;
  164. size_t line_len, off_last = 0;
  165. ssize_t ret;
  166. int err = 0;
  167. u64 addr, last_addr = start_addr;
  168. while (off_last < *len) {
  169. size_t off, read_bytes, written_bytes;
  170. unsigned char tmp[BUFSZ];
  171. ret = getline(&line, &line_len, f);
  172. if (feof(f))
  173. break;
  174. if (ret < 0) {
  175. pr_debug("getline failed\n");
  176. err = -1;
  177. break;
  178. }
  179. /* read objdump data into temporary buffer */
  180. read_bytes = read_objdump_line(line, tmp, sizeof(tmp));
  181. if (!read_bytes)
  182. continue;
  183. if (sscanf(line, "%"PRIx64, &addr) != 1)
  184. continue;
  185. if (addr < last_addr) {
  186. pr_debug("addr going backwards, read beyond section?\n");
  187. break;
  188. }
  189. last_addr = addr;
  190. /* copy it from temporary buffer to 'buf' according
  191. * to address on current objdump line */
  192. off = addr - start_addr;
  193. if (off >= *len)
  194. break;
  195. written_bytes = MIN(read_bytes, *len - off);
  196. memcpy(buf + off, tmp, written_bytes);
  197. off_last = off + written_bytes;
  198. }
  199. /* len returns number of bytes that could not be read */
  200. *len -= off_last;
  201. free(line);
  202. return err;
  203. }
  204. /*
  205. * Only gets GNU objdump version. Returns 0 for llvm-objdump.
  206. */
  207. static int objdump_version(void)
  208. {
  209. size_t line_len;
  210. char cmd[PATH_MAX * 2];
  211. char *line = NULL;
  212. const char *fmt;
  213. FILE *f;
  214. int ret;
  215. int version_tmp, version_num = 0;
  216. char *version = 0, *token;
  217. fmt = "%s --version";
  218. ret = snprintf(cmd, sizeof(cmd), fmt, test_objdump_path);
  219. if (ret <= 0 || (size_t)ret >= sizeof(cmd))
  220. return -1;
  221. /* Ignore objdump errors */
  222. strcat(cmd, " 2>/dev/null");
  223. f = popen(cmd, "r");
  224. if (!f) {
  225. pr_debug("popen failed\n");
  226. return -1;
  227. }
  228. /* Get first line of objdump --version output */
  229. ret = getline(&line, &line_len, f);
  230. pclose(f);
  231. if (ret < 0) {
  232. pr_debug("getline failed\n");
  233. return -1;
  234. }
  235. token = strsep(&line, " ");
  236. if (token != NULL && !strcmp(token, "GNU")) {
  237. // version is last part of first line of objdump --version output.
  238. while ((token = strsep(&line, " ")))
  239. version = token;
  240. // Convert version into a format we can compare with
  241. token = strsep(&version, ".");
  242. version_num = atoi(token);
  243. if (version_num)
  244. version_num *= 10000;
  245. token = strsep(&version, ".");
  246. version_tmp = atoi(token);
  247. if (token)
  248. version_num += version_tmp * 100;
  249. token = strsep(&version, ".");
  250. version_tmp = atoi(token);
  251. if (token)
  252. version_num += version_tmp;
  253. }
  254. return version_num;
  255. }
  256. static int read_via_objdump(const char *filename, u64 addr, void *buf,
  257. size_t len)
  258. {
  259. u64 stop_address = addr + len;
  260. struct utsname uname_buf;
  261. char cmd[PATH_MAX * 2];
  262. const char *fmt;
  263. FILE *f;
  264. int ret;
  265. ret = uname(&uname_buf);
  266. if (ret) {
  267. pr_debug("uname failed\n");
  268. return -1;
  269. }
  270. if (!strncmp(uname_buf.machine, "riscv", 5)) {
  271. int version = objdump_version();
  272. /* Default to this workaround if version parsing fails */
  273. if (version < 0 || version > 24100) {
  274. /*
  275. * Starting at riscv objdump version 2.41, dumping in
  276. * the middle of an instruction is not supported. riscv
  277. * instructions are aligned along 2-byte intervals and
  278. * can be either 2-bytes or 4-bytes. This makes it
  279. * possible that the stop-address lands in the middle of
  280. * a 4-byte instruction. Increase the stop_address by
  281. * two to ensure an instruction is not cut in half, but
  282. * leave the len as-is so only the expected number of
  283. * bytes are collected.
  284. */
  285. stop_address += 2;
  286. }
  287. }
  288. fmt = "%s -z -d --start-address=0x%"PRIx64" --stop-address=0x%"PRIx64" %s";
  289. ret = snprintf(cmd, sizeof(cmd), fmt, test_objdump_path, addr, stop_address,
  290. filename);
  291. if (ret <= 0 || (size_t)ret >= sizeof(cmd))
  292. return -1;
  293. pr_debug("Objdump command is: %s\n", cmd);
  294. /* Ignore objdump errors */
  295. strcat(cmd, " 2>/dev/null");
  296. f = popen(cmd, "r");
  297. if (!f) {
  298. pr_debug("popen failed\n");
  299. return -1;
  300. }
  301. ret = read_objdump_output(f, buf, &len, addr);
  302. if (len) {
  303. pr_debug("objdump read too few bytes: %zd\n", len);
  304. if (!ret)
  305. ret = len;
  306. }
  307. pclose(f);
  308. return ret;
  309. }
  310. static void dump_buf(unsigned char *buf, size_t len)
  311. {
  312. size_t i;
  313. for (i = 0; i < len; i++) {
  314. pr_debug("0x%02x ", buf[i]);
  315. if (i % 16 == 15)
  316. pr_debug("\n");
  317. }
  318. pr_debug("\n");
  319. }
  320. static int read_object_code(u64 addr, size_t len, u8 cpumode,
  321. struct thread *thread,
  322. struct rb_root *tested_sections)
  323. {
  324. struct addr_location al;
  325. unsigned char buf1[BUFSZ] = {0};
  326. unsigned char buf2[BUFSZ] = {0};
  327. size_t ret_len;
  328. u64 objdump_addr;
  329. u64 skip_addr;
  330. const char *objdump_name;
  331. char decomp_name[KMOD_DECOMP_LEN];
  332. bool decomp = false;
  333. int ret, err = 0;
  334. struct dso *dso;
  335. pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr);
  336. addr_location__init(&al);
  337. if (!thread__find_map(thread, cpumode, addr, &al) || !map__dso(al.map)) {
  338. if (cpumode == PERF_RECORD_MISC_HYPERVISOR) {
  339. pr_debug("Hypervisor address can not be resolved - skipping\n");
  340. goto out;
  341. }
  342. pr_debug("thread__find_map failed\n");
  343. err = -1;
  344. goto out;
  345. }
  346. dso = map__dso(al.map);
  347. pr_debug("File is: %s\n", dso__long_name(dso));
  348. if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS && !dso__is_kcore(dso)) {
  349. pr_debug("Unexpected kernel address - skipping\n");
  350. goto out;
  351. }
  352. /*
  353. * Don't retest the same addresses. objdump struggles with kcore - try
  354. * each map only once even if the address is different.
  355. */
  356. skip_addr = dso__is_kcore(dso) ? map__start(al.map) : al.addr;
  357. if (tested_code_insert_or_exists(dso__long_name(dso), skip_addr,
  358. tested_sections)) {
  359. pr_debug("Already tested %s @ %#"PRIx64" - skipping\n",
  360. dso__long_name(dso), skip_addr);
  361. goto out;
  362. }
  363. pr_debug("On file address is: %#"PRIx64"\n", al.addr);
  364. if (len > BUFSZ)
  365. len = BUFSZ;
  366. /* Do not go off the map */
  367. if (addr + len > map__end(al.map))
  368. len = map__end(al.map) - addr;
  369. /*
  370. * Some architectures (ex: powerpc) have stubs (trampolines) in kernel
  371. * modules to manage long jumps. Check if the ip offset falls in stubs
  372. * sections for kernel modules. And skip module address after text end
  373. */
  374. if (dso__is_kmod(dso) && al.addr > dso__text_end(dso)) {
  375. pr_debug("skipping the module address %#"PRIx64" after text end\n", al.addr);
  376. goto out;
  377. }
  378. /* Read the object code using perf */
  379. ret_len = dso__data_read_offset(dso, maps__machine(thread__maps(thread)),
  380. al.addr, buf1, len);
  381. if (ret_len != len) {
  382. pr_debug("dso__data_read_offset failed\n");
  383. err = -1;
  384. goto out;
  385. }
  386. /*
  387. * Converting addresses for use by objdump requires more information.
  388. * map__load() does that. See map__rip_2objdump() for details.
  389. */
  390. if (map__load(al.map)) {
  391. err = -1;
  392. goto out;
  393. }
  394. objdump_name = dso__long_name(dso);
  395. if (dso__needs_decompress(dso)) {
  396. if (dso__decompress_kmodule_path(dso, objdump_name,
  397. decomp_name,
  398. sizeof(decomp_name)) < 0) {
  399. pr_debug("decompression failed\n");
  400. err = -1;
  401. goto out;
  402. }
  403. decomp = true;
  404. objdump_name = decomp_name;
  405. }
  406. /* Read the object code using objdump */
  407. objdump_addr = map__rip_2objdump(al.map, al.addr);
  408. ret = read_via_objdump(objdump_name, objdump_addr, buf2, len);
  409. if (decomp)
  410. unlink(objdump_name);
  411. if (ret > 0) {
  412. /*
  413. * The kernel maps are inaccurate - assume objdump is right in
  414. * that case.
  415. */
  416. if (cpumode == PERF_RECORD_MISC_KERNEL ||
  417. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) {
  418. len -= ret;
  419. if (len) {
  420. pr_debug("Reducing len to %zu\n", len);
  421. } else if (dso__is_kcore(dso)) {
  422. /*
  423. * objdump cannot handle very large segments
  424. * that may be found in kcore.
  425. */
  426. pr_debug("objdump failed for kcore");
  427. pr_debug(" - skipping\n");
  428. } else {
  429. err = -1;
  430. }
  431. goto out;
  432. }
  433. }
  434. if (ret < 0) {
  435. pr_debug("read_via_objdump failed\n");
  436. err = -1;
  437. goto out;
  438. }
  439. /* The results should be identical */
  440. if (memcmp(buf1, buf2, len)) {
  441. pr_debug("Bytes read differ from those read by objdump\n");
  442. pr_debug("buf1 (dso):\n");
  443. dump_buf(buf1, len);
  444. pr_debug("buf2 (objdump):\n");
  445. dump_buf(buf2, len);
  446. err = -1;
  447. goto out;
  448. }
  449. pr_debug("Bytes read match those read by objdump\n");
  450. out:
  451. addr_location__exit(&al);
  452. return err;
  453. }
  454. static int process_sample_event(struct machine *machine, struct evlist *evlist,
  455. union perf_event *event,
  456. struct rb_root *tested_sections)
  457. {
  458. struct perf_sample sample;
  459. struct thread *thread;
  460. int ret;
  461. perf_sample__init(&sample, /*all=*/false);
  462. ret = evlist__parse_sample(evlist, event, &sample);
  463. if (ret) {
  464. pr_debug("evlist__parse_sample failed\n");
  465. ret = -1;
  466. goto out;
  467. }
  468. thread = machine__findnew_thread(machine, sample.pid, sample.tid);
  469. if (!thread) {
  470. pr_debug("machine__findnew_thread failed\n");
  471. ret = -1;
  472. goto out;
  473. }
  474. ret = read_object_code(sample.ip, READLEN, sample.cpumode, thread,
  475. tested_sections);
  476. thread__put(thread);
  477. out:
  478. perf_sample__exit(&sample);
  479. return ret;
  480. }
  481. static int process_event(struct machine *machine, struct evlist *evlist,
  482. union perf_event *event, struct rb_root *tested_sections)
  483. {
  484. if (event->header.type == PERF_RECORD_SAMPLE)
  485. return process_sample_event(machine, evlist, event,
  486. tested_sections);
  487. if (event->header.type == PERF_RECORD_THROTTLE ||
  488. event->header.type == PERF_RECORD_UNTHROTTLE)
  489. return 0;
  490. if (event->header.type < PERF_RECORD_MAX) {
  491. int ret;
  492. ret = machine__process_event(machine, event, NULL);
  493. if (ret < 0)
  494. pr_debug("machine__process_event failed, event type %u\n",
  495. event->header.type);
  496. return ret;
  497. }
  498. return 0;
  499. }
  500. static int process_events(struct machine *machine, struct evlist *evlist,
  501. struct rb_root *tested_sections)
  502. {
  503. union perf_event *event;
  504. struct mmap *md;
  505. int i, ret;
  506. for (i = 0; i < evlist->core.nr_mmaps; i++) {
  507. md = &evlist->mmap[i];
  508. if (perf_mmap__read_init(&md->core) < 0)
  509. continue;
  510. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  511. ret = process_event(machine, evlist, event, tested_sections);
  512. perf_mmap__consume(&md->core);
  513. if (ret < 0)
  514. return ret;
  515. }
  516. perf_mmap__read_done(&md->core);
  517. }
  518. return 0;
  519. }
  520. static int comp(const void *a, const void *b)
  521. {
  522. return *(int *)a - *(int *)b;
  523. }
  524. static void do_sort_something(void)
  525. {
  526. int buf[40960], i;
  527. for (i = 0; i < (int)ARRAY_SIZE(buf); i++)
  528. buf[i] = ARRAY_SIZE(buf) - i - 1;
  529. qsort(buf, ARRAY_SIZE(buf), sizeof(int), comp);
  530. for (i = 0; i < (int)ARRAY_SIZE(buf); i++) {
  531. if (buf[i] != i) {
  532. pr_debug("qsort failed\n");
  533. break;
  534. }
  535. }
  536. }
  537. static void sort_something(void)
  538. {
  539. int i;
  540. for (i = 0; i < 10; i++)
  541. do_sort_something();
  542. }
  543. static void syscall_something(void)
  544. {
  545. int pipefd[2];
  546. int i;
  547. for (i = 0; i < 1000; i++) {
  548. if (pipe(pipefd) < 0) {
  549. pr_debug("pipe failed\n");
  550. break;
  551. }
  552. close(pipefd[1]);
  553. close(pipefd[0]);
  554. }
  555. }
  556. static void fs_something(void)
  557. {
  558. const char *test_file_name = "temp-perf-code-reading-test-file--";
  559. FILE *f;
  560. int i;
  561. for (i = 0; i < 1000; i++) {
  562. f = fopen(test_file_name, "w+");
  563. if (f) {
  564. fclose(f);
  565. unlink(test_file_name);
  566. }
  567. }
  568. }
  569. static void do_something(void)
  570. {
  571. fs_something();
  572. sort_something();
  573. syscall_something();
  574. }
  575. enum {
  576. TEST_CODE_READING_OK,
  577. TEST_CODE_READING_NO_VMLINUX,
  578. TEST_CODE_READING_NO_KCORE,
  579. TEST_CODE_READING_NO_ACCESS,
  580. TEST_CODE_READING_NO_KERNEL_OBJ,
  581. };
  582. static int do_test_code_reading(bool try_kcore)
  583. {
  584. struct machine *machine;
  585. struct thread *thread;
  586. struct record_opts opts = {
  587. .mmap_pages = UINT_MAX,
  588. .user_freq = UINT_MAX,
  589. .user_interval = ULLONG_MAX,
  590. .freq = 500,
  591. .target = {
  592. .uses_mmap = true,
  593. },
  594. };
  595. struct rb_root tested_sections = RB_ROOT;
  596. struct perf_thread_map *threads = NULL;
  597. struct perf_cpu_map *cpus = NULL;
  598. struct evlist *evlist = NULL;
  599. struct evsel *evsel = NULL;
  600. int err = -1, ret;
  601. pid_t pid;
  602. struct map *map;
  603. bool have_vmlinux, have_kcore;
  604. struct dso *dso;
  605. const char *events[] = { "cpu-cycles", "cpu-cycles:u", "cpu-clock", "cpu-clock:u", NULL };
  606. int evidx = 0;
  607. struct perf_env host_env;
  608. pid = getpid();
  609. perf_env__init(&host_env);
  610. machine = machine__new_host(&host_env);
  611. ret = machine__create_kernel_maps(machine);
  612. if (ret < 0) {
  613. pr_debug("machine__create_kernel_maps failed\n");
  614. goto out_err;
  615. }
  616. /* Force the use of kallsyms instead of vmlinux to try kcore */
  617. if (try_kcore)
  618. symbol_conf.kallsyms_name = "/proc/kallsyms";
  619. /* Load kernel map */
  620. map = machine__kernel_map(machine);
  621. ret = map__load(map);
  622. if (ret < 0) {
  623. pr_debug("map__load failed\n");
  624. goto out_err;
  625. }
  626. dso = map__dso(map);
  627. have_vmlinux = dso__is_vmlinux(dso);
  628. have_kcore = dso__is_kcore(dso);
  629. /* 2nd time through we just try kcore */
  630. if (try_kcore && !have_kcore)
  631. return TEST_CODE_READING_NO_KCORE;
  632. /* No point getting kernel events if there is no kernel object */
  633. if (!have_vmlinux && !have_kcore)
  634. evidx++;
  635. threads = thread_map__new_by_tid(pid);
  636. if (!threads) {
  637. pr_debug("thread_map__new_by_tid failed\n");
  638. goto out_err;
  639. }
  640. ret = perf_event__synthesize_thread_map(NULL, threads,
  641. perf_event__process, machine,
  642. true, false);
  643. if (ret < 0) {
  644. pr_debug("perf_event__synthesize_thread_map failed\n");
  645. goto out_err;
  646. }
  647. thread = machine__findnew_thread(machine, pid, pid);
  648. if (!thread) {
  649. pr_debug("machine__findnew_thread failed\n");
  650. goto out_put;
  651. }
  652. cpus = perf_cpu_map__new_online_cpus();
  653. if (!cpus) {
  654. pr_debug("perf_cpu_map__new failed\n");
  655. goto out_put;
  656. }
  657. while (events[evidx]) {
  658. const char *str;
  659. evlist = evlist__new();
  660. if (!evlist) {
  661. pr_debug("evlist__new failed\n");
  662. goto out_put;
  663. }
  664. perf_evlist__set_maps(&evlist->core, cpus, threads);
  665. str = events[evidx];
  666. pr_debug("Parsing event '%s'\n", str);
  667. ret = parse_event(evlist, str);
  668. if (ret < 0) {
  669. pr_debug("parse_events failed\n");
  670. goto out_put;
  671. }
  672. evlist__config(evlist, &opts, NULL);
  673. evlist__for_each_entry(evlist, evsel) {
  674. evsel->core.attr.comm = 1;
  675. evsel->core.attr.disabled = 1;
  676. evsel->core.attr.enable_on_exec = 0;
  677. }
  678. ret = evlist__open(evlist);
  679. if (ret < 0) {
  680. evidx++;
  681. if (events[evidx] == NULL && verbose > 0) {
  682. char errbuf[512];
  683. evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
  684. pr_debug("perf_evlist__open() failed!\n%s\n", errbuf);
  685. }
  686. perf_evlist__set_maps(&evlist->core, NULL, NULL);
  687. evlist__delete(evlist);
  688. evlist = NULL;
  689. continue;
  690. }
  691. break;
  692. }
  693. if (events[evidx] == NULL)
  694. goto out_put;
  695. ret = evlist__mmap(evlist, UINT_MAX);
  696. if (ret < 0) {
  697. pr_debug("evlist__mmap failed\n");
  698. goto out_put;
  699. }
  700. evlist__enable(evlist);
  701. do_something();
  702. evlist__disable(evlist);
  703. ret = process_events(machine, evlist, &tested_sections);
  704. if (ret < 0)
  705. goto out_put;
  706. if (!have_vmlinux && !have_kcore && !try_kcore)
  707. err = TEST_CODE_READING_NO_KERNEL_OBJ;
  708. else if (!have_vmlinux && !try_kcore)
  709. err = TEST_CODE_READING_NO_VMLINUX;
  710. else if (strstr(events[evidx], ":u"))
  711. err = TEST_CODE_READING_NO_ACCESS;
  712. else
  713. err = TEST_CODE_READING_OK;
  714. out_put:
  715. thread__put(thread);
  716. out_err:
  717. evlist__delete(evlist);
  718. perf_cpu_map__put(cpus);
  719. perf_thread_map__put(threads);
  720. machine__delete(machine);
  721. perf_env__exit(&host_env);
  722. tested_sections__free(&tested_sections);
  723. return err;
  724. }
  725. static int test__code_reading(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  726. {
  727. int ret;
  728. ret = do_test_code_reading(false);
  729. if (!ret)
  730. ret = do_test_code_reading(true);
  731. switch (ret) {
  732. case TEST_CODE_READING_OK:
  733. return 0;
  734. case TEST_CODE_READING_NO_VMLINUX:
  735. pr_debug("no vmlinux\n");
  736. return 0;
  737. case TEST_CODE_READING_NO_KCORE:
  738. pr_debug("no kcore\n");
  739. return 0;
  740. case TEST_CODE_READING_NO_ACCESS:
  741. pr_debug("no access\n");
  742. return 0;
  743. case TEST_CODE_READING_NO_KERNEL_OBJ:
  744. pr_debug("no kernel obj\n");
  745. return 0;
  746. default:
  747. return -1;
  748. };
  749. }
  750. DEFINE_SUITE("Object code reading", code_reading);