builtin-test.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * builtin-test.c
  3. *
  4. * Builtin regression testing command: ever growing number of sanity tests
  5. */
  6. #include "builtin.h"
  7. #include "util/cache.h"
  8. #include "util/debug.h"
  9. #include "util/parse-options.h"
  10. #include "util/session.h"
  11. #include "util/symbol.h"
  12. #include "util/thread.h"
  13. static long page_size;
  14. static int vmlinux_matches_kallsyms_filter(struct map *map __used, struct symbol *sym)
  15. {
  16. bool *visited = symbol__priv(sym);
  17. *visited = true;
  18. return 0;
  19. }
  20. static int test__vmlinux_matches_kallsyms(void)
  21. {
  22. int err = -1;
  23. struct rb_node *nd;
  24. struct symbol *sym;
  25. struct map *kallsyms_map, *vmlinux_map;
  26. struct machine kallsyms, vmlinux;
  27. enum map_type type = MAP__FUNCTION;
  28. struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", };
  29. /*
  30. * Step 1:
  31. *
  32. * Init the machines that will hold kernel, modules obtained from
  33. * both vmlinux + .ko files and from /proc/kallsyms split by modules.
  34. */
  35. machine__init(&kallsyms, "", HOST_KERNEL_ID);
  36. machine__init(&vmlinux, "", HOST_KERNEL_ID);
  37. /*
  38. * Step 2:
  39. *
  40. * Create the kernel maps for kallsyms and the DSO where we will then
  41. * load /proc/kallsyms. Also create the modules maps from /proc/modules
  42. * and find the .ko files that match them in /lib/modules/`uname -r`/.
  43. */
  44. if (machine__create_kernel_maps(&kallsyms) < 0) {
  45. pr_debug("machine__create_kernel_maps ");
  46. return -1;
  47. }
  48. /*
  49. * Step 3:
  50. *
  51. * Load and split /proc/kallsyms into multiple maps, one per module.
  52. */
  53. if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
  54. pr_debug("dso__load_kallsyms ");
  55. goto out;
  56. }
  57. /*
  58. * Step 4:
  59. *
  60. * kallsyms will be internally on demand sorted by name so that we can
  61. * find the reference relocation * symbol, i.e. the symbol we will use
  62. * to see if the running kernel was relocated by checking if it has the
  63. * same value in the vmlinux file we load.
  64. */
  65. kallsyms_map = machine__kernel_map(&kallsyms, type);
  66. sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL);
  67. if (sym == NULL) {
  68. pr_debug("dso__find_symbol_by_name ");
  69. goto out;
  70. }
  71. ref_reloc_sym.addr = sym->start;
  72. /*
  73. * Step 5:
  74. *
  75. * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
  76. */
  77. if (machine__create_kernel_maps(&vmlinux) < 0) {
  78. pr_debug("machine__create_kernel_maps ");
  79. goto out;
  80. }
  81. vmlinux_map = machine__kernel_map(&vmlinux, type);
  82. map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym;
  83. /*
  84. * Step 6:
  85. *
  86. * Locate a vmlinux file in the vmlinux path that has a buildid that
  87. * matches the one of the running kernel.
  88. *
  89. * While doing that look if we find the ref reloc symbol, if we find it
  90. * we'll have its ref_reloc_symbol.unrelocated_addr and then
  91. * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
  92. * to fixup the symbols.
  93. */
  94. if (machine__load_vmlinux_path(&vmlinux, type,
  95. vmlinux_matches_kallsyms_filter) <= 0) {
  96. pr_debug("machine__load_vmlinux_path ");
  97. goto out;
  98. }
  99. err = 0;
  100. /*
  101. * Step 7:
  102. *
  103. * Now look at the symbols in the vmlinux DSO and check if we find all of them
  104. * in the kallsyms dso. For the ones that are in both, check its names and
  105. * end addresses too.
  106. */
  107. for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
  108. struct symbol *pair;
  109. sym = rb_entry(nd, struct symbol, rb_node);
  110. pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
  111. if (pair && pair->start == sym->start) {
  112. next_pair:
  113. if (strcmp(sym->name, pair->name) == 0) {
  114. /*
  115. * kallsyms don't have the symbol end, so we
  116. * set that by using the next symbol start - 1,
  117. * in some cases we get this up to a page
  118. * wrong, trace_kmalloc when I was developing
  119. * this code was one such example, 2106 bytes
  120. * off the real size. More than that and we
  121. * _really_ have a problem.
  122. */
  123. s64 skew = sym->end - pair->end;
  124. if (llabs(skew) < page_size)
  125. continue;
  126. pr_debug("%#Lx: diff end addr for %s v: %#Lx k: %#Lx\n",
  127. sym->start, sym->name, sym->end, pair->end);
  128. } else {
  129. struct rb_node *nnd = rb_prev(&pair->rb_node);
  130. if (nnd) {
  131. struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
  132. if (next->start == sym->start) {
  133. pair = next;
  134. goto next_pair;
  135. }
  136. }
  137. pr_debug("%#Lx: diff name v: %s k: %s\n",
  138. sym->start, sym->name, pair->name);
  139. }
  140. } else
  141. pr_debug("%#Lx: %s not on kallsyms\n", sym->start, sym->name);
  142. err = -1;
  143. }
  144. if (!verbose)
  145. goto out;
  146. pr_info("Maps only in vmlinux:\n");
  147. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  148. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  149. /*
  150. * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
  151. * the kernel will have the path for the vmlinux file being used,
  152. * so use the short name, less descriptive but the same ("[kernel]" in
  153. * both cases.
  154. */
  155. pair = map_groups__find_by_name(&kallsyms.kmaps, type,
  156. (pos->dso->kernel ?
  157. pos->dso->short_name :
  158. pos->dso->name));
  159. if (pair)
  160. pair->priv = 1;
  161. else
  162. map__fprintf(pos, stderr);
  163. }
  164. pr_info("Maps in vmlinux with a different name in kallsyms:\n");
  165. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  166. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  167. pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
  168. if (pair == NULL || pair->priv)
  169. continue;
  170. if (pair->start == pos->start) {
  171. pair->priv = 1;
  172. pr_info(" %Lx-%Lx %Lx %s in kallsyms as",
  173. pos->start, pos->end, pos->pgoff, pos->dso->name);
  174. if (pos->pgoff != pair->pgoff || pos->end != pair->end)
  175. pr_info(": \n*%Lx-%Lx %Lx",
  176. pair->start, pair->end, pair->pgoff);
  177. pr_info(" %s\n", pair->dso->name);
  178. pair->priv = 1;
  179. }
  180. }
  181. pr_info("Maps only in kallsyms:\n");
  182. for (nd = rb_first(&kallsyms.kmaps.maps[type]);
  183. nd; nd = rb_next(nd)) {
  184. struct map *pos = rb_entry(nd, struct map, rb_node);
  185. if (!pos->priv)
  186. map__fprintf(pos, stderr);
  187. }
  188. out:
  189. return err;
  190. }
  191. static struct test {
  192. const char *desc;
  193. int (*func)(void);
  194. } tests[] = {
  195. {
  196. .desc = "vmlinux symtab matches kallsyms",
  197. .func = test__vmlinux_matches_kallsyms,
  198. },
  199. {
  200. .func = NULL,
  201. },
  202. };
  203. static int __cmd_test(void)
  204. {
  205. int i = 0;
  206. page_size = sysconf(_SC_PAGE_SIZE);
  207. while (tests[i].func) {
  208. int err;
  209. pr_info("%2d: %s:", i + 1, tests[i].desc);
  210. pr_debug("\n--- start ---\n");
  211. err = tests[i].func();
  212. pr_debug("---- end ----\n%s:", tests[i].desc);
  213. pr_info(" %s\n", err ? "FAILED!\n" : "Ok");
  214. ++i;
  215. }
  216. return 0;
  217. }
  218. static const char * const test_usage[] = {
  219. "perf test [<options>]",
  220. NULL,
  221. };
  222. static const struct option test_options[] = {
  223. OPT_INTEGER('v', "verbose", &verbose,
  224. "be more verbose (show symbol address, etc)"),
  225. OPT_END()
  226. };
  227. int cmd_test(int argc, const char **argv, const char *prefix __used)
  228. {
  229. argc = parse_options(argc, argv, test_options, test_usage, 0);
  230. if (argc)
  231. usage_with_options(test_usage, test_options);
  232. symbol_conf.priv_size = sizeof(int);
  233. symbol_conf.sort_by_name = true;
  234. symbol_conf.try_vmlinux_path = true;
  235. if (symbol__init() < 0)
  236. return -1;
  237. setup_pager();
  238. return __cmd_test();
  239. }