dso.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. switch (type) {
  38. case DSO_BINARY_TYPE__DEBUGLINK: {
  39. char *debuglink;
  40. strncpy(filename, dso->long_name, size);
  41. debuglink = filename + dso->long_name_len;
  42. while (debuglink != filename && *debuglink != '/')
  43. debuglink--;
  44. if (*debuglink == '/')
  45. debuglink++;
  46. ret = filename__read_debuglink(dso->long_name, debuglink,
  47. size - (debuglink - filename));
  48. }
  49. break;
  50. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  51. /* skip the locally configured cache if a symfs is given */
  52. if (symbol_conf.symfs[0] ||
  53. (dso__build_id_filename(dso, filename, size) == NULL))
  54. ret = -1;
  55. break;
  56. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  57. snprintf(filename, size, "%s/usr/lib/debug%s.debug",
  58. symbol_conf.symfs, dso->long_name);
  59. break;
  60. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  61. snprintf(filename, size, "%s/usr/lib/debug%s",
  62. symbol_conf.symfs, dso->long_name);
  63. break;
  64. case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
  65. {
  66. const char *last_slash;
  67. size_t len;
  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 = scnprintf(filename, size, "%s", symbol_conf.symfs);
  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. snprintf(filename, size,
  92. "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
  93. symbol_conf.symfs, 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. snprintf(filename, size, "%s%s",
  99. symbol_conf.symfs, dso->long_name);
  100. break;
  101. case DSO_BINARY_TYPE__GUEST_KMODULE:
  102. snprintf(filename, size, "%s%s%s", symbol_conf.symfs,
  103. root_dir, dso->long_name);
  104. break;
  105. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  106. snprintf(filename, size, "%s%s", symbol_conf.symfs,
  107. dso->long_name);
  108. break;
  109. case DSO_BINARY_TYPE__KCORE:
  110. case DSO_BINARY_TYPE__GUEST_KCORE:
  111. snprintf(filename, size, "%s", dso->long_name);
  112. break;
  113. default:
  114. case DSO_BINARY_TYPE__KALLSYMS:
  115. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  116. case DSO_BINARY_TYPE__JAVA_JIT:
  117. case DSO_BINARY_TYPE__NOT_FOUND:
  118. ret = -1;
  119. break;
  120. }
  121. return ret;
  122. }
  123. /*
  124. * Global list of open DSOs and the counter.
  125. */
  126. static LIST_HEAD(dso__data_open);
  127. static long dso__data_open_cnt;
  128. static void dso__list_add(struct dso *dso)
  129. {
  130. list_add_tail(&dso->data.open_entry, &dso__data_open);
  131. dso__data_open_cnt++;
  132. }
  133. static void dso__list_del(struct dso *dso)
  134. {
  135. list_del(&dso->data.open_entry);
  136. WARN_ONCE(dso__data_open_cnt <= 0,
  137. "DSO data fd counter out of bounds.");
  138. dso__data_open_cnt--;
  139. }
  140. static void close_first_dso(void);
  141. static int do_open(char *name)
  142. {
  143. int fd;
  144. do {
  145. fd = open(name, O_RDONLY);
  146. if (fd >= 0)
  147. return fd;
  148. pr_debug("dso open failed, mmap: %s\n", strerror(errno));
  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. if (!dso->data.file_size) {
  437. if (fstat(dso->data.fd, &st)) {
  438. pr_err("dso mmap failed, fstat: %s\n", strerror(errno));
  439. return -1;
  440. }
  441. dso->data.file_size = st.st_size;
  442. }
  443. return 0;
  444. }
  445. /**
  446. * dso__data_size - Return dso data size
  447. * @dso: dso object
  448. * @machine: machine object
  449. *
  450. * Return: dso data size
  451. */
  452. off_t dso__data_size(struct dso *dso, struct machine *machine)
  453. {
  454. int fd;
  455. fd = dso__data_fd(dso, machine);
  456. if (fd < 0)
  457. return fd;
  458. if (data_file_size(dso))
  459. return -1;
  460. /* For now just estimate dso data size is close to file size */
  461. return dso->data.file_size;
  462. }
  463. static ssize_t data_read_offset(struct dso *dso, u64 offset,
  464. u8 *data, ssize_t size)
  465. {
  466. if (data_file_size(dso))
  467. return -1;
  468. /* Check the offset sanity. */
  469. if (offset > dso->data.file_size)
  470. return -1;
  471. if (offset + size < offset)
  472. return -1;
  473. return cached_read(dso, offset, data, size);
  474. }
  475. /**
  476. * dso__data_read_offset - Read data from dso file offset
  477. * @dso: dso object
  478. * @machine: machine object
  479. * @offset: file offset
  480. * @data: buffer to store data
  481. * @size: size of the @data buffer
  482. *
  483. * External interface to read data from dso file offset. Open
  484. * dso data file and use cached_read to get the data.
  485. */
  486. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  487. u64 offset, u8 *data, ssize_t size)
  488. {
  489. if (dso__data_fd(dso, machine) < 0)
  490. return -1;
  491. return data_read_offset(dso, offset, data, size);
  492. }
  493. /**
  494. * dso__data_read_addr - Read data from dso address
  495. * @dso: dso object
  496. * @machine: machine object
  497. * @add: virtual memory address
  498. * @data: buffer to store data
  499. * @size: size of the @data buffer
  500. *
  501. * External interface to read data from dso address.
  502. */
  503. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  504. struct machine *machine, u64 addr,
  505. u8 *data, ssize_t size)
  506. {
  507. u64 offset = map->map_ip(map, addr);
  508. return dso__data_read_offset(dso, machine, offset, data, size);
  509. }
  510. struct map *dso__new_map(const char *name)
  511. {
  512. struct map *map = NULL;
  513. struct dso *dso = dso__new(name);
  514. if (dso)
  515. map = map__new2(0, dso, MAP__FUNCTION);
  516. return map;
  517. }
  518. struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
  519. const char *short_name, int dso_type)
  520. {
  521. /*
  522. * The kernel dso could be created by build_id processing.
  523. */
  524. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
  525. /*
  526. * We need to run this in all cases, since during the build_id
  527. * processing we had no idea this was the kernel dso.
  528. */
  529. if (dso != NULL) {
  530. dso__set_short_name(dso, short_name, false);
  531. dso->kernel = dso_type;
  532. }
  533. return dso;
  534. }
  535. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
  536. {
  537. if (name == NULL)
  538. return;
  539. if (dso->long_name_allocated)
  540. free((char *)dso->long_name);
  541. dso->long_name = name;
  542. dso->long_name_len = strlen(name);
  543. dso->long_name_allocated = name_allocated;
  544. }
  545. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
  546. {
  547. if (name == NULL)
  548. return;
  549. if (dso->short_name_allocated)
  550. free((char *)dso->short_name);
  551. dso->short_name = name;
  552. dso->short_name_len = strlen(name);
  553. dso->short_name_allocated = name_allocated;
  554. }
  555. static void dso__set_basename(struct dso *dso)
  556. {
  557. /*
  558. * basename() may modify path buffer, so we must pass
  559. * a copy.
  560. */
  561. char *base, *lname = strdup(dso->long_name);
  562. if (!lname)
  563. return;
  564. /*
  565. * basename() may return a pointer to internal
  566. * storage which is reused in subsequent calls
  567. * so copy the result.
  568. */
  569. base = strdup(basename(lname));
  570. free(lname);
  571. if (!base)
  572. return;
  573. dso__set_short_name(dso, base, true);
  574. }
  575. int dso__name_len(const struct dso *dso)
  576. {
  577. if (!dso)
  578. return strlen("[unknown]");
  579. if (verbose)
  580. return dso->long_name_len;
  581. return dso->short_name_len;
  582. }
  583. bool dso__loaded(const struct dso *dso, enum map_type type)
  584. {
  585. return dso->loaded & (1 << type);
  586. }
  587. bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
  588. {
  589. return dso->sorted_by_name & (1 << type);
  590. }
  591. void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
  592. {
  593. dso->sorted_by_name |= (1 << type);
  594. }
  595. struct dso *dso__new(const char *name)
  596. {
  597. struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
  598. if (dso != NULL) {
  599. int i;
  600. strcpy(dso->name, name);
  601. dso__set_long_name(dso, dso->name, false);
  602. dso__set_short_name(dso, dso->name, false);
  603. for (i = 0; i < MAP__NR_TYPES; ++i)
  604. dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
  605. dso->data.cache = RB_ROOT;
  606. dso->data.fd = -1;
  607. dso->data.status = DSO_DATA_STATUS_UNKNOWN;
  608. dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  609. dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
  610. dso->is_64_bit = (sizeof(void *) == 8);
  611. dso->loaded = 0;
  612. dso->rel = 0;
  613. dso->sorted_by_name = 0;
  614. dso->has_build_id = 0;
  615. dso->has_srcline = 1;
  616. dso->a2l_fails = 1;
  617. dso->kernel = DSO_TYPE_USER;
  618. dso->needs_swap = DSO_SWAP__UNSET;
  619. INIT_LIST_HEAD(&dso->node);
  620. INIT_LIST_HEAD(&dso->data.open_entry);
  621. }
  622. return dso;
  623. }
  624. void dso__delete(struct dso *dso)
  625. {
  626. int i;
  627. for (i = 0; i < MAP__NR_TYPES; ++i)
  628. symbols__delete(&dso->symbols[i]);
  629. if (dso->short_name_allocated) {
  630. zfree((char **)&dso->short_name);
  631. dso->short_name_allocated = false;
  632. }
  633. if (dso->long_name_allocated) {
  634. zfree((char **)&dso->long_name);
  635. dso->long_name_allocated = false;
  636. }
  637. dso__data_close(dso);
  638. dso_cache__free(&dso->data.cache);
  639. dso__free_a2l(dso);
  640. zfree(&dso->symsrc_filename);
  641. free(dso);
  642. }
  643. void dso__set_build_id(struct dso *dso, void *build_id)
  644. {
  645. memcpy(dso->build_id, build_id, sizeof(dso->build_id));
  646. dso->has_build_id = 1;
  647. }
  648. bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  649. {
  650. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  651. }
  652. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  653. {
  654. char path[PATH_MAX];
  655. if (machine__is_default_guest(machine))
  656. return;
  657. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  658. if (sysfs__read_build_id(path, dso->build_id,
  659. sizeof(dso->build_id)) == 0)
  660. dso->has_build_id = true;
  661. }
  662. int dso__kernel_module_get_build_id(struct dso *dso,
  663. const char *root_dir)
  664. {
  665. char filename[PATH_MAX];
  666. /*
  667. * kernel module short names are of the form "[module]" and
  668. * we need just "module" here.
  669. */
  670. const char *name = dso->short_name + 1;
  671. snprintf(filename, sizeof(filename),
  672. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  673. root_dir, (int)strlen(name) - 1, name);
  674. if (sysfs__read_build_id(filename, dso->build_id,
  675. sizeof(dso->build_id)) == 0)
  676. dso->has_build_id = true;
  677. return 0;
  678. }
  679. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  680. {
  681. bool have_build_id = false;
  682. struct dso *pos;
  683. list_for_each_entry(pos, head, node) {
  684. if (with_hits && !pos->hit)
  685. continue;
  686. if (pos->has_build_id) {
  687. have_build_id = true;
  688. continue;
  689. }
  690. if (filename__read_build_id(pos->long_name, pos->build_id,
  691. sizeof(pos->build_id)) > 0) {
  692. have_build_id = true;
  693. pos->has_build_id = true;
  694. }
  695. }
  696. return have_build_id;
  697. }
  698. void dsos__add(struct list_head *head, struct dso *dso)
  699. {
  700. list_add_tail(&dso->node, head);
  701. }
  702. struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short)
  703. {
  704. struct dso *pos;
  705. if (cmp_short) {
  706. list_for_each_entry(pos, head, node)
  707. if (strcmp(pos->short_name, name) == 0)
  708. return pos;
  709. return NULL;
  710. }
  711. list_for_each_entry(pos, head, node)
  712. if (strcmp(pos->long_name, name) == 0)
  713. return pos;
  714. return NULL;
  715. }
  716. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  717. {
  718. struct dso *dso = dsos__find(head, name, false);
  719. if (!dso) {
  720. dso = dso__new(name);
  721. if (dso != NULL) {
  722. dsos__add(head, dso);
  723. dso__set_basename(dso);
  724. }
  725. }
  726. return dso;
  727. }
  728. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  729. bool (skip)(struct dso *dso, int parm), int parm)
  730. {
  731. struct dso *pos;
  732. size_t ret = 0;
  733. list_for_each_entry(pos, head, node) {
  734. if (skip && skip(pos, parm))
  735. continue;
  736. ret += dso__fprintf_buildid(pos, fp);
  737. ret += fprintf(fp, " %s\n", pos->long_name);
  738. }
  739. return ret;
  740. }
  741. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  742. {
  743. struct dso *pos;
  744. size_t ret = 0;
  745. list_for_each_entry(pos, head, node) {
  746. int i;
  747. for (i = 0; i < MAP__NR_TYPES; ++i)
  748. ret += dso__fprintf(pos, i, fp);
  749. }
  750. return ret;
  751. }
  752. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  753. {
  754. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  755. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  756. return fprintf(fp, "%s", sbuild_id);
  757. }
  758. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
  759. {
  760. struct rb_node *nd;
  761. size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
  762. if (dso->short_name != dso->long_name)
  763. ret += fprintf(fp, "%s, ", dso->long_name);
  764. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  765. dso__loaded(dso, type) ? "" : "NOT ");
  766. ret += dso__fprintf_buildid(dso, fp);
  767. ret += fprintf(fp, ")\n");
  768. for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
  769. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  770. ret += symbol__fprintf(pos, fp);
  771. }
  772. return ret;
  773. }
  774. enum dso_type dso__type(struct dso *dso, struct machine *machine)
  775. {
  776. int fd;
  777. fd = dso__data_fd(dso, machine);
  778. if (fd < 0)
  779. return DSO__TYPE_UNKNOWN;
  780. return dso__type_fd(fd);
  781. }