dso.c 26 KB

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