gcov.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This code provides functions to handle gcc and llvm coverage info format
  4. * introduced with gcc 4.7 and llvm clang compiled with -coverage option.
  5. *
  6. * This file is based heavily on gcc_4_7.c and clang.c file.
  7. * - https://github.com/torvalds/linux/blob/master/kernel/gcov/gcc_4_7.c
  8. * - https://github.com/torvalds/linux/commits/master/kernel/gcov/clang.c
  9. */
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #if BITS_PER_LONG >= 64
  15. typedef long gcov_type;
  16. #else
  17. typedef long long gcov_type;
  18. #endif
  19. typedef uint64_t u64;
  20. typedef uint32_t u32;
  21. /*
  22. * Profiling data types used for gcc 3.4 and above - these are defined by
  23. * gcc and need to be kept as close to the original definition as possible to
  24. * remain compatible.
  25. */
  26. #define GCOV_DATA_MAGIC ((unsigned int) 0x67636461)
  27. #define GCOV_TAG_FUNCTION ((unsigned int) 0x01000000)
  28. #define GCOV_TAG_COUNTER_BASE ((unsigned int) 0x01a10000)
  29. #define GCOV_TAG_FOR_COUNTER(count) \
  30. (GCOV_TAG_COUNTER_BASE + ((unsigned int) (count) << 17))
  31. #ifndef __clang__
  32. /*
  33. * GCC -coverage support
  34. *
  35. * Data structures and runtime functions for handling coverage information
  36. * generated by GCC with -coverage option. Supports GCC 4.7+ format.
  37. * Based on Linux kernel's gcov/gcc_4_7.c implementation.
  38. */
  39. #if (__GNUC__ >= 15)
  40. #define GCOV_COUNTERS 10
  41. #elif (__GNUC__ >= 14)
  42. #define GCOV_COUNTERS 9
  43. #elif (__GNUC__ >= 10)
  44. #define GCOV_COUNTERS 8
  45. #elif (__GNUC__ >= 7)
  46. #define GCOV_COUNTERS 9
  47. #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
  48. #define GCOV_COUNTERS 10
  49. #else
  50. #define GCOV_COUNTERS 9
  51. #endif
  52. #define GCOV_TAG_FUNCTION_LENGTH 3
  53. /* Since GCC 12.1 sizes are in BYTES and not in WORDS (4B). */
  54. #if (__GNUC__ >= 12)
  55. #define GCOV_UNIT_SIZE 4
  56. #else
  57. #define GCOV_UNIT_SIZE 1
  58. #endif
  59. /**
  60. * struct gcov_ctr_info - information about counters for a single function
  61. * @num: number of counter values for this type
  62. * @values: array of counter values for this type
  63. *
  64. * This data is generated by gcc during compilation and doesn't change
  65. * at run-time with the exception of the values array.
  66. */
  67. struct gcov_ctr_info {
  68. unsigned int num;
  69. gcov_type *values;
  70. };
  71. /**
  72. * struct gcov_fn_info - profiling metadata per function for GCC
  73. * @key: comdat key for detecting selected comdat function
  74. * @ident: unique identifier of the function
  75. * @lineno_checksum: function line number checksum
  76. * @cfg_checksum: control flow graph checksum
  77. * @ctrs: instrumented counters (flexible array member)
  78. *
  79. * This data is generated by gcc during compilation and doesn't change
  80. * at run-time. Uses trailing array idiom for counters.
  81. */
  82. struct gcov_fn_info {
  83. const struct gcov_info *key;
  84. unsigned int ident;
  85. unsigned int lineno_checksum;
  86. unsigned int cfg_checksum;
  87. struct gcov_ctr_info ctrs[];
  88. };
  89. /**
  90. * struct gcov_info - coverage info per object file
  91. * @version: gcov version magic indicating the gcc version used for compilation
  92. * @next: list head for a singly-linked list
  93. * @stamp: uniquifying time stamp
  94. * @checksum: unique object checksum
  95. * @filename: name of the associated gcov data file
  96. * @merge: merge functions (null for unused counter type)
  97. * @n_functions: number of instrumented functions
  98. * @functions: pointer to pointers to function information
  99. *
  100. * This data is generated by gcc during compilation and doesn't change
  101. * at run-time with the exception of the next pointer.
  102. */
  103. struct gcov_info {
  104. unsigned int version;
  105. struct gcov_info *next;
  106. unsigned int stamp;
  107. /* Since GCC 12.1 a checksum field is added. */
  108. #if (__GNUC__ >= 12)
  109. unsigned int checksum;
  110. #endif
  111. const char *filename;
  112. void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
  113. unsigned int n_functions;
  114. struct gcov_fn_info **functions;
  115. };
  116. #else
  117. /*
  118. * LLVM/Clang -coverage support
  119. *
  120. * Data structures and runtime callbacks for handling coverage information
  121. * generated by LLVM/Clang with -coverage option. Based on Linux kernel's
  122. * gcov/clang.c implementation.
  123. */
  124. /**
  125. * llvm_gcov_callback - LLVM coverage callback function type
  126. *
  127. * Callback function type used by LLVM for coverage data writeout and flush operations.
  128. */
  129. typedef void (*llvm_gcov_callback)(void);
  130. /**
  131. * struct gcov_fn_info - profiling metadata per function for LLVM/Clang
  132. * @next: pointer to next function info in linked list
  133. * @ident: unique identifier of the function
  134. * @checksum: function checksum for validation
  135. * @cfg_checksum: control flow graph checksum for validation
  136. * @num_counters: number of coverage counters for this function
  137. * @counters: array of 64-bit counter values
  138. *
  139. * This structure stores coverage information for a single function when
  140. * compiled with LLVM/Clang using the -coverage option. The data is generated
  141. * during compilation and updated at runtime by LLVM's coverage instrumentation.
  142. */
  143. struct gcov_fn_info {
  144. struct gcov_fn_info *next;
  145. u32 ident;
  146. u32 checksum;
  147. u32 cfg_checksum;
  148. u32 num_counters;
  149. u64 *counters;
  150. };
  151. /**
  152. * struct gcov_info - coverage info per object file for LLVM/Clang
  153. * @next: pointer to next gcov_info in linked list
  154. * @filename: name of the associated gcov data file
  155. * @version: gcov version magic indicating the compiler version used
  156. * @checksum: unique object checksum
  157. * @functions: linked list of function-level coverage information
  158. *
  159. * This structure stores coverage information for an entire object file when
  160. * compiled with LLVM/Clang. It serves as the container for all functions'
  161. * coverage data within that compilation unit. The data is generated during
  162. * compilation and the counters are updated at runtime.
  163. */
  164. struct gcov_info {
  165. struct gcov_info *next;
  166. const char *filename;
  167. unsigned int version;
  168. u32 checksum;
  169. struct gcov_fn_info *functions;
  170. };
  171. /* Pointer to currently active gcov_info during LLVM gcov information initialization */
  172. static struct gcov_info *current_info;
  173. /* Pointer to currently active gcov_fn_info during function processing (also serves as tail pointer) */
  174. static struct gcov_fn_info *current_function;
  175. #endif
  176. /**
  177. * struct gcov_data - analyzed coverage data
  178. * @next: list head for a singly-linked list
  179. * @filename: name of the associated gcov data file
  180. * @buffer: buffer pointer to save gcda data via convert_to_gcda
  181. * @size: buffer size in bytes
  182. *
  183. */
  184. struct gcov_data {
  185. struct gcov_data *next;
  186. const char *filename;
  187. char *buffer;
  188. size_t size;
  189. };
  190. /* Head of linked list containing all gcov_info from compiled object files */
  191. struct gcov_info *gcov_info_head = NULL;
  192. /* Head of linked list storing collected coverage data after gcov_collect(0) */
  193. struct gcov_data *gcov_data_head = NULL;
  194. /**
  195. * store_gcov_u32 - store 32 bit number in gcov format to buffer
  196. * @buffer: target buffer or NULL
  197. * @off: offset into the buffer
  198. * @v: value to be stored
  199. *
  200. * Number format defined by gcc: numbers are recorded in the 32 bit
  201. * unsigned binary form of the endianness of the machine generating the
  202. * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
  203. * store anything.
  204. */
  205. size_t store_gcov_u32(void *buffer, size_t off, u32 v)
  206. {
  207. u32 *data;
  208. if (buffer) {
  209. data = buffer + off;
  210. *data = v;
  211. }
  212. return sizeof(*data);
  213. }
  214. /**
  215. * store_gcov_u64 - store 64 bit number in gcov format to buffer
  216. * @buffer: target buffer or NULL
  217. * @off: offset into the buffer
  218. * @v: value to be stored
  219. *
  220. * Number format defined by gcc: numbers are recorded in the 32 bit
  221. * unsigned binary form of the endianness of the machine generating the
  222. * file. 64 bit numbers are stored as two 32 bit numbers, the low part
  223. * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
  224. * anything.
  225. */
  226. size_t store_gcov_u64(void *buffer, size_t off, u64 v)
  227. {
  228. u32 *data;
  229. if (buffer) {
  230. data = buffer + off;
  231. data[0] = (v & 0xffffffffUL);
  232. data[1] = (v >> 32);
  233. }
  234. return sizeof(*data) * 2;
  235. }
  236. /**
  237. * gcov_data_link - link/add coverage data set to the global list
  238. * @data: pointer to gcov_data structure to be linked
  239. *
  240. * Adds the given coverage data structure to the head of the global
  241. * gcov_data_head linked list. This function is called after collecting
  242. * and converting coverage data from gcov_info structures.
  243. */
  244. void gcov_data_link(struct gcov_data *data)
  245. {
  246. data->next = gcov_data_head;
  247. gcov_data_head = data;
  248. }
  249. #ifndef __clang__
  250. /*
  251. * GCC-specific stub functions
  252. *
  253. * These functions are called by GCC-generated profiling code but are
  254. * not used in this bare-metal implementation. They are provided as
  255. * stubs to satisfy linker references.
  256. */
  257. /**
  258. * gcov_info_link - link/add coverage info set to the list
  259. * @info: coverage info set
  260. */
  261. void gcov_info_link(struct gcov_info *info)
  262. {
  263. info->next = gcov_info_head;
  264. gcov_info_head = info;
  265. }
  266. /**
  267. * gcov_info_unlink - unlink/remove coverage info set from the list
  268. * @prev: previous coverage info set
  269. * @info: coverage info set
  270. */
  271. void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
  272. {
  273. if (prev)
  274. prev->next = info->next;
  275. else
  276. gcov_info_head = info->next;
  277. }
  278. /*
  279. * Determine whether a counter is active. Doesn't change at run-time.
  280. */
  281. static int counter_active(struct gcov_info *info, unsigned int type)
  282. {
  283. return info->merge[type] ? 1 : 0;
  284. }
  285. /*
  286. * These functions may be referenced by gcc-generated profiling code but serve
  287. * no function for kernel profiling.
  288. */
  289. void __gcov_flush(void)
  290. {
  291. /* Unused. */
  292. }
  293. void __gcov_merge_add(gcov_type *counters, unsigned int n_counters)
  294. {
  295. /* Unused. */
  296. }
  297. void __gcov_merge_single(gcov_type *counters, unsigned int n_counters)
  298. {
  299. /* Unused. */
  300. }
  301. void __gcov_merge_delta(gcov_type *counters, unsigned int n_counters)
  302. {
  303. /* Unused. */
  304. }
  305. void __gcov_merge_ior(gcov_type *counters, unsigned int n_counters)
  306. {
  307. /* Unused. */
  308. }
  309. void __gcov_merge_time_profile(gcov_type *counters, unsigned int n_counters)
  310. {
  311. /* Unused. */
  312. }
  313. void __gcov_merge_icall_topn(gcov_type *counters, unsigned int n_counters)
  314. {
  315. /* Unused. */
  316. }
  317. void __gcov_exit(void)
  318. {
  319. /* Unused. */
  320. }
  321. /*
  322. * __gcov_init is called by gcc-generated constructor code for each object
  323. * file compiled with -fprofile-arcs.
  324. */
  325. void __gcov_init(struct gcov_info *info)
  326. {
  327. if (info) {
  328. gcov_info_link(info);
  329. }
  330. }
  331. /**
  332. * convert_to_gcda - convert coverage info set to gcda file format
  333. * @buffer: the buffer to store file data or %NULL if no data should be stored
  334. * @info: coverage info set to be converted
  335. *
  336. * Returns the number of bytes that were/would have been stored into the buffer.
  337. */
  338. size_t convert_to_gcda(char *buffer, struct gcov_info *info)
  339. {
  340. struct gcov_fn_info *fi_ptr;
  341. struct gcov_ctr_info *ci_ptr;
  342. unsigned int fi_idx;
  343. unsigned int ct_idx;
  344. unsigned int cv_idx;
  345. size_t pos = 0;
  346. /* File header. */
  347. pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
  348. pos += store_gcov_u32(buffer, pos, info->version);
  349. pos += store_gcov_u32(buffer, pos, info->stamp);
  350. #if (__GNUC__ >= 12)
  351. /* Use zero as checksum of the compilation unit. */
  352. pos += store_gcov_u32(buffer, pos, 0);
  353. #endif
  354. for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
  355. fi_ptr = info->functions[fi_idx];
  356. /* Function record. */
  357. pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
  358. pos += store_gcov_u32(buffer, pos,
  359. GCOV_TAG_FUNCTION_LENGTH * GCOV_UNIT_SIZE);
  360. pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
  361. pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
  362. pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
  363. ci_ptr = fi_ptr->ctrs;
  364. for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
  365. if (!counter_active(info, ct_idx))
  366. continue;
  367. /* Counter record. */
  368. pos += store_gcov_u32(buffer, pos,
  369. GCOV_TAG_FOR_COUNTER(ct_idx));
  370. pos += store_gcov_u32(buffer, pos,
  371. ci_ptr->num * 2 * GCOV_UNIT_SIZE);
  372. for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
  373. pos += store_gcov_u64(buffer, pos,
  374. ci_ptr->values[cv_idx]);
  375. }
  376. ci_ptr++;
  377. }
  378. }
  379. return pos;
  380. }
  381. #else
  382. /*
  383. * LLVM/Clang -coverage runtime stub functions
  384. *
  385. * These functions are called by Clang-generated profiling code when
  386. * compiled with -coverage option. They are provided as stubs to
  387. * satisfy linker references in this bare-metal implementation.
  388. */
  389. /**
  390. * llvm_gcov_init - initialize LLVM coverage runtime
  391. * @writeout: callback for writing coverage data
  392. * @flush: callback for flushing coverage data
  393. *
  394. * Called by Clang runtime to initialize coverage data structures.
  395. */
  396. void llvm_gcov_init(llvm_gcov_callback writeout, llvm_gcov_callback flush)
  397. {
  398. struct gcov_info *info = (struct gcov_info *)malloc(sizeof(*info));
  399. if (!info) {
  400. return;
  401. }
  402. info->next = NULL;
  403. info->functions = NULL;
  404. current_info = info;
  405. current_function = NULL;
  406. if (gcov_info_head == NULL) {
  407. gcov_info_head = current_info;
  408. } else {
  409. gcov_info_head->next = current_info;
  410. }
  411. writeout();
  412. current_info = NULL;
  413. }
  414. /**
  415. * llvm_gcda_start_file - start coverage data for a source file
  416. * @orig_filename: original source file name
  417. * @version: gcov version magic
  418. * @checksum: compilation unit checksum
  419. *
  420. * Called by Clang runtime at the start of coverage data emission.
  421. */
  422. void llvm_gcda_start_file(const char *orig_filename, u32 version, u32 checksum)
  423. {
  424. if (!current_info) {
  425. return;
  426. }
  427. current_info->filename = orig_filename;
  428. current_info->version = version;
  429. current_info->checksum = checksum;
  430. }
  431. /**
  432. * llvm_gcda_emit_function - emit coverage data for a function
  433. * @ident: function unique identifier
  434. * @func_checksum: function checksum
  435. * @cfg_checksum: control flow graph checksum
  436. *
  437. * Called by Clang runtime to emit coverage data for each function.
  438. */
  439. void llvm_gcda_emit_function(u32 ident, u32 func_checksum, u32 cfg_checksum)
  440. {
  441. struct gcov_fn_info *info = (struct gcov_fn_info *)malloc(sizeof(*info));
  442. if (!info) {
  443. return;
  444. }
  445. info->ident = ident;
  446. info->checksum = func_checksum;
  447. info->cfg_checksum = cfg_checksum;
  448. info->next = NULL;
  449. if (current_info->functions == NULL) {
  450. current_info->functions = info;
  451. } else {
  452. current_function->next = info;
  453. }
  454. current_function = info;
  455. }
  456. /**
  457. * llvm_gcda_emit_arcs - emit coverage counters for function arcs
  458. * @num_counters: number of arc counters
  459. * @counters: array of 64-bit counter values
  460. *
  461. * Called by Clang runtime to emit arc coverage counters.
  462. */
  463. void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
  464. {
  465. if (current_function) {
  466. current_function->num_counters = num_counters;
  467. current_function->counters = counters;
  468. }
  469. }
  470. /**
  471. * llvm_gcda_summary_info - emit summary information
  472. *
  473. * Stub function for coverage summary (not used in this implementation).
  474. */
  475. void llvm_gcda_summary_info(void)
  476. {
  477. }
  478. /**
  479. * llvm_gcda_end_file - end coverage data emission for a file
  480. *
  481. * Called by Clang runtime at the end of coverage data emission.
  482. */
  483. void llvm_gcda_end_file(void)
  484. {
  485. }
  486. /**
  487. * convert_to_gcda - convert profiling data set to gcda file format
  488. * @buffer: the buffer to store file data or %NULL if no data should be stored
  489. * @info: profiling data set to be converted
  490. *
  491. * Returns the number of bytes that were/would have been stored into the buffer.
  492. */
  493. size_t convert_to_gcda(char *buffer, struct gcov_info *info)
  494. {
  495. struct gcov_fn_info *fi_ptr;
  496. size_t pos = 0;
  497. /* File header. */
  498. pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
  499. pos += store_gcov_u32(buffer, pos, info->version);
  500. pos += store_gcov_u32(buffer, pos, info->checksum);
  501. for (fi_ptr = info->functions; fi_ptr != NULL; fi_ptr = fi_ptr->next) {
  502. u32 i;
  503. pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
  504. pos += store_gcov_u32(buffer, pos, 3);
  505. pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
  506. pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
  507. pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
  508. pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
  509. pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
  510. for (i = 0; i < fi_ptr->num_counters; i++) {
  511. pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);
  512. }
  513. }
  514. return pos;
  515. }
  516. #endif
  517. /* Dump buffer in hex format, 20 bytes per line */
  518. #define NUM_OCTETS_PER_LINE 20
  519. #define FLUSH_OUTPUT() fflush(stdout)
  520. /**
  521. * hexdumpbuf - dump buffer content as hex to stdout
  522. * @buf: pointer to buffer to dump
  523. * @sz: size of buffer in bytes
  524. *
  525. * Dumps buffer content in hexadecimal format, 20 bytes per line.
  526. * Modified based on: https://github.com/astarasikov/lk/commit/2a4af09a894194dfaff3e05f6fd505241d54d074
  527. */
  528. static void hexdumpbuf(char *buf, unsigned long sz)
  529. {
  530. unsigned long rem, cur = 0, i = 0;
  531. FLUSH_OUTPUT();
  532. while (cur < sz) {
  533. rem = ((sz - cur) < NUM_OCTETS_PER_LINE) ? (sz - cur) : NUM_OCTETS_PER_LINE;
  534. for (i = 0; i < rem; i++) {
  535. printf("%02x", buf[cur + i]);
  536. }
  537. printf("\n");
  538. FLUSH_OUTPUT();
  539. cur += rem;
  540. }
  541. }
  542. /**
  543. * dump_gcov_info - convert and dump single gcov_info as hex
  544. * @info: pointer to gcov_info structure to dump
  545. *
  546. * Converts coverage data to gcda format and prints as hex to console
  547. * for debugging purposes.
  548. */
  549. static void dump_gcov_info(struct gcov_info *info)
  550. {
  551. size_t sz = 0;
  552. char *bufptr = NULL;
  553. if (!info) {
  554. return;
  555. }
  556. sz = convert_to_gcda(NULL, info);
  557. bufptr = (char *)malloc(sz);
  558. if (bufptr == NULL) {
  559. printf("ERROR: Can't allocate gcda buffer for %s\n", info->filename);
  560. return;
  561. }
  562. sz = convert_to_gcda(bufptr, info);
  563. hexdumpbuf(bufptr, sz);
  564. free(bufptr);
  565. printf("\nCREATE: %s\n", info->filename);
  566. }
  567. /**
  568. * gcov_dump - dump all coverage data to console
  569. *
  570. * Iterates through gcov_info_head linked list and converts each gcov_info
  571. * to gcda format, then prints as hexadecimal to stdout for debugging.
  572. */
  573. void gcov_dump(void)
  574. {
  575. struct gcov_info *info;
  576. if (!gcov_info_head) {
  577. return;
  578. }
  579. printf("\nDump coverage data start\n");
  580. FLUSH_OUTPUT();
  581. for (info = gcov_info_head; info != NULL; info = info->next) {
  582. dump_gcov_info(info);
  583. }
  584. printf("\nDump coverage data finish\n");
  585. FLUSH_OUTPUT();
  586. }
  587. /**
  588. * gcov_free - free all collected coverage data
  589. *
  590. * Frees all gcov_data structures and their associated buffers from
  591. * the gcov_data_head linked list. Should be called before collecting
  592. * new coverage data to avoid memory leaks.
  593. */
  594. void gcov_free(void)
  595. {
  596. struct gcov_data *data;
  597. if (gcov_data_head == NULL) {
  598. return;
  599. }
  600. for (data = gcov_data_head; data != NULL; data = data->next) {
  601. if (data->buffer) {
  602. free(data->buffer);
  603. }
  604. free(data);
  605. }
  606. gcov_data_head = NULL;
  607. }
  608. /**
  609. * gcov_collect - collect and convert coverage data from gcov_info_head
  610. * @interface: output interface selector
  611. * - 0: collect only, store in gcov_data_head
  612. * - 1: collect and save to .gcda files
  613. * - >1: collect and dump to console via gcov_dump()
  614. *
  615. * Collects coverage data from gcov_info_head, converts to gcda format,
  616. * and stores in gcov_data_head linked list. May fail if heap is insufficient.
  617. *
  618. * Returns: 0 on success, -1 on failure
  619. */
  620. int gcov_collect(unsigned long interface)
  621. {
  622. struct gcov_info *info;
  623. struct gcov_data *data;
  624. size_t sz = 0, count = 0;
  625. char *bufptr = NULL;
  626. /* Check if coverage info exists */
  627. if (!gcov_info_head) {
  628. return -1;
  629. }
  630. /* Dump to console if interface > 1 */
  631. if (interface > 1) {
  632. gcov_dump();
  633. return 0;
  634. }
  635. /* Free previous coverage data to collect latest */
  636. gcov_free();
  637. for (info = gcov_info_head; info != NULL; info = info->next) {
  638. sz = convert_to_gcda(NULL, info);
  639. bufptr = (char *)malloc(sz);
  640. data = (struct gcov_data *)malloc(sizeof(struct gcov_data));
  641. if ((bufptr == NULL) || (data == NULL)) {
  642. printf("Can't allocate gcda buffer for %s\n", info->filename);
  643. return -1;
  644. }
  645. data->filename = info->filename;
  646. data->buffer = bufptr;
  647. data->size = sz;
  648. convert_to_gcda(bufptr, info);
  649. gcov_data_link(data);
  650. if (interface == 1) {
  651. FILE *fp = fopen(data->filename, "wb");
  652. if (fp != NULL) {
  653. printf("Create and store coverage data in %s file\n", data->filename);
  654. fwrite(data->buffer, 1, (size_t)data->size, fp);
  655. fclose(fp);
  656. } else {
  657. printf("Unable to open %s file\n", data->filename);
  658. }
  659. }
  660. count += 1;
  661. }
  662. if (count) {
  663. printf("%lu files coverage data collected, see gcov_data_head=0x%lx\n", (unsigned long)count, (unsigned long)gcov_data_head);
  664. }
  665. return 0;
  666. }