dso.c 13 KB

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