dso.c 35 KB

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