dso.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. #include <asm/bug.h>
  2. #include <sys/time.h>
  3. #include <sys/resource.h>
  4. #include "symbol.h"
  5. #include "dso.h"
  6. #include "machine.h"
  7. #include "auxtrace.h"
  8. #include "util.h"
  9. #include "debug.h"
  10. char dso__symtab_origin(const struct dso *dso)
  11. {
  12. static const char origin[] = {
  13. [DSO_BINARY_TYPE__KALLSYMS] = 'k',
  14. [DSO_BINARY_TYPE__VMLINUX] = 'v',
  15. [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
  16. [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
  17. [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
  18. [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
  19. [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
  20. [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
  21. [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
  22. [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
  23. [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
  24. [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
  25. [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
  26. [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
  27. [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
  28. [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
  29. };
  30. if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
  31. return '!';
  32. return origin[dso->symtab_type];
  33. }
  34. int dso__read_binary_type_filename(const struct dso *dso,
  35. enum dso_binary_type type,
  36. char *root_dir, char *filename, size_t size)
  37. {
  38. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  39. int ret = 0;
  40. size_t len;
  41. switch (type) {
  42. case DSO_BINARY_TYPE__DEBUGLINK: {
  43. char *debuglink;
  44. len = __symbol__join_symfs(filename, size, dso->long_name);
  45. debuglink = filename + len;
  46. while (debuglink != filename && *debuglink != '/')
  47. debuglink--;
  48. if (*debuglink == '/')
  49. debuglink++;
  50. ret = filename__read_debuglink(filename, debuglink,
  51. size - (debuglink - filename));
  52. }
  53. break;
  54. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  55. /* skip the locally configured cache if a symfs is given */
  56. if (symbol_conf.symfs[0] ||
  57. (dso__build_id_filename(dso, filename, size) == NULL))
  58. ret = -1;
  59. break;
  60. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  61. len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
  62. snprintf(filename + len, size - len, "%s.debug", dso->long_name);
  63. break;
  64. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  65. len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
  66. snprintf(filename + len, size - len, "%s", dso->long_name);
  67. break;
  68. case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
  69. {
  70. const char *last_slash;
  71. size_t dir_size;
  72. last_slash = dso->long_name + dso->long_name_len;
  73. while (last_slash != dso->long_name && *last_slash != '/')
  74. last_slash--;
  75. len = __symbol__join_symfs(filename, size, "");
  76. dir_size = last_slash - dso->long_name + 2;
  77. if (dir_size > (size - len)) {
  78. ret = -1;
  79. break;
  80. }
  81. len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
  82. len += scnprintf(filename + len , size - len, ".debug%s",
  83. last_slash);
  84. break;
  85. }
  86. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  87. if (!dso->has_build_id) {
  88. ret = -1;
  89. break;
  90. }
  91. build_id__sprintf(dso->build_id,
  92. sizeof(dso->build_id),
  93. build_id_hex);
  94. len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
  95. snprintf(filename + len, size - len, "%.2s/%s.debug",
  96. build_id_hex, build_id_hex + 2);
  97. break;
  98. case DSO_BINARY_TYPE__VMLINUX:
  99. case DSO_BINARY_TYPE__GUEST_VMLINUX:
  100. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  101. __symbol__join_symfs(filename, size, dso->long_name);
  102. break;
  103. case DSO_BINARY_TYPE__GUEST_KMODULE:
  104. case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
  105. path__join3(filename, size, symbol_conf.symfs,
  106. root_dir, dso->long_name);
  107. break;
  108. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  109. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
  110. __symbol__join_symfs(filename, size, dso->long_name);
  111. break;
  112. case DSO_BINARY_TYPE__KCORE:
  113. case DSO_BINARY_TYPE__GUEST_KCORE:
  114. snprintf(filename, size, "%s", dso->long_name);
  115. break;
  116. default:
  117. case DSO_BINARY_TYPE__KALLSYMS:
  118. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  119. case DSO_BINARY_TYPE__JAVA_JIT:
  120. case DSO_BINARY_TYPE__NOT_FOUND:
  121. ret = -1;
  122. break;
  123. }
  124. return ret;
  125. }
  126. static const struct {
  127. const char *fmt;
  128. int (*decompress)(const char *input, int output);
  129. } compressions[] = {
  130. #ifdef HAVE_ZLIB_SUPPORT
  131. { "gz", gzip_decompress_to_file },
  132. #endif
  133. #ifdef HAVE_LZMA_SUPPORT
  134. { "xz", lzma_decompress_to_file },
  135. #endif
  136. { NULL, NULL },
  137. };
  138. bool is_supported_compression(const char *ext)
  139. {
  140. unsigned i;
  141. for (i = 0; compressions[i].fmt; i++) {
  142. if (!strcmp(ext, compressions[i].fmt))
  143. return true;
  144. }
  145. return false;
  146. }
  147. bool is_kernel_module(const char *pathname, int cpumode)
  148. {
  149. struct kmod_path m;
  150. int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
  151. WARN_ONCE(mode != cpumode,
  152. "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
  153. cpumode);
  154. switch (mode) {
  155. case PERF_RECORD_MISC_USER:
  156. case PERF_RECORD_MISC_HYPERVISOR:
  157. case PERF_RECORD_MISC_GUEST_USER:
  158. return false;
  159. /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
  160. default:
  161. if (kmod_path__parse(&m, pathname)) {
  162. pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
  163. pathname);
  164. return true;
  165. }
  166. }
  167. return m.kmod;
  168. }
  169. bool decompress_to_file(const char *ext, const char *filename, int output_fd)
  170. {
  171. unsigned i;
  172. for (i = 0; compressions[i].fmt; i++) {
  173. if (!strcmp(ext, compressions[i].fmt))
  174. return !compressions[i].decompress(filename,
  175. output_fd);
  176. }
  177. return false;
  178. }
  179. bool dso__needs_decompress(struct dso *dso)
  180. {
  181. return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
  182. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
  183. }
  184. /*
  185. * Parses kernel module specified in @path and updates
  186. * @m argument like:
  187. *
  188. * @comp - true if @path contains supported compression suffix,
  189. * false otherwise
  190. * @kmod - true if @path contains '.ko' suffix in right position,
  191. * false otherwise
  192. * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
  193. * of the kernel module without suffixes, otherwise strudup-ed
  194. * base name of @path
  195. * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
  196. * the compression suffix
  197. *
  198. * Returns 0 if there's no strdup error, -ENOMEM otherwise.
  199. */
  200. int __kmod_path__parse(struct kmod_path *m, const char *path,
  201. bool alloc_name, bool alloc_ext)
  202. {
  203. const char *name = strrchr(path, '/');
  204. const char *ext = strrchr(path, '.');
  205. bool is_simple_name = false;
  206. memset(m, 0x0, sizeof(*m));
  207. name = name ? name + 1 : path;
  208. /*
  209. * '.' is also a valid character for module name. For example:
  210. * [aaa.bbb] is a valid module name. '[' should have higher
  211. * priority than '.ko' suffix.
  212. *
  213. * The kernel names are from machine__mmap_name. Such
  214. * name should belong to kernel itself, not kernel module.
  215. */
  216. if (name[0] == '[') {
  217. is_simple_name = true;
  218. if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
  219. (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
  220. (strncmp(name, "[vdso]", 6) == 0) ||
  221. (strncmp(name, "[vsyscall]", 10) == 0)) {
  222. m->kmod = false;
  223. } else
  224. m->kmod = true;
  225. }
  226. /* No extension, just return name. */
  227. if ((ext == NULL) || is_simple_name) {
  228. if (alloc_name) {
  229. m->name = strdup(name);
  230. return m->name ? 0 : -ENOMEM;
  231. }
  232. return 0;
  233. }
  234. if (is_supported_compression(ext + 1)) {
  235. m->comp = true;
  236. ext -= 3;
  237. }
  238. /* Check .ko extension only if there's enough name left. */
  239. if (ext > name)
  240. m->kmod = !strncmp(ext, ".ko", 3);
  241. if (alloc_name) {
  242. if (m->kmod) {
  243. if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
  244. return -ENOMEM;
  245. } else {
  246. if (asprintf(&m->name, "%s", name) == -1)
  247. return -ENOMEM;
  248. }
  249. strxfrchar(m->name, '-', '_');
  250. }
  251. if (alloc_ext && m->comp) {
  252. m->ext = strdup(ext + 4);
  253. if (!m->ext) {
  254. free((void *) m->name);
  255. return -ENOMEM;
  256. }
  257. }
  258. return 0;
  259. }
  260. /*
  261. * Global list of open DSOs and the counter.
  262. */
  263. static LIST_HEAD(dso__data_open);
  264. static long dso__data_open_cnt;
  265. static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
  266. static void dso__list_add(struct dso *dso)
  267. {
  268. list_add_tail(&dso->data.open_entry, &dso__data_open);
  269. dso__data_open_cnt++;
  270. }
  271. static void dso__list_del(struct dso *dso)
  272. {
  273. list_del(&dso->data.open_entry);
  274. WARN_ONCE(dso__data_open_cnt <= 0,
  275. "DSO data fd counter out of bounds.");
  276. dso__data_open_cnt--;
  277. }
  278. static void close_first_dso(void);
  279. static int do_open(char *name)
  280. {
  281. int fd;
  282. char sbuf[STRERR_BUFSIZE];
  283. do {
  284. fd = open(name, O_RDONLY);
  285. if (fd >= 0)
  286. return fd;
  287. pr_debug("dso open failed: %s\n",
  288. strerror_r(errno, sbuf, sizeof(sbuf)));
  289. if (!dso__data_open_cnt || errno != EMFILE)
  290. break;
  291. close_first_dso();
  292. } while (1);
  293. return -1;
  294. }
  295. static int __open_dso(struct dso *dso, struct machine *machine)
  296. {
  297. int fd;
  298. char *root_dir = (char *)"";
  299. char *name = malloc(PATH_MAX);
  300. if (!name)
  301. return -ENOMEM;
  302. if (machine)
  303. root_dir = machine->root_dir;
  304. if (dso__read_binary_type_filename(dso, dso->binary_type,
  305. root_dir, name, PATH_MAX)) {
  306. free(name);
  307. return -EINVAL;
  308. }
  309. fd = do_open(name);
  310. free(name);
  311. return fd;
  312. }
  313. static void check_data_close(void);
  314. /**
  315. * dso_close - Open DSO data file
  316. * @dso: dso object
  317. *
  318. * Open @dso's data file descriptor and updates
  319. * list/count of open DSO objects.
  320. */
  321. static int open_dso(struct dso *dso, struct machine *machine)
  322. {
  323. int fd = __open_dso(dso, machine);
  324. if (fd >= 0) {
  325. dso__list_add(dso);
  326. /*
  327. * Check if we crossed the allowed number
  328. * of opened DSOs and close one if needed.
  329. */
  330. check_data_close();
  331. }
  332. return fd;
  333. }
  334. static void close_data_fd(struct dso *dso)
  335. {
  336. if (dso->data.fd >= 0) {
  337. close(dso->data.fd);
  338. dso->data.fd = -1;
  339. dso->data.file_size = 0;
  340. dso__list_del(dso);
  341. }
  342. }
  343. /**
  344. * dso_close - Close DSO data file
  345. * @dso: dso object
  346. *
  347. * Close @dso's data file descriptor and updates
  348. * list/count of open DSO objects.
  349. */
  350. static void close_dso(struct dso *dso)
  351. {
  352. close_data_fd(dso);
  353. }
  354. static void close_first_dso(void)
  355. {
  356. struct dso *dso;
  357. dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
  358. close_dso(dso);
  359. }
  360. static rlim_t get_fd_limit(void)
  361. {
  362. struct rlimit l;
  363. rlim_t limit = 0;
  364. /* Allow half of the current open fd limit. */
  365. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  366. if (l.rlim_cur == RLIM_INFINITY)
  367. limit = l.rlim_cur;
  368. else
  369. limit = l.rlim_cur / 2;
  370. } else {
  371. pr_err("failed to get fd limit\n");
  372. limit = 1;
  373. }
  374. return limit;
  375. }
  376. static bool may_cache_fd(void)
  377. {
  378. static rlim_t limit;
  379. if (!limit)
  380. limit = get_fd_limit();
  381. if (limit == RLIM_INFINITY)
  382. return true;
  383. return limit > (rlim_t) dso__data_open_cnt;
  384. }
  385. /*
  386. * Check and close LRU dso if we crossed allowed limit
  387. * for opened dso file descriptors. The limit is half
  388. * of the RLIMIT_NOFILE files opened.
  389. */
  390. static void check_data_close(void)
  391. {
  392. bool cache_fd = may_cache_fd();
  393. if (!cache_fd)
  394. close_first_dso();
  395. }
  396. /**
  397. * dso__data_close - Close DSO data file
  398. * @dso: dso object
  399. *
  400. * External interface to close @dso's data file descriptor.
  401. */
  402. void dso__data_close(struct dso *dso)
  403. {
  404. pthread_mutex_lock(&dso__data_open_lock);
  405. close_dso(dso);
  406. pthread_mutex_unlock(&dso__data_open_lock);
  407. }
  408. static void try_to_open_dso(struct dso *dso, struct machine *machine)
  409. {
  410. enum dso_binary_type binary_type_data[] = {
  411. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  412. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  413. DSO_BINARY_TYPE__NOT_FOUND,
  414. };
  415. int i = 0;
  416. if (dso->data.fd >= 0)
  417. return;
  418. if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
  419. dso->data.fd = open_dso(dso, machine);
  420. goto out;
  421. }
  422. do {
  423. dso->binary_type = binary_type_data[i++];
  424. dso->data.fd = open_dso(dso, machine);
  425. if (dso->data.fd >= 0)
  426. goto out;
  427. } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
  428. out:
  429. if (dso->data.fd >= 0)
  430. dso->data.status = DSO_DATA_STATUS_OK;
  431. else
  432. dso->data.status = DSO_DATA_STATUS_ERROR;
  433. }
  434. /**
  435. * dso__data_get_fd - Get dso's data file descriptor
  436. * @dso: dso object
  437. * @machine: machine object
  438. *
  439. * External interface to find dso's file, open it and
  440. * returns file descriptor. It should be paired with
  441. * dso__data_put_fd() if it returns non-negative value.
  442. */
  443. int dso__data_get_fd(struct dso *dso, struct machine *machine)
  444. {
  445. if (dso->data.status == DSO_DATA_STATUS_ERROR)
  446. return -1;
  447. if (pthread_mutex_lock(&dso__data_open_lock) < 0)
  448. return -1;
  449. try_to_open_dso(dso, machine);
  450. if (dso->data.fd < 0)
  451. pthread_mutex_unlock(&dso__data_open_lock);
  452. return dso->data.fd;
  453. }
  454. void dso__data_put_fd(struct dso *dso __maybe_unused)
  455. {
  456. pthread_mutex_unlock(&dso__data_open_lock);
  457. }
  458. bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
  459. {
  460. u32 flag = 1 << by;
  461. if (dso->data.status_seen & flag)
  462. return true;
  463. dso->data.status_seen |= flag;
  464. return false;
  465. }
  466. static void
  467. dso_cache__free(struct dso *dso)
  468. {
  469. struct rb_root *root = &dso->data.cache;
  470. struct rb_node *next = rb_first(root);
  471. pthread_mutex_lock(&dso->lock);
  472. while (next) {
  473. struct dso_cache *cache;
  474. cache = rb_entry(next, struct dso_cache, rb_node);
  475. next = rb_next(&cache->rb_node);
  476. rb_erase(&cache->rb_node, root);
  477. free(cache);
  478. }
  479. pthread_mutex_unlock(&dso->lock);
  480. }
  481. static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
  482. {
  483. const struct rb_root *root = &dso->data.cache;
  484. struct rb_node * const *p = &root->rb_node;
  485. const struct rb_node *parent = NULL;
  486. struct dso_cache *cache;
  487. while (*p != NULL) {
  488. u64 end;
  489. parent = *p;
  490. cache = rb_entry(parent, struct dso_cache, rb_node);
  491. end = cache->offset + DSO__DATA_CACHE_SIZE;
  492. if (offset < cache->offset)
  493. p = &(*p)->rb_left;
  494. else if (offset >= end)
  495. p = &(*p)->rb_right;
  496. else
  497. return cache;
  498. }
  499. return NULL;
  500. }
  501. static struct dso_cache *
  502. dso_cache__insert(struct dso *dso, struct dso_cache *new)
  503. {
  504. struct rb_root *root = &dso->data.cache;
  505. struct rb_node **p = &root->rb_node;
  506. struct rb_node *parent = NULL;
  507. struct dso_cache *cache;
  508. u64 offset = new->offset;
  509. pthread_mutex_lock(&dso->lock);
  510. while (*p != NULL) {
  511. u64 end;
  512. parent = *p;
  513. cache = rb_entry(parent, struct dso_cache, rb_node);
  514. end = cache->offset + DSO__DATA_CACHE_SIZE;
  515. if (offset < cache->offset)
  516. p = &(*p)->rb_left;
  517. else if (offset >= end)
  518. p = &(*p)->rb_right;
  519. else
  520. goto out;
  521. }
  522. rb_link_node(&new->rb_node, parent, p);
  523. rb_insert_color(&new->rb_node, root);
  524. cache = NULL;
  525. out:
  526. pthread_mutex_unlock(&dso->lock);
  527. return cache;
  528. }
  529. static ssize_t
  530. dso_cache__memcpy(struct dso_cache *cache, u64 offset,
  531. u8 *data, u64 size)
  532. {
  533. u64 cache_offset = offset - cache->offset;
  534. u64 cache_size = min(cache->size - cache_offset, size);
  535. memcpy(data, cache->data + cache_offset, cache_size);
  536. return cache_size;
  537. }
  538. static ssize_t
  539. dso_cache__read(struct dso *dso, struct machine *machine,
  540. u64 offset, u8 *data, ssize_t size)
  541. {
  542. struct dso_cache *cache;
  543. struct dso_cache *old;
  544. ssize_t ret;
  545. do {
  546. u64 cache_offset;
  547. cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
  548. if (!cache)
  549. return -ENOMEM;
  550. pthread_mutex_lock(&dso__data_open_lock);
  551. /*
  552. * dso->data.fd might be closed if other thread opened another
  553. * file (dso) due to open file limit (RLIMIT_NOFILE).
  554. */
  555. try_to_open_dso(dso, machine);
  556. if (dso->data.fd < 0) {
  557. ret = -errno;
  558. dso->data.status = DSO_DATA_STATUS_ERROR;
  559. break;
  560. }
  561. cache_offset = offset & DSO__DATA_CACHE_MASK;
  562. ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
  563. if (ret <= 0)
  564. break;
  565. cache->offset = cache_offset;
  566. cache->size = ret;
  567. } while (0);
  568. pthread_mutex_unlock(&dso__data_open_lock);
  569. if (ret > 0) {
  570. old = dso_cache__insert(dso, cache);
  571. if (old) {
  572. /* we lose the race */
  573. free(cache);
  574. cache = old;
  575. }
  576. ret = dso_cache__memcpy(cache, offset, data, size);
  577. }
  578. if (ret <= 0)
  579. free(cache);
  580. return ret;
  581. }
  582. static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
  583. u64 offset, u8 *data, ssize_t size)
  584. {
  585. struct dso_cache *cache;
  586. cache = dso_cache__find(dso, offset);
  587. if (cache)
  588. return dso_cache__memcpy(cache, offset, data, size);
  589. else
  590. return dso_cache__read(dso, machine, offset, data, size);
  591. }
  592. /*
  593. * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
  594. * in the rb_tree. Any read to already cached data is served
  595. * by cached data.
  596. */
  597. static ssize_t cached_read(struct dso *dso, struct machine *machine,
  598. u64 offset, u8 *data, ssize_t size)
  599. {
  600. ssize_t r = 0;
  601. u8 *p = data;
  602. do {
  603. ssize_t ret;
  604. ret = dso_cache_read(dso, machine, offset, p, size);
  605. if (ret < 0)
  606. return ret;
  607. /* Reached EOF, return what we have. */
  608. if (!ret)
  609. break;
  610. BUG_ON(ret > size);
  611. r += ret;
  612. p += ret;
  613. offset += ret;
  614. size -= ret;
  615. } while (size);
  616. return r;
  617. }
  618. static int data_file_size(struct dso *dso, struct machine *machine)
  619. {
  620. int ret = 0;
  621. struct stat st;
  622. char sbuf[STRERR_BUFSIZE];
  623. if (dso->data.file_size)
  624. return 0;
  625. if (dso->data.status == DSO_DATA_STATUS_ERROR)
  626. return -1;
  627. pthread_mutex_lock(&dso__data_open_lock);
  628. /*
  629. * dso->data.fd might be closed if other thread opened another
  630. * file (dso) due to open file limit (RLIMIT_NOFILE).
  631. */
  632. try_to_open_dso(dso, machine);
  633. if (dso->data.fd < 0) {
  634. ret = -errno;
  635. dso->data.status = DSO_DATA_STATUS_ERROR;
  636. goto out;
  637. }
  638. if (fstat(dso->data.fd, &st) < 0) {
  639. ret = -errno;
  640. pr_err("dso cache fstat failed: %s\n",
  641. strerror_r(errno, sbuf, sizeof(sbuf)));
  642. dso->data.status = DSO_DATA_STATUS_ERROR;
  643. goto out;
  644. }
  645. dso->data.file_size = st.st_size;
  646. out:
  647. pthread_mutex_unlock(&dso__data_open_lock);
  648. return ret;
  649. }
  650. /**
  651. * dso__data_size - Return dso data size
  652. * @dso: dso object
  653. * @machine: machine object
  654. *
  655. * Return: dso data size
  656. */
  657. off_t dso__data_size(struct dso *dso, struct machine *machine)
  658. {
  659. if (data_file_size(dso, machine))
  660. return -1;
  661. /* For now just estimate dso data size is close to file size */
  662. return dso->data.file_size;
  663. }
  664. static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
  665. u64 offset, u8 *data, ssize_t size)
  666. {
  667. if (data_file_size(dso, machine))
  668. return -1;
  669. /* Check the offset sanity. */
  670. if (offset > dso->data.file_size)
  671. return -1;
  672. if (offset + size < offset)
  673. return -1;
  674. return cached_read(dso, machine, offset, data, size);
  675. }
  676. /**
  677. * dso__data_read_offset - Read data from dso file offset
  678. * @dso: dso object
  679. * @machine: machine object
  680. * @offset: file offset
  681. * @data: buffer to store data
  682. * @size: size of the @data buffer
  683. *
  684. * External interface to read data from dso file offset. Open
  685. * dso data file and use cached_read to get the data.
  686. */
  687. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  688. u64 offset, u8 *data, ssize_t size)
  689. {
  690. if (dso->data.status == DSO_DATA_STATUS_ERROR)
  691. return -1;
  692. return data_read_offset(dso, machine, offset, data, size);
  693. }
  694. /**
  695. * dso__data_read_addr - Read data from dso address
  696. * @dso: dso object
  697. * @machine: machine object
  698. * @add: virtual memory address
  699. * @data: buffer to store data
  700. * @size: size of the @data buffer
  701. *
  702. * External interface to read data from dso address.
  703. */
  704. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  705. struct machine *machine, u64 addr,
  706. u8 *data, ssize_t size)
  707. {
  708. u64 offset = map->map_ip(map, addr);
  709. return dso__data_read_offset(dso, machine, offset, data, size);
  710. }
  711. struct map *dso__new_map(const char *name)
  712. {
  713. struct map *map = NULL;
  714. struct dso *dso = dso__new(name);
  715. if (dso)
  716. map = map__new2(0, dso, MAP__FUNCTION);
  717. return map;
  718. }
  719. struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
  720. const char *short_name, int dso_type)
  721. {
  722. /*
  723. * The kernel dso could be created by build_id processing.
  724. */
  725. struct dso *dso = machine__findnew_dso(machine, name);
  726. /*
  727. * We need to run this in all cases, since during the build_id
  728. * processing we had no idea this was the kernel dso.
  729. */
  730. if (dso != NULL) {
  731. dso__set_short_name(dso, short_name, false);
  732. dso->kernel = dso_type;
  733. }
  734. return dso;
  735. }
  736. /*
  737. * Find a matching entry and/or link current entry to RB tree.
  738. * Either one of the dso or name parameter must be non-NULL or the
  739. * function will not work.
  740. */
  741. static struct dso *__dso__findlink_by_longname(struct rb_root *root,
  742. struct dso *dso, const char *name)
  743. {
  744. struct rb_node **p = &root->rb_node;
  745. struct rb_node *parent = NULL;
  746. if (!name)
  747. name = dso->long_name;
  748. /*
  749. * Find node with the matching name
  750. */
  751. while (*p) {
  752. struct dso *this = rb_entry(*p, struct dso, rb_node);
  753. int rc = strcmp(name, this->long_name);
  754. parent = *p;
  755. if (rc == 0) {
  756. /*
  757. * In case the new DSO is a duplicate of an existing
  758. * one, print an one-time warning & put the new entry
  759. * at the end of the list of duplicates.
  760. */
  761. if (!dso || (dso == this))
  762. return this; /* Find matching dso */
  763. /*
  764. * The core kernel DSOs may have duplicated long name.
  765. * In this case, the short name should be different.
  766. * Comparing the short names to differentiate the DSOs.
  767. */
  768. rc = strcmp(dso->short_name, this->short_name);
  769. if (rc == 0) {
  770. pr_err("Duplicated dso name: %s\n", name);
  771. return NULL;
  772. }
  773. }
  774. if (rc < 0)
  775. p = &parent->rb_left;
  776. else
  777. p = &parent->rb_right;
  778. }
  779. if (dso) {
  780. /* Add new node and rebalance tree */
  781. rb_link_node(&dso->rb_node, parent, p);
  782. rb_insert_color(&dso->rb_node, root);
  783. dso->root = root;
  784. }
  785. return NULL;
  786. }
  787. static inline struct dso *__dso__find_by_longname(struct rb_root *root,
  788. const char *name)
  789. {
  790. return __dso__findlink_by_longname(root, NULL, name);
  791. }
  792. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
  793. {
  794. struct rb_root *root = dso->root;
  795. if (name == NULL)
  796. return;
  797. if (dso->long_name_allocated)
  798. free((char *)dso->long_name);
  799. if (root) {
  800. rb_erase(&dso->rb_node, root);
  801. /*
  802. * __dso__findlink_by_longname() isn't guaranteed to add it
  803. * back, so a clean removal is required here.
  804. */
  805. RB_CLEAR_NODE(&dso->rb_node);
  806. dso->root = NULL;
  807. }
  808. dso->long_name = name;
  809. dso->long_name_len = strlen(name);
  810. dso->long_name_allocated = name_allocated;
  811. if (root)
  812. __dso__findlink_by_longname(root, dso, NULL);
  813. }
  814. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
  815. {
  816. if (name == NULL)
  817. return;
  818. if (dso->short_name_allocated)
  819. free((char *)dso->short_name);
  820. dso->short_name = name;
  821. dso->short_name_len = strlen(name);
  822. dso->short_name_allocated = name_allocated;
  823. }
  824. static void dso__set_basename(struct dso *dso)
  825. {
  826. /*
  827. * basename() may modify path buffer, so we must pass
  828. * a copy.
  829. */
  830. char *base, *lname = strdup(dso->long_name);
  831. if (!lname)
  832. return;
  833. /*
  834. * basename() may return a pointer to internal
  835. * storage which is reused in subsequent calls
  836. * so copy the result.
  837. */
  838. base = strdup(basename(lname));
  839. free(lname);
  840. if (!base)
  841. return;
  842. dso__set_short_name(dso, base, true);
  843. }
  844. int dso__name_len(const struct dso *dso)
  845. {
  846. if (!dso)
  847. return strlen("[unknown]");
  848. if (verbose)
  849. return dso->long_name_len;
  850. return dso->short_name_len;
  851. }
  852. bool dso__loaded(const struct dso *dso, enum map_type type)
  853. {
  854. return dso->loaded & (1 << type);
  855. }
  856. bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
  857. {
  858. return dso->sorted_by_name & (1 << type);
  859. }
  860. void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
  861. {
  862. dso->sorted_by_name |= (1 << type);
  863. }
  864. struct dso *dso__new(const char *name)
  865. {
  866. struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
  867. if (dso != NULL) {
  868. int i;
  869. strcpy(dso->name, name);
  870. dso__set_long_name(dso, dso->name, false);
  871. dso__set_short_name(dso, dso->name, false);
  872. for (i = 0; i < MAP__NR_TYPES; ++i)
  873. dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
  874. dso->data.cache = RB_ROOT;
  875. dso->data.fd = -1;
  876. dso->data.status = DSO_DATA_STATUS_UNKNOWN;
  877. dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  878. dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
  879. dso->is_64_bit = (sizeof(void *) == 8);
  880. dso->loaded = 0;
  881. dso->rel = 0;
  882. dso->sorted_by_name = 0;
  883. dso->has_build_id = 0;
  884. dso->has_srcline = 1;
  885. dso->a2l_fails = 1;
  886. dso->kernel = DSO_TYPE_USER;
  887. dso->needs_swap = DSO_SWAP__UNSET;
  888. RB_CLEAR_NODE(&dso->rb_node);
  889. dso->root = NULL;
  890. INIT_LIST_HEAD(&dso->node);
  891. INIT_LIST_HEAD(&dso->data.open_entry);
  892. pthread_mutex_init(&dso->lock, NULL);
  893. atomic_set(&dso->refcnt, 1);
  894. }
  895. return dso;
  896. }
  897. void dso__delete(struct dso *dso)
  898. {
  899. int i;
  900. if (!RB_EMPTY_NODE(&dso->rb_node))
  901. pr_err("DSO %s is still in rbtree when being deleted!\n",
  902. dso->long_name);
  903. for (i = 0; i < MAP__NR_TYPES; ++i)
  904. symbols__delete(&dso->symbols[i]);
  905. if (dso->short_name_allocated) {
  906. zfree((char **)&dso->short_name);
  907. dso->short_name_allocated = false;
  908. }
  909. if (dso->long_name_allocated) {
  910. zfree((char **)&dso->long_name);
  911. dso->long_name_allocated = false;
  912. }
  913. dso__data_close(dso);
  914. auxtrace_cache__free(dso->auxtrace_cache);
  915. dso_cache__free(dso);
  916. dso__free_a2l(dso);
  917. zfree(&dso->symsrc_filename);
  918. pthread_mutex_destroy(&dso->lock);
  919. free(dso);
  920. }
  921. struct dso *dso__get(struct dso *dso)
  922. {
  923. if (dso)
  924. atomic_inc(&dso->refcnt);
  925. return dso;
  926. }
  927. void dso__put(struct dso *dso)
  928. {
  929. if (dso && atomic_dec_and_test(&dso->refcnt))
  930. dso__delete(dso);
  931. }
  932. void dso__set_build_id(struct dso *dso, void *build_id)
  933. {
  934. memcpy(dso->build_id, build_id, sizeof(dso->build_id));
  935. dso->has_build_id = 1;
  936. }
  937. bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  938. {
  939. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  940. }
  941. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  942. {
  943. char path[PATH_MAX];
  944. if (machine__is_default_guest(machine))
  945. return;
  946. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  947. if (sysfs__read_build_id(path, dso->build_id,
  948. sizeof(dso->build_id)) == 0)
  949. dso->has_build_id = true;
  950. }
  951. int dso__kernel_module_get_build_id(struct dso *dso,
  952. const char *root_dir)
  953. {
  954. char filename[PATH_MAX];
  955. /*
  956. * kernel module short names are of the form "[module]" and
  957. * we need just "module" here.
  958. */
  959. const char *name = dso->short_name + 1;
  960. snprintf(filename, sizeof(filename),
  961. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  962. root_dir, (int)strlen(name) - 1, name);
  963. if (sysfs__read_build_id(filename, dso->build_id,
  964. sizeof(dso->build_id)) == 0)
  965. dso->has_build_id = true;
  966. return 0;
  967. }
  968. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  969. {
  970. bool have_build_id = false;
  971. struct dso *pos;
  972. list_for_each_entry(pos, head, node) {
  973. if (with_hits && !pos->hit)
  974. continue;
  975. if (pos->has_build_id) {
  976. have_build_id = true;
  977. continue;
  978. }
  979. if (filename__read_build_id(pos->long_name, pos->build_id,
  980. sizeof(pos->build_id)) > 0) {
  981. have_build_id = true;
  982. pos->has_build_id = true;
  983. }
  984. }
  985. return have_build_id;
  986. }
  987. void __dsos__add(struct dsos *dsos, struct dso *dso)
  988. {
  989. list_add_tail(&dso->node, &dsos->head);
  990. __dso__findlink_by_longname(&dsos->root, dso, NULL);
  991. /*
  992. * It is now in the linked list, grab a reference, then garbage collect
  993. * this when needing memory, by looking at LRU dso instances in the
  994. * list with atomic_read(&dso->refcnt) == 1, i.e. no references
  995. * anywhere besides the one for the list, do, under a lock for the
  996. * list: remove it from the list, then a dso__put(), that probably will
  997. * be the last and will then call dso__delete(), end of life.
  998. *
  999. * That, or at the end of the 'struct machine' lifetime, when all
  1000. * 'struct dso' instances will be removed from the list, in
  1001. * dsos__exit(), if they have no other reference from some other data
  1002. * structure.
  1003. *
  1004. * E.g.: after processing a 'perf.data' file and storing references
  1005. * to objects instantiated while processing events, we will have
  1006. * references to the 'thread', 'map', 'dso' structs all from 'struct
  1007. * hist_entry' instances, but we may not need anything not referenced,
  1008. * so we might as well call machines__exit()/machines__delete() and
  1009. * garbage collect it.
  1010. */
  1011. dso__get(dso);
  1012. }
  1013. void dsos__add(struct dsos *dsos, struct dso *dso)
  1014. {
  1015. pthread_rwlock_wrlock(&dsos->lock);
  1016. __dsos__add(dsos, dso);
  1017. pthread_rwlock_unlock(&dsos->lock);
  1018. }
  1019. struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
  1020. {
  1021. struct dso *pos;
  1022. if (cmp_short) {
  1023. list_for_each_entry(pos, &dsos->head, node)
  1024. if (strcmp(pos->short_name, name) == 0)
  1025. return pos;
  1026. return NULL;
  1027. }
  1028. return __dso__find_by_longname(&dsos->root, name);
  1029. }
  1030. struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
  1031. {
  1032. struct dso *dso;
  1033. pthread_rwlock_rdlock(&dsos->lock);
  1034. dso = __dsos__find(dsos, name, cmp_short);
  1035. pthread_rwlock_unlock(&dsos->lock);
  1036. return dso;
  1037. }
  1038. struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
  1039. {
  1040. struct dso *dso = dso__new(name);
  1041. if (dso != NULL) {
  1042. __dsos__add(dsos, dso);
  1043. dso__set_basename(dso);
  1044. /* Put dso here because __dsos_add already got it */
  1045. dso__put(dso);
  1046. }
  1047. return dso;
  1048. }
  1049. struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
  1050. {
  1051. struct dso *dso = __dsos__find(dsos, name, false);
  1052. return dso ? dso : __dsos__addnew(dsos, name);
  1053. }
  1054. struct dso *dsos__findnew(struct dsos *dsos, const char *name)
  1055. {
  1056. struct dso *dso;
  1057. pthread_rwlock_wrlock(&dsos->lock);
  1058. dso = dso__get(__dsos__findnew(dsos, name));
  1059. pthread_rwlock_unlock(&dsos->lock);
  1060. return dso;
  1061. }
  1062. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1063. bool (skip)(struct dso *dso, int parm), int parm)
  1064. {
  1065. struct dso *pos;
  1066. size_t ret = 0;
  1067. list_for_each_entry(pos, head, node) {
  1068. if (skip && skip(pos, parm))
  1069. continue;
  1070. ret += dso__fprintf_buildid(pos, fp);
  1071. ret += fprintf(fp, " %s\n", pos->long_name);
  1072. }
  1073. return ret;
  1074. }
  1075. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1076. {
  1077. struct dso *pos;
  1078. size_t ret = 0;
  1079. list_for_each_entry(pos, head, node) {
  1080. int i;
  1081. for (i = 0; i < MAP__NR_TYPES; ++i)
  1082. ret += dso__fprintf(pos, i, fp);
  1083. }
  1084. return ret;
  1085. }
  1086. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  1087. {
  1088. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1089. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  1090. return fprintf(fp, "%s", sbuild_id);
  1091. }
  1092. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
  1093. {
  1094. struct rb_node *nd;
  1095. size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
  1096. if (dso->short_name != dso->long_name)
  1097. ret += fprintf(fp, "%s, ", dso->long_name);
  1098. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  1099. dso__loaded(dso, type) ? "" : "NOT ");
  1100. ret += dso__fprintf_buildid(dso, fp);
  1101. ret += fprintf(fp, ")\n");
  1102. for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
  1103. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  1104. ret += symbol__fprintf(pos, fp);
  1105. }
  1106. return ret;
  1107. }
  1108. enum dso_type dso__type(struct dso *dso, struct machine *machine)
  1109. {
  1110. int fd;
  1111. enum dso_type type = DSO__TYPE_UNKNOWN;
  1112. fd = dso__data_get_fd(dso, machine);
  1113. if (fd >= 0) {
  1114. type = dso__type_fd(fd);
  1115. dso__data_put_fd(dso);
  1116. }
  1117. return type;
  1118. }
  1119. int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
  1120. {
  1121. int idx, errnum = dso->load_errno;
  1122. /*
  1123. * This must have a same ordering as the enum dso_load_errno.
  1124. */
  1125. static const char *dso_load__error_str[] = {
  1126. "Internal tools/perf/ library error",
  1127. "Invalid ELF file",
  1128. "Can not read build id",
  1129. "Mismatching build id",
  1130. "Decompression failure",
  1131. };
  1132. BUG_ON(buflen == 0);
  1133. if (errnum >= 0) {
  1134. const char *err = strerror_r(errnum, buf, buflen);
  1135. if (err != buf)
  1136. scnprintf(buf, buflen, "%s", err);
  1137. return 0;
  1138. }
  1139. if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
  1140. return -1;
  1141. idx = errnum - __DSO_LOAD_ERRNO__START;
  1142. scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
  1143. return 0;
  1144. }