build-id.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * build-id.c
  3. *
  4. * build-id support
  5. *
  6. * Copyright (C) 2009, 2010 Red Hat Inc.
  7. * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
  8. */
  9. #include "util.h"
  10. #include <stdio.h>
  11. #include "build-id.h"
  12. #include "event.h"
  13. #include "symbol.h"
  14. #include <linux/kernel.h>
  15. #include "debug.h"
  16. #include "session.h"
  17. #include "tool.h"
  18. #include "header.h"
  19. #include "vdso.h"
  20. static bool no_buildid_cache;
  21. int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,
  22. union perf_event *event,
  23. struct perf_sample *sample,
  24. struct perf_evsel *evsel __maybe_unused,
  25. struct machine *machine)
  26. {
  27. struct addr_location al;
  28. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  29. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  30. sample->tid);
  31. if (thread == NULL) {
  32. pr_err("problem processing %d event, skipping it.\n",
  33. event->header.type);
  34. return -1;
  35. }
  36. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al);
  37. if (al.map != NULL)
  38. al.map->dso->hit = 1;
  39. return 0;
  40. }
  41. static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
  42. union perf_event *event,
  43. struct perf_sample *sample
  44. __maybe_unused,
  45. struct machine *machine)
  46. {
  47. struct thread *thread = machine__findnew_thread(machine,
  48. event->fork.pid,
  49. event->fork.tid);
  50. dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
  51. event->fork.ppid, event->fork.ptid);
  52. if (thread) {
  53. rb_erase(&thread->rb_node, &machine->threads);
  54. if (machine->last_match == thread)
  55. thread__zput(machine->last_match);
  56. thread__put(thread);
  57. }
  58. return 0;
  59. }
  60. struct perf_tool build_id__mark_dso_hit_ops = {
  61. .sample = build_id__mark_dso_hit,
  62. .mmap = perf_event__process_mmap,
  63. .mmap2 = perf_event__process_mmap2,
  64. .fork = perf_event__process_fork,
  65. .exit = perf_event__exit_del_thread,
  66. .attr = perf_event__process_attr,
  67. .build_id = perf_event__process_build_id,
  68. };
  69. int build_id__sprintf(const u8 *build_id, int len, char *bf)
  70. {
  71. char *bid = bf;
  72. const u8 *raw = build_id;
  73. int i;
  74. for (i = 0; i < len; ++i) {
  75. sprintf(bid, "%02x", *raw);
  76. ++raw;
  77. bid += 2;
  78. }
  79. return raw - build_id;
  80. }
  81. /* asnprintf consolidates asprintf and snprintf */
  82. static int asnprintf(char **strp, size_t size, const char *fmt, ...)
  83. {
  84. va_list ap;
  85. int ret;
  86. if (!strp)
  87. return -EINVAL;
  88. va_start(ap, fmt);
  89. if (*strp)
  90. ret = vsnprintf(*strp, size, fmt, ap);
  91. else
  92. ret = vasprintf(strp, fmt, ap);
  93. va_end(ap);
  94. return ret;
  95. }
  96. static char *build_id__filename(const char *sbuild_id, char *bf, size_t size)
  97. {
  98. char *tmp = bf;
  99. int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
  100. sbuild_id, sbuild_id + 2);
  101. if (ret < 0 || (tmp && size < (unsigned int)ret))
  102. return NULL;
  103. return bf;
  104. }
  105. char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
  106. {
  107. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  108. if (!dso->has_build_id)
  109. return NULL;
  110. build_id__sprintf(dso->build_id, sizeof(dso->build_id), build_id_hex);
  111. return build_id__filename(build_id_hex, bf, size);
  112. }
  113. #define dsos__for_each_with_build_id(pos, head) \
  114. list_for_each_entry(pos, head, node) \
  115. if (!pos->has_build_id) \
  116. continue; \
  117. else
  118. static int write_buildid(const char *name, size_t name_len, u8 *build_id,
  119. pid_t pid, u16 misc, int fd)
  120. {
  121. int err;
  122. struct build_id_event b;
  123. size_t len;
  124. len = name_len + 1;
  125. len = PERF_ALIGN(len, NAME_ALIGN);
  126. memset(&b, 0, sizeof(b));
  127. memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
  128. b.pid = pid;
  129. b.header.misc = misc;
  130. b.header.size = sizeof(b) + len;
  131. err = writen(fd, &b, sizeof(b));
  132. if (err < 0)
  133. return err;
  134. return write_padded(fd, name, name_len + 1, len);
  135. }
  136. static int __dsos__write_buildid_table(struct list_head *head,
  137. struct machine *machine,
  138. pid_t pid, u16 misc, int fd)
  139. {
  140. char nm[PATH_MAX];
  141. struct dso *pos;
  142. dsos__for_each_with_build_id(pos, head) {
  143. int err;
  144. const char *name;
  145. size_t name_len;
  146. if (!pos->hit)
  147. continue;
  148. if (dso__is_vdso(pos)) {
  149. name = pos->short_name;
  150. name_len = pos->short_name_len + 1;
  151. } else if (dso__is_kcore(pos)) {
  152. machine__mmap_name(machine, nm, sizeof(nm));
  153. name = nm;
  154. name_len = strlen(nm) + 1;
  155. } else {
  156. name = pos->long_name;
  157. name_len = pos->long_name_len + 1;
  158. }
  159. err = write_buildid(name, name_len, pos->build_id,
  160. pid, misc, fd);
  161. if (err)
  162. return err;
  163. }
  164. return 0;
  165. }
  166. static int machine__write_buildid_table(struct machine *machine, int fd)
  167. {
  168. int err;
  169. u16 kmisc = PERF_RECORD_MISC_KERNEL,
  170. umisc = PERF_RECORD_MISC_USER;
  171. if (!machine__is_host(machine)) {
  172. kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
  173. umisc = PERF_RECORD_MISC_GUEST_USER;
  174. }
  175. err = __dsos__write_buildid_table(&machine->kernel_dsos.head, machine,
  176. machine->pid, kmisc, fd);
  177. if (err == 0)
  178. err = __dsos__write_buildid_table(&machine->user_dsos.head,
  179. machine, machine->pid, umisc,
  180. fd);
  181. return err;
  182. }
  183. int perf_session__write_buildid_table(struct perf_session *session, int fd)
  184. {
  185. struct rb_node *nd;
  186. int err = machine__write_buildid_table(&session->machines.host, fd);
  187. if (err)
  188. return err;
  189. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  190. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  191. err = machine__write_buildid_table(pos, fd);
  192. if (err)
  193. break;
  194. }
  195. return err;
  196. }
  197. static int __dsos__hit_all(struct list_head *head)
  198. {
  199. struct dso *pos;
  200. list_for_each_entry(pos, head, node)
  201. pos->hit = true;
  202. return 0;
  203. }
  204. static int machine__hit_all_dsos(struct machine *machine)
  205. {
  206. int err;
  207. err = __dsos__hit_all(&machine->kernel_dsos.head);
  208. if (err)
  209. return err;
  210. return __dsos__hit_all(&machine->user_dsos.head);
  211. }
  212. int dsos__hit_all(struct perf_session *session)
  213. {
  214. struct rb_node *nd;
  215. int err;
  216. err = machine__hit_all_dsos(&session->machines.host);
  217. if (err)
  218. return err;
  219. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  220. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  221. err = machine__hit_all_dsos(pos);
  222. if (err)
  223. return err;
  224. }
  225. return 0;
  226. }
  227. void disable_buildid_cache(void)
  228. {
  229. no_buildid_cache = true;
  230. }
  231. static char *build_id_cache__dirname_from_path(const char *name,
  232. bool is_kallsyms, bool is_vdso)
  233. {
  234. char *realname = (char *)name, *filename;
  235. bool slash = is_kallsyms || is_vdso;
  236. if (!slash) {
  237. realname = realpath(name, NULL);
  238. if (!realname)
  239. return NULL;
  240. }
  241. if (asprintf(&filename, "%s%s%s", buildid_dir, slash ? "/" : "",
  242. is_vdso ? DSO__NAME_VDSO : realname) < 0)
  243. filename = NULL;
  244. if (!slash)
  245. free(realname);
  246. return filename;
  247. }
  248. int build_id_cache__list_build_ids(const char *pathname,
  249. struct strlist **result)
  250. {
  251. struct strlist *list;
  252. char *dir_name;
  253. DIR *dir;
  254. struct dirent *d;
  255. int ret = 0;
  256. list = strlist__new(true, NULL);
  257. dir_name = build_id_cache__dirname_from_path(pathname, false, false);
  258. if (!list || !dir_name) {
  259. ret = -ENOMEM;
  260. goto out;
  261. }
  262. /* List up all dirents */
  263. dir = opendir(dir_name);
  264. if (!dir) {
  265. ret = -errno;
  266. goto out;
  267. }
  268. while ((d = readdir(dir)) != NULL) {
  269. if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
  270. continue;
  271. strlist__add(list, d->d_name);
  272. }
  273. closedir(dir);
  274. out:
  275. free(dir_name);
  276. if (ret)
  277. strlist__delete(list);
  278. else
  279. *result = list;
  280. return ret;
  281. }
  282. int build_id_cache__add_s(const char *sbuild_id, const char *name,
  283. bool is_kallsyms, bool is_vdso)
  284. {
  285. const size_t size = PATH_MAX;
  286. char *realname = NULL, *filename = NULL, *dir_name = NULL,
  287. *linkname = zalloc(size), *targetname, *tmp;
  288. int err = -1;
  289. if (!is_kallsyms) {
  290. realname = realpath(name, NULL);
  291. if (!realname)
  292. goto out_free;
  293. }
  294. dir_name = build_id_cache__dirname_from_path(name, is_kallsyms, is_vdso);
  295. if (!dir_name)
  296. goto out_free;
  297. if (mkdir_p(dir_name, 0755))
  298. goto out_free;
  299. if (asprintf(&filename, "%s/%s", dir_name, sbuild_id) < 0) {
  300. filename = NULL;
  301. goto out_free;
  302. }
  303. if (access(filename, F_OK)) {
  304. if (is_kallsyms) {
  305. if (copyfile("/proc/kallsyms", filename))
  306. goto out_free;
  307. } else if (link(realname, filename) && errno != EEXIST &&
  308. copyfile(name, filename))
  309. goto out_free;
  310. }
  311. if (!build_id__filename(sbuild_id, linkname, size))
  312. goto out_free;
  313. tmp = strrchr(linkname, '/');
  314. *tmp = '\0';
  315. if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
  316. goto out_free;
  317. *tmp = '/';
  318. targetname = filename + strlen(buildid_dir) - 5;
  319. memcpy(targetname, "../..", 5);
  320. if (symlink(targetname, linkname) == 0)
  321. err = 0;
  322. out_free:
  323. if (!is_kallsyms)
  324. free(realname);
  325. free(filename);
  326. free(dir_name);
  327. free(linkname);
  328. return err;
  329. }
  330. static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
  331. const char *name, bool is_kallsyms,
  332. bool is_vdso)
  333. {
  334. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  335. build_id__sprintf(build_id, build_id_size, sbuild_id);
  336. return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso);
  337. }
  338. bool build_id_cache__cached(const char *sbuild_id)
  339. {
  340. bool ret = false;
  341. char *filename = build_id__filename(sbuild_id, NULL, 0);
  342. if (filename && !access(filename, F_OK))
  343. ret = true;
  344. free(filename);
  345. return ret;
  346. }
  347. int build_id_cache__remove_s(const char *sbuild_id)
  348. {
  349. const size_t size = PATH_MAX;
  350. char *filename = zalloc(size),
  351. *linkname = zalloc(size), *tmp;
  352. int err = -1;
  353. if (filename == NULL || linkname == NULL)
  354. goto out_free;
  355. if (!build_id__filename(sbuild_id, linkname, size))
  356. goto out_free;
  357. if (access(linkname, F_OK))
  358. goto out_free;
  359. if (readlink(linkname, filename, size - 1) < 0)
  360. goto out_free;
  361. if (unlink(linkname))
  362. goto out_free;
  363. /*
  364. * Since the link is relative, we must make it absolute:
  365. */
  366. tmp = strrchr(linkname, '/') + 1;
  367. snprintf(tmp, size - (tmp - linkname), "%s", filename);
  368. if (unlink(linkname))
  369. goto out_free;
  370. err = 0;
  371. out_free:
  372. free(filename);
  373. free(linkname);
  374. return err;
  375. }
  376. static int dso__cache_build_id(struct dso *dso, struct machine *machine)
  377. {
  378. bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
  379. bool is_vdso = dso__is_vdso(dso);
  380. const char *name = dso->long_name;
  381. char nm[PATH_MAX];
  382. if (dso__is_kcore(dso)) {
  383. is_kallsyms = true;
  384. machine__mmap_name(machine, nm, sizeof(nm));
  385. name = nm;
  386. }
  387. return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
  388. is_kallsyms, is_vdso);
  389. }
  390. static int __dsos__cache_build_ids(struct list_head *head,
  391. struct machine *machine)
  392. {
  393. struct dso *pos;
  394. int err = 0;
  395. dsos__for_each_with_build_id(pos, head)
  396. if (dso__cache_build_id(pos, machine))
  397. err = -1;
  398. return err;
  399. }
  400. static int machine__cache_build_ids(struct machine *machine)
  401. {
  402. int ret = __dsos__cache_build_ids(&machine->kernel_dsos.head, machine);
  403. ret |= __dsos__cache_build_ids(&machine->user_dsos.head, machine);
  404. return ret;
  405. }
  406. int perf_session__cache_build_ids(struct perf_session *session)
  407. {
  408. struct rb_node *nd;
  409. int ret;
  410. if (no_buildid_cache)
  411. return 0;
  412. if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
  413. return -1;
  414. ret = machine__cache_build_ids(&session->machines.host);
  415. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  416. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  417. ret |= machine__cache_build_ids(pos);
  418. }
  419. return ret ? -1 : 0;
  420. }
  421. static bool machine__read_build_ids(struct machine *machine, bool with_hits)
  422. {
  423. bool ret;
  424. ret = __dsos__read_build_ids(&machine->kernel_dsos.head, with_hits);
  425. ret |= __dsos__read_build_ids(&machine->user_dsos.head, with_hits);
  426. return ret;
  427. }
  428. bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
  429. {
  430. struct rb_node *nd;
  431. bool ret = machine__read_build_ids(&session->machines.host, with_hits);
  432. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  433. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  434. ret |= machine__read_build_ids(pos, with_hits);
  435. }
  436. return ret;
  437. }