clang.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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/seq_file.h>
  50. #include <linux/slab.h>
  51. #include <linux/vmalloc.h>
  52. #include "gcov.h"
  53. typedef void (*llvm_gcov_callback)(void);
  54. struct gcov_info {
  55. struct list_head head;
  56. const char *filename;
  57. unsigned int version;
  58. u32 checksum;
  59. struct list_head functions;
  60. };
  61. struct gcov_fn_info {
  62. struct list_head head;
  63. u32 ident;
  64. u32 checksum;
  65. u8 use_extra_checksum;
  66. u32 cfg_checksum;
  67. u32 num_counters;
  68. u64 *counters;
  69. const char *function_name;
  70. };
  71. static struct gcov_info *current_info;
  72. static LIST_HEAD(clang_gcov_list);
  73. void llvm_gcov_init(llvm_gcov_callback writeout, llvm_gcov_callback flush)
  74. {
  75. struct gcov_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
  76. if (!info)
  77. return;
  78. INIT_LIST_HEAD(&info->head);
  79. INIT_LIST_HEAD(&info->functions);
  80. mutex_lock(&gcov_lock);
  81. list_add_tail(&info->head, &clang_gcov_list);
  82. current_info = info;
  83. writeout();
  84. current_info = NULL;
  85. if (gcov_events_enabled)
  86. gcov_event(GCOV_ADD, info);
  87. mutex_unlock(&gcov_lock);
  88. }
  89. EXPORT_SYMBOL(llvm_gcov_init);
  90. void llvm_gcda_start_file(const char *orig_filename, const char version[4],
  91. u32 checksum)
  92. {
  93. current_info->filename = orig_filename;
  94. memcpy(&current_info->version, version, sizeof(current_info->version));
  95. current_info->checksum = checksum;
  96. }
  97. EXPORT_SYMBOL(llvm_gcda_start_file);
  98. void llvm_gcda_emit_function(u32 ident, const char *function_name,
  99. u32 func_checksum, u8 use_extra_checksum, u32 cfg_checksum)
  100. {
  101. struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
  102. if (!info)
  103. return;
  104. INIT_LIST_HEAD(&info->head);
  105. info->ident = ident;
  106. info->checksum = func_checksum;
  107. info->use_extra_checksum = use_extra_checksum;
  108. info->cfg_checksum = cfg_checksum;
  109. if (function_name)
  110. info->function_name = kstrdup(function_name, GFP_KERNEL);
  111. list_add_tail(&info->head, &current_info->functions);
  112. }
  113. EXPORT_SYMBOL(llvm_gcda_emit_function);
  114. void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
  115. {
  116. struct gcov_fn_info *info = list_last_entry(&current_info->functions,
  117. struct gcov_fn_info, head);
  118. info->num_counters = num_counters;
  119. info->counters = counters;
  120. }
  121. EXPORT_SYMBOL(llvm_gcda_emit_arcs);
  122. void llvm_gcda_summary_info(void)
  123. {
  124. }
  125. EXPORT_SYMBOL(llvm_gcda_summary_info);
  126. void llvm_gcda_end_file(void)
  127. {
  128. }
  129. EXPORT_SYMBOL(llvm_gcda_end_file);
  130. /**
  131. * gcov_info_filename - return info filename
  132. * @info: profiling data set
  133. */
  134. const char *gcov_info_filename(struct gcov_info *info)
  135. {
  136. return info->filename;
  137. }
  138. /**
  139. * gcov_info_version - return info version
  140. * @info: profiling data set
  141. */
  142. unsigned int gcov_info_version(struct gcov_info *info)
  143. {
  144. return info->version;
  145. }
  146. /**
  147. * gcov_info_next - return next profiling data set
  148. * @info: profiling data set
  149. *
  150. * Returns next gcov_info following @info or first gcov_info in the chain if
  151. * @info is %NULL.
  152. */
  153. struct gcov_info *gcov_info_next(struct gcov_info *info)
  154. {
  155. if (!info)
  156. return list_first_entry_or_null(&clang_gcov_list,
  157. struct gcov_info, head);
  158. if (list_is_last(&info->head, &clang_gcov_list))
  159. return NULL;
  160. return list_next_entry(info, head);
  161. }
  162. /**
  163. * gcov_info_link - link/add profiling data set to the list
  164. * @info: profiling data set
  165. */
  166. void gcov_info_link(struct gcov_info *info)
  167. {
  168. list_add_tail(&info->head, &clang_gcov_list);
  169. }
  170. /**
  171. * gcov_info_unlink - unlink/remove profiling data set from the list
  172. * @prev: previous profiling data set
  173. * @info: profiling data set
  174. */
  175. void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
  176. {
  177. /* Generic code unlinks while iterating. */
  178. __list_del_entry(&info->head);
  179. }
  180. /**
  181. * gcov_info_within_module - check if a profiling data set belongs to a module
  182. * @info: profiling data set
  183. * @mod: module
  184. *
  185. * Returns true if profiling data belongs module, false otherwise.
  186. */
  187. bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
  188. {
  189. return within_module((unsigned long)info->filename, mod);
  190. }
  191. /* Symbolic links to be created for each profiling data file. */
  192. const struct gcov_link gcov_link[] = {
  193. { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
  194. { 0, NULL},
  195. };
  196. /**
  197. * gcov_info_reset - reset profiling data to zero
  198. * @info: profiling data set
  199. */
  200. void gcov_info_reset(struct gcov_info *info)
  201. {
  202. struct gcov_fn_info *fn;
  203. list_for_each_entry(fn, &info->functions, head)
  204. memset(fn->counters, 0,
  205. sizeof(fn->counters[0]) * fn->num_counters);
  206. }
  207. /**
  208. * gcov_info_is_compatible - check if profiling data can be added
  209. * @info1: first profiling data set
  210. * @info2: second profiling data set
  211. *
  212. * Returns non-zero if profiling data can be added, zero otherwise.
  213. */
  214. int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
  215. {
  216. struct gcov_fn_info *fn_ptr1 = list_first_entry_or_null(
  217. &info1->functions, struct gcov_fn_info, head);
  218. struct gcov_fn_info *fn_ptr2 = list_first_entry_or_null(
  219. &info2->functions, struct gcov_fn_info, head);
  220. if (info1->checksum != info2->checksum)
  221. return false;
  222. if (!fn_ptr1)
  223. return fn_ptr1 == fn_ptr2;
  224. while (!list_is_last(&fn_ptr1->head, &info1->functions) &&
  225. !list_is_last(&fn_ptr2->head, &info2->functions)) {
  226. if (fn_ptr1->checksum != fn_ptr2->checksum)
  227. return false;
  228. if (fn_ptr1->use_extra_checksum != fn_ptr2->use_extra_checksum)
  229. return false;
  230. if (fn_ptr1->use_extra_checksum &&
  231. fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
  232. return false;
  233. fn_ptr1 = list_next_entry(fn_ptr1, head);
  234. fn_ptr2 = list_next_entry(fn_ptr2, head);
  235. }
  236. return list_is_last(&fn_ptr1->head, &info1->functions) &&
  237. list_is_last(&fn_ptr2->head, &info2->functions);
  238. }
  239. /**
  240. * gcov_info_add - add up profiling data
  241. * @dest: profiling data set to which data is added
  242. * @source: profiling data set which is added
  243. *
  244. * Adds profiling counts of @source to @dest.
  245. */
  246. void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
  247. {
  248. struct gcov_fn_info *dfn_ptr;
  249. struct gcov_fn_info *sfn_ptr = list_first_entry_or_null(&src->functions,
  250. struct gcov_fn_info, head);
  251. list_for_each_entry(dfn_ptr, &dst->functions, head) {
  252. u32 i;
  253. for (i = 0; i < sfn_ptr->num_counters; i++)
  254. dfn_ptr->counters[i] += sfn_ptr->counters[i];
  255. }
  256. }
  257. static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
  258. {
  259. size_t cv_size; /* counter values size */
  260. struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
  261. GFP_KERNEL);
  262. if (!fn_dup)
  263. return NULL;
  264. INIT_LIST_HEAD(&fn_dup->head);
  265. fn_dup->function_name = kstrdup(fn->function_name, GFP_KERNEL);
  266. if (!fn_dup->function_name)
  267. goto err_name;
  268. cv_size = fn->num_counters * sizeof(fn->counters[0]);
  269. fn_dup->counters = vmalloc(cv_size);
  270. if (!fn_dup->counters)
  271. goto err_counters;
  272. memcpy(fn_dup->counters, fn->counters, cv_size);
  273. return fn_dup;
  274. err_counters:
  275. kfree(fn_dup->function_name);
  276. err_name:
  277. kfree(fn_dup);
  278. return NULL;
  279. }
  280. /**
  281. * gcov_info_dup - duplicate profiling data set
  282. * @info: profiling data set to duplicate
  283. *
  284. * Return newly allocated duplicate on success, %NULL on error.
  285. */
  286. struct gcov_info *gcov_info_dup(struct gcov_info *info)
  287. {
  288. struct gcov_info *dup;
  289. struct gcov_fn_info *fn;
  290. dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
  291. if (!dup)
  292. return NULL;
  293. INIT_LIST_HEAD(&dup->head);
  294. INIT_LIST_HEAD(&dup->functions);
  295. dup->filename = kstrdup(info->filename, GFP_KERNEL);
  296. if (!dup->filename)
  297. goto err;
  298. list_for_each_entry(fn, &info->functions, head) {
  299. struct gcov_fn_info *fn_dup = gcov_fn_info_dup(fn);
  300. if (!fn_dup)
  301. goto err;
  302. list_add_tail(&fn_dup->head, &dup->functions);
  303. }
  304. return dup;
  305. err:
  306. gcov_info_free(dup);
  307. return NULL;
  308. }
  309. /**
  310. * gcov_info_free - release memory for profiling data set duplicate
  311. * @info: profiling data set duplicate to free
  312. */
  313. void gcov_info_free(struct gcov_info *info)
  314. {
  315. struct gcov_fn_info *fn, *tmp;
  316. list_for_each_entry_safe(fn, tmp, &info->functions, head) {
  317. kfree(fn->function_name);
  318. vfree(fn->counters);
  319. list_del(&fn->head);
  320. kfree(fn);
  321. }
  322. kfree(info->filename);
  323. kfree(info);
  324. }
  325. #define ITER_STRIDE PAGE_SIZE
  326. /**
  327. * struct gcov_iterator - specifies current file position in logical records
  328. * @info: associated profiling data
  329. * @buffer: buffer containing file data
  330. * @size: size of buffer
  331. * @pos: current position in file
  332. */
  333. struct gcov_iterator {
  334. struct gcov_info *info;
  335. void *buffer;
  336. size_t size;
  337. loff_t pos;
  338. };
  339. /**
  340. * store_gcov_u32 - store 32 bit number in gcov format to buffer
  341. * @buffer: target buffer or NULL
  342. * @off: offset into the buffer
  343. * @v: value to be stored
  344. *
  345. * Number format defined by gcc: numbers are recorded in the 32 bit
  346. * unsigned binary form of the endianness of the machine generating the
  347. * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
  348. * store anything.
  349. */
  350. static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
  351. {
  352. u32 *data;
  353. if (buffer) {
  354. data = buffer + off;
  355. *data = v;
  356. }
  357. return sizeof(*data);
  358. }
  359. /**
  360. * store_gcov_u64 - store 64 bit number in gcov format to buffer
  361. * @buffer: target buffer or NULL
  362. * @off: offset into the buffer
  363. * @v: value to be stored
  364. *
  365. * Number format defined by gcc: numbers are recorded in the 32 bit
  366. * unsigned binary form of the endianness of the machine generating the
  367. * file. 64 bit numbers are stored as two 32 bit numbers, the low part
  368. * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
  369. * anything.
  370. */
  371. static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
  372. {
  373. u32 *data;
  374. if (buffer) {
  375. data = buffer + off;
  376. data[0] = (v & 0xffffffffUL);
  377. data[1] = (v >> 32);
  378. }
  379. return sizeof(*data) * 2;
  380. }
  381. /**
  382. * convert_to_gcda - convert profiling data set to gcda file format
  383. * @buffer: the buffer to store file data or %NULL if no data should be stored
  384. * @info: profiling data set to be converted
  385. *
  386. * Returns the number of bytes that were/would have been stored into the buffer.
  387. */
  388. static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
  389. {
  390. struct gcov_fn_info *fi_ptr;
  391. size_t pos = 0;
  392. /* File header. */
  393. pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
  394. pos += store_gcov_u32(buffer, pos, info->version);
  395. pos += store_gcov_u32(buffer, pos, info->checksum);
  396. list_for_each_entry(fi_ptr, &info->functions, head) {
  397. u32 i;
  398. u32 len = 2;
  399. if (fi_ptr->use_extra_checksum)
  400. len++;
  401. pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
  402. pos += store_gcov_u32(buffer, pos, len);
  403. pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
  404. pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
  405. if (fi_ptr->use_extra_checksum)
  406. pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
  407. pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
  408. pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
  409. for (i = 0; i < fi_ptr->num_counters; i++)
  410. pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);
  411. }
  412. return pos;
  413. }
  414. /**
  415. * gcov_iter_new - allocate and initialize profiling data iterator
  416. * @info: profiling data set to be iterated
  417. *
  418. * Return file iterator on success, %NULL otherwise.
  419. */
  420. struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
  421. {
  422. struct gcov_iterator *iter;
  423. iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
  424. if (!iter)
  425. goto err_free;
  426. iter->info = info;
  427. /* Dry-run to get the actual buffer size. */
  428. iter->size = convert_to_gcda(NULL, info);
  429. iter->buffer = vmalloc(iter->size);
  430. if (!iter->buffer)
  431. goto err_free;
  432. convert_to_gcda(iter->buffer, info);
  433. return iter;
  434. err_free:
  435. kfree(iter);
  436. return NULL;
  437. }
  438. /**
  439. * gcov_iter_get_info - return profiling data set for given file iterator
  440. * @iter: file iterator
  441. */
  442. void gcov_iter_free(struct gcov_iterator *iter)
  443. {
  444. vfree(iter->buffer);
  445. kfree(iter);
  446. }
  447. /**
  448. * gcov_iter_get_info - return profiling data set for given file iterator
  449. * @iter: file iterator
  450. */
  451. struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
  452. {
  453. return iter->info;
  454. }
  455. /**
  456. * gcov_iter_start - reset file iterator to starting position
  457. * @iter: file iterator
  458. */
  459. void gcov_iter_start(struct gcov_iterator *iter)
  460. {
  461. iter->pos = 0;
  462. }
  463. /**
  464. * gcov_iter_next - advance file iterator to next logical record
  465. * @iter: file iterator
  466. *
  467. * Return zero if new position is valid, non-zero if iterator has reached end.
  468. */
  469. int gcov_iter_next(struct gcov_iterator *iter)
  470. {
  471. if (iter->pos < iter->size)
  472. iter->pos += ITER_STRIDE;
  473. if (iter->pos >= iter->size)
  474. return -EINVAL;
  475. return 0;
  476. }
  477. /**
  478. * gcov_iter_write - write data for current pos to seq_file
  479. * @iter: file iterator
  480. * @seq: seq_file handle
  481. *
  482. * Return zero on success, non-zero otherwise.
  483. */
  484. int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
  485. {
  486. size_t len;
  487. if (iter->pos >= iter->size)
  488. return -EINVAL;
  489. len = ITER_STRIDE;
  490. if (iter->pos + len > iter->size)
  491. len = iter->size - iter->pos;
  492. seq_write(seq, iter->buffer + iter->pos, len);
  493. return 0;
  494. }