build-id.c 13 KB

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