tracepoint-update.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <getopt.h>
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <pthread.h>
  13. #include "elf-parse.h"
  14. static Elf_Shdr *check_data_sec;
  15. static Elf_Shdr *tracepoint_data_sec;
  16. static inline void *get_index(void *start, int entsize, int index)
  17. {
  18. return start + (entsize * index);
  19. }
  20. static int compare_strings(const void *a, const void *b)
  21. {
  22. const char *av = *(const char **)a;
  23. const char *bv = *(const char **)b;
  24. return strcmp(av, bv);
  25. }
  26. struct elf_tracepoint {
  27. Elf_Ehdr *ehdr;
  28. const char **array;
  29. int count;
  30. };
  31. #define REALLOC_SIZE (1 << 10)
  32. #define REALLOC_MASK (REALLOC_SIZE - 1)
  33. static int add_string(const char *str, const char ***vals, int *count)
  34. {
  35. const char **array = *vals;
  36. if (!(*count & REALLOC_MASK)) {
  37. int size = (*count) + REALLOC_SIZE;
  38. array = realloc(array, sizeof(char *) * size);
  39. if (!array) {
  40. fprintf(stderr, "Failed memory allocation\n");
  41. free(*vals);
  42. *vals = NULL;
  43. return -1;
  44. }
  45. *vals = array;
  46. }
  47. array[(*count)++] = str;
  48. return 0;
  49. }
  50. /**
  51. * for_each_shdr_str - iterator that reads strings that are in an ELF section.
  52. * @len: "int" to hold the length of the current string
  53. * @ehdr: A pointer to the ehdr of the ELF file
  54. * @sec: The section that has the strings to iterate on
  55. *
  56. * This is a for loop that iterates over all the nul terminated strings
  57. * that are in a given ELF section. The variable "str" will hold
  58. * the current string for each iteration and the passed in @len will
  59. * contain the strlen() of that string.
  60. */
  61. #define for_each_shdr_str(len, ehdr, sec) \
  62. for (const char *str = (void *)(ehdr) + shdr_offset(sec), \
  63. *end = str + shdr_size(sec); \
  64. len = strlen(str), str < end; \
  65. str += (len) + 1)
  66. static void make_trace_array(struct elf_tracepoint *etrace)
  67. {
  68. Elf_Ehdr *ehdr = etrace->ehdr;
  69. const char **vals = NULL;
  70. int count = 0;
  71. int len;
  72. etrace->array = NULL;
  73. /*
  74. * The __tracepoint_check section is filled with strings of the
  75. * names of tracepoints (in tracepoint_strings). Create an array
  76. * that points to each string and then sort the array.
  77. */
  78. for_each_shdr_str(len, ehdr, check_data_sec) {
  79. if (!len)
  80. continue;
  81. if (add_string(str, &vals, &count) < 0)
  82. return;
  83. }
  84. /* If CONFIG_TRACEPOINT_VERIFY_USED is not set, there's nothing to do */
  85. if (!count)
  86. return;
  87. qsort(vals, count, sizeof(char *), compare_strings);
  88. etrace->array = vals;
  89. etrace->count = count;
  90. }
  91. static int find_event(const char *str, void *array, size_t size)
  92. {
  93. return bsearch(&str, array, size, sizeof(char *), compare_strings) != NULL;
  94. }
  95. static void check_tracepoints(struct elf_tracepoint *etrace, const char *fname)
  96. {
  97. Elf_Ehdr *ehdr = etrace->ehdr;
  98. int len;
  99. if (!etrace->array)
  100. return;
  101. /*
  102. * The __tracepoints_strings section holds all the names of the
  103. * defined tracepoints. If any of them are not in the
  104. * __tracepoint_check_section it means they are not used.
  105. */
  106. for_each_shdr_str(len, ehdr, tracepoint_data_sec) {
  107. if (!len)
  108. continue;
  109. if (!find_event(str, etrace->array, etrace->count)) {
  110. fprintf(stderr, "warning: tracepoint '%s' is unused", str);
  111. if (fname)
  112. fprintf(stderr, " in module %s\n", fname);
  113. else
  114. fprintf(stderr, "\n");
  115. }
  116. }
  117. free(etrace->array);
  118. }
  119. static void *tracepoint_check(struct elf_tracepoint *etrace, const char *fname)
  120. {
  121. make_trace_array(etrace);
  122. check_tracepoints(etrace, fname);
  123. return NULL;
  124. }
  125. static int process_tracepoints(bool mod, void *addr, const char *fname)
  126. {
  127. struct elf_tracepoint etrace = {0};
  128. Elf_Ehdr *ehdr = addr;
  129. Elf_Shdr *shdr_start;
  130. Elf_Shdr *string_sec;
  131. const char *secstrings;
  132. unsigned int shnum;
  133. unsigned int shstrndx;
  134. int shentsize;
  135. int idx;
  136. int done = 2;
  137. shdr_start = (Elf_Shdr *)((char *)ehdr + ehdr_shoff(ehdr));
  138. shentsize = ehdr_shentsize(ehdr);
  139. shstrndx = ehdr_shstrndx(ehdr);
  140. if (shstrndx == SHN_XINDEX)
  141. shstrndx = shdr_link(shdr_start);
  142. string_sec = get_index(shdr_start, shentsize, shstrndx);
  143. secstrings = (const char *)ehdr + shdr_offset(string_sec);
  144. shnum = ehdr_shnum(ehdr);
  145. if (shnum == SHN_UNDEF)
  146. shnum = shdr_size(shdr_start);
  147. for (int i = 0; done && i < shnum; i++) {
  148. Elf_Shdr *shdr = get_index(shdr_start, shentsize, i);
  149. idx = shdr_name(shdr);
  150. /* locate the __tracepoint_check in vmlinux */
  151. if (!strcmp(secstrings + idx, "__tracepoint_check")) {
  152. check_data_sec = shdr;
  153. done--;
  154. }
  155. /* locate the __tracepoints_ptrs section in vmlinux */
  156. if (!strcmp(secstrings + idx, "__tracepoints_strings")) {
  157. tracepoint_data_sec = shdr;
  158. done--;
  159. }
  160. }
  161. /*
  162. * Modules may not have either section. But if it has one section,
  163. * it should have both of them.
  164. */
  165. if (mod && !check_data_sec && !tracepoint_data_sec)
  166. return 0;
  167. if (!check_data_sec) {
  168. if (mod) {
  169. fprintf(stderr, "warning: Module %s has only unused tracepoints\n", fname);
  170. /* Do not fail build */
  171. return 0;
  172. }
  173. fprintf(stderr, "no __tracepoint_check in file: %s\n", fname);
  174. return -1;
  175. }
  176. if (!tracepoint_data_sec) {
  177. /* A module may reference only exported tracepoints */
  178. if (mod)
  179. return 0;
  180. fprintf(stderr, "no __tracepoint_strings in file: %s\n", fname);
  181. return -1;
  182. }
  183. if (!mod)
  184. fname = NULL;
  185. etrace.ehdr = ehdr;
  186. tracepoint_check(&etrace, fname);
  187. return 0;
  188. }
  189. int main(int argc, char *argv[])
  190. {
  191. int n_error = 0;
  192. size_t size = 0;
  193. void *addr = NULL;
  194. bool mod = false;
  195. if (argc > 1 && strcmp(argv[1], "--module") == 0) {
  196. mod = true;
  197. argc--;
  198. argv++;
  199. }
  200. if (argc < 2) {
  201. if (mod)
  202. fprintf(stderr, "usage: tracepoint-update --module module...\n");
  203. else
  204. fprintf(stderr, "usage: tracepoint-update vmlinux...\n");
  205. return 0;
  206. }
  207. /* Process each file in turn, allowing deep failure. */
  208. for (int i = 1; i < argc; i++) {
  209. addr = elf_map(argv[i], &size, 1 << ET_REL);
  210. if (!addr) {
  211. ++n_error;
  212. continue;
  213. }
  214. if (process_tracepoints(mod, addr, argv[i]))
  215. ++n_error;
  216. elf_unmap(addr, size);
  217. }
  218. return !!n_error;
  219. }