dso.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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 = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
  442. if (ret <= 0)
  443. break;
  444. cache->offset = cache_offset;
  445. cache->size = ret;
  446. dso_cache__insert(&dso->data.cache, cache);
  447. ret = dso_cache__memcpy(cache, offset, data, size);
  448. } while (0);
  449. if (ret <= 0)
  450. free(cache);
  451. return ret;
  452. }
  453. static ssize_t dso_cache_read(struct dso *dso, u64 offset,
  454. u8 *data, ssize_t size)
  455. {
  456. struct dso_cache *cache;
  457. cache = dso_cache__find(&dso->data.cache, offset);
  458. if (cache)
  459. return dso_cache__memcpy(cache, offset, data, size);
  460. else
  461. return dso_cache__read(dso, offset, data, size);
  462. }
  463. /*
  464. * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
  465. * in the rb_tree. Any read to already cached data is served
  466. * by cached data.
  467. */
  468. static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
  469. {
  470. ssize_t r = 0;
  471. u8 *p = data;
  472. do {
  473. ssize_t ret;
  474. ret = dso_cache_read(dso, offset, p, size);
  475. if (ret < 0)
  476. return ret;
  477. /* Reached EOF, return what we have. */
  478. if (!ret)
  479. break;
  480. BUG_ON(ret > size);
  481. r += ret;
  482. p += ret;
  483. offset += ret;
  484. size -= ret;
  485. } while (size);
  486. return r;
  487. }
  488. static int data_file_size(struct dso *dso)
  489. {
  490. struct stat st;
  491. char sbuf[STRERR_BUFSIZE];
  492. if (!dso->data.file_size) {
  493. if (fstat(dso->data.fd, &st)) {
  494. pr_err("dso mmap failed, fstat: %s\n",
  495. strerror_r(errno, sbuf, sizeof(sbuf)));
  496. return -1;
  497. }
  498. dso->data.file_size = st.st_size;
  499. }
  500. return 0;
  501. }
  502. /**
  503. * dso__data_size - Return dso data size
  504. * @dso: dso object
  505. * @machine: machine object
  506. *
  507. * Return: dso data size
  508. */
  509. off_t dso__data_size(struct dso *dso, struct machine *machine)
  510. {
  511. int fd;
  512. fd = dso__data_fd(dso, machine);
  513. if (fd < 0)
  514. return fd;
  515. if (data_file_size(dso))
  516. return -1;
  517. /* For now just estimate dso data size is close to file size */
  518. return dso->data.file_size;
  519. }
  520. static ssize_t data_read_offset(struct dso *dso, u64 offset,
  521. u8 *data, ssize_t size)
  522. {
  523. if (data_file_size(dso))
  524. return -1;
  525. /* Check the offset sanity. */
  526. if (offset > dso->data.file_size)
  527. return -1;
  528. if (offset + size < offset)
  529. return -1;
  530. return cached_read(dso, offset, data, size);
  531. }
  532. /**
  533. * dso__data_read_offset - Read data from dso file offset
  534. * @dso: dso object
  535. * @machine: machine object
  536. * @offset: file offset
  537. * @data: buffer to store data
  538. * @size: size of the @data buffer
  539. *
  540. * External interface to read data from dso file offset. Open
  541. * dso data file and use cached_read to get the data.
  542. */
  543. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  544. u64 offset, u8 *data, ssize_t size)
  545. {
  546. if (dso__data_fd(dso, machine) < 0)
  547. return -1;
  548. return data_read_offset(dso, offset, data, size);
  549. }
  550. /**
  551. * dso__data_read_addr - Read data from dso address
  552. * @dso: dso object
  553. * @machine: machine object
  554. * @add: virtual memory address
  555. * @data: buffer to store data
  556. * @size: size of the @data buffer
  557. *
  558. * External interface to read data from dso address.
  559. */
  560. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  561. struct machine *machine, u64 addr,
  562. u8 *data, ssize_t size)
  563. {
  564. u64 offset = map->map_ip(map, addr);
  565. return dso__data_read_offset(dso, machine, offset, data, size);
  566. }
  567. struct map *dso__new_map(const char *name)
  568. {
  569. struct map *map = NULL;
  570. struct dso *dso = dso__new(name);
  571. if (dso)
  572. map = map__new2(0, dso, MAP__FUNCTION);
  573. return map;
  574. }
  575. struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
  576. const char *short_name, int dso_type)
  577. {
  578. /*
  579. * The kernel dso could be created by build_id processing.
  580. */
  581. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
  582. /*
  583. * We need to run this in all cases, since during the build_id
  584. * processing we had no idea this was the kernel dso.
  585. */
  586. if (dso != NULL) {
  587. dso__set_short_name(dso, short_name, false);
  588. dso->kernel = dso_type;
  589. }
  590. return dso;
  591. }
  592. /*
  593. * Find a matching entry and/or link current entry to RB tree.
  594. * Either one of the dso or name parameter must be non-NULL or the
  595. * function will not work.
  596. */
  597. static struct dso *dso__findlink_by_longname(struct rb_root *root,
  598. struct dso *dso, const char *name)
  599. {
  600. struct rb_node **p = &root->rb_node;
  601. struct rb_node *parent = NULL;
  602. if (!name)
  603. name = dso->long_name;
  604. /*
  605. * Find node with the matching name
  606. */
  607. while (*p) {
  608. struct dso *this = rb_entry(*p, struct dso, rb_node);
  609. int rc = strcmp(name, this->long_name);
  610. parent = *p;
  611. if (rc == 0) {
  612. /*
  613. * In case the new DSO is a duplicate of an existing
  614. * one, print an one-time warning & put the new entry
  615. * at the end of the list of duplicates.
  616. */
  617. if (!dso || (dso == this))
  618. return this; /* Find matching dso */
  619. /*
  620. * The core kernel DSOs may have duplicated long name.
  621. * In this case, the short name should be different.
  622. * Comparing the short names to differentiate the DSOs.
  623. */
  624. rc = strcmp(dso->short_name, this->short_name);
  625. if (rc == 0) {
  626. pr_err("Duplicated dso name: %s\n", name);
  627. return NULL;
  628. }
  629. }
  630. if (rc < 0)
  631. p = &parent->rb_left;
  632. else
  633. p = &parent->rb_right;
  634. }
  635. if (dso) {
  636. /* Add new node and rebalance tree */
  637. rb_link_node(&dso->rb_node, parent, p);
  638. rb_insert_color(&dso->rb_node, root);
  639. }
  640. return NULL;
  641. }
  642. static inline struct dso *
  643. dso__find_by_longname(const struct rb_root *root, const char *name)
  644. {
  645. return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
  646. }
  647. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
  648. {
  649. if (name == NULL)
  650. return;
  651. if (dso->long_name_allocated)
  652. free((char *)dso->long_name);
  653. dso->long_name = name;
  654. dso->long_name_len = strlen(name);
  655. dso->long_name_allocated = name_allocated;
  656. }
  657. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
  658. {
  659. if (name == NULL)
  660. return;
  661. if (dso->short_name_allocated)
  662. free((char *)dso->short_name);
  663. dso->short_name = name;
  664. dso->short_name_len = strlen(name);
  665. dso->short_name_allocated = name_allocated;
  666. }
  667. static void dso__set_basename(struct dso *dso)
  668. {
  669. /*
  670. * basename() may modify path buffer, so we must pass
  671. * a copy.
  672. */
  673. char *base, *lname = strdup(dso->long_name);
  674. if (!lname)
  675. return;
  676. /*
  677. * basename() may return a pointer to internal
  678. * storage which is reused in subsequent calls
  679. * so copy the result.
  680. */
  681. base = strdup(basename(lname));
  682. free(lname);
  683. if (!base)
  684. return;
  685. dso__set_short_name(dso, base, true);
  686. }
  687. int dso__name_len(const struct dso *dso)
  688. {
  689. if (!dso)
  690. return strlen("[unknown]");
  691. if (verbose)
  692. return dso->long_name_len;
  693. return dso->short_name_len;
  694. }
  695. bool dso__loaded(const struct dso *dso, enum map_type type)
  696. {
  697. return dso->loaded & (1 << type);
  698. }
  699. bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
  700. {
  701. return dso->sorted_by_name & (1 << type);
  702. }
  703. void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
  704. {
  705. dso->sorted_by_name |= (1 << type);
  706. }
  707. struct dso *dso__new(const char *name)
  708. {
  709. struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
  710. if (dso != NULL) {
  711. int i;
  712. strcpy(dso->name, name);
  713. dso__set_long_name(dso, dso->name, false);
  714. dso__set_short_name(dso, dso->name, false);
  715. for (i = 0; i < MAP__NR_TYPES; ++i)
  716. dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
  717. dso->data.cache = RB_ROOT;
  718. dso->data.fd = -1;
  719. dso->data.status = DSO_DATA_STATUS_UNKNOWN;
  720. dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  721. dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
  722. dso->is_64_bit = (sizeof(void *) == 8);
  723. dso->loaded = 0;
  724. dso->rel = 0;
  725. dso->sorted_by_name = 0;
  726. dso->has_build_id = 0;
  727. dso->has_srcline = 1;
  728. dso->a2l_fails = 1;
  729. dso->kernel = DSO_TYPE_USER;
  730. dso->needs_swap = DSO_SWAP__UNSET;
  731. RB_CLEAR_NODE(&dso->rb_node);
  732. INIT_LIST_HEAD(&dso->node);
  733. INIT_LIST_HEAD(&dso->data.open_entry);
  734. }
  735. return dso;
  736. }
  737. void dso__delete(struct dso *dso)
  738. {
  739. int i;
  740. if (!RB_EMPTY_NODE(&dso->rb_node))
  741. pr_err("DSO %s is still in rbtree when being deleted!\n",
  742. dso->long_name);
  743. for (i = 0; i < MAP__NR_TYPES; ++i)
  744. symbols__delete(&dso->symbols[i]);
  745. if (dso->short_name_allocated) {
  746. zfree((char **)&dso->short_name);
  747. dso->short_name_allocated = false;
  748. }
  749. if (dso->long_name_allocated) {
  750. zfree((char **)&dso->long_name);
  751. dso->long_name_allocated = false;
  752. }
  753. dso__data_close(dso);
  754. dso_cache__free(&dso->data.cache);
  755. dso__free_a2l(dso);
  756. zfree(&dso->symsrc_filename);
  757. free(dso);
  758. }
  759. void dso__set_build_id(struct dso *dso, void *build_id)
  760. {
  761. memcpy(dso->build_id, build_id, sizeof(dso->build_id));
  762. dso->has_build_id = 1;
  763. }
  764. bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  765. {
  766. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  767. }
  768. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  769. {
  770. char path[PATH_MAX];
  771. if (machine__is_default_guest(machine))
  772. return;
  773. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  774. if (sysfs__read_build_id(path, dso->build_id,
  775. sizeof(dso->build_id)) == 0)
  776. dso->has_build_id = true;
  777. }
  778. int dso__kernel_module_get_build_id(struct dso *dso,
  779. const char *root_dir)
  780. {
  781. char filename[PATH_MAX];
  782. /*
  783. * kernel module short names are of the form "[module]" and
  784. * we need just "module" here.
  785. */
  786. const char *name = dso->short_name + 1;
  787. snprintf(filename, sizeof(filename),
  788. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  789. root_dir, (int)strlen(name) - 1, name);
  790. if (sysfs__read_build_id(filename, dso->build_id,
  791. sizeof(dso->build_id)) == 0)
  792. dso->has_build_id = true;
  793. return 0;
  794. }
  795. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  796. {
  797. bool have_build_id = false;
  798. struct dso *pos;
  799. list_for_each_entry(pos, head, node) {
  800. if (with_hits && !pos->hit)
  801. continue;
  802. if (pos->has_build_id) {
  803. have_build_id = true;
  804. continue;
  805. }
  806. if (filename__read_build_id(pos->long_name, pos->build_id,
  807. sizeof(pos->build_id)) > 0) {
  808. have_build_id = true;
  809. pos->has_build_id = true;
  810. }
  811. }
  812. return have_build_id;
  813. }
  814. void dsos__add(struct dsos *dsos, struct dso *dso)
  815. {
  816. list_add_tail(&dso->node, &dsos->head);
  817. dso__findlink_by_longname(&dsos->root, dso, NULL);
  818. }
  819. struct dso *dsos__find(const struct dsos *dsos, const char *name,
  820. bool cmp_short)
  821. {
  822. struct dso *pos;
  823. if (cmp_short) {
  824. list_for_each_entry(pos, &dsos->head, node)
  825. if (strcmp(pos->short_name, name) == 0)
  826. return pos;
  827. return NULL;
  828. }
  829. return dso__find_by_longname(&dsos->root, name);
  830. }
  831. struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
  832. {
  833. struct dso *dso = dsos__find(dsos, name, false);
  834. if (!dso) {
  835. dso = dso__new(name);
  836. if (dso != NULL) {
  837. dsos__add(dsos, dso);
  838. dso__set_basename(dso);
  839. }
  840. }
  841. return dso;
  842. }
  843. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  844. bool (skip)(struct dso *dso, int parm), int parm)
  845. {
  846. struct dso *pos;
  847. size_t ret = 0;
  848. list_for_each_entry(pos, head, node) {
  849. if (skip && skip(pos, parm))
  850. continue;
  851. ret += dso__fprintf_buildid(pos, fp);
  852. ret += fprintf(fp, " %s\n", pos->long_name);
  853. }
  854. return ret;
  855. }
  856. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  857. {
  858. struct dso *pos;
  859. size_t ret = 0;
  860. list_for_each_entry(pos, head, node) {
  861. int i;
  862. for (i = 0; i < MAP__NR_TYPES; ++i)
  863. ret += dso__fprintf(pos, i, fp);
  864. }
  865. return ret;
  866. }
  867. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  868. {
  869. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  870. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  871. return fprintf(fp, "%s", sbuild_id);
  872. }
  873. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
  874. {
  875. struct rb_node *nd;
  876. size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
  877. if (dso->short_name != dso->long_name)
  878. ret += fprintf(fp, "%s, ", dso->long_name);
  879. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  880. dso__loaded(dso, type) ? "" : "NOT ");
  881. ret += dso__fprintf_buildid(dso, fp);
  882. ret += fprintf(fp, ")\n");
  883. for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
  884. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  885. ret += symbol__fprintf(pos, fp);
  886. }
  887. return ret;
  888. }
  889. enum dso_type dso__type(struct dso *dso, struct machine *machine)
  890. {
  891. int fd;
  892. fd = dso__data_fd(dso, machine);
  893. if (fd < 0)
  894. return DSO__TYPE_UNKNOWN;
  895. return dso__type_fd(fd);
  896. }