build-id.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. machine->last_match = NULL;
  55. thread__delete(thread);
  56. }
  57. return 0;
  58. }
  59. struct perf_tool build_id__mark_dso_hit_ops = {
  60. .sample = build_id__mark_dso_hit,
  61. .mmap = perf_event__process_mmap,
  62. .mmap2 = perf_event__process_mmap2,
  63. .fork = perf_event__process_fork,
  64. .exit = perf_event__exit_del_thread,
  65. .attr = perf_event__process_attr,
  66. .build_id = perf_event__process_build_id,
  67. };
  68. int build_id__sprintf(const u8 *build_id, int len, char *bf)
  69. {
  70. char *bid = bf;
  71. const u8 *raw = build_id;
  72. int i;
  73. for (i = 0; i < len; ++i) {
  74. sprintf(bid, "%02x", *raw);
  75. ++raw;
  76. bid += 2;
  77. }
  78. return raw - build_id;
  79. }
  80. char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
  81. {
  82. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  83. if (!dso->has_build_id)
  84. return NULL;
  85. build_id__sprintf(dso->build_id, sizeof(dso->build_id), build_id_hex);
  86. if (bf == NULL) {
  87. if (asprintf(&bf, "%s/.build-id/%.2s/%s", buildid_dir,
  88. build_id_hex, build_id_hex + 2) < 0)
  89. return NULL;
  90. } else
  91. snprintf(bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
  92. build_id_hex, build_id_hex + 2);
  93. return bf;
  94. }
  95. #define dsos__for_each_with_build_id(pos, head) \
  96. list_for_each_entry(pos, head, node) \
  97. if (!pos->has_build_id) \
  98. continue; \
  99. else
  100. static int write_buildid(const char *name, size_t name_len, u8 *build_id,
  101. pid_t pid, u16 misc, int fd)
  102. {
  103. int err;
  104. struct build_id_event b;
  105. size_t len;
  106. len = name_len + 1;
  107. len = PERF_ALIGN(len, NAME_ALIGN);
  108. memset(&b, 0, sizeof(b));
  109. memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
  110. b.pid = pid;
  111. b.header.misc = misc;
  112. b.header.size = sizeof(b) + len;
  113. err = writen(fd, &b, sizeof(b));
  114. if (err < 0)
  115. return err;
  116. return write_padded(fd, name, name_len + 1, len);
  117. }
  118. static int __dsos__write_buildid_table(struct list_head *head,
  119. struct machine *machine,
  120. pid_t pid, u16 misc, int fd)
  121. {
  122. char nm[PATH_MAX];
  123. struct dso *pos;
  124. dsos__for_each_with_build_id(pos, head) {
  125. int err;
  126. const char *name;
  127. size_t name_len;
  128. if (!pos->hit)
  129. continue;
  130. if (dso__is_vdso(pos)) {
  131. name = pos->short_name;
  132. name_len = pos->short_name_len + 1;
  133. } else if (dso__is_kcore(pos)) {
  134. machine__mmap_name(machine, nm, sizeof(nm));
  135. name = nm;
  136. name_len = strlen(nm) + 1;
  137. } else {
  138. name = pos->long_name;
  139. name_len = pos->long_name_len + 1;
  140. }
  141. err = write_buildid(name, name_len, pos->build_id,
  142. pid, misc, fd);
  143. if (err)
  144. return err;
  145. }
  146. return 0;
  147. }
  148. static int machine__write_buildid_table(struct machine *machine, int fd)
  149. {
  150. int err;
  151. u16 kmisc = PERF_RECORD_MISC_KERNEL,
  152. umisc = PERF_RECORD_MISC_USER;
  153. if (!machine__is_host(machine)) {
  154. kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
  155. umisc = PERF_RECORD_MISC_GUEST_USER;
  156. }
  157. err = __dsos__write_buildid_table(&machine->kernel_dsos.head, machine,
  158. machine->pid, kmisc, fd);
  159. if (err == 0)
  160. err = __dsos__write_buildid_table(&machine->user_dsos.head,
  161. machine, machine->pid, umisc,
  162. fd);
  163. return err;
  164. }
  165. int perf_session__write_buildid_table(struct perf_session *session, int fd)
  166. {
  167. struct rb_node *nd;
  168. int err = machine__write_buildid_table(&session->machines.host, fd);
  169. if (err)
  170. return err;
  171. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  172. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  173. err = machine__write_buildid_table(pos, fd);
  174. if (err)
  175. break;
  176. }
  177. return err;
  178. }
  179. static int __dsos__hit_all(struct list_head *head)
  180. {
  181. struct dso *pos;
  182. list_for_each_entry(pos, head, node)
  183. pos->hit = true;
  184. return 0;
  185. }
  186. static int machine__hit_all_dsos(struct machine *machine)
  187. {
  188. int err;
  189. err = __dsos__hit_all(&machine->kernel_dsos.head);
  190. if (err)
  191. return err;
  192. return __dsos__hit_all(&machine->user_dsos.head);
  193. }
  194. int dsos__hit_all(struct perf_session *session)
  195. {
  196. struct rb_node *nd;
  197. int err;
  198. err = machine__hit_all_dsos(&session->machines.host);
  199. if (err)
  200. return err;
  201. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  202. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  203. err = machine__hit_all_dsos(pos);
  204. if (err)
  205. return err;
  206. }
  207. return 0;
  208. }
  209. void disable_buildid_cache(void)
  210. {
  211. no_buildid_cache = true;
  212. }
  213. int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
  214. const char *name, bool is_kallsyms, bool is_vdso)
  215. {
  216. const size_t size = PATH_MAX;
  217. char *realname, *filename = zalloc(size),
  218. *linkname = zalloc(size), *targetname;
  219. int len, err = -1;
  220. bool slash = is_kallsyms || is_vdso;
  221. if (is_kallsyms) {
  222. if (symbol_conf.kptr_restrict) {
  223. pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
  224. err = 0;
  225. goto out_free;
  226. }
  227. realname = (char *) name;
  228. } else
  229. realname = realpath(name, NULL);
  230. if (realname == NULL || filename == NULL || linkname == NULL)
  231. goto out_free;
  232. len = scnprintf(filename, size, "%s%s%s",
  233. debugdir, slash ? "/" : "",
  234. is_vdso ? DSO__NAME_VDSO : realname);
  235. if (mkdir_p(filename, 0755))
  236. goto out_free;
  237. snprintf(filename + len, size - len, "/%s", sbuild_id);
  238. if (access(filename, F_OK)) {
  239. if (is_kallsyms) {
  240. if (copyfile("/proc/kallsyms", filename))
  241. goto out_free;
  242. } else if (link(realname, filename) && copyfile(name, filename))
  243. goto out_free;
  244. }
  245. len = scnprintf(linkname, size, "%s/.build-id/%.2s",
  246. debugdir, sbuild_id);
  247. if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
  248. goto out_free;
  249. snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
  250. targetname = filename + strlen(debugdir) - 5;
  251. memcpy(targetname, "../..", 5);
  252. if (symlink(targetname, linkname) == 0)
  253. err = 0;
  254. out_free:
  255. if (!is_kallsyms)
  256. free(realname);
  257. free(filename);
  258. free(linkname);
  259. return err;
  260. }
  261. static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
  262. const char *name, const char *debugdir,
  263. bool is_kallsyms, bool is_vdso)
  264. {
  265. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  266. build_id__sprintf(build_id, build_id_size, sbuild_id);
  267. return build_id_cache__add_s(sbuild_id, debugdir, name,
  268. is_kallsyms, is_vdso);
  269. }
  270. int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
  271. {
  272. const size_t size = PATH_MAX;
  273. char *filename = zalloc(size),
  274. *linkname = zalloc(size);
  275. int err = -1;
  276. if (filename == NULL || linkname == NULL)
  277. goto out_free;
  278. snprintf(linkname, size, "%s/.build-id/%.2s/%s",
  279. debugdir, sbuild_id, sbuild_id + 2);
  280. if (access(linkname, F_OK))
  281. goto out_free;
  282. if (readlink(linkname, filename, size - 1) < 0)
  283. goto out_free;
  284. if (unlink(linkname))
  285. goto out_free;
  286. /*
  287. * Since the link is relative, we must make it absolute:
  288. */
  289. snprintf(linkname, size, "%s/.build-id/%.2s/%s",
  290. debugdir, sbuild_id, filename);
  291. if (unlink(linkname))
  292. goto out_free;
  293. err = 0;
  294. out_free:
  295. free(filename);
  296. free(linkname);
  297. return err;
  298. }
  299. static int dso__cache_build_id(struct dso *dso, struct machine *machine,
  300. const char *debugdir)
  301. {
  302. bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
  303. bool is_vdso = dso__is_vdso(dso);
  304. const char *name = dso->long_name;
  305. char nm[PATH_MAX];
  306. if (dso__is_kcore(dso)) {
  307. is_kallsyms = true;
  308. machine__mmap_name(machine, nm, sizeof(nm));
  309. name = nm;
  310. }
  311. return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
  312. debugdir, is_kallsyms, is_vdso);
  313. }
  314. static int __dsos__cache_build_ids(struct list_head *head,
  315. struct machine *machine, const char *debugdir)
  316. {
  317. struct dso *pos;
  318. int err = 0;
  319. dsos__for_each_with_build_id(pos, head)
  320. if (dso__cache_build_id(pos, machine, debugdir))
  321. err = -1;
  322. return err;
  323. }
  324. static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
  325. {
  326. int ret = __dsos__cache_build_ids(&machine->kernel_dsos.head, machine,
  327. debugdir);
  328. ret |= __dsos__cache_build_ids(&machine->user_dsos.head, machine,
  329. debugdir);
  330. return ret;
  331. }
  332. int perf_session__cache_build_ids(struct perf_session *session)
  333. {
  334. struct rb_node *nd;
  335. int ret;
  336. if (no_buildid_cache)
  337. return 0;
  338. if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
  339. return -1;
  340. ret = machine__cache_build_ids(&session->machines.host, buildid_dir);
  341. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  342. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  343. ret |= machine__cache_build_ids(pos, buildid_dir);
  344. }
  345. return ret ? -1 : 0;
  346. }
  347. static bool machine__read_build_ids(struct machine *machine, bool with_hits)
  348. {
  349. bool ret;
  350. ret = __dsos__read_build_ids(&machine->kernel_dsos.head, with_hits);
  351. ret |= __dsos__read_build_ids(&machine->user_dsos.head, with_hits);
  352. return ret;
  353. }
  354. bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
  355. {
  356. struct rb_node *nd;
  357. bool ret = machine__read_build_ids(&session->machines.host, with_hits);
  358. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  359. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  360. ret |= machine__read_build_ids(pos, with_hits);
  361. }
  362. return ret;
  363. }