dso.c 13 KB

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