dso.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. #include "symbol.h"
  2. #include "dso.h"
  3. #include "machine.h"
  4. #include "util.h"
  5. #include "debug.h"
  6. char dso__symtab_origin(const struct dso *dso)
  7. {
  8. static const char origin[] = {
  9. [DSO_BINARY_TYPE__KALLSYMS] = 'k',
  10. [DSO_BINARY_TYPE__VMLINUX] = 'v',
  11. [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
  12. [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
  13. [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
  14. [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
  15. [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
  16. [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
  17. [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
  18. [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
  19. [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
  20. [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
  21. [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
  22. [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
  23. };
  24. if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
  25. return '!';
  26. return origin[dso->symtab_type];
  27. }
  28. int dso__read_binary_type_filename(const struct dso *dso,
  29. enum dso_binary_type type,
  30. char *root_dir, char *filename, size_t size)
  31. {
  32. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  33. int ret = 0;
  34. switch (type) {
  35. case DSO_BINARY_TYPE__DEBUGLINK: {
  36. char *debuglink;
  37. strncpy(filename, dso->long_name, size);
  38. debuglink = filename + dso->long_name_len;
  39. while (debuglink != filename && *debuglink != '/')
  40. debuglink--;
  41. if (*debuglink == '/')
  42. debuglink++;
  43. ret = filename__read_debuglink(dso->long_name, debuglink,
  44. size - (debuglink - filename));
  45. }
  46. break;
  47. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  48. /* skip the locally configured cache if a symfs is given */
  49. if (symbol_conf.symfs[0] ||
  50. (dso__build_id_filename(dso, filename, size) == NULL))
  51. ret = -1;
  52. break;
  53. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  54. snprintf(filename, size, "%s/usr/lib/debug%s.debug",
  55. symbol_conf.symfs, dso->long_name);
  56. break;
  57. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  58. snprintf(filename, size, "%s/usr/lib/debug%s",
  59. symbol_conf.symfs, dso->long_name);
  60. break;
  61. case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
  62. {
  63. const char *last_slash;
  64. size_t len;
  65. size_t dir_size;
  66. last_slash = dso->long_name + dso->long_name_len;
  67. while (last_slash != dso->long_name && *last_slash != '/')
  68. last_slash--;
  69. len = scnprintf(filename, size, "%s", symbol_conf.symfs);
  70. dir_size = last_slash - dso->long_name + 2;
  71. if (dir_size > (size - len)) {
  72. ret = -1;
  73. break;
  74. }
  75. len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
  76. len += scnprintf(filename + len , size - len, ".debug%s",
  77. last_slash);
  78. break;
  79. }
  80. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  81. if (!dso->has_build_id) {
  82. ret = -1;
  83. break;
  84. }
  85. build_id__sprintf(dso->build_id,
  86. sizeof(dso->build_id),
  87. build_id_hex);
  88. snprintf(filename, size,
  89. "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
  90. symbol_conf.symfs, build_id_hex, build_id_hex + 2);
  91. break;
  92. case DSO_BINARY_TYPE__VMLINUX:
  93. case DSO_BINARY_TYPE__GUEST_VMLINUX:
  94. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  95. snprintf(filename, size, "%s%s",
  96. symbol_conf.symfs, dso->long_name);
  97. break;
  98. case DSO_BINARY_TYPE__GUEST_KMODULE:
  99. snprintf(filename, size, "%s%s%s", symbol_conf.symfs,
  100. root_dir, dso->long_name);
  101. break;
  102. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  103. snprintf(filename, size, "%s%s", symbol_conf.symfs,
  104. dso->long_name);
  105. break;
  106. case DSO_BINARY_TYPE__KCORE:
  107. case DSO_BINARY_TYPE__GUEST_KCORE:
  108. snprintf(filename, size, "%s", dso->long_name);
  109. break;
  110. default:
  111. case DSO_BINARY_TYPE__KALLSYMS:
  112. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  113. case DSO_BINARY_TYPE__JAVA_JIT:
  114. case DSO_BINARY_TYPE__NOT_FOUND:
  115. ret = -1;
  116. break;
  117. }
  118. return ret;
  119. }
  120. static int open_dso(struct dso *dso, struct machine *machine)
  121. {
  122. int fd;
  123. char *root_dir = (char *)"";
  124. char *name = malloc(PATH_MAX);
  125. if (!name)
  126. return -ENOMEM;
  127. if (machine)
  128. root_dir = machine->root_dir;
  129. if (dso__read_binary_type_filename(dso, dso->binary_type,
  130. root_dir, name, PATH_MAX)) {
  131. free(name);
  132. return -EINVAL;
  133. }
  134. fd = open(name, O_RDONLY);
  135. free(name);
  136. return fd;
  137. }
  138. int dso__data_fd(struct dso *dso, struct machine *machine)
  139. {
  140. enum dso_binary_type binary_type_data[] = {
  141. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  142. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  143. DSO_BINARY_TYPE__NOT_FOUND,
  144. };
  145. int i = 0;
  146. if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND)
  147. return open_dso(dso, machine);
  148. do {
  149. int fd;
  150. dso->binary_type = binary_type_data[i++];
  151. fd = open_dso(dso, machine);
  152. if (fd >= 0)
  153. return fd;
  154. } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
  155. return -EINVAL;
  156. }
  157. static void
  158. dso_cache__free(struct rb_root *root)
  159. {
  160. struct rb_node *next = rb_first(root);
  161. while (next) {
  162. struct dso_cache *cache;
  163. cache = rb_entry(next, struct dso_cache, rb_node);
  164. next = rb_next(&cache->rb_node);
  165. rb_erase(&cache->rb_node, root);
  166. free(cache);
  167. }
  168. }
  169. static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
  170. {
  171. struct rb_node * const *p = &root->rb_node;
  172. const struct rb_node *parent = NULL;
  173. struct dso_cache *cache;
  174. while (*p != NULL) {
  175. u64 end;
  176. parent = *p;
  177. cache = rb_entry(parent, struct dso_cache, rb_node);
  178. end = cache->offset + DSO__DATA_CACHE_SIZE;
  179. if (offset < cache->offset)
  180. p = &(*p)->rb_left;
  181. else if (offset >= end)
  182. p = &(*p)->rb_right;
  183. else
  184. return cache;
  185. }
  186. return NULL;
  187. }
  188. static void
  189. dso_cache__insert(struct rb_root *root, struct dso_cache *new)
  190. {
  191. struct rb_node **p = &root->rb_node;
  192. struct rb_node *parent = NULL;
  193. struct dso_cache *cache;
  194. u64 offset = new->offset;
  195. while (*p != NULL) {
  196. u64 end;
  197. parent = *p;
  198. cache = rb_entry(parent, struct dso_cache, rb_node);
  199. end = cache->offset + DSO__DATA_CACHE_SIZE;
  200. if (offset < cache->offset)
  201. p = &(*p)->rb_left;
  202. else if (offset >= end)
  203. p = &(*p)->rb_right;
  204. }
  205. rb_link_node(&new->rb_node, parent, p);
  206. rb_insert_color(&new->rb_node, root);
  207. }
  208. static ssize_t
  209. dso_cache__memcpy(struct dso_cache *cache, u64 offset,
  210. u8 *data, u64 size)
  211. {
  212. u64 cache_offset = offset - cache->offset;
  213. u64 cache_size = min(cache->size - cache_offset, size);
  214. memcpy(data, cache->data + cache_offset, cache_size);
  215. return cache_size;
  216. }
  217. static ssize_t
  218. dso_cache__read(struct dso *dso, struct machine *machine,
  219. u64 offset, u8 *data, ssize_t size)
  220. {
  221. struct dso_cache *cache;
  222. ssize_t ret;
  223. int fd;
  224. fd = dso__data_fd(dso, machine);
  225. if (fd < 0)
  226. return -1;
  227. do {
  228. u64 cache_offset;
  229. ret = -ENOMEM;
  230. cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
  231. if (!cache)
  232. break;
  233. cache_offset = offset & DSO__DATA_CACHE_MASK;
  234. ret = -EINVAL;
  235. if (-1 == lseek(fd, cache_offset, SEEK_SET))
  236. break;
  237. ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
  238. if (ret <= 0)
  239. break;
  240. cache->offset = cache_offset;
  241. cache->size = ret;
  242. dso_cache__insert(&dso->cache, cache);
  243. ret = dso_cache__memcpy(cache, offset, data, size);
  244. } while (0);
  245. if (ret <= 0)
  246. free(cache);
  247. close(fd);
  248. return ret;
  249. }
  250. static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
  251. u64 offset, u8 *data, ssize_t size)
  252. {
  253. struct dso_cache *cache;
  254. cache = dso_cache__find(&dso->cache, offset);
  255. if (cache)
  256. return dso_cache__memcpy(cache, offset, data, size);
  257. else
  258. return dso_cache__read(dso, machine, offset, data, size);
  259. }
  260. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  261. u64 offset, u8 *data, ssize_t size)
  262. {
  263. ssize_t r = 0;
  264. u8 *p = data;
  265. do {
  266. ssize_t ret;
  267. ret = dso_cache_read(dso, machine, offset, p, size);
  268. if (ret < 0)
  269. return ret;
  270. /* Reached EOF, return what we have. */
  271. if (!ret)
  272. break;
  273. BUG_ON(ret > size);
  274. r += ret;
  275. p += ret;
  276. offset += ret;
  277. size -= ret;
  278. } while (size);
  279. return r;
  280. }
  281. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  282. struct machine *machine, u64 addr,
  283. u8 *data, ssize_t size)
  284. {
  285. u64 offset = map->map_ip(map, addr);
  286. return dso__data_read_offset(dso, machine, offset, data, size);
  287. }
  288. struct map *dso__new_map(const char *name)
  289. {
  290. struct map *map = NULL;
  291. struct dso *dso = dso__new(name);
  292. if (dso)
  293. map = map__new2(0, dso, MAP__FUNCTION);
  294. return map;
  295. }
  296. struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
  297. const char *short_name, int dso_type)
  298. {
  299. /*
  300. * The kernel dso could be created by build_id processing.
  301. */
  302. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
  303. /*
  304. * We need to run this in all cases, since during the build_id
  305. * processing we had no idea this was the kernel dso.
  306. */
  307. if (dso != NULL) {
  308. dso__set_short_name(dso, short_name, false);
  309. dso->kernel = dso_type;
  310. }
  311. return dso;
  312. }
  313. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
  314. {
  315. if (name == NULL)
  316. return;
  317. if (dso->long_name_allocated)
  318. free((char *)dso->long_name);
  319. dso->long_name = name;
  320. dso->long_name_len = strlen(name);
  321. dso->long_name_allocated = name_allocated;
  322. }
  323. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
  324. {
  325. if (name == NULL)
  326. return;
  327. if (dso->short_name_allocated)
  328. free((char *)dso->short_name);
  329. dso->short_name = name;
  330. dso->short_name_len = strlen(name);
  331. dso->short_name_allocated = name_allocated;
  332. }
  333. static void dso__set_basename(struct dso *dso)
  334. {
  335. /*
  336. * basename() may modify path buffer, so we must pass
  337. * a copy.
  338. */
  339. char *base, *lname = strdup(dso->long_name);
  340. if (!lname)
  341. return;
  342. /*
  343. * basename() may return a pointer to internal
  344. * storage which is reused in subsequent calls
  345. * so copy the result.
  346. */
  347. base = strdup(basename(lname));
  348. free(lname);
  349. if (!base)
  350. return;
  351. dso__set_short_name(dso, base, true);
  352. }
  353. int dso__name_len(const struct dso *dso)
  354. {
  355. if (!dso)
  356. return strlen("[unknown]");
  357. if (verbose)
  358. return dso->long_name_len;
  359. return dso->short_name_len;
  360. }
  361. bool dso__loaded(const struct dso *dso, enum map_type type)
  362. {
  363. return dso->loaded & (1 << type);
  364. }
  365. bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
  366. {
  367. return dso->sorted_by_name & (1 << type);
  368. }
  369. void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
  370. {
  371. dso->sorted_by_name |= (1 << type);
  372. }
  373. struct dso *dso__new(const char *name)
  374. {
  375. struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
  376. if (dso != NULL) {
  377. int i;
  378. strcpy(dso->name, name);
  379. dso__set_long_name(dso, dso->name, false);
  380. dso__set_short_name(dso, dso->name, false);
  381. for (i = 0; i < MAP__NR_TYPES; ++i)
  382. dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
  383. dso->cache = RB_ROOT;
  384. dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  385. dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
  386. dso->loaded = 0;
  387. dso->rel = 0;
  388. dso->sorted_by_name = 0;
  389. dso->has_build_id = 0;
  390. dso->has_srcline = 1;
  391. dso->a2l_fails = 1;
  392. dso->kernel = DSO_TYPE_USER;
  393. dso->needs_swap = DSO_SWAP__UNSET;
  394. INIT_LIST_HEAD(&dso->node);
  395. }
  396. return dso;
  397. }
  398. void dso__delete(struct dso *dso)
  399. {
  400. int i;
  401. for (i = 0; i < MAP__NR_TYPES; ++i)
  402. symbols__delete(&dso->symbols[i]);
  403. if (dso->short_name_allocated) {
  404. zfree((char **)&dso->short_name);
  405. dso->short_name_allocated = false;
  406. }
  407. if (dso->long_name_allocated) {
  408. zfree((char **)&dso->long_name);
  409. dso->long_name_allocated = false;
  410. }
  411. dso_cache__free(&dso->cache);
  412. dso__free_a2l(dso);
  413. zfree(&dso->symsrc_filename);
  414. free(dso);
  415. }
  416. void dso__set_build_id(struct dso *dso, void *build_id)
  417. {
  418. memcpy(dso->build_id, build_id, sizeof(dso->build_id));
  419. dso->has_build_id = 1;
  420. }
  421. bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  422. {
  423. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  424. }
  425. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  426. {
  427. char path[PATH_MAX];
  428. if (machine__is_default_guest(machine))
  429. return;
  430. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  431. if (sysfs__read_build_id(path, dso->build_id,
  432. sizeof(dso->build_id)) == 0)
  433. dso->has_build_id = true;
  434. }
  435. int dso__kernel_module_get_build_id(struct dso *dso,
  436. const char *root_dir)
  437. {
  438. char filename[PATH_MAX];
  439. /*
  440. * kernel module short names are of the form "[module]" and
  441. * we need just "module" here.
  442. */
  443. const char *name = dso->short_name + 1;
  444. snprintf(filename, sizeof(filename),
  445. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  446. root_dir, (int)strlen(name) - 1, name);
  447. if (sysfs__read_build_id(filename, dso->build_id,
  448. sizeof(dso->build_id)) == 0)
  449. dso->has_build_id = true;
  450. return 0;
  451. }
  452. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  453. {
  454. bool have_build_id = false;
  455. struct dso *pos;
  456. list_for_each_entry(pos, head, node) {
  457. if (with_hits && !pos->hit)
  458. continue;
  459. if (pos->has_build_id) {
  460. have_build_id = true;
  461. continue;
  462. }
  463. if (filename__read_build_id(pos->long_name, pos->build_id,
  464. sizeof(pos->build_id)) > 0) {
  465. have_build_id = true;
  466. pos->has_build_id = true;
  467. }
  468. }
  469. return have_build_id;
  470. }
  471. void dsos__add(struct list_head *head, struct dso *dso)
  472. {
  473. list_add_tail(&dso->node, head);
  474. }
  475. struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short)
  476. {
  477. struct dso *pos;
  478. if (cmp_short) {
  479. list_for_each_entry(pos, head, node)
  480. if (strcmp(pos->short_name, name) == 0)
  481. return pos;
  482. return NULL;
  483. }
  484. list_for_each_entry(pos, head, node)
  485. if (strcmp(pos->long_name, name) == 0)
  486. return pos;
  487. return NULL;
  488. }
  489. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  490. {
  491. struct dso *dso = dsos__find(head, name, false);
  492. if (!dso) {
  493. dso = dso__new(name);
  494. if (dso != NULL) {
  495. dsos__add(head, dso);
  496. dso__set_basename(dso);
  497. }
  498. }
  499. return dso;
  500. }
  501. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  502. bool (skip)(struct dso *dso, int parm), int parm)
  503. {
  504. struct dso *pos;
  505. size_t ret = 0;
  506. list_for_each_entry(pos, head, node) {
  507. if (skip && skip(pos, parm))
  508. continue;
  509. ret += dso__fprintf_buildid(pos, fp);
  510. ret += fprintf(fp, " %s\n", pos->long_name);
  511. }
  512. return ret;
  513. }
  514. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  515. {
  516. struct dso *pos;
  517. size_t ret = 0;
  518. list_for_each_entry(pos, head, node) {
  519. int i;
  520. for (i = 0; i < MAP__NR_TYPES; ++i)
  521. ret += dso__fprintf(pos, i, fp);
  522. }
  523. return ret;
  524. }
  525. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  526. {
  527. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  528. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  529. return fprintf(fp, "%s", sbuild_id);
  530. }
  531. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
  532. {
  533. struct rb_node *nd;
  534. size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
  535. if (dso->short_name != dso->long_name)
  536. ret += fprintf(fp, "%s, ", dso->long_name);
  537. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  538. dso__loaded(dso, type) ? "" : "NOT ");
  539. ret += dso__fprintf_buildid(dso, fp);
  540. ret += fprintf(fp, ")\n");
  541. for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
  542. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  543. ret += symbol__fprintf(pos, fp);
  544. }
  545. return ret;
  546. }