vmlinux-kallsyms.c 6.7 KB

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