dso.c 31 KB

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