dso.c 34 KB

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