dso.c 33 KB

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