dso.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 "util.h"
  8. #include "debug.h"
  9. char dso__symtab_origin(const struct dso *dso)
  10. {
  11. static const char origin[] = {
  12. [DSO_BINARY_TYPE__KALLSYMS] = 'k',
  13. [DSO_BINARY_TYPE__VMLINUX] = 'v',
  14. [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
  15. [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
  16. [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
  17. [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
  18. [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
  19. [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
  20. [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
  21. [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
  22. [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
  23. [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
  24. [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
  25. [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
  26. };
  27. if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
  28. return '!';
  29. return origin[dso->symtab_type];
  30. }
  31. int dso__read_binary_type_filename(const struct dso *dso,
  32. enum dso_binary_type type,
  33. char *root_dir, char *filename, size_t size)
  34. {
  35. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  36. int ret = 0;
  37. size_t len;
  38. switch (type) {
  39. case DSO_BINARY_TYPE__DEBUGLINK: {
  40. char *debuglink;
  41. strncpy(filename, dso->long_name, size);
  42. debuglink = filename + dso->long_name_len;
  43. while (debuglink != filename && *debuglink != '/')
  44. debuglink--;
  45. if (*debuglink == '/')
  46. debuglink++;
  47. ret = filename__read_debuglink(dso->long_name, debuglink,
  48. size - (debuglink - filename));
  49. }
  50. break;
  51. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  52. /* skip the locally configured cache if a symfs is given */
  53. if (symbol_conf.symfs[0] ||
  54. (dso__build_id_filename(dso, filename, size) == NULL))
  55. ret = -1;
  56. break;
  57. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  58. len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
  59. snprintf(filename + len, size - len, "%s.debug", dso->long_name);
  60. break;
  61. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  62. len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
  63. snprintf(filename + len, size - len, "%s", dso->long_name);
  64. break;
  65. case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
  66. {
  67. const char *last_slash;
  68. size_t dir_size;
  69. last_slash = dso->long_name + dso->long_name_len;
  70. while (last_slash != dso->long_name && *last_slash != '/')
  71. last_slash--;
  72. len = __symbol__join_symfs(filename, size, "");
  73. dir_size = last_slash - dso->long_name + 2;
  74. if (dir_size > (size - len)) {
  75. ret = -1;
  76. break;
  77. }
  78. len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
  79. len += scnprintf(filename + len , size - len, ".debug%s",
  80. last_slash);
  81. break;
  82. }
  83. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  84. if (!dso->has_build_id) {
  85. ret = -1;
  86. break;
  87. }
  88. build_id__sprintf(dso->build_id,
  89. sizeof(dso->build_id),
  90. build_id_hex);
  91. len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
  92. snprintf(filename + len, size - len, "%.2s/%s.debug",
  93. build_id_hex, build_id_hex + 2);
  94. break;
  95. case DSO_BINARY_TYPE__VMLINUX:
  96. case DSO_BINARY_TYPE__GUEST_VMLINUX:
  97. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  98. __symbol__join_symfs(filename, size, dso->long_name);
  99. break;
  100. case DSO_BINARY_TYPE__GUEST_KMODULE:
  101. path__join3(filename, size, symbol_conf.symfs,
  102. root_dir, dso->long_name);
  103. break;
  104. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  105. __symbol__join_symfs(filename, size, dso->long_name);
  106. break;
  107. case DSO_BINARY_TYPE__KCORE:
  108. case DSO_BINARY_TYPE__GUEST_KCORE:
  109. snprintf(filename, size, "%s", dso->long_name);
  110. break;
  111. default:
  112. case DSO_BINARY_TYPE__KALLSYMS:
  113. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  114. case DSO_BINARY_TYPE__JAVA_JIT:
  115. case DSO_BINARY_TYPE__NOT_FOUND:
  116. ret = -1;
  117. break;
  118. }
  119. return ret;
  120. }
  121. /*
  122. * Global list of open DSOs and the counter.
  123. */
  124. static LIST_HEAD(dso__data_open);
  125. static long dso__data_open_cnt;
  126. static void dso__list_add(struct dso *dso)
  127. {
  128. list_add_tail(&dso->data.open_entry, &dso__data_open);
  129. dso__data_open_cnt++;
  130. }
  131. static void dso__list_del(struct dso *dso)
  132. {
  133. list_del(&dso->data.open_entry);
  134. WARN_ONCE(dso__data_open_cnt <= 0,
  135. "DSO data fd counter out of bounds.");
  136. dso__data_open_cnt--;
  137. }
  138. static void close_first_dso(void);
  139. static int do_open(char *name)
  140. {
  141. int fd;
  142. char sbuf[STRERR_BUFSIZE];
  143. do {
  144. fd = open(name, O_RDONLY);
  145. if (fd >= 0)
  146. return fd;
  147. pr_debug("dso open failed, mmap: %s\n",
  148. strerror_r(errno, sbuf, sizeof(sbuf)));
  149. if (!dso__data_open_cnt || errno != EMFILE)
  150. break;
  151. close_first_dso();
  152. } while (1);
  153. return -1;
  154. }
  155. static int __open_dso(struct dso *dso, struct machine *machine)
  156. {
  157. int fd;
  158. char *root_dir = (char *)"";
  159. char *name = malloc(PATH_MAX);
  160. if (!name)
  161. return -ENOMEM;
  162. if (machine)
  163. root_dir = machine->root_dir;
  164. if (dso__read_binary_type_filename(dso, dso->binary_type,
  165. root_dir, name, PATH_MAX)) {
  166. free(name);
  167. return -EINVAL;
  168. }
  169. fd = do_open(name);
  170. free(name);
  171. return fd;
  172. }
  173. static void check_data_close(void);
  174. /**
  175. * dso_close - Open DSO data file
  176. * @dso: dso object
  177. *
  178. * Open @dso's data file descriptor and updates
  179. * list/count of open DSO objects.
  180. */
  181. static int open_dso(struct dso *dso, struct machine *machine)
  182. {
  183. int fd = __open_dso(dso, machine);
  184. if (fd >= 0) {
  185. dso__list_add(dso);
  186. /*
  187. * Check if we crossed the allowed number
  188. * of opened DSOs and close one if needed.
  189. */
  190. check_data_close();
  191. }
  192. return fd;
  193. }
  194. static void close_data_fd(struct dso *dso)
  195. {
  196. if (dso->data.fd >= 0) {
  197. close(dso->data.fd);
  198. dso->data.fd = -1;
  199. dso->data.file_size = 0;
  200. dso__list_del(dso);
  201. }
  202. }
  203. /**
  204. * dso_close - Close DSO data file
  205. * @dso: dso object
  206. *
  207. * Close @dso's data file descriptor and updates
  208. * list/count of open DSO objects.
  209. */
  210. static void close_dso(struct dso *dso)
  211. {
  212. close_data_fd(dso);
  213. }
  214. static void close_first_dso(void)
  215. {
  216. struct dso *dso;
  217. dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
  218. close_dso(dso);
  219. }
  220. static rlim_t get_fd_limit(void)
  221. {
  222. struct rlimit l;
  223. rlim_t limit = 0;
  224. /* Allow half of the current open fd limit. */
  225. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  226. if (l.rlim_cur == RLIM_INFINITY)
  227. limit = l.rlim_cur;
  228. else
  229. limit = l.rlim_cur / 2;
  230. } else {
  231. pr_err("failed to get fd limit\n");
  232. limit = 1;
  233. }
  234. return limit;
  235. }
  236. static bool may_cache_fd(void)
  237. {
  238. static rlim_t limit;
  239. if (!limit)
  240. limit = get_fd_limit();
  241. if (limit == RLIM_INFINITY)
  242. return true;
  243. return limit > (rlim_t) dso__data_open_cnt;
  244. }
  245. /*
  246. * Check and close LRU dso if we crossed allowed limit
  247. * for opened dso file descriptors. The limit is half
  248. * of the RLIMIT_NOFILE files opened.
  249. */
  250. static void check_data_close(void)
  251. {
  252. bool cache_fd = may_cache_fd();
  253. if (!cache_fd)
  254. close_first_dso();
  255. }
  256. /**
  257. * dso__data_close - Close DSO data file
  258. * @dso: dso object
  259. *
  260. * External interface to close @dso's data file descriptor.
  261. */
  262. void dso__data_close(struct dso *dso)
  263. {
  264. close_dso(dso);
  265. }
  266. /**
  267. * dso__data_fd - Get dso's data file descriptor
  268. * @dso: dso object
  269. * @machine: machine object
  270. *
  271. * External interface to find dso's file, open it and
  272. * returns file descriptor.
  273. */
  274. int dso__data_fd(struct dso *dso, struct machine *machine)
  275. {
  276. enum dso_binary_type binary_type_data[] = {
  277. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  278. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  279. DSO_BINARY_TYPE__NOT_FOUND,
  280. };
  281. int i = 0;
  282. if (dso->data.status == DSO_DATA_STATUS_ERROR)
  283. return -1;
  284. if (dso->data.fd >= 0)
  285. goto out;
  286. if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
  287. dso->data.fd = open_dso(dso, machine);
  288. goto out;
  289. }
  290. do {
  291. dso->binary_type = binary_type_data[i++];
  292. dso->data.fd = open_dso(dso, machine);
  293. if (dso->data.fd >= 0)
  294. goto out;
  295. } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
  296. out:
  297. if (dso->data.fd >= 0)
  298. dso->data.status = DSO_DATA_STATUS_OK;
  299. else
  300. dso->data.status = DSO_DATA_STATUS_ERROR;
  301. return dso->data.fd;
  302. }
  303. bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
  304. {
  305. u32 flag = 1 << by;
  306. if (dso->data.status_seen & flag)
  307. return true;
  308. dso->data.status_seen |= flag;
  309. return false;
  310. }
  311. static void
  312. dso_cache__free(struct rb_root *root)
  313. {
  314. struct rb_node *next = rb_first(root);
  315. while (next) {
  316. struct dso_cache *cache;
  317. cache = rb_entry(next, struct dso_cache, rb_node);
  318. next = rb_next(&cache->rb_node);
  319. rb_erase(&cache->rb_node, root);
  320. free(cache);
  321. }
  322. }
  323. static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
  324. {
  325. struct rb_node * const *p = &root->rb_node;
  326. const struct rb_node *parent = NULL;
  327. struct dso_cache *cache;
  328. while (*p != NULL) {
  329. u64 end;
  330. parent = *p;
  331. cache = rb_entry(parent, struct dso_cache, rb_node);
  332. end = cache->offset + DSO__DATA_CACHE_SIZE;
  333. if (offset < cache->offset)
  334. p = &(*p)->rb_left;
  335. else if (offset >= end)
  336. p = &(*p)->rb_right;
  337. else
  338. return cache;
  339. }
  340. return NULL;
  341. }
  342. static void
  343. dso_cache__insert(struct rb_root *root, struct dso_cache *new)
  344. {
  345. struct rb_node **p = &root->rb_node;
  346. struct rb_node *parent = NULL;
  347. struct dso_cache *cache;
  348. u64 offset = new->offset;
  349. while (*p != NULL) {
  350. u64 end;
  351. parent = *p;
  352. cache = rb_entry(parent, struct dso_cache, rb_node);
  353. end = cache->offset + DSO__DATA_CACHE_SIZE;
  354. if (offset < cache->offset)
  355. p = &(*p)->rb_left;
  356. else if (offset >= end)
  357. p = &(*p)->rb_right;
  358. }
  359. rb_link_node(&new->rb_node, parent, p);
  360. rb_insert_color(&new->rb_node, root);
  361. }
  362. static ssize_t
  363. dso_cache__memcpy(struct dso_cache *cache, u64 offset,
  364. u8 *data, u64 size)
  365. {
  366. u64 cache_offset = offset - cache->offset;
  367. u64 cache_size = min(cache->size - cache_offset, size);
  368. memcpy(data, cache->data + cache_offset, cache_size);
  369. return cache_size;
  370. }
  371. static ssize_t
  372. dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
  373. {
  374. struct dso_cache *cache;
  375. ssize_t ret;
  376. do {
  377. u64 cache_offset;
  378. ret = -ENOMEM;
  379. cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
  380. if (!cache)
  381. break;
  382. cache_offset = offset & DSO__DATA_CACHE_MASK;
  383. ret = -EINVAL;
  384. if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET))
  385. break;
  386. ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE);
  387. if (ret <= 0)
  388. break;
  389. cache->offset = cache_offset;
  390. cache->size = ret;
  391. dso_cache__insert(&dso->data.cache, cache);
  392. ret = dso_cache__memcpy(cache, offset, data, size);
  393. } while (0);
  394. if (ret <= 0)
  395. free(cache);
  396. return ret;
  397. }
  398. static ssize_t dso_cache_read(struct dso *dso, u64 offset,
  399. u8 *data, ssize_t size)
  400. {
  401. struct dso_cache *cache;
  402. cache = dso_cache__find(&dso->data.cache, offset);
  403. if (cache)
  404. return dso_cache__memcpy(cache, offset, data, size);
  405. else
  406. return dso_cache__read(dso, offset, data, size);
  407. }
  408. /*
  409. * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
  410. * in the rb_tree. Any read to already cached data is served
  411. * by cached data.
  412. */
  413. static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
  414. {
  415. ssize_t r = 0;
  416. u8 *p = data;
  417. do {
  418. ssize_t ret;
  419. ret = dso_cache_read(dso, offset, p, size);
  420. if (ret < 0)
  421. return ret;
  422. /* Reached EOF, return what we have. */
  423. if (!ret)
  424. break;
  425. BUG_ON(ret > size);
  426. r += ret;
  427. p += ret;
  428. offset += ret;
  429. size -= ret;
  430. } while (size);
  431. return r;
  432. }
  433. static int data_file_size(struct dso *dso)
  434. {
  435. struct stat st;
  436. char sbuf[STRERR_BUFSIZE];
  437. if (!dso->data.file_size) {
  438. if (fstat(dso->data.fd, &st)) {
  439. pr_err("dso mmap failed, fstat: %s\n",
  440. strerror_r(errno, sbuf, sizeof(sbuf)));
  441. return -1;
  442. }
  443. dso->data.file_size = st.st_size;
  444. }
  445. return 0;
  446. }
  447. /**
  448. * dso__data_size - Return dso data size
  449. * @dso: dso object
  450. * @machine: machine object
  451. *
  452. * Return: dso data size
  453. */
  454. off_t dso__data_size(struct dso *dso, struct machine *machine)
  455. {
  456. int fd;
  457. fd = dso__data_fd(dso, machine);
  458. if (fd < 0)
  459. return fd;
  460. if (data_file_size(dso))
  461. return -1;
  462. /* For now just estimate dso data size is close to file size */
  463. return dso->data.file_size;
  464. }
  465. static ssize_t data_read_offset(struct dso *dso, u64 offset,
  466. u8 *data, ssize_t size)
  467. {
  468. if (data_file_size(dso))
  469. return -1;
  470. /* Check the offset sanity. */
  471. if (offset > dso->data.file_size)
  472. return -1;
  473. if (offset + size < offset)
  474. return -1;
  475. return cached_read(dso, offset, data, size);
  476. }
  477. /**
  478. * dso__data_read_offset - Read data from dso file offset
  479. * @dso: dso object
  480. * @machine: machine object
  481. * @offset: file offset
  482. * @data: buffer to store data
  483. * @size: size of the @data buffer
  484. *
  485. * External interface to read data from dso file offset. Open
  486. * dso data file and use cached_read to get the data.
  487. */
  488. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  489. u64 offset, u8 *data, ssize_t size)
  490. {
  491. if (dso__data_fd(dso, machine) < 0)
  492. return -1;
  493. return data_read_offset(dso, offset, data, size);
  494. }
  495. /**
  496. * dso__data_read_addr - Read data from dso address
  497. * @dso: dso object
  498. * @machine: machine object
  499. * @add: virtual memory address
  500. * @data: buffer to store data
  501. * @size: size of the @data buffer
  502. *
  503. * External interface to read data from dso address.
  504. */
  505. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  506. struct machine *machine, u64 addr,
  507. u8 *data, ssize_t size)
  508. {
  509. u64 offset = map->map_ip(map, addr);
  510. return dso__data_read_offset(dso, machine, offset, data, size);
  511. }
  512. struct map *dso__new_map(const char *name)
  513. {
  514. struct map *map = NULL;
  515. struct dso *dso = dso__new(name);
  516. if (dso)
  517. map = map__new2(0, dso, MAP__FUNCTION);
  518. return map;
  519. }
  520. struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
  521. const char *short_name, int dso_type)
  522. {
  523. /*
  524. * The kernel dso could be created by build_id processing.
  525. */
  526. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
  527. /*
  528. * We need to run this in all cases, since during the build_id
  529. * processing we had no idea this was the kernel dso.
  530. */
  531. if (dso != NULL) {
  532. dso__set_short_name(dso, short_name, false);
  533. dso->kernel = dso_type;
  534. }
  535. return dso;
  536. }
  537. /*
  538. * Find a matching entry and/or link current entry to RB tree.
  539. * Either one of the dso or name parameter must be non-NULL or the
  540. * function will not work.
  541. */
  542. static struct dso *dso__findlink_by_longname(struct rb_root *root,
  543. struct dso *dso, const char *name)
  544. {
  545. struct rb_node **p = &root->rb_node;
  546. struct rb_node *parent = NULL;
  547. if (!name)
  548. name = dso->long_name;
  549. /*
  550. * Find node with the matching name
  551. */
  552. while (*p) {
  553. struct dso *this = rb_entry(*p, struct dso, rb_node);
  554. int rc = strcmp(name, this->long_name);
  555. parent = *p;
  556. if (rc == 0) {
  557. /*
  558. * In case the new DSO is a duplicate of an existing
  559. * one, print an one-time warning & put the new entry
  560. * at the end of the list of duplicates.
  561. */
  562. if (!dso || (dso == this))
  563. return this; /* Find matching dso */
  564. /*
  565. * The core kernel DSOs may have duplicated long name.
  566. * In this case, the short name should be different.
  567. * Comparing the short names to differentiate the DSOs.
  568. */
  569. rc = strcmp(dso->short_name, this->short_name);
  570. if (rc == 0) {
  571. pr_err("Duplicated dso name: %s\n", name);
  572. return NULL;
  573. }
  574. }
  575. if (rc < 0)
  576. p = &parent->rb_left;
  577. else
  578. p = &parent->rb_right;
  579. }
  580. if (dso) {
  581. /* Add new node and rebalance tree */
  582. rb_link_node(&dso->rb_node, parent, p);
  583. rb_insert_color(&dso->rb_node, root);
  584. }
  585. return NULL;
  586. }
  587. static inline struct dso *
  588. dso__find_by_longname(const struct rb_root *root, const char *name)
  589. {
  590. return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
  591. }
  592. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
  593. {
  594. if (name == NULL)
  595. return;
  596. if (dso->long_name_allocated)
  597. free((char *)dso->long_name);
  598. dso->long_name = name;
  599. dso->long_name_len = strlen(name);
  600. dso->long_name_allocated = name_allocated;
  601. }
  602. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
  603. {
  604. if (name == NULL)
  605. return;
  606. if (dso->short_name_allocated)
  607. free((char *)dso->short_name);
  608. dso->short_name = name;
  609. dso->short_name_len = strlen(name);
  610. dso->short_name_allocated = name_allocated;
  611. }
  612. static void dso__set_basename(struct dso *dso)
  613. {
  614. /*
  615. * basename() may modify path buffer, so we must pass
  616. * a copy.
  617. */
  618. char *base, *lname = strdup(dso->long_name);
  619. if (!lname)
  620. return;
  621. /*
  622. * basename() may return a pointer to internal
  623. * storage which is reused in subsequent calls
  624. * so copy the result.
  625. */
  626. base = strdup(basename(lname));
  627. free(lname);
  628. if (!base)
  629. return;
  630. dso__set_short_name(dso, base, true);
  631. }
  632. int dso__name_len(const struct dso *dso)
  633. {
  634. if (!dso)
  635. return strlen("[unknown]");
  636. if (verbose)
  637. return dso->long_name_len;
  638. return dso->short_name_len;
  639. }
  640. bool dso__loaded(const struct dso *dso, enum map_type type)
  641. {
  642. return dso->loaded & (1 << type);
  643. }
  644. bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
  645. {
  646. return dso->sorted_by_name & (1 << type);
  647. }
  648. void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
  649. {
  650. dso->sorted_by_name |= (1 << type);
  651. }
  652. struct dso *dso__new(const char *name)
  653. {
  654. struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
  655. if (dso != NULL) {
  656. int i;
  657. strcpy(dso->name, name);
  658. dso__set_long_name(dso, dso->name, false);
  659. dso__set_short_name(dso, dso->name, false);
  660. for (i = 0; i < MAP__NR_TYPES; ++i)
  661. dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
  662. dso->data.cache = RB_ROOT;
  663. dso->data.fd = -1;
  664. dso->data.status = DSO_DATA_STATUS_UNKNOWN;
  665. dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  666. dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
  667. dso->is_64_bit = (sizeof(void *) == 8);
  668. dso->loaded = 0;
  669. dso->rel = 0;
  670. dso->sorted_by_name = 0;
  671. dso->has_build_id = 0;
  672. dso->has_srcline = 1;
  673. dso->a2l_fails = 1;
  674. dso->kernel = DSO_TYPE_USER;
  675. dso->needs_swap = DSO_SWAP__UNSET;
  676. RB_CLEAR_NODE(&dso->rb_node);
  677. INIT_LIST_HEAD(&dso->node);
  678. INIT_LIST_HEAD(&dso->data.open_entry);
  679. }
  680. return dso;
  681. }
  682. void dso__delete(struct dso *dso)
  683. {
  684. int i;
  685. if (!RB_EMPTY_NODE(&dso->rb_node))
  686. pr_err("DSO %s is still in rbtree when being deleted!\n",
  687. dso->long_name);
  688. for (i = 0; i < MAP__NR_TYPES; ++i)
  689. symbols__delete(&dso->symbols[i]);
  690. if (dso->short_name_allocated) {
  691. zfree((char **)&dso->short_name);
  692. dso->short_name_allocated = false;
  693. }
  694. if (dso->long_name_allocated) {
  695. zfree((char **)&dso->long_name);
  696. dso->long_name_allocated = false;
  697. }
  698. dso__data_close(dso);
  699. dso_cache__free(&dso->data.cache);
  700. dso__free_a2l(dso);
  701. zfree(&dso->symsrc_filename);
  702. free(dso);
  703. }
  704. void dso__set_build_id(struct dso *dso, void *build_id)
  705. {
  706. memcpy(dso->build_id, build_id, sizeof(dso->build_id));
  707. dso->has_build_id = 1;
  708. }
  709. bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  710. {
  711. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  712. }
  713. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  714. {
  715. char path[PATH_MAX];
  716. if (machine__is_default_guest(machine))
  717. return;
  718. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  719. if (sysfs__read_build_id(path, dso->build_id,
  720. sizeof(dso->build_id)) == 0)
  721. dso->has_build_id = true;
  722. }
  723. int dso__kernel_module_get_build_id(struct dso *dso,
  724. const char *root_dir)
  725. {
  726. char filename[PATH_MAX];
  727. /*
  728. * kernel module short names are of the form "[module]" and
  729. * we need just "module" here.
  730. */
  731. const char *name = dso->short_name + 1;
  732. snprintf(filename, sizeof(filename),
  733. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  734. root_dir, (int)strlen(name) - 1, name);
  735. if (sysfs__read_build_id(filename, dso->build_id,
  736. sizeof(dso->build_id)) == 0)
  737. dso->has_build_id = true;
  738. return 0;
  739. }
  740. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  741. {
  742. bool have_build_id = false;
  743. struct dso *pos;
  744. list_for_each_entry(pos, head, node) {
  745. if (with_hits && !pos->hit)
  746. continue;
  747. if (pos->has_build_id) {
  748. have_build_id = true;
  749. continue;
  750. }
  751. if (filename__read_build_id(pos->long_name, pos->build_id,
  752. sizeof(pos->build_id)) > 0) {
  753. have_build_id = true;
  754. pos->has_build_id = true;
  755. }
  756. }
  757. return have_build_id;
  758. }
  759. void dsos__add(struct dsos *dsos, struct dso *dso)
  760. {
  761. list_add_tail(&dso->node, &dsos->head);
  762. dso__findlink_by_longname(&dsos->root, dso, NULL);
  763. }
  764. struct dso *dsos__find(const struct dsos *dsos, const char *name,
  765. bool cmp_short)
  766. {
  767. struct dso *pos;
  768. if (cmp_short) {
  769. list_for_each_entry(pos, &dsos->head, node)
  770. if (strcmp(pos->short_name, name) == 0)
  771. return pos;
  772. return NULL;
  773. }
  774. return dso__find_by_longname(&dsos->root, name);
  775. }
  776. struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
  777. {
  778. struct dso *dso = dsos__find(dsos, name, false);
  779. if (!dso) {
  780. dso = dso__new(name);
  781. if (dso != NULL) {
  782. dsos__add(dsos, dso);
  783. dso__set_basename(dso);
  784. }
  785. }
  786. return dso;
  787. }
  788. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  789. bool (skip)(struct dso *dso, int parm), int parm)
  790. {
  791. struct dso *pos;
  792. size_t ret = 0;
  793. list_for_each_entry(pos, head, node) {
  794. if (skip && skip(pos, parm))
  795. continue;
  796. ret += dso__fprintf_buildid(pos, fp);
  797. ret += fprintf(fp, " %s\n", pos->long_name);
  798. }
  799. return ret;
  800. }
  801. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  802. {
  803. struct dso *pos;
  804. size_t ret = 0;
  805. list_for_each_entry(pos, head, node) {
  806. int i;
  807. for (i = 0; i < MAP__NR_TYPES; ++i)
  808. ret += dso__fprintf(pos, i, fp);
  809. }
  810. return ret;
  811. }
  812. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  813. {
  814. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  815. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  816. return fprintf(fp, "%s", sbuild_id);
  817. }
  818. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
  819. {
  820. struct rb_node *nd;
  821. size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
  822. if (dso->short_name != dso->long_name)
  823. ret += fprintf(fp, "%s, ", dso->long_name);
  824. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  825. dso__loaded(dso, type) ? "" : "NOT ");
  826. ret += dso__fprintf_buildid(dso, fp);
  827. ret += fprintf(fp, ")\n");
  828. for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
  829. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  830. ret += symbol__fprintf(pos, fp);
  831. }
  832. return ret;
  833. }
  834. enum dso_type dso__type(struct dso *dso, struct machine *machine)
  835. {
  836. int fd;
  837. fd = dso__data_fd(dso, machine);
  838. if (fd < 0)
  839. return DSO__TYPE_UNKNOWN;
  840. return dso__type_fd(fd);
  841. }