build-id.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  29. sample->tid);
  30. if (thread == NULL) {
  31. pr_err("problem processing %d event, skipping it.\n",
  32. event->header.type);
  33. return -1;
  34. }
  35. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, &al);
  36. if (al.map != NULL)
  37. al.map->dso->hit = 1;
  38. thread__put(thread);
  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. machine__remove_thread(machine, thread);
  54. thread__put(thread);
  55. }
  56. return 0;
  57. }
  58. struct perf_tool build_id__mark_dso_hit_ops = {
  59. .sample = build_id__mark_dso_hit,
  60. .mmap = perf_event__process_mmap,
  61. .mmap2 = perf_event__process_mmap2,
  62. .fork = perf_event__process_fork,
  63. .exit = perf_event__exit_del_thread,
  64. .attr = perf_event__process_attr,
  65. .build_id = perf_event__process_build_id,
  66. .ordered_events = true,
  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 (bid - bf) + 1;
  79. }
  80. int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
  81. {
  82. char notes[PATH_MAX];
  83. u8 build_id[BUILD_ID_SIZE];
  84. int ret;
  85. if (!root_dir)
  86. root_dir = "";
  87. scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
  88. ret = sysfs__read_build_id(notes, build_id, sizeof(build_id));
  89. if (ret < 0)
  90. return ret;
  91. return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  92. }
  93. int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
  94. {
  95. u8 build_id[BUILD_ID_SIZE];
  96. int ret;
  97. ret = filename__read_build_id(pathname, build_id, sizeof(build_id));
  98. if (ret < 0)
  99. return ret;
  100. else if (ret != sizeof(build_id))
  101. return -EINVAL;
  102. return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
  103. }
  104. /* asnprintf consolidates asprintf and snprintf */
  105. static int asnprintf(char **strp, size_t size, const char *fmt, ...)
  106. {
  107. va_list ap;
  108. int ret;
  109. if (!strp)
  110. return -EINVAL;
  111. va_start(ap, fmt);
  112. if (*strp)
  113. ret = vsnprintf(*strp, size, fmt, ap);
  114. else
  115. ret = vasprintf(strp, fmt, ap);
  116. va_end(ap);
  117. return ret;
  118. }
  119. static char *build_id__filename(const char *sbuild_id, char *bf, size_t size)
  120. {
  121. char *tmp = bf;
  122. int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
  123. sbuild_id, sbuild_id + 2);
  124. if (ret < 0 || (tmp && size < (unsigned int)ret))
  125. return NULL;
  126. return bf;
  127. }
  128. char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
  129. {
  130. char build_id_hex[SBUILD_ID_SIZE];
  131. if (!dso->has_build_id)
  132. return NULL;
  133. build_id__sprintf(dso->build_id, sizeof(dso->build_id), build_id_hex);
  134. return build_id__filename(build_id_hex, bf, size);
  135. }
  136. bool dso__build_id_is_kmod(const struct dso *dso, char *bf, size_t size)
  137. {
  138. char *id_name, *ch;
  139. struct stat sb;
  140. id_name = dso__build_id_filename(dso, bf, size);
  141. if (!id_name)
  142. goto err;
  143. if (access(id_name, F_OK))
  144. goto err;
  145. if (lstat(id_name, &sb) == -1)
  146. goto err;
  147. if ((size_t)sb.st_size > size - 1)
  148. goto err;
  149. if (readlink(id_name, bf, size - 1) < 0)
  150. goto err;
  151. bf[sb.st_size] = '\0';
  152. /*
  153. * link should be:
  154. * ../../lib/modules/4.4.0-rc4/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko/a09fe3eb3147dafa4e3b31dbd6257e4d696bdc92
  155. */
  156. ch = strrchr(bf, '/');
  157. if (!ch)
  158. goto err;
  159. if (ch - 3 < bf)
  160. goto err;
  161. return strncmp(".ko", ch - 3, 3) == 0;
  162. err:
  163. /*
  164. * If dso__build_id_filename work, get id_name again,
  165. * because id_name points to bf and is broken.
  166. */
  167. if (id_name)
  168. id_name = dso__build_id_filename(dso, bf, size);
  169. pr_err("Invalid build id: %s\n", id_name ? :
  170. dso->long_name ? :
  171. dso->short_name ? :
  172. "[unknown]");
  173. return false;
  174. }
  175. #define dsos__for_each_with_build_id(pos, head) \
  176. list_for_each_entry(pos, head, node) \
  177. if (!pos->has_build_id) \
  178. continue; \
  179. else
  180. static int write_buildid(const char *name, size_t name_len, u8 *build_id,
  181. pid_t pid, u16 misc, int fd)
  182. {
  183. int err;
  184. struct build_id_event b;
  185. size_t len;
  186. len = name_len + 1;
  187. len = PERF_ALIGN(len, NAME_ALIGN);
  188. memset(&b, 0, sizeof(b));
  189. memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
  190. b.pid = pid;
  191. b.header.misc = misc;
  192. b.header.size = sizeof(b) + len;
  193. err = writen(fd, &b, sizeof(b));
  194. if (err < 0)
  195. return err;
  196. return write_padded(fd, name, name_len + 1, len);
  197. }
  198. static int machine__write_buildid_table(struct machine *machine, int fd)
  199. {
  200. int err = 0;
  201. char nm[PATH_MAX];
  202. struct dso *pos;
  203. u16 kmisc = PERF_RECORD_MISC_KERNEL,
  204. umisc = PERF_RECORD_MISC_USER;
  205. if (!machine__is_host(machine)) {
  206. kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
  207. umisc = PERF_RECORD_MISC_GUEST_USER;
  208. }
  209. dsos__for_each_with_build_id(pos, &machine->dsos.head) {
  210. const char *name;
  211. size_t name_len;
  212. bool in_kernel = false;
  213. if (!pos->hit && !dso__is_vdso(pos))
  214. continue;
  215. if (dso__is_vdso(pos)) {
  216. name = pos->short_name;
  217. name_len = pos->short_name_len;
  218. } else if (dso__is_kcore(pos)) {
  219. machine__mmap_name(machine, nm, sizeof(nm));
  220. name = nm;
  221. name_len = strlen(nm);
  222. } else {
  223. name = pos->long_name;
  224. name_len = pos->long_name_len;
  225. }
  226. in_kernel = pos->kernel ||
  227. is_kernel_module(name,
  228. PERF_RECORD_MISC_CPUMODE_UNKNOWN);
  229. err = write_buildid(name, name_len, pos->build_id, machine->pid,
  230. in_kernel ? kmisc : umisc, fd);
  231. if (err)
  232. break;
  233. }
  234. return err;
  235. }
  236. int perf_session__write_buildid_table(struct perf_session *session, int fd)
  237. {
  238. struct rb_node *nd;
  239. int err = machine__write_buildid_table(&session->machines.host, fd);
  240. if (err)
  241. return err;
  242. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  243. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  244. err = machine__write_buildid_table(pos, fd);
  245. if (err)
  246. break;
  247. }
  248. return err;
  249. }
  250. static int __dsos__hit_all(struct list_head *head)
  251. {
  252. struct dso *pos;
  253. list_for_each_entry(pos, head, node)
  254. pos->hit = true;
  255. return 0;
  256. }
  257. static int machine__hit_all_dsos(struct machine *machine)
  258. {
  259. return __dsos__hit_all(&machine->dsos.head);
  260. }
  261. int dsos__hit_all(struct perf_session *session)
  262. {
  263. struct rb_node *nd;
  264. int err;
  265. err = machine__hit_all_dsos(&session->machines.host);
  266. if (err)
  267. return err;
  268. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  269. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  270. err = machine__hit_all_dsos(pos);
  271. if (err)
  272. return err;
  273. }
  274. return 0;
  275. }
  276. void disable_buildid_cache(void)
  277. {
  278. no_buildid_cache = true;
  279. }
  280. static char *build_id_cache__dirname_from_path(const char *name,
  281. bool is_kallsyms, bool is_vdso)
  282. {
  283. char *realname = (char *)name, *filename;
  284. bool slash = is_kallsyms || is_vdso;
  285. if (!slash) {
  286. realname = realpath(name, NULL);
  287. if (!realname)
  288. return NULL;
  289. }
  290. if (asprintf(&filename, "%s%s%s", buildid_dir, slash ? "/" : "",
  291. is_vdso ? DSO__NAME_VDSO : realname) < 0)
  292. filename = NULL;
  293. if (!slash)
  294. free(realname);
  295. return filename;
  296. }
  297. int build_id_cache__list_build_ids(const char *pathname,
  298. struct strlist **result)
  299. {
  300. char *dir_name;
  301. int ret = 0;
  302. dir_name = build_id_cache__dirname_from_path(pathname, false, false);
  303. if (!dir_name)
  304. return -ENOMEM;
  305. *result = lsdir(dir_name, lsdir_no_dot_filter);
  306. if (!*result)
  307. ret = -errno;
  308. free(dir_name);
  309. return ret;
  310. }
  311. int build_id_cache__add_s(const char *sbuild_id, const char *name,
  312. bool is_kallsyms, bool is_vdso)
  313. {
  314. const size_t size = PATH_MAX;
  315. char *realname = NULL, *filename = NULL, *dir_name = NULL,
  316. *linkname = zalloc(size), *targetname, *tmp;
  317. int err = -1;
  318. if (!is_kallsyms) {
  319. realname = realpath(name, NULL);
  320. if (!realname)
  321. goto out_free;
  322. }
  323. dir_name = build_id_cache__dirname_from_path(name, is_kallsyms, is_vdso);
  324. if (!dir_name)
  325. goto out_free;
  326. if (mkdir_p(dir_name, 0755))
  327. goto out_free;
  328. if (asprintf(&filename, "%s/%s", dir_name, sbuild_id) < 0) {
  329. filename = NULL;
  330. goto out_free;
  331. }
  332. if (access(filename, F_OK)) {
  333. if (is_kallsyms) {
  334. if (copyfile("/proc/kallsyms", filename))
  335. goto out_free;
  336. } else if (link(realname, filename) && errno != EEXIST &&
  337. copyfile(name, filename))
  338. goto out_free;
  339. }
  340. if (!build_id__filename(sbuild_id, linkname, size))
  341. goto out_free;
  342. tmp = strrchr(linkname, '/');
  343. *tmp = '\0';
  344. if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
  345. goto out_free;
  346. *tmp = '/';
  347. targetname = filename + strlen(buildid_dir) - 5;
  348. memcpy(targetname, "../..", 5);
  349. if (symlink(targetname, linkname) == 0)
  350. err = 0;
  351. out_free:
  352. if (!is_kallsyms)
  353. free(realname);
  354. free(filename);
  355. free(dir_name);
  356. free(linkname);
  357. return err;
  358. }
  359. static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
  360. const char *name, bool is_kallsyms,
  361. bool is_vdso)
  362. {
  363. char sbuild_id[SBUILD_ID_SIZE];
  364. build_id__sprintf(build_id, build_id_size, sbuild_id);
  365. return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso);
  366. }
  367. bool build_id_cache__cached(const char *sbuild_id)
  368. {
  369. bool ret = false;
  370. char *filename = build_id__filename(sbuild_id, NULL, 0);
  371. if (filename && !access(filename, F_OK))
  372. ret = true;
  373. free(filename);
  374. return ret;
  375. }
  376. int build_id_cache__remove_s(const char *sbuild_id)
  377. {
  378. const size_t size = PATH_MAX;
  379. char *filename = zalloc(size),
  380. *linkname = zalloc(size), *tmp;
  381. int err = -1;
  382. if (filename == NULL || linkname == NULL)
  383. goto out_free;
  384. if (!build_id__filename(sbuild_id, linkname, size))
  385. goto out_free;
  386. if (access(linkname, F_OK))
  387. goto out_free;
  388. if (readlink(linkname, filename, size - 1) < 0)
  389. goto out_free;
  390. if (unlink(linkname))
  391. goto out_free;
  392. /*
  393. * Since the link is relative, we must make it absolute:
  394. */
  395. tmp = strrchr(linkname, '/') + 1;
  396. snprintf(tmp, size - (tmp - linkname), "%s", filename);
  397. if (unlink(linkname))
  398. goto out_free;
  399. err = 0;
  400. out_free:
  401. free(filename);
  402. free(linkname);
  403. return err;
  404. }
  405. static int dso__cache_build_id(struct dso *dso, struct machine *machine)
  406. {
  407. bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
  408. bool is_vdso = dso__is_vdso(dso);
  409. const char *name = dso->long_name;
  410. char nm[PATH_MAX];
  411. if (dso__is_kcore(dso)) {
  412. is_kallsyms = true;
  413. machine__mmap_name(machine, nm, sizeof(nm));
  414. name = nm;
  415. }
  416. return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
  417. is_kallsyms, is_vdso);
  418. }
  419. static int __dsos__cache_build_ids(struct list_head *head,
  420. struct machine *machine)
  421. {
  422. struct dso *pos;
  423. int err = 0;
  424. dsos__for_each_with_build_id(pos, head)
  425. if (dso__cache_build_id(pos, machine))
  426. err = -1;
  427. return err;
  428. }
  429. static int machine__cache_build_ids(struct machine *machine)
  430. {
  431. return __dsos__cache_build_ids(&machine->dsos.head, machine);
  432. }
  433. int perf_session__cache_build_ids(struct perf_session *session)
  434. {
  435. struct rb_node *nd;
  436. int ret;
  437. if (no_buildid_cache)
  438. return 0;
  439. if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
  440. return -1;
  441. ret = machine__cache_build_ids(&session->machines.host);
  442. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  443. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  444. ret |= machine__cache_build_ids(pos);
  445. }
  446. return ret ? -1 : 0;
  447. }
  448. static bool machine__read_build_ids(struct machine *machine, bool with_hits)
  449. {
  450. return __dsos__read_build_ids(&machine->dsos.head, with_hits);
  451. }
  452. bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
  453. {
  454. struct rb_node *nd;
  455. bool ret = machine__read_build_ids(&session->machines.host, with_hits);
  456. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  457. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  458. ret |= machine__read_build_ids(pos, with_hits);
  459. }
  460. return ret;
  461. }