clang.c 16 KB

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