dso.c 24 KB

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