dso.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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.fd >= 0)
  283. return dso->data.fd;
  284. if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
  285. dso->data.fd = open_dso(dso, machine);
  286. return dso->data.fd;
  287. }
  288. do {
  289. int fd;
  290. dso->binary_type = binary_type_data[i++];
  291. fd = open_dso(dso, machine);
  292. if (fd >= 0)
  293. return dso->data.fd = fd;
  294. } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
  295. return -EINVAL;
  296. }
  297. static void
  298. dso_cache__free(struct rb_root *root)
  299. {
  300. struct rb_node *next = rb_first(root);
  301. while (next) {
  302. struct dso_cache *cache;
  303. cache = rb_entry(next, struct dso_cache, rb_node);
  304. next = rb_next(&cache->rb_node);
  305. rb_erase(&cache->rb_node, root);
  306. free(cache);
  307. }
  308. }
  309. static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
  310. {
  311. struct rb_node * const *p = &root->rb_node;
  312. const struct rb_node *parent = NULL;
  313. struct dso_cache *cache;
  314. while (*p != NULL) {
  315. u64 end;
  316. parent = *p;
  317. cache = rb_entry(parent, struct dso_cache, rb_node);
  318. end = cache->offset + DSO__DATA_CACHE_SIZE;
  319. if (offset < cache->offset)
  320. p = &(*p)->rb_left;
  321. else if (offset >= end)
  322. p = &(*p)->rb_right;
  323. else
  324. return cache;
  325. }
  326. return NULL;
  327. }
  328. static void
  329. dso_cache__insert(struct rb_root *root, struct dso_cache *new)
  330. {
  331. struct rb_node **p = &root->rb_node;
  332. struct rb_node *parent = NULL;
  333. struct dso_cache *cache;
  334. u64 offset = new->offset;
  335. while (*p != NULL) {
  336. u64 end;
  337. parent = *p;
  338. cache = rb_entry(parent, struct dso_cache, rb_node);
  339. end = cache->offset + DSO__DATA_CACHE_SIZE;
  340. if (offset < cache->offset)
  341. p = &(*p)->rb_left;
  342. else if (offset >= end)
  343. p = &(*p)->rb_right;
  344. }
  345. rb_link_node(&new->rb_node, parent, p);
  346. rb_insert_color(&new->rb_node, root);
  347. }
  348. static ssize_t
  349. dso_cache__memcpy(struct dso_cache *cache, u64 offset,
  350. u8 *data, u64 size)
  351. {
  352. u64 cache_offset = offset - cache->offset;
  353. u64 cache_size = min(cache->size - cache_offset, size);
  354. memcpy(data, cache->data + cache_offset, cache_size);
  355. return cache_size;
  356. }
  357. static ssize_t
  358. dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
  359. {
  360. struct dso_cache *cache;
  361. ssize_t ret;
  362. do {
  363. u64 cache_offset;
  364. ret = -ENOMEM;
  365. cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
  366. if (!cache)
  367. break;
  368. cache_offset = offset & DSO__DATA_CACHE_MASK;
  369. ret = -EINVAL;
  370. if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET))
  371. break;
  372. ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE);
  373. if (ret <= 0)
  374. break;
  375. cache->offset = cache_offset;
  376. cache->size = ret;
  377. dso_cache__insert(&dso->data.cache, cache);
  378. ret = dso_cache__memcpy(cache, offset, data, size);
  379. } while (0);
  380. if (ret <= 0)
  381. free(cache);
  382. return ret;
  383. }
  384. static ssize_t dso_cache_read(struct dso *dso, u64 offset,
  385. u8 *data, ssize_t size)
  386. {
  387. struct dso_cache *cache;
  388. cache = dso_cache__find(&dso->data.cache, offset);
  389. if (cache)
  390. return dso_cache__memcpy(cache, offset, data, size);
  391. else
  392. return dso_cache__read(dso, offset, data, size);
  393. }
  394. /*
  395. * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
  396. * in the rb_tree. Any read to already cached data is served
  397. * by cached data.
  398. */
  399. static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
  400. {
  401. ssize_t r = 0;
  402. u8 *p = data;
  403. do {
  404. ssize_t ret;
  405. ret = dso_cache_read(dso, offset, p, size);
  406. if (ret < 0)
  407. return ret;
  408. /* Reached EOF, return what we have. */
  409. if (!ret)
  410. break;
  411. BUG_ON(ret > size);
  412. r += ret;
  413. p += ret;
  414. offset += ret;
  415. size -= ret;
  416. } while (size);
  417. return r;
  418. }
  419. static int data_file_size(struct dso *dso)
  420. {
  421. struct stat st;
  422. if (!dso->data.file_size) {
  423. if (fstat(dso->data.fd, &st)) {
  424. pr_err("dso mmap failed, fstat: %s\n", strerror(errno));
  425. return -1;
  426. }
  427. dso->data.file_size = st.st_size;
  428. }
  429. return 0;
  430. }
  431. static ssize_t data_read_offset(struct dso *dso, u64 offset,
  432. u8 *data, ssize_t size)
  433. {
  434. if (data_file_size(dso))
  435. return -1;
  436. /* Check the offset sanity. */
  437. if (offset > dso->data.file_size)
  438. return -1;
  439. if (offset + size < offset)
  440. return -1;
  441. return cached_read(dso, offset, data, size);
  442. }
  443. /**
  444. * dso__data_read_offset - Read data from dso file offset
  445. * @dso: dso object
  446. * @machine: machine object
  447. * @offset: file offset
  448. * @data: buffer to store data
  449. * @size: size of the @data buffer
  450. *
  451. * External interface to read data from dso file offset. Open
  452. * dso data file and use cached_read to get the data.
  453. */
  454. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  455. u64 offset, u8 *data, ssize_t size)
  456. {
  457. if (dso__data_fd(dso, machine) < 0)
  458. return -1;
  459. return data_read_offset(dso, offset, data, size);
  460. }
  461. /**
  462. * dso__data_read_addr - Read data from dso address
  463. * @dso: dso object
  464. * @machine: machine object
  465. * @add: virtual memory address
  466. * @data: buffer to store data
  467. * @size: size of the @data buffer
  468. *
  469. * External interface to read data from dso address.
  470. */
  471. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  472. struct machine *machine, u64 addr,
  473. u8 *data, ssize_t size)
  474. {
  475. u64 offset = map->map_ip(map, addr);
  476. return dso__data_read_offset(dso, machine, offset, data, size);
  477. }
  478. struct map *dso__new_map(const char *name)
  479. {
  480. struct map *map = NULL;
  481. struct dso *dso = dso__new(name);
  482. if (dso)
  483. map = map__new2(0, dso, MAP__FUNCTION);
  484. return map;
  485. }
  486. struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
  487. const char *short_name, int dso_type)
  488. {
  489. /*
  490. * The kernel dso could be created by build_id processing.
  491. */
  492. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
  493. /*
  494. * We need to run this in all cases, since during the build_id
  495. * processing we had no idea this was the kernel dso.
  496. */
  497. if (dso != NULL) {
  498. dso__set_short_name(dso, short_name, false);
  499. dso->kernel = dso_type;
  500. }
  501. return dso;
  502. }
  503. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
  504. {
  505. if (name == NULL)
  506. return;
  507. if (dso->long_name_allocated)
  508. free((char *)dso->long_name);
  509. dso->long_name = name;
  510. dso->long_name_len = strlen(name);
  511. dso->long_name_allocated = name_allocated;
  512. }
  513. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
  514. {
  515. if (name == NULL)
  516. return;
  517. if (dso->short_name_allocated)
  518. free((char *)dso->short_name);
  519. dso->short_name = name;
  520. dso->short_name_len = strlen(name);
  521. dso->short_name_allocated = name_allocated;
  522. }
  523. static void dso__set_basename(struct dso *dso)
  524. {
  525. /*
  526. * basename() may modify path buffer, so we must pass
  527. * a copy.
  528. */
  529. char *base, *lname = strdup(dso->long_name);
  530. if (!lname)
  531. return;
  532. /*
  533. * basename() may return a pointer to internal
  534. * storage which is reused in subsequent calls
  535. * so copy the result.
  536. */
  537. base = strdup(basename(lname));
  538. free(lname);
  539. if (!base)
  540. return;
  541. dso__set_short_name(dso, base, true);
  542. }
  543. int dso__name_len(const struct dso *dso)
  544. {
  545. if (!dso)
  546. return strlen("[unknown]");
  547. if (verbose)
  548. return dso->long_name_len;
  549. return dso->short_name_len;
  550. }
  551. bool dso__loaded(const struct dso *dso, enum map_type type)
  552. {
  553. return dso->loaded & (1 << type);
  554. }
  555. bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
  556. {
  557. return dso->sorted_by_name & (1 << type);
  558. }
  559. void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
  560. {
  561. dso->sorted_by_name |= (1 << type);
  562. }
  563. struct dso *dso__new(const char *name)
  564. {
  565. struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
  566. if (dso != NULL) {
  567. int i;
  568. strcpy(dso->name, name);
  569. dso__set_long_name(dso, dso->name, false);
  570. dso__set_short_name(dso, dso->name, false);
  571. for (i = 0; i < MAP__NR_TYPES; ++i)
  572. dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
  573. dso->data.cache = RB_ROOT;
  574. dso->data.fd = -1;
  575. dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  576. dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
  577. dso->loaded = 0;
  578. dso->rel = 0;
  579. dso->sorted_by_name = 0;
  580. dso->has_build_id = 0;
  581. dso->has_srcline = 1;
  582. dso->a2l_fails = 1;
  583. dso->kernel = DSO_TYPE_USER;
  584. dso->needs_swap = DSO_SWAP__UNSET;
  585. INIT_LIST_HEAD(&dso->node);
  586. INIT_LIST_HEAD(&dso->data.open_entry);
  587. }
  588. return dso;
  589. }
  590. void dso__delete(struct dso *dso)
  591. {
  592. int i;
  593. for (i = 0; i < MAP__NR_TYPES; ++i)
  594. symbols__delete(&dso->symbols[i]);
  595. if (dso->short_name_allocated) {
  596. zfree((char **)&dso->short_name);
  597. dso->short_name_allocated = false;
  598. }
  599. if (dso->long_name_allocated) {
  600. zfree((char **)&dso->long_name);
  601. dso->long_name_allocated = false;
  602. }
  603. dso__data_close(dso);
  604. dso_cache__free(&dso->data.cache);
  605. dso__free_a2l(dso);
  606. zfree(&dso->symsrc_filename);
  607. free(dso);
  608. }
  609. void dso__set_build_id(struct dso *dso, void *build_id)
  610. {
  611. memcpy(dso->build_id, build_id, sizeof(dso->build_id));
  612. dso->has_build_id = 1;
  613. }
  614. bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  615. {
  616. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  617. }
  618. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  619. {
  620. char path[PATH_MAX];
  621. if (machine__is_default_guest(machine))
  622. return;
  623. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  624. if (sysfs__read_build_id(path, dso->build_id,
  625. sizeof(dso->build_id)) == 0)
  626. dso->has_build_id = true;
  627. }
  628. int dso__kernel_module_get_build_id(struct dso *dso,
  629. const char *root_dir)
  630. {
  631. char filename[PATH_MAX];
  632. /*
  633. * kernel module short names are of the form "[module]" and
  634. * we need just "module" here.
  635. */
  636. const char *name = dso->short_name + 1;
  637. snprintf(filename, sizeof(filename),
  638. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  639. root_dir, (int)strlen(name) - 1, name);
  640. if (sysfs__read_build_id(filename, dso->build_id,
  641. sizeof(dso->build_id)) == 0)
  642. dso->has_build_id = true;
  643. return 0;
  644. }
  645. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  646. {
  647. bool have_build_id = false;
  648. struct dso *pos;
  649. list_for_each_entry(pos, head, node) {
  650. if (with_hits && !pos->hit)
  651. continue;
  652. if (pos->has_build_id) {
  653. have_build_id = true;
  654. continue;
  655. }
  656. if (filename__read_build_id(pos->long_name, pos->build_id,
  657. sizeof(pos->build_id)) > 0) {
  658. have_build_id = true;
  659. pos->has_build_id = true;
  660. }
  661. }
  662. return have_build_id;
  663. }
  664. void dsos__add(struct list_head *head, struct dso *dso)
  665. {
  666. list_add_tail(&dso->node, head);
  667. }
  668. struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short)
  669. {
  670. struct dso *pos;
  671. if (cmp_short) {
  672. list_for_each_entry(pos, head, node)
  673. if (strcmp(pos->short_name, name) == 0)
  674. return pos;
  675. return NULL;
  676. }
  677. list_for_each_entry(pos, head, node)
  678. if (strcmp(pos->long_name, name) == 0)
  679. return pos;
  680. return NULL;
  681. }
  682. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  683. {
  684. struct dso *dso = dsos__find(head, name, false);
  685. if (!dso) {
  686. dso = dso__new(name);
  687. if (dso != NULL) {
  688. dsos__add(head, dso);
  689. dso__set_basename(dso);
  690. }
  691. }
  692. return dso;
  693. }
  694. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  695. bool (skip)(struct dso *dso, int parm), int parm)
  696. {
  697. struct dso *pos;
  698. size_t ret = 0;
  699. list_for_each_entry(pos, head, node) {
  700. if (skip && skip(pos, parm))
  701. continue;
  702. ret += dso__fprintf_buildid(pos, fp);
  703. ret += fprintf(fp, " %s\n", pos->long_name);
  704. }
  705. return ret;
  706. }
  707. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  708. {
  709. struct dso *pos;
  710. size_t ret = 0;
  711. list_for_each_entry(pos, head, node) {
  712. int i;
  713. for (i = 0; i < MAP__NR_TYPES; ++i)
  714. ret += dso__fprintf(pos, i, fp);
  715. }
  716. return ret;
  717. }
  718. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  719. {
  720. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  721. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  722. return fprintf(fp, "%s", sbuild_id);
  723. }
  724. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
  725. {
  726. struct rb_node *nd;
  727. size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
  728. if (dso->short_name != dso->long_name)
  729. ret += fprintf(fp, "%s, ", dso->long_name);
  730. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  731. dso__loaded(dso, type) ? "" : "NOT ");
  732. ret += dso__fprintf_buildid(dso, fp);
  733. ret += fprintf(fp, ")\n");
  734. for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
  735. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  736. ret += symbol__fprintf(pos, fp);
  737. }
  738. return ret;
  739. }