dso.c 13 KB

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