dso.c 34 KB

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