dso.c 14 KB

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