build-id.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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)
  214. continue;
  215. if (dso__is_vdso(pos)) {
  216. name = pos->short_name;
  217. name_len = pos->short_name_len + 1;
  218. } else if (dso__is_kcore(pos)) {
  219. machine__mmap_name(machine, nm, sizeof(nm));
  220. name = nm;
  221. name_len = strlen(nm) + 1;
  222. } else {
  223. name = pos->long_name;
  224. name_len = pos->long_name_len + 1;
  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. struct strlist *list;
  301. char *dir_name;
  302. DIR *dir;
  303. struct dirent *d;
  304. int ret = 0;
  305. list = strlist__new(NULL, NULL);
  306. dir_name = build_id_cache__dirname_from_path(pathname, false, false);
  307. if (!list || !dir_name) {
  308. ret = -ENOMEM;
  309. goto out;
  310. }
  311. /* List up all dirents */
  312. dir = opendir(dir_name);
  313. if (!dir) {
  314. ret = -errno;
  315. goto out;
  316. }
  317. while ((d = readdir(dir)) != NULL) {
  318. if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
  319. continue;
  320. strlist__add(list, d->d_name);
  321. }
  322. closedir(dir);
  323. out:
  324. free(dir_name);
  325. if (ret)
  326. strlist__delete(list);
  327. else
  328. *result = list;
  329. return ret;
  330. }
  331. int build_id_cache__add_s(const char *sbuild_id, const char *name,
  332. bool is_kallsyms, bool is_vdso)
  333. {
  334. const size_t size = PATH_MAX;
  335. char *realname = NULL, *filename = NULL, *dir_name = NULL,
  336. *linkname = zalloc(size), *targetname, *tmp;
  337. int err = -1;
  338. if (!is_kallsyms) {
  339. realname = realpath(name, NULL);
  340. if (!realname)
  341. goto out_free;
  342. }
  343. dir_name = build_id_cache__dirname_from_path(name, is_kallsyms, is_vdso);
  344. if (!dir_name)
  345. goto out_free;
  346. if (mkdir_p(dir_name, 0755))
  347. goto out_free;
  348. if (asprintf(&filename, "%s/%s", dir_name, sbuild_id) < 0) {
  349. filename = NULL;
  350. goto out_free;
  351. }
  352. if (access(filename, F_OK)) {
  353. if (is_kallsyms) {
  354. if (copyfile("/proc/kallsyms", filename))
  355. goto out_free;
  356. } else if (link(realname, filename) && errno != EEXIST &&
  357. copyfile(name, filename))
  358. goto out_free;
  359. }
  360. if (!build_id__filename(sbuild_id, linkname, size))
  361. goto out_free;
  362. tmp = strrchr(linkname, '/');
  363. *tmp = '\0';
  364. if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
  365. goto out_free;
  366. *tmp = '/';
  367. targetname = filename + strlen(buildid_dir) - 5;
  368. memcpy(targetname, "../..", 5);
  369. if (symlink(targetname, linkname) == 0)
  370. err = 0;
  371. out_free:
  372. if (!is_kallsyms)
  373. free(realname);
  374. free(filename);
  375. free(dir_name);
  376. free(linkname);
  377. return err;
  378. }
  379. static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
  380. const char *name, bool is_kallsyms,
  381. bool is_vdso)
  382. {
  383. char sbuild_id[SBUILD_ID_SIZE];
  384. build_id__sprintf(build_id, build_id_size, sbuild_id);
  385. return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso);
  386. }
  387. bool build_id_cache__cached(const char *sbuild_id)
  388. {
  389. bool ret = false;
  390. char *filename = build_id__filename(sbuild_id, NULL, 0);
  391. if (filename && !access(filename, F_OK))
  392. ret = true;
  393. free(filename);
  394. return ret;
  395. }
  396. int build_id_cache__remove_s(const char *sbuild_id)
  397. {
  398. const size_t size = PATH_MAX;
  399. char *filename = zalloc(size),
  400. *linkname = zalloc(size), *tmp;
  401. int err = -1;
  402. if (filename == NULL || linkname == NULL)
  403. goto out_free;
  404. if (!build_id__filename(sbuild_id, linkname, size))
  405. goto out_free;
  406. if (access(linkname, F_OK))
  407. goto out_free;
  408. if (readlink(linkname, filename, size - 1) < 0)
  409. goto out_free;
  410. if (unlink(linkname))
  411. goto out_free;
  412. /*
  413. * Since the link is relative, we must make it absolute:
  414. */
  415. tmp = strrchr(linkname, '/') + 1;
  416. snprintf(tmp, size - (tmp - linkname), "%s", filename);
  417. if (unlink(linkname))
  418. goto out_free;
  419. err = 0;
  420. out_free:
  421. free(filename);
  422. free(linkname);
  423. return err;
  424. }
  425. static int dso__cache_build_id(struct dso *dso, struct machine *machine)
  426. {
  427. bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
  428. bool is_vdso = dso__is_vdso(dso);
  429. const char *name = dso->long_name;
  430. char nm[PATH_MAX];
  431. if (dso__is_kcore(dso)) {
  432. is_kallsyms = true;
  433. machine__mmap_name(machine, nm, sizeof(nm));
  434. name = nm;
  435. }
  436. return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
  437. is_kallsyms, is_vdso);
  438. }
  439. static int __dsos__cache_build_ids(struct list_head *head,
  440. struct machine *machine)
  441. {
  442. struct dso *pos;
  443. int err = 0;
  444. dsos__for_each_with_build_id(pos, head)
  445. if (dso__cache_build_id(pos, machine))
  446. err = -1;
  447. return err;
  448. }
  449. static int machine__cache_build_ids(struct machine *machine)
  450. {
  451. return __dsos__cache_build_ids(&machine->dsos.head, machine);
  452. }
  453. int perf_session__cache_build_ids(struct perf_session *session)
  454. {
  455. struct rb_node *nd;
  456. int ret;
  457. if (no_buildid_cache)
  458. return 0;
  459. if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
  460. return -1;
  461. ret = machine__cache_build_ids(&session->machines.host);
  462. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  463. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  464. ret |= machine__cache_build_ids(pos);
  465. }
  466. return ret ? -1 : 0;
  467. }
  468. static bool machine__read_build_ids(struct machine *machine, bool with_hits)
  469. {
  470. return __dsos__read_build_ids(&machine->dsos.head, with_hits);
  471. }
  472. bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
  473. {
  474. struct rb_node *nd;
  475. bool ret = machine__read_build_ids(&session->machines.host, with_hits);
  476. for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
  477. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  478. ret |= machine__read_build_ids(pos, with_hits);
  479. }
  480. return ret;
  481. }