dso.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <asm/bug.h>
  3. #include <linux/kernel.h>
  4. #include <sys/time.h>
  5. #include <sys/resource.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include "compress.h"
  12. #include "path.h"
  13. #include "symbol.h"
  14. #include "srcline.h"
  15. #include "dso.h"
  16. #include "machine.h"
  17. #include "auxtrace.h"
  18. #include "util.h"
  19. #include "debug.h"
  20. #include "string2.h"
  21. #include "vdso.h"
  22. static const char * const debuglink_paths[] = {
  23. "%.0s%s",
  24. "%s/%s",
  25. "%s/.debug/%s",
  26. "/usr/lib/debug%s/%s"
  27. };
  28. char dso__symtab_origin(const struct dso *dso)
  29. {
  30. static const char origin[] = {
  31. [DSO_BINARY_TYPE__KALLSYMS] = 'k',
  32. [DSO_BINARY_TYPE__VMLINUX] = 'v',
  33. [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
  34. [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
  35. [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
  36. [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
  37. [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
  38. [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
  39. [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
  40. [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
  41. [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
  42. [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
  43. [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
  44. [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
  45. [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
  46. [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
  47. [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
  48. };
  49. if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
  50. return '!';
  51. return origin[dso->symtab_type];
  52. }
  53. int dso__read_binary_type_filename(const struct dso *dso,
  54. enum dso_binary_type type,
  55. char *root_dir, char *filename, size_t size)
  56. {
  57. char build_id_hex[SBUILD_ID_SIZE];
  58. int ret = 0;
  59. size_t len;
  60. switch (type) {
  61. case DSO_BINARY_TYPE__DEBUGLINK:
  62. {
  63. const char *last_slash;
  64. char dso_dir[PATH_MAX];
  65. char symfile[PATH_MAX];
  66. unsigned int i;
  67. len = __symbol__join_symfs(filename, size, dso->long_name);
  68. last_slash = filename + len;
  69. while (last_slash != filename && *last_slash != '/')
  70. last_slash--;
  71. strncpy(dso_dir, filename, last_slash - filename);
  72. dso_dir[last_slash-filename] = '\0';
  73. if (!is_regular_file(filename)) {
  74. ret = -1;
  75. break;
  76. }
  77. ret = filename__read_debuglink(filename, symfile, PATH_MAX);
  78. if (ret)
  79. break;
  80. /* Check predefined locations where debug file might reside */
  81. ret = -1;
  82. for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
  83. snprintf(filename, size,
  84. debuglink_paths[i], dso_dir, symfile);
  85. if (is_regular_file(filename)) {
  86. ret = 0;
  87. break;
  88. }
  89. }
  90. break;
  91. }
  92. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  93. if (dso__build_id_filename(dso, filename, size, false) == NULL)
  94. ret = -1;
  95. break;
  96. case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
  97. if (dso__build_id_filename(dso, filename, size, true) == NULL)
  98. ret = -1;
  99. break;
  100. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  101. len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
  102. snprintf(filename + len, size - len, "%s.debug", dso->long_name);
  103. break;
  104. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  105. len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
  106. snprintf(filename + len, size - len, "%s", dso->long_name);
  107. break;
  108. case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
  109. {
  110. const char *last_slash;
  111. size_t dir_size;
  112. last_slash = dso->long_name + dso->long_name_len;
  113. while (last_slash != dso->long_name && *last_slash != '/')
  114. last_slash--;
  115. len = __symbol__join_symfs(filename, size, "");
  116. dir_size = last_slash - dso->long_name + 2;
  117. if (dir_size > (size - len)) {
  118. ret = -1;
  119. break;
  120. }
  121. len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
  122. len += scnprintf(filename + len , size - len, ".debug%s",
  123. last_slash);
  124. break;
  125. }
  126. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  127. if (!dso->has_build_id) {
  128. ret = -1;
  129. break;
  130. }
  131. build_id__sprintf(dso->build_id,
  132. sizeof(dso->build_id),
  133. build_id_hex);
  134. len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
  135. snprintf(filename + len, size - len, "%.2s/%s.debug",
  136. build_id_hex, build_id_hex + 2);
  137. break;
  138. case DSO_BINARY_TYPE__VMLINUX:
  139. case DSO_BINARY_TYPE__GUEST_VMLINUX:
  140. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  141. __symbol__join_symfs(filename, size, dso->long_name);
  142. break;
  143. case DSO_BINARY_TYPE__GUEST_KMODULE:
  144. case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
  145. path__join3(filename, size, symbol_conf.symfs,
  146. root_dir, dso->long_name);
  147. break;
  148. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  149. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
  150. __symbol__join_symfs(filename, size, dso->long_name);
  151. break;
  152. case DSO_BINARY_TYPE__KCORE:
  153. case DSO_BINARY_TYPE__GUEST_KCORE:
  154. snprintf(filename, size, "%s", dso->long_name);
  155. break;
  156. default:
  157. case DSO_BINARY_TYPE__KALLSYMS:
  158. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  159. case DSO_BINARY_TYPE__JAVA_JIT:
  160. case DSO_BINARY_TYPE__NOT_FOUND:
  161. ret = -1;
  162. break;
  163. }
  164. return ret;
  165. }
  166. enum {
  167. COMP_ID__NONE = 0,
  168. };
  169. static const struct {
  170. const char *fmt;
  171. int (*decompress)(const char *input, int output);
  172. bool (*is_compressed)(const char *input);
  173. } compressions[] = {
  174. [COMP_ID__NONE] = { .fmt = NULL, },
  175. #ifdef HAVE_ZLIB_SUPPORT
  176. { "gz", gzip_decompress_to_file, gzip_is_compressed },
  177. #endif
  178. #ifdef HAVE_LZMA_SUPPORT
  179. { "xz", lzma_decompress_to_file, lzma_is_compressed },
  180. #endif
  181. { NULL, NULL, NULL },
  182. };
  183. static int is_supported_compression(const char *ext)
  184. {
  185. unsigned i;
  186. for (i = 1; compressions[i].fmt; i++) {
  187. if (!strcmp(ext, compressions[i].fmt))
  188. return i;
  189. }
  190. return COMP_ID__NONE;
  191. }
  192. bool is_kernel_module(const char *pathname, int cpumode)
  193. {
  194. struct kmod_path m;
  195. int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
  196. WARN_ONCE(mode != cpumode,
  197. "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
  198. cpumode);
  199. switch (mode) {
  200. case PERF_RECORD_MISC_USER:
  201. case PERF_RECORD_MISC_HYPERVISOR:
  202. case PERF_RECORD_MISC_GUEST_USER:
  203. return false;
  204. /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
  205. default:
  206. if (kmod_path__parse(&m, pathname)) {
  207. pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
  208. pathname);
  209. return true;
  210. }
  211. }
  212. return m.kmod;
  213. }
  214. bool dso__needs_decompress(struct dso *dso)
  215. {
  216. return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
  217. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
  218. }
  219. static int decompress_kmodule(struct dso *dso, const char *name,
  220. char *pathname, size_t len)
  221. {
  222. char tmpbuf[] = KMOD_DECOMP_NAME;
  223. int fd = -1;
  224. if (!dso__needs_decompress(dso))
  225. return -1;
  226. if (dso->comp == COMP_ID__NONE)
  227. return -1;
  228. /*
  229. * We have proper compression id for DSO and yet the file
  230. * behind the 'name' can still be plain uncompressed object.
  231. *
  232. * The reason is behind the logic we open the DSO object files,
  233. * when we try all possible 'debug' objects until we find the
  234. * data. So even if the DSO is represented by 'krava.xz' module,
  235. * we can end up here opening ~/.debug/....23432432/debug' file
  236. * which is not compressed.
  237. *
  238. * To keep this transparent, we detect this and return the file
  239. * descriptor to the uncompressed file.
  240. */
  241. if (!compressions[dso->comp].is_compressed(name))
  242. return open(name, O_RDONLY);
  243. fd = mkstemp(tmpbuf);
  244. if (fd < 0) {
  245. dso->load_errno = errno;
  246. return -1;
  247. }
  248. if (compressions[dso->comp].decompress(name, fd)) {
  249. dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
  250. close(fd);
  251. fd = -1;
  252. }
  253. if (!pathname || (fd < 0))
  254. unlink(tmpbuf);
  255. if (pathname && (fd >= 0))
  256. strncpy(pathname, tmpbuf, len);
  257. return fd;
  258. }
  259. int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
  260. {
  261. return decompress_kmodule(dso, name, NULL, 0);
  262. }
  263. int dso__decompress_kmodule_path(struct dso *dso, const char *name,
  264. char *pathname, size_t len)
  265. {
  266. int fd = decompress_kmodule(dso, name, pathname, len);
  267. close(fd);
  268. return fd >= 0 ? 0 : -1;
  269. }
  270. /*
  271. * Parses kernel module specified in @path and updates
  272. * @m argument like:
  273. *
  274. * @comp - true if @path contains supported compression suffix,
  275. * false otherwise
  276. * @kmod - true if @path contains '.ko' suffix in right position,
  277. * false otherwise
  278. * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
  279. * of the kernel module without suffixes, otherwise strudup-ed
  280. * base name of @path
  281. * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
  282. * the compression suffix
  283. *
  284. * Returns 0 if there's no strdup error, -ENOMEM otherwise.
  285. */
  286. int __kmod_path__parse(struct kmod_path *m, const char *path,
  287. bool alloc_name)
  288. {
  289. const char *name = strrchr(path, '/');
  290. const char *ext = strrchr(path, '.');
  291. bool is_simple_name = false;
  292. memset(m, 0x0, sizeof(*m));
  293. name = name ? name + 1 : path;
  294. /*
  295. * '.' is also a valid character for module name. For example:
  296. * [aaa.bbb] is a valid module name. '[' should have higher
  297. * priority than '.ko' suffix.
  298. *
  299. * The kernel names are from machine__mmap_name. Such
  300. * name should belong to kernel itself, not kernel module.
  301. */
  302. if (name[0] == '[') {
  303. is_simple_name = true;
  304. if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
  305. (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
  306. (strncmp(name, "[vdso]", 6) == 0) ||
  307. (strncmp(name, "[vdso32]", 8) == 0) ||
  308. (strncmp(name, "[vdsox32]", 9) == 0) ||
  309. (strncmp(name, "[vsyscall]", 10) == 0)) {
  310. m->kmod = false;
  311. } else
  312. m->kmod = true;
  313. }
  314. /* No extension, just return name. */
  315. if ((ext == NULL) || is_simple_name) {
  316. if (alloc_name) {
  317. m->name = strdup(name);
  318. return m->name ? 0 : -ENOMEM;
  319. }
  320. return 0;
  321. }
  322. m->comp = is_supported_compression(ext + 1);
  323. if (m->comp > COMP_ID__NONE)
  324. ext -= 3;
  325. /* Check .ko extension only if there's enough name left. */
  326. if (ext > name)
  327. m->kmod = !strncmp(ext, ".ko", 3);
  328. if (alloc_name) {
  329. if (m->kmod) {
  330. if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
  331. return -ENOMEM;
  332. } else {
  333. if (asprintf(&m->name, "%s", name) == -1)
  334. return -ENOMEM;
  335. }
  336. strxfrchar(m->name, '-', '_');
  337. }
  338. return 0;
  339. }
  340. void dso__set_module_info(struct dso *dso, struct kmod_path *m,
  341. struct machine *machine)
  342. {
  343. if (machine__is_host(machine))
  344. dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
  345. else
  346. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
  347. /* _KMODULE_COMP should be next to _KMODULE */
  348. if (m->kmod && m->comp) {
  349. dso->symtab_type++;
  350. dso->comp = m->comp;
  351. }
  352. dso__set_short_name(dso, strdup(m->name), true);
  353. }
  354. /*
  355. * Global list of open DSOs and the counter.
  356. */
  357. static LIST_HEAD(dso__data_open);
  358. static long dso__data_open_cnt;
  359. static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
  360. static void dso__list_add(struct dso *dso)
  361. {
  362. list_add_tail(&dso->data.open_entry, &dso__data_open);
  363. dso__data_open_cnt++;
  364. }
  365. static void dso__list_del(struct dso *dso)
  366. {
  367. list_del(&dso->data.open_entry);
  368. WARN_ONCE(dso__data_open_cnt <= 0,
  369. "DSO data fd counter out of bounds.");
  370. dso__data_open_cnt--;
  371. }
  372. static void close_first_dso(void);
  373. static int do_open(char *name)
  374. {
  375. int fd;
  376. char sbuf[STRERR_BUFSIZE];
  377. do {
  378. fd = open(name, O_RDONLY|O_CLOEXEC);
  379. if (fd >= 0)
  380. return fd;
  381. pr_debug("dso open failed: %s\n",
  382. str_error_r(errno, sbuf, sizeof(sbuf)));
  383. if (!dso__data_open_cnt || errno != EMFILE)
  384. break;
  385. close_first_dso();
  386. } while (1);
  387. return -1;
  388. }
  389. static int __open_dso(struct dso *dso, struct machine *machine)
  390. {
  391. int fd = -EINVAL;
  392. char *root_dir = (char *)"";
  393. char *name = malloc(PATH_MAX);
  394. bool decomp = false;
  395. if (!name)
  396. return -ENOMEM;
  397. if (machine)
  398. root_dir = machine->root_dir;
  399. if (dso__read_binary_type_filename(dso, dso->binary_type,
  400. root_dir, name, PATH_MAX))
  401. goto out;
  402. if (!is_regular_file(name))
  403. goto out;
  404. if (dso__needs_decompress(dso)) {
  405. char newpath[KMOD_DECOMP_LEN];
  406. size_t len = sizeof(newpath);
  407. if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
  408. fd = -dso->load_errno;
  409. goto out;
  410. }
  411. decomp = true;
  412. strcpy(name, newpath);
  413. }
  414. fd = do_open(name);
  415. if (decomp)
  416. unlink(name);
  417. out:
  418. free(name);
  419. return fd;
  420. }
  421. static void check_data_close(void);
  422. /**
  423. * dso_close - Open DSO data file
  424. * @dso: dso object
  425. *
  426. * Open @dso's data file descriptor and updates
  427. * list/count of open DSO objects.
  428. */
  429. static int open_dso(struct dso *dso, struct machine *machine)
  430. {
  431. int fd;
  432. struct nscookie nsc;
  433. if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
  434. nsinfo__mountns_enter(dso->nsinfo, &nsc);
  435. fd = __open_dso(dso, machine);
  436. if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
  437. nsinfo__mountns_exit(&nsc);
  438. if (fd >= 0) {
  439. dso__list_add(dso);
  440. /*
  441. * Check if we crossed the allowed number
  442. * of opened DSOs and close one if needed.
  443. */
  444. check_data_close();
  445. }
  446. return fd;
  447. }
  448. static void close_data_fd(struct dso *dso)
  449. {
  450. if (dso->data.fd >= 0) {
  451. close(dso->data.fd);
  452. dso->data.fd = -1;
  453. dso->data.file_size = 0;
  454. dso__list_del(dso);
  455. }
  456. }
  457. /**
  458. * dso_close - Close DSO data file
  459. * @dso: dso object
  460. *
  461. * Close @dso's data file descriptor and updates
  462. * list/count of open DSO objects.
  463. */
  464. static void close_dso(struct dso *dso)
  465. {
  466. close_data_fd(dso);
  467. }
  468. static void close_first_dso(void)
  469. {
  470. struct dso *dso;
  471. dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
  472. close_dso(dso);
  473. }
  474. static rlim_t get_fd_limit(void)
  475. {
  476. struct rlimit l;
  477. rlim_t limit = 0;
  478. /* Allow half of the current open fd limit. */
  479. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  480. if (l.rlim_cur == RLIM_INFINITY)
  481. limit = l.rlim_cur;
  482. else
  483. limit = l.rlim_cur / 2;
  484. } else {
  485. pr_err("failed to get fd limit\n");
  486. limit = 1;
  487. }
  488. return limit;
  489. }
  490. static rlim_t fd_limit;
  491. /*
  492. * Used only by tests/dso-data.c to reset the environment
  493. * for tests. I dont expect we should change this during
  494. * standard runtime.
  495. */
  496. void reset_fd_limit(void)
  497. {
  498. fd_limit = 0;
  499. }
  500. static bool may_cache_fd(void)
  501. {
  502. if (!fd_limit)
  503. fd_limit = get_fd_limit();
  504. if (fd_limit == RLIM_INFINITY)
  505. return true;
  506. return fd_limit > (rlim_t) dso__data_open_cnt;
  507. }
  508. /*
  509. * Check and close LRU dso if we crossed allowed limit
  510. * for opened dso file descriptors. The limit is half
  511. * of the RLIMIT_NOFILE files opened.
  512. */
  513. static void check_data_close(void)
  514. {
  515. bool cache_fd = may_cache_fd();
  516. if (!cache_fd)
  517. close_first_dso();
  518. }
  519. /**
  520. * dso__data_close - Close DSO data file
  521. * @dso: dso object
  522. *
  523. * External interface to close @dso's data file descriptor.
  524. */
  525. void dso__data_close(struct dso *dso)
  526. {
  527. pthread_mutex_lock(&dso__data_open_lock);
  528. close_dso(dso);
  529. pthread_mutex_unlock(&dso__data_open_lock);
  530. }
  531. static void try_to_open_dso(struct dso *dso, struct machine *machine)
  532. {
  533. enum dso_binary_type binary_type_data[] = {
  534. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  535. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  536. DSO_BINARY_TYPE__NOT_FOUND,
  537. };
  538. int i = 0;
  539. if (dso->data.fd >= 0)
  540. return;
  541. if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
  542. dso->data.fd = open_dso(dso, machine);
  543. goto out;
  544. }
  545. do {
  546. dso->binary_type = binary_type_data[i++];
  547. dso->data.fd = open_dso(dso, machine);
  548. if (dso->data.fd >= 0)
  549. goto out;
  550. } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
  551. out:
  552. if (dso->data.fd >= 0)
  553. dso->data.status = DSO_DATA_STATUS_OK;
  554. else
  555. dso->data.status = DSO_DATA_STATUS_ERROR;
  556. }
  557. /**
  558. * dso__data_get_fd - Get dso's data file descriptor
  559. * @dso: dso object
  560. * @machine: machine object
  561. *
  562. * External interface to find dso's file, open it and
  563. * returns file descriptor. It should be paired with
  564. * dso__data_put_fd() if it returns non-negative value.
  565. */
  566. int dso__data_get_fd(struct dso *dso, struct machine *machine)
  567. {
  568. if (dso->data.status == DSO_DATA_STATUS_ERROR)
  569. return -1;
  570. if (pthread_mutex_lock(&dso__data_open_lock) < 0)
  571. return -1;
  572. try_to_open_dso(dso, machine);
  573. if (dso->data.fd < 0)
  574. pthread_mutex_unlock(&dso__data_open_lock);
  575. return dso->data.fd;
  576. }
  577. void dso__data_put_fd(struct dso *dso __maybe_unused)
  578. {
  579. pthread_mutex_unlock(&dso__data_open_lock);
  580. }
  581. bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
  582. {
  583. u32 flag = 1 << by;
  584. if (dso->data.status_seen & flag)
  585. return true;
  586. dso->data.status_seen |= flag;
  587. return false;
  588. }
  589. static void
  590. dso_cache__free(struct dso *dso)
  591. {
  592. struct rb_root *root = &dso->data.cache;
  593. struct rb_node *next = rb_first(root);
  594. pthread_mutex_lock(&dso->lock);
  595. while (next) {
  596. struct dso_cache *cache;
  597. cache = rb_entry(next, struct dso_cache, rb_node);
  598. next = rb_next(&cache->rb_node);
  599. rb_erase(&cache->rb_node, root);
  600. free(cache);
  601. }
  602. pthread_mutex_unlock(&dso->lock);
  603. }
  604. static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
  605. {
  606. const struct rb_root *root = &dso->data.cache;
  607. struct rb_node * const *p = &root->rb_node;
  608. const struct rb_node *parent = NULL;
  609. struct dso_cache *cache;
  610. while (*p != NULL) {
  611. u64 end;
  612. parent = *p;
  613. cache = rb_entry(parent, struct dso_cache, rb_node);
  614. end = cache->offset + DSO__DATA_CACHE_SIZE;
  615. if (offset < cache->offset)
  616. p = &(*p)->rb_left;
  617. else if (offset >= end)
  618. p = &(*p)->rb_right;
  619. else
  620. return cache;
  621. }
  622. return NULL;
  623. }
  624. static struct dso_cache *
  625. dso_cache__insert(struct dso *dso, struct dso_cache *new)
  626. {
  627. struct rb_root *root = &dso->data.cache;
  628. struct rb_node **p = &root->rb_node;
  629. struct rb_node *parent = NULL;
  630. struct dso_cache *cache;
  631. u64 offset = new->offset;
  632. pthread_mutex_lock(&dso->lock);
  633. while (*p != NULL) {
  634. u64 end;
  635. parent = *p;
  636. cache = rb_entry(parent, struct dso_cache, rb_node);
  637. end = cache->offset + DSO__DATA_CACHE_SIZE;
  638. if (offset < cache->offset)
  639. p = &(*p)->rb_left;
  640. else if (offset >= end)
  641. p = &(*p)->rb_right;
  642. else
  643. goto out;
  644. }
  645. rb_link_node(&new->rb_node, parent, p);
  646. rb_insert_color(&new->rb_node, root);
  647. cache = NULL;
  648. out:
  649. pthread_mutex_unlock(&dso->lock);
  650. return cache;
  651. }
  652. static ssize_t
  653. dso_cache__memcpy(struct dso_cache *cache, u64 offset,
  654. u8 *data, u64 size)
  655. {
  656. u64 cache_offset = offset - cache->offset;
  657. u64 cache_size = min(cache->size - cache_offset, size);
  658. memcpy(data, cache->data + cache_offset, cache_size);
  659. return cache_size;
  660. }
  661. static ssize_t
  662. dso_cache__read(struct dso *dso, struct machine *machine,
  663. u64 offset, u8 *data, ssize_t size)
  664. {
  665. struct dso_cache *cache;
  666. struct dso_cache *old;
  667. ssize_t ret;
  668. do {
  669. u64 cache_offset;
  670. cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
  671. if (!cache)
  672. return -ENOMEM;
  673. pthread_mutex_lock(&dso__data_open_lock);
  674. /*
  675. * dso->data.fd might be closed if other thread opened another
  676. * file (dso) due to open file limit (RLIMIT_NOFILE).
  677. */
  678. try_to_open_dso(dso, machine);
  679. if (dso->data.fd < 0) {
  680. ret = -errno;
  681. dso->data.status = DSO_DATA_STATUS_ERROR;
  682. break;
  683. }
  684. cache_offset = offset & DSO__DATA_CACHE_MASK;
  685. ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
  686. if (ret <= 0)
  687. break;
  688. cache->offset = cache_offset;
  689. cache->size = ret;
  690. } while (0);
  691. pthread_mutex_unlock(&dso__data_open_lock);
  692. if (ret > 0) {
  693. old = dso_cache__insert(dso, cache);
  694. if (old) {
  695. /* we lose the race */
  696. free(cache);
  697. cache = old;
  698. }
  699. ret = dso_cache__memcpy(cache, offset, data, size);
  700. }
  701. if (ret <= 0)
  702. free(cache);
  703. return ret;
  704. }
  705. static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
  706. u64 offset, u8 *data, ssize_t size)
  707. {
  708. struct dso_cache *cache;
  709. cache = dso_cache__find(dso, offset);
  710. if (cache)
  711. return dso_cache__memcpy(cache, offset, data, size);
  712. else
  713. return dso_cache__read(dso, machine, offset, data, size);
  714. }
  715. /*
  716. * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
  717. * in the rb_tree. Any read to already cached data is served
  718. * by cached data.
  719. */
  720. static ssize_t cached_read(struct dso *dso, struct machine *machine,
  721. u64 offset, u8 *data, ssize_t size)
  722. {
  723. ssize_t r = 0;
  724. u8 *p = data;
  725. do {
  726. ssize_t ret;
  727. ret = dso_cache_read(dso, machine, offset, p, size);
  728. if (ret < 0)
  729. return ret;
  730. /* Reached EOF, return what we have. */
  731. if (!ret)
  732. break;
  733. BUG_ON(ret > size);
  734. r += ret;
  735. p += ret;
  736. offset += ret;
  737. size -= ret;
  738. } while (size);
  739. return r;
  740. }
  741. static int data_file_size(struct dso *dso, struct machine *machine)
  742. {
  743. int ret = 0;
  744. struct stat st;
  745. char sbuf[STRERR_BUFSIZE];
  746. if (dso->data.file_size)
  747. return 0;
  748. if (dso->data.status == DSO_DATA_STATUS_ERROR)
  749. return -1;
  750. pthread_mutex_lock(&dso__data_open_lock);
  751. /*
  752. * dso->data.fd might be closed if other thread opened another
  753. * file (dso) due to open file limit (RLIMIT_NOFILE).
  754. */
  755. try_to_open_dso(dso, machine);
  756. if (dso->data.fd < 0) {
  757. ret = -errno;
  758. dso->data.status = DSO_DATA_STATUS_ERROR;
  759. goto out;
  760. }
  761. if (fstat(dso->data.fd, &st) < 0) {
  762. ret = -errno;
  763. pr_err("dso cache fstat failed: %s\n",
  764. str_error_r(errno, sbuf, sizeof(sbuf)));
  765. dso->data.status = DSO_DATA_STATUS_ERROR;
  766. goto out;
  767. }
  768. dso->data.file_size = st.st_size;
  769. out:
  770. pthread_mutex_unlock(&dso__data_open_lock);
  771. return ret;
  772. }
  773. /**
  774. * dso__data_size - Return dso data size
  775. * @dso: dso object
  776. * @machine: machine object
  777. *
  778. * Return: dso data size
  779. */
  780. off_t dso__data_size(struct dso *dso, struct machine *machine)
  781. {
  782. if (data_file_size(dso, machine))
  783. return -1;
  784. /* For now just estimate dso data size is close to file size */
  785. return dso->data.file_size;
  786. }
  787. static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
  788. u64 offset, u8 *data, ssize_t size)
  789. {
  790. if (data_file_size(dso, machine))
  791. return -1;
  792. /* Check the offset sanity. */
  793. if (offset > dso->data.file_size)
  794. return -1;
  795. if (offset + size < offset)
  796. return -1;
  797. return cached_read(dso, machine, offset, data, size);
  798. }
  799. /**
  800. * dso__data_read_offset - Read data from dso file offset
  801. * @dso: dso object
  802. * @machine: machine object
  803. * @offset: file offset
  804. * @data: buffer to store data
  805. * @size: size of the @data buffer
  806. *
  807. * External interface to read data from dso file offset. Open
  808. * dso data file and use cached_read to get the data.
  809. */
  810. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  811. u64 offset, u8 *data, ssize_t size)
  812. {
  813. if (dso->data.status == DSO_DATA_STATUS_ERROR)
  814. return -1;
  815. return data_read_offset(dso, machine, offset, data, size);
  816. }
  817. /**
  818. * dso__data_read_addr - Read data from dso address
  819. * @dso: dso object
  820. * @machine: machine object
  821. * @add: virtual memory address
  822. * @data: buffer to store data
  823. * @size: size of the @data buffer
  824. *
  825. * External interface to read data from dso address.
  826. */
  827. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  828. struct machine *machine, u64 addr,
  829. u8 *data, ssize_t size)
  830. {
  831. u64 offset = map->map_ip(map, addr);
  832. return dso__data_read_offset(dso, machine, offset, data, size);
  833. }
  834. struct map *dso__new_map(const char *name)
  835. {
  836. struct map *map = NULL;
  837. struct dso *dso = dso__new(name);
  838. if (dso)
  839. map = map__new2(0, dso);
  840. return map;
  841. }
  842. struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
  843. const char *short_name, int dso_type)
  844. {
  845. /*
  846. * The kernel dso could be created by build_id processing.
  847. */
  848. struct dso *dso = machine__findnew_dso(machine, name);
  849. /*
  850. * We need to run this in all cases, since during the build_id
  851. * processing we had no idea this was the kernel dso.
  852. */
  853. if (dso != NULL) {
  854. dso__set_short_name(dso, short_name, false);
  855. dso->kernel = dso_type;
  856. }
  857. return dso;
  858. }
  859. /*
  860. * Find a matching entry and/or link current entry to RB tree.
  861. * Either one of the dso or name parameter must be non-NULL or the
  862. * function will not work.
  863. */
  864. static struct dso *__dso__findlink_by_longname(struct rb_root *root,
  865. struct dso *dso, const char *name)
  866. {
  867. struct rb_node **p = &root->rb_node;
  868. struct rb_node *parent = NULL;
  869. if (!name)
  870. name = dso->long_name;
  871. /*
  872. * Find node with the matching name
  873. */
  874. while (*p) {
  875. struct dso *this = rb_entry(*p, struct dso, rb_node);
  876. int rc = strcmp(name, this->long_name);
  877. parent = *p;
  878. if (rc == 0) {
  879. /*
  880. * In case the new DSO is a duplicate of an existing
  881. * one, print a one-time warning & put the new entry
  882. * at the end of the list of duplicates.
  883. */
  884. if (!dso || (dso == this))
  885. return this; /* Find matching dso */
  886. /*
  887. * The core kernel DSOs may have duplicated long name.
  888. * In this case, the short name should be different.
  889. * Comparing the short names to differentiate the DSOs.
  890. */
  891. rc = strcmp(dso->short_name, this->short_name);
  892. if (rc == 0) {
  893. pr_err("Duplicated dso name: %s\n", name);
  894. return NULL;
  895. }
  896. }
  897. if (rc < 0)
  898. p = &parent->rb_left;
  899. else
  900. p = &parent->rb_right;
  901. }
  902. if (dso) {
  903. /* Add new node and rebalance tree */
  904. rb_link_node(&dso->rb_node, parent, p);
  905. rb_insert_color(&dso->rb_node, root);
  906. dso->root = root;
  907. }
  908. return NULL;
  909. }
  910. static inline struct dso *__dso__find_by_longname(struct rb_root *root,
  911. const char *name)
  912. {
  913. return __dso__findlink_by_longname(root, NULL, name);
  914. }
  915. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
  916. {
  917. struct rb_root *root = dso->root;
  918. if (name == NULL)
  919. return;
  920. if (dso->long_name_allocated)
  921. free((char *)dso->long_name);
  922. if (root) {
  923. rb_erase(&dso->rb_node, root);
  924. /*
  925. * __dso__findlink_by_longname() isn't guaranteed to add it
  926. * back, so a clean removal is required here.
  927. */
  928. RB_CLEAR_NODE(&dso->rb_node);
  929. dso->root = NULL;
  930. }
  931. dso->long_name = name;
  932. dso->long_name_len = strlen(name);
  933. dso->long_name_allocated = name_allocated;
  934. if (root)
  935. __dso__findlink_by_longname(root, dso, NULL);
  936. }
  937. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
  938. {
  939. if (name == NULL)
  940. return;
  941. if (dso->short_name_allocated)
  942. free((char *)dso->short_name);
  943. dso->short_name = name;
  944. dso->short_name_len = strlen(name);
  945. dso->short_name_allocated = name_allocated;
  946. }
  947. static void dso__set_basename(struct dso *dso)
  948. {
  949. /*
  950. * basename() may modify path buffer, so we must pass
  951. * a copy.
  952. */
  953. char *base, *lname = strdup(dso->long_name);
  954. if (!lname)
  955. return;
  956. /*
  957. * basename() may return a pointer to internal
  958. * storage which is reused in subsequent calls
  959. * so copy the result.
  960. */
  961. base = strdup(basename(lname));
  962. free(lname);
  963. if (!base)
  964. return;
  965. dso__set_short_name(dso, base, true);
  966. }
  967. int dso__name_len(const struct dso *dso)
  968. {
  969. if (!dso)
  970. return strlen("[unknown]");
  971. if (verbose > 0)
  972. return dso->long_name_len;
  973. return dso->short_name_len;
  974. }
  975. bool dso__loaded(const struct dso *dso)
  976. {
  977. return dso->loaded;
  978. }
  979. bool dso__sorted_by_name(const struct dso *dso)
  980. {
  981. return dso->sorted_by_name;
  982. }
  983. void dso__set_sorted_by_name(struct dso *dso)
  984. {
  985. dso->sorted_by_name = true;
  986. }
  987. struct dso *dso__new(const char *name)
  988. {
  989. struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
  990. if (dso != NULL) {
  991. strcpy(dso->name, name);
  992. dso__set_long_name(dso, dso->name, false);
  993. dso__set_short_name(dso, dso->name, false);
  994. dso->symbols = dso->symbol_names = RB_ROOT;
  995. dso->data.cache = RB_ROOT;
  996. dso->inlined_nodes = RB_ROOT;
  997. dso->srclines = RB_ROOT;
  998. dso->data.fd = -1;
  999. dso->data.status = DSO_DATA_STATUS_UNKNOWN;
  1000. dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  1001. dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
  1002. dso->is_64_bit = (sizeof(void *) == 8);
  1003. dso->loaded = 0;
  1004. dso->rel = 0;
  1005. dso->sorted_by_name = 0;
  1006. dso->has_build_id = 0;
  1007. dso->has_srcline = 1;
  1008. dso->a2l_fails = 1;
  1009. dso->kernel = DSO_TYPE_USER;
  1010. dso->needs_swap = DSO_SWAP__UNSET;
  1011. dso->comp = COMP_ID__NONE;
  1012. RB_CLEAR_NODE(&dso->rb_node);
  1013. dso->root = NULL;
  1014. INIT_LIST_HEAD(&dso->node);
  1015. INIT_LIST_HEAD(&dso->data.open_entry);
  1016. pthread_mutex_init(&dso->lock, NULL);
  1017. refcount_set(&dso->refcnt, 1);
  1018. }
  1019. return dso;
  1020. }
  1021. void dso__delete(struct dso *dso)
  1022. {
  1023. if (!RB_EMPTY_NODE(&dso->rb_node))
  1024. pr_err("DSO %s is still in rbtree when being deleted!\n",
  1025. dso->long_name);
  1026. /* free inlines first, as they reference symbols */
  1027. inlines__tree_delete(&dso->inlined_nodes);
  1028. srcline__tree_delete(&dso->srclines);
  1029. symbols__delete(&dso->symbols);
  1030. if (dso->short_name_allocated) {
  1031. zfree((char **)&dso->short_name);
  1032. dso->short_name_allocated = false;
  1033. }
  1034. if (dso->long_name_allocated) {
  1035. zfree((char **)&dso->long_name);
  1036. dso->long_name_allocated = false;
  1037. }
  1038. dso__data_close(dso);
  1039. auxtrace_cache__free(dso->auxtrace_cache);
  1040. dso_cache__free(dso);
  1041. dso__free_a2l(dso);
  1042. zfree(&dso->symsrc_filename);
  1043. nsinfo__zput(dso->nsinfo);
  1044. pthread_mutex_destroy(&dso->lock);
  1045. free(dso);
  1046. }
  1047. struct dso *dso__get(struct dso *dso)
  1048. {
  1049. if (dso)
  1050. refcount_inc(&dso->refcnt);
  1051. return dso;
  1052. }
  1053. void dso__put(struct dso *dso)
  1054. {
  1055. if (dso && refcount_dec_and_test(&dso->refcnt))
  1056. dso__delete(dso);
  1057. }
  1058. void dso__set_build_id(struct dso *dso, void *build_id)
  1059. {
  1060. memcpy(dso->build_id, build_id, sizeof(dso->build_id));
  1061. dso->has_build_id = 1;
  1062. }
  1063. bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  1064. {
  1065. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  1066. }
  1067. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  1068. {
  1069. char path[PATH_MAX];
  1070. if (machine__is_default_guest(machine))
  1071. return;
  1072. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  1073. if (sysfs__read_build_id(path, dso->build_id,
  1074. sizeof(dso->build_id)) == 0)
  1075. dso->has_build_id = true;
  1076. }
  1077. int dso__kernel_module_get_build_id(struct dso *dso,
  1078. const char *root_dir)
  1079. {
  1080. char filename[PATH_MAX];
  1081. /*
  1082. * kernel module short names are of the form "[module]" and
  1083. * we need just "module" here.
  1084. */
  1085. const char *name = dso->short_name + 1;
  1086. snprintf(filename, sizeof(filename),
  1087. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  1088. root_dir, (int)strlen(name) - 1, name);
  1089. if (sysfs__read_build_id(filename, dso->build_id,
  1090. sizeof(dso->build_id)) == 0)
  1091. dso->has_build_id = true;
  1092. return 0;
  1093. }
  1094. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  1095. {
  1096. bool have_build_id = false;
  1097. struct dso *pos;
  1098. struct nscookie nsc;
  1099. list_for_each_entry(pos, head, node) {
  1100. if (with_hits && !pos->hit && !dso__is_vdso(pos))
  1101. continue;
  1102. if (pos->has_build_id) {
  1103. have_build_id = true;
  1104. continue;
  1105. }
  1106. nsinfo__mountns_enter(pos->nsinfo, &nsc);
  1107. if (filename__read_build_id(pos->long_name, pos->build_id,
  1108. sizeof(pos->build_id)) > 0) {
  1109. have_build_id = true;
  1110. pos->has_build_id = true;
  1111. }
  1112. nsinfo__mountns_exit(&nsc);
  1113. }
  1114. return have_build_id;
  1115. }
  1116. void __dsos__add(struct dsos *dsos, struct dso *dso)
  1117. {
  1118. list_add_tail(&dso->node, &dsos->head);
  1119. __dso__findlink_by_longname(&dsos->root, dso, NULL);
  1120. /*
  1121. * It is now in the linked list, grab a reference, then garbage collect
  1122. * this when needing memory, by looking at LRU dso instances in the
  1123. * list with atomic_read(&dso->refcnt) == 1, i.e. no references
  1124. * anywhere besides the one for the list, do, under a lock for the
  1125. * list: remove it from the list, then a dso__put(), that probably will
  1126. * be the last and will then call dso__delete(), end of life.
  1127. *
  1128. * That, or at the end of the 'struct machine' lifetime, when all
  1129. * 'struct dso' instances will be removed from the list, in
  1130. * dsos__exit(), if they have no other reference from some other data
  1131. * structure.
  1132. *
  1133. * E.g.: after processing a 'perf.data' file and storing references
  1134. * to objects instantiated while processing events, we will have
  1135. * references to the 'thread', 'map', 'dso' structs all from 'struct
  1136. * hist_entry' instances, but we may not need anything not referenced,
  1137. * so we might as well call machines__exit()/machines__delete() and
  1138. * garbage collect it.
  1139. */
  1140. dso__get(dso);
  1141. }
  1142. void dsos__add(struct dsos *dsos, struct dso *dso)
  1143. {
  1144. down_write(&dsos->lock);
  1145. __dsos__add(dsos, dso);
  1146. up_write(&dsos->lock);
  1147. }
  1148. struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
  1149. {
  1150. struct dso *pos;
  1151. if (cmp_short) {
  1152. list_for_each_entry(pos, &dsos->head, node)
  1153. if (strcmp(pos->short_name, name) == 0)
  1154. return pos;
  1155. return NULL;
  1156. }
  1157. return __dso__find_by_longname(&dsos->root, name);
  1158. }
  1159. struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
  1160. {
  1161. struct dso *dso;
  1162. down_read(&dsos->lock);
  1163. dso = __dsos__find(dsos, name, cmp_short);
  1164. up_read(&dsos->lock);
  1165. return dso;
  1166. }
  1167. struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
  1168. {
  1169. struct dso *dso = dso__new(name);
  1170. if (dso != NULL) {
  1171. __dsos__add(dsos, dso);
  1172. dso__set_basename(dso);
  1173. /* Put dso here because __dsos_add already got it */
  1174. dso__put(dso);
  1175. }
  1176. return dso;
  1177. }
  1178. struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
  1179. {
  1180. struct dso *dso = __dsos__find(dsos, name, false);
  1181. return dso ? dso : __dsos__addnew(dsos, name);
  1182. }
  1183. struct dso *dsos__findnew(struct dsos *dsos, const char *name)
  1184. {
  1185. struct dso *dso;
  1186. down_write(&dsos->lock);
  1187. dso = dso__get(__dsos__findnew(dsos, name));
  1188. up_write(&dsos->lock);
  1189. return dso;
  1190. }
  1191. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1192. bool (skip)(struct dso *dso, int parm), int parm)
  1193. {
  1194. struct dso *pos;
  1195. size_t ret = 0;
  1196. list_for_each_entry(pos, head, node) {
  1197. if (skip && skip(pos, parm))
  1198. continue;
  1199. ret += dso__fprintf_buildid(pos, fp);
  1200. ret += fprintf(fp, " %s\n", pos->long_name);
  1201. }
  1202. return ret;
  1203. }
  1204. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1205. {
  1206. struct dso *pos;
  1207. size_t ret = 0;
  1208. list_for_each_entry(pos, head, node) {
  1209. ret += dso__fprintf(pos, fp);
  1210. }
  1211. return ret;
  1212. }
  1213. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  1214. {
  1215. char sbuild_id[SBUILD_ID_SIZE];
  1216. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  1217. return fprintf(fp, "%s", sbuild_id);
  1218. }
  1219. size_t dso__fprintf(struct dso *dso, FILE *fp)
  1220. {
  1221. struct rb_node *nd;
  1222. size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
  1223. if (dso->short_name != dso->long_name)
  1224. ret += fprintf(fp, "%s, ", dso->long_name);
  1225. ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
  1226. ret += dso__fprintf_buildid(dso, fp);
  1227. ret += fprintf(fp, ")\n");
  1228. for (nd = rb_first(&dso->symbols); nd; nd = rb_next(nd)) {
  1229. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  1230. ret += symbol__fprintf(pos, fp);
  1231. }
  1232. return ret;
  1233. }
  1234. enum dso_type dso__type(struct dso *dso, struct machine *machine)
  1235. {
  1236. int fd;
  1237. enum dso_type type = DSO__TYPE_UNKNOWN;
  1238. fd = dso__data_get_fd(dso, machine);
  1239. if (fd >= 0) {
  1240. type = dso__type_fd(fd);
  1241. dso__data_put_fd(dso);
  1242. }
  1243. return type;
  1244. }
  1245. int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
  1246. {
  1247. int idx, errnum = dso->load_errno;
  1248. /*
  1249. * This must have a same ordering as the enum dso_load_errno.
  1250. */
  1251. static const char *dso_load__error_str[] = {
  1252. "Internal tools/perf/ library error",
  1253. "Invalid ELF file",
  1254. "Can not read build id",
  1255. "Mismatching build id",
  1256. "Decompression failure",
  1257. };
  1258. BUG_ON(buflen == 0);
  1259. if (errnum >= 0) {
  1260. const char *err = str_error_r(errnum, buf, buflen);
  1261. if (err != buf)
  1262. scnprintf(buf, buflen, "%s", err);
  1263. return 0;
  1264. }
  1265. if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
  1266. return -1;
  1267. idx = errnum - __DSO_LOAD_ERRNO__START;
  1268. scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
  1269. return 0;
  1270. }