vmlinux-kallsyms.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include <linux/compiler.h>
  2. #include <linux/rbtree.h>
  3. #include <string.h>
  4. #include "map.h"
  5. #include "symbol.h"
  6. #include "util.h"
  7. #include "tests.h"
  8. #include "debug.h"
  9. #include "machine.h"
  10. static int vmlinux_matches_kallsyms_filter(struct map *map __maybe_unused,
  11. struct symbol *sym)
  12. {
  13. bool *visited = symbol__priv(sym);
  14. *visited = true;
  15. return 0;
  16. }
  17. #define UM(x) kallsyms_map->unmap_ip(kallsyms_map, (x))
  18. int test__vmlinux_matches_kallsyms(void)
  19. {
  20. int err = -1;
  21. struct rb_node *nd;
  22. struct symbol *sym;
  23. struct map *kallsyms_map, *vmlinux_map;
  24. struct machine kallsyms, vmlinux;
  25. enum map_type type = MAP__FUNCTION;
  26. u64 mem_start, mem_end;
  27. /*
  28. * Step 1:
  29. *
  30. * Init the machines that will hold kernel, modules obtained from
  31. * both vmlinux + .ko files and from /proc/kallsyms split by modules.
  32. */
  33. machine__init(&kallsyms, "", HOST_KERNEL_ID);
  34. machine__init(&vmlinux, "", HOST_KERNEL_ID);
  35. /*
  36. * Step 2:
  37. *
  38. * Create the kernel maps for kallsyms and the DSO where we will then
  39. * load /proc/kallsyms. Also create the modules maps from /proc/modules
  40. * and find the .ko files that match them in /lib/modules/`uname -r`/.
  41. */
  42. if (machine__create_kernel_maps(&kallsyms) < 0) {
  43. pr_debug("machine__create_kernel_maps ");
  44. goto out;
  45. }
  46. /*
  47. * Step 3:
  48. *
  49. * Load and split /proc/kallsyms into multiple maps, one per module.
  50. */
  51. if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
  52. pr_debug("dso__load_kallsyms ");
  53. goto out;
  54. }
  55. /*
  56. * Step 4:
  57. *
  58. * kallsyms will be internally on demand sorted by name so that we can
  59. * find the reference relocation * symbol, i.e. the symbol we will use
  60. * to see if the running kernel was relocated by checking if it has the
  61. * same value in the vmlinux file we load.
  62. */
  63. kallsyms_map = machine__kernel_map(&kallsyms, type);
  64. /*
  65. * Step 5:
  66. *
  67. * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
  68. */
  69. if (machine__create_kernel_maps(&vmlinux) < 0) {
  70. pr_debug("machine__create_kernel_maps ");
  71. goto out;
  72. }
  73. vmlinux_map = machine__kernel_map(&vmlinux, type);
  74. /*
  75. * Step 6:
  76. *
  77. * Locate a vmlinux file in the vmlinux path that has a buildid that
  78. * matches the one of the running kernel.
  79. *
  80. * While doing that look if we find the ref reloc symbol, if we find it
  81. * we'll have its ref_reloc_symbol.unrelocated_addr and then
  82. * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
  83. * to fixup the symbols.
  84. */
  85. if (machine__load_vmlinux_path(&vmlinux, type,
  86. vmlinux_matches_kallsyms_filter) <= 0) {
  87. pr_debug("Couldn't find a vmlinux that matches the kernel running on this machine, skipping test\n");
  88. err = TEST_SKIP;
  89. goto out;
  90. }
  91. err = 0;
  92. /*
  93. * Step 7:
  94. *
  95. * Now look at the symbols in the vmlinux DSO and check if we find all of them
  96. * in the kallsyms dso. For the ones that are in both, check its names and
  97. * end addresses too.
  98. */
  99. for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
  100. struct symbol *pair, *first_pair;
  101. bool backwards = true;
  102. sym = rb_entry(nd, struct symbol, rb_node);
  103. if (sym->start == sym->end)
  104. continue;
  105. mem_start = vmlinux_map->unmap_ip(vmlinux_map, sym->start);
  106. mem_end = vmlinux_map->unmap_ip(vmlinux_map, sym->end);
  107. first_pair = machine__find_kernel_symbol(&kallsyms, type,
  108. mem_start, NULL, NULL);
  109. pair = first_pair;
  110. if (pair && UM(pair->start) == mem_start) {
  111. next_pair:
  112. if (strcmp(sym->name, pair->name) == 0) {
  113. /*
  114. * kallsyms don't have the symbol end, so we
  115. * set that by using the next symbol start - 1,
  116. * in some cases we get this up to a page
  117. * wrong, trace_kmalloc when I was developing
  118. * this code was one such example, 2106 bytes
  119. * off the real size. More than that and we
  120. * _really_ have a problem.
  121. */
  122. s64 skew = mem_end - UM(pair->end);
  123. if (llabs(skew) >= page_size)
  124. pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
  125. mem_start, sym->name, mem_end,
  126. UM(pair->end));
  127. /*
  128. * Do not count this as a failure, because we
  129. * could really find a case where it's not
  130. * possible to get proper function end from
  131. * kallsyms.
  132. */
  133. continue;
  134. } else {
  135. struct rb_node *nnd;
  136. detour:
  137. nnd = backwards ? rb_prev(&pair->rb_node) :
  138. rb_next(&pair->rb_node);
  139. if (nnd) {
  140. struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
  141. if (UM(next->start) == mem_start) {
  142. pair = next;
  143. goto next_pair;
  144. }
  145. }
  146. if (backwards) {
  147. backwards = false;
  148. pair = first_pair;
  149. goto detour;
  150. }
  151. pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
  152. mem_start, sym->name, pair->name);
  153. }
  154. } else
  155. pr_debug("%#" PRIx64 ": %s not on kallsyms\n",
  156. mem_start, sym->name);
  157. err = -1;
  158. }
  159. if (!verbose)
  160. goto out;
  161. pr_info("Maps only in vmlinux:\n");
  162. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  163. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  164. /*
  165. * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
  166. * the kernel will have the path for the vmlinux file being used,
  167. * so use the short name, less descriptive but the same ("[kernel]" in
  168. * both cases.
  169. */
  170. pair = map_groups__find_by_name(&kallsyms.kmaps, type,
  171. (pos->dso->kernel ?
  172. pos->dso->short_name :
  173. pos->dso->name));
  174. if (pair)
  175. pair->priv = 1;
  176. else
  177. map__fprintf(pos, stderr);
  178. }
  179. pr_info("Maps in vmlinux with a different name in kallsyms:\n");
  180. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  181. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  182. mem_start = vmlinux_map->unmap_ip(vmlinux_map, pos->start);
  183. mem_end = vmlinux_map->unmap_ip(vmlinux_map, pos->end);
  184. pair = map_groups__find(&kallsyms.kmaps, type, mem_start);
  185. if (pair == NULL || pair->priv)
  186. continue;
  187. if (pair->start == mem_start) {
  188. pair->priv = 1;
  189. pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
  190. pos->start, pos->end, pos->pgoff, pos->dso->name);
  191. if (mem_end != pair->end)
  192. pr_info(":\n*%" PRIx64 "-%" PRIx64 " %" PRIx64,
  193. pair->start, pair->end, pair->pgoff);
  194. pr_info(" %s\n", pair->dso->name);
  195. pair->priv = 1;
  196. }
  197. }
  198. pr_info("Maps only in kallsyms:\n");
  199. for (nd = rb_first(&kallsyms.kmaps.maps[type]);
  200. nd; nd = rb_next(nd)) {
  201. struct map *pos = rb_entry(nd, struct map, rb_node);
  202. if (!pos->priv)
  203. map__fprintf(pos, stderr);
  204. }
  205. out:
  206. machine__exit(&kallsyms);
  207. machine__exit(&vmlinux);
  208. return err;
  209. }