clang.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Google, Inc.
  4. * modified from kernel/gcov/gcc_4_7.c
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. *
  16. * LLVM uses profiling data that's deliberately similar to GCC, but has a
  17. * very different way of exporting that data. LLVM calls llvm_gcov_init() once
  18. * per module, and provides a couple of callbacks that we can use to ask for
  19. * more data.
  20. *
  21. * We care about the "writeout" callback, which in turn calls back into
  22. * compiler-rt/this module to dump all the gathered coverage data to disk:
  23. *
  24. * llvm_gcda_start_file()
  25. * llvm_gcda_emit_function()
  26. * llvm_gcda_emit_arcs()
  27. * llvm_gcda_emit_function()
  28. * llvm_gcda_emit_arcs()
  29. * [... repeats for each function ...]
  30. * llvm_gcda_summary_info()
  31. * llvm_gcda_end_file()
  32. *
  33. * This design is much more stateless and unstructured than gcc's, and is
  34. * intended to run at process exit. This forces us to keep some local state
  35. * about which module we're dealing with at the moment. On the other hand, it
  36. * also means we don't depend as much on how LLVM represents profiling data
  37. * internally.
  38. *
  39. * See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more
  40. * details on how this works, particularly GCOVProfiler::emitProfileArcs(),
  41. * GCOVProfiler::insertCounterWriteout(), and
  42. * GCOVProfiler::insertFlush().
  43. */
  44. #define pr_fmt(fmt) "gcov: " fmt
  45. #include <linux/kernel.h>
  46. #include <linux/list.h>
  47. #include <linux/printk.h>
  48. #include <linux/ratelimit.h>
  49. #include <linux/slab.h>
  50. #include <linux/mm.h>
  51. #include "gcov.h"
  52. typedef void (*llvm_gcov_callback)(void);
  53. struct gcov_info {
  54. struct list_head head;
  55. const char *filename;
  56. unsigned int version;
  57. u32 checksum;
  58. struct list_head functions;
  59. };
  60. struct gcov_fn_info {
  61. struct list_head head;
  62. u32 ident;
  63. u32 checksum;
  64. u32 cfg_checksum;
  65. u32 num_counters;
  66. u64 *counters;
  67. };
  68. static struct gcov_info *current_info;
  69. static LIST_HEAD(clang_gcov_list);
  70. void llvm_gcov_init(llvm_gcov_callback writeout, llvm_gcov_callback flush)
  71. {
  72. struct gcov_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
  73. if (!info)
  74. return;
  75. INIT_LIST_HEAD(&info->head);
  76. INIT_LIST_HEAD(&info->functions);
  77. mutex_lock(&gcov_lock);
  78. list_add_tail(&info->head, &clang_gcov_list);
  79. current_info = info;
  80. writeout();
  81. current_info = NULL;
  82. if (gcov_events_enabled)
  83. gcov_event(GCOV_ADD, info);
  84. mutex_unlock(&gcov_lock);
  85. }
  86. EXPORT_SYMBOL(llvm_gcov_init);
  87. void llvm_gcda_start_file(const char *orig_filename, u32 version, u32 checksum)
  88. {
  89. current_info->filename = orig_filename;
  90. current_info->version = version;
  91. current_info->checksum = checksum;
  92. }
  93. EXPORT_SYMBOL(llvm_gcda_start_file);
  94. void llvm_gcda_emit_function(u32 ident, u32 func_checksum, u32 cfg_checksum)
  95. {
  96. struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
  97. if (!info)
  98. return;
  99. INIT_LIST_HEAD(&info->head);
  100. info->ident = ident;
  101. info->checksum = func_checksum;
  102. info->cfg_checksum = cfg_checksum;
  103. list_add_tail(&info->head, &current_info->functions);
  104. }
  105. EXPORT_SYMBOL(llvm_gcda_emit_function);
  106. void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
  107. {
  108. struct gcov_fn_info *info = list_last_entry(&current_info->functions,
  109. struct gcov_fn_info, head);
  110. info->num_counters = num_counters;
  111. info->counters = counters;
  112. }
  113. EXPORT_SYMBOL(llvm_gcda_emit_arcs);
  114. void llvm_gcda_summary_info(void)
  115. {
  116. }
  117. EXPORT_SYMBOL(llvm_gcda_summary_info);
  118. void llvm_gcda_end_file(void)
  119. {
  120. }
  121. EXPORT_SYMBOL(llvm_gcda_end_file);
  122. /**
  123. * gcov_info_filename - return info filename
  124. * @info: profiling data set
  125. */
  126. const char *gcov_info_filename(struct gcov_info *info)
  127. {
  128. return info->filename;
  129. }
  130. /**
  131. * gcov_info_version - return info version
  132. * @info: profiling data set
  133. */
  134. unsigned int gcov_info_version(struct gcov_info *info)
  135. {
  136. return info->version;
  137. }
  138. /**
  139. * gcov_info_next - return next profiling data set
  140. * @info: profiling data set
  141. *
  142. * Returns next gcov_info following @info or first gcov_info in the chain if
  143. * @info is %NULL.
  144. */
  145. struct gcov_info *gcov_info_next(struct gcov_info *info)
  146. {
  147. if (!info)
  148. return list_first_entry_or_null(&clang_gcov_list,
  149. struct gcov_info, head);
  150. if (list_is_last(&info->head, &clang_gcov_list))
  151. return NULL;
  152. return list_next_entry(info, head);
  153. }
  154. /**
  155. * gcov_info_link - link/add profiling data set to the list
  156. * @info: profiling data set
  157. */
  158. void gcov_info_link(struct gcov_info *info)
  159. {
  160. list_add_tail(&info->head, &clang_gcov_list);
  161. }
  162. /**
  163. * gcov_info_unlink - unlink/remove profiling data set from the list
  164. * @prev: previous profiling data set
  165. * @info: profiling data set
  166. */
  167. void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
  168. {
  169. /* Generic code unlinks while iterating. */
  170. __list_del_entry(&info->head);
  171. }
  172. /**
  173. * gcov_info_within_module - check if a profiling data set belongs to a module
  174. * @info: profiling data set
  175. * @mod: module
  176. *
  177. * Returns true if profiling data belongs module, false otherwise.
  178. */
  179. bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
  180. {
  181. return within_module((unsigned long)info->filename, mod);
  182. }
  183. /* Symbolic links to be created for each profiling data file. */
  184. const struct gcov_link gcov_link[] = {
  185. { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
  186. { 0, NULL},
  187. };
  188. /**
  189. * gcov_info_reset - reset profiling data to zero
  190. * @info: profiling data set
  191. */
  192. void gcov_info_reset(struct gcov_info *info)
  193. {
  194. struct gcov_fn_info *fn;
  195. list_for_each_entry(fn, &info->functions, head)
  196. memset(fn->counters, 0,
  197. sizeof(fn->counters[0]) * fn->num_counters);
  198. }
  199. /**
  200. * gcov_info_is_compatible - check if profiling data can be added
  201. * @info1: first profiling data set
  202. * @info2: second profiling data set
  203. *
  204. * Returns non-zero if profiling data can be added, zero otherwise.
  205. */
  206. int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
  207. {
  208. struct gcov_fn_info *fn_ptr1 = list_first_entry_or_null(
  209. &info1->functions, struct gcov_fn_info, head);
  210. struct gcov_fn_info *fn_ptr2 = list_first_entry_or_null(
  211. &info2->functions, struct gcov_fn_info, head);
  212. if (info1->checksum != info2->checksum)
  213. return false;
  214. if (!fn_ptr1)
  215. return fn_ptr1 == fn_ptr2;
  216. while (!list_is_last(&fn_ptr1->head, &info1->functions) &&
  217. !list_is_last(&fn_ptr2->head, &info2->functions)) {
  218. if (fn_ptr1->checksum != fn_ptr2->checksum)
  219. return false;
  220. if (fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
  221. return false;
  222. fn_ptr1 = list_next_entry(fn_ptr1, head);
  223. fn_ptr2 = list_next_entry(fn_ptr2, head);
  224. }
  225. return list_is_last(&fn_ptr1->head, &info1->functions) &&
  226. list_is_last(&fn_ptr2->head, &info2->functions);
  227. }
  228. /**
  229. * gcov_info_add - add up profiling data
  230. * @dest: profiling data set to which data is added
  231. * @source: profiling data set which is added
  232. *
  233. * Adds profiling counts of @source to @dest.
  234. */
  235. void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
  236. {
  237. struct gcov_fn_info *dfn_ptr;
  238. struct gcov_fn_info *sfn_ptr = list_first_entry_or_null(&src->functions,
  239. struct gcov_fn_info, head);
  240. list_for_each_entry(dfn_ptr, &dst->functions, head) {
  241. u32 i;
  242. for (i = 0; i < sfn_ptr->num_counters; i++)
  243. dfn_ptr->counters[i] += sfn_ptr->counters[i];
  244. sfn_ptr = list_next_entry(sfn_ptr, head);
  245. }
  246. }
  247. static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
  248. {
  249. size_t cv_size; /* counter values size */
  250. struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
  251. GFP_KERNEL);
  252. if (!fn_dup)
  253. return NULL;
  254. INIT_LIST_HEAD(&fn_dup->head);
  255. cv_size = fn->num_counters * sizeof(fn->counters[0]);
  256. fn_dup->counters = kvmalloc(cv_size, GFP_KERNEL);
  257. if (!fn_dup->counters) {
  258. kfree(fn_dup);
  259. return NULL;
  260. }
  261. memcpy(fn_dup->counters, fn->counters, cv_size);
  262. return fn_dup;
  263. }
  264. /**
  265. * gcov_info_dup - duplicate profiling data set
  266. * @info: profiling data set to duplicate
  267. *
  268. * Return newly allocated duplicate on success, %NULL on error.
  269. */
  270. struct gcov_info *gcov_info_dup(struct gcov_info *info)
  271. {
  272. struct gcov_info *dup;
  273. struct gcov_fn_info *fn;
  274. dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
  275. if (!dup)
  276. return NULL;
  277. INIT_LIST_HEAD(&dup->head);
  278. INIT_LIST_HEAD(&dup->functions);
  279. dup->filename = kstrdup(info->filename, GFP_KERNEL);
  280. if (!dup->filename)
  281. goto err;
  282. list_for_each_entry(fn, &info->functions, head) {
  283. struct gcov_fn_info *fn_dup = gcov_fn_info_dup(fn);
  284. if (!fn_dup)
  285. goto err;
  286. list_add_tail(&fn_dup->head, &dup->functions);
  287. }
  288. return dup;
  289. err:
  290. gcov_info_free(dup);
  291. return NULL;
  292. }
  293. /**
  294. * gcov_info_free - release memory for profiling data set duplicate
  295. * @info: profiling data set duplicate to free
  296. */
  297. void gcov_info_free(struct gcov_info *info)
  298. {
  299. struct gcov_fn_info *fn, *tmp;
  300. list_for_each_entry_safe(fn, tmp, &info->functions, head) {
  301. kvfree(fn->counters);
  302. list_del(&fn->head);
  303. kfree(fn);
  304. }
  305. kfree(info->filename);
  306. kfree(info);
  307. }
  308. /**
  309. * convert_to_gcda - convert profiling data set to gcda file format
  310. * @buffer: the buffer to store file data or %NULL if no data should be stored
  311. * @info: profiling data set to be converted
  312. *
  313. * Returns the number of bytes that were/would have been stored into the buffer.
  314. */
  315. size_t convert_to_gcda(char *buffer, struct gcov_info *info)
  316. {
  317. struct gcov_fn_info *fi_ptr;
  318. size_t pos = 0;
  319. /* File header. */
  320. pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
  321. pos += store_gcov_u32(buffer, pos, info->version);
  322. pos += store_gcov_u32(buffer, pos, info->checksum);
  323. list_for_each_entry(fi_ptr, &info->functions, head) {
  324. u32 i;
  325. pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
  326. pos += store_gcov_u32(buffer, pos, 3);
  327. pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
  328. pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
  329. pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
  330. pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
  331. pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
  332. for (i = 0; i < fi_ptr->num_counters; i++)
  333. pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);
  334. }
  335. return pos;
  336. }