gcov.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This code provides functions to handle gcc's coverage info format
  4. * introduced with gcc 4.7.
  5. *
  6. * This file is based heavily on gcc_3_4.c file.
  7. *
  8. * For a better understanding, refer to gcc source:
  9. * gcc/gcov-io.h
  10. * libgcc/libgcov.c
  11. *
  12. * Uses gcc-internal data definitions.
  13. */
  14. // Modified based on https://github.com/torvalds/linux/blob/master/kernel/gcov/gcc_4_7.c
  15. #include <stdint.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. //#define DEBUG
  20. #if BITS_PER_LONG >= 64
  21. typedef long gcov_type;
  22. #else
  23. typedef long long gcov_type;
  24. #endif
  25. typedef uint64_t u64;
  26. typedef uint32_t u32;
  27. /*
  28. * Profiling data types used for gcc 3.4 and above - these are defined by
  29. * gcc and need to be kept as close to the original definition as possible to
  30. * remain compatible.
  31. */
  32. #define GCOV_DATA_MAGIC ((unsigned int) 0x67636461)
  33. #define GCOV_TAG_FUNCTION ((unsigned int) 0x01000000)
  34. #define GCOV_TAG_COUNTER_BASE ((unsigned int) 0x01a10000)
  35. #define GCOV_TAG_FOR_COUNTER(count) \
  36. (GCOV_TAG_COUNTER_BASE + ((unsigned int) (count) << 17))
  37. #if (__GNUC__ >= 14)
  38. #define GCOV_COUNTERS 9
  39. #elif (__GNUC__ >= 10)
  40. #define GCOV_COUNTERS 8
  41. #elif (__GNUC__ >= 7)
  42. #define GCOV_COUNTERS 9
  43. #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
  44. #define GCOV_COUNTERS 10
  45. #else
  46. #define GCOV_COUNTERS 9
  47. #endif
  48. #define GCOV_TAG_FUNCTION_LENGTH 3
  49. /* Since GCC 12.1 sizes are in BYTES and not in WORDS (4B). */
  50. #if (__GNUC__ >= 12)
  51. #define GCOV_UNIT_SIZE 4
  52. #else
  53. #define GCOV_UNIT_SIZE 1
  54. #endif
  55. /**
  56. * struct gcov_ctr_info - information about counters for a single function
  57. * @num: number of counter values for this type
  58. * @values: array of counter values for this type
  59. *
  60. * This data is generated by gcc during compilation and doesn't change
  61. * at run-time with the exception of the values array.
  62. */
  63. struct gcov_ctr_info {
  64. unsigned int num;
  65. gcov_type *values;
  66. };
  67. /**
  68. * struct gcov_fn_info - profiling meta data per function
  69. * @key: comdat key
  70. * @ident: unique ident of function
  71. * @lineno_checksum: function lineo_checksum
  72. * @cfg_checksum: function cfg checksum
  73. * @ctrs: instrumented counters
  74. *
  75. * This data is generated by gcc during compilation and doesn't change
  76. * at run-time.
  77. *
  78. * Information about a single function. This uses the trailing array
  79. * idiom. The number of counters is determined from the merge pointer
  80. * array in gcov_info. The key is used to detect which of a set of
  81. * comdat functions was selected -- it points to the gcov_info object
  82. * of the object file containing the selected comdat function.
  83. */
  84. struct gcov_fn_info {
  85. const struct gcov_info *key;
  86. unsigned int ident;
  87. unsigned int lineno_checksum;
  88. unsigned int cfg_checksum;
  89. struct gcov_ctr_info ctrs[];
  90. };
  91. /**
  92. * struct gcov_info - coverage info per object file
  93. * @version: gcov version magic indicating the gcc version used for compilation
  94. * @next: list head for a singly-linked list
  95. * @stamp: uniquifying time stamp
  96. * @checksum: unique object checksum
  97. * @filename: name of the associated gcov data file
  98. * @merge: merge functions (null for unused counter type)
  99. * @n_functions: number of instrumented functions
  100. * @functions: pointer to pointers to function information
  101. *
  102. * This data is generated by gcc during compilation and doesn't change
  103. * at run-time with the exception of the next pointer.
  104. */
  105. struct gcov_info {
  106. unsigned int version;
  107. struct gcov_info *next;
  108. unsigned int stamp;
  109. /* Since GCC 12.1 a checksum field is added. */
  110. #if (__GNUC__ >= 12)
  111. unsigned int checksum;
  112. #endif
  113. const char *filename;
  114. void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
  115. unsigned int n_functions;
  116. struct gcov_fn_info **functions;
  117. };
  118. /**
  119. * struct gcov_data - analyzed coverage data
  120. * @next: list head for a singly-linked list
  121. * @filename: name of the associated gcov data file
  122. * @buffer: buffer pointer to save gcda data via convert_to_gcda
  123. * @size: buffer size in bytes
  124. *
  125. */
  126. struct gcov_data {
  127. struct gcov_data *next;
  128. const char *filename;
  129. char *buffer;
  130. size_t size;
  131. };
  132. struct gcov_info *gcov_info_head = NULL;
  133. /** where coverage data stored in after execute gcov_collect(0) */
  134. struct gcov_data *gcov_data_head = NULL;
  135. /**
  136. * gcov_info_filename - return info filename
  137. * @info: coverage info set
  138. */
  139. const char *gcov_info_filename(struct gcov_info *info)
  140. {
  141. return info->filename;
  142. }
  143. /**
  144. * gcov_info_version - return info version
  145. * @info: coverage info set
  146. */
  147. unsigned int gcov_info_version(struct gcov_info *info)
  148. {
  149. return info->version;
  150. }
  151. /**
  152. * gcov_info_next - return next coverage info set
  153. * @info: coverage info set
  154. *
  155. * Returns next gcov_info following @info or first gcov_info in the chain if
  156. * @info is %NULL.
  157. */
  158. struct gcov_info *gcov_info_next(struct gcov_info *info)
  159. {
  160. if (!info)
  161. return gcov_info_head;
  162. return info->next;
  163. }
  164. /**
  165. * gcov_info_link - link/add coverage info set to the list
  166. * @info: coverage info set
  167. */
  168. void gcov_info_link(struct gcov_info *info)
  169. {
  170. info->next = gcov_info_head;
  171. gcov_info_head = info;
  172. }
  173. /**
  174. * gcov_info_unlink - unlink/remove coverage info set from the list
  175. * @prev: previous coverage info set
  176. * @info: coverage info set
  177. */
  178. void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
  179. {
  180. if (prev)
  181. prev->next = info->next;
  182. else
  183. gcov_info_head = info->next;
  184. }
  185. /**
  186. * gcov_data_link - link/add coverage data set to the list
  187. * @info: coverage data set
  188. */
  189. void gcov_data_link(struct gcov_data *data)
  190. {
  191. data->next = gcov_data_head;
  192. gcov_data_head = data;
  193. }
  194. /*
  195. * Determine whether a counter is active. Doesn't change at run-time.
  196. */
  197. static int counter_active(struct gcov_info *info, unsigned int type)
  198. {
  199. return info->merge[type] ? 1 : 0;
  200. }
  201. /**
  202. * store_gcov_u32 - store 32 bit number in gcov format to buffer
  203. * @buffer: target buffer or NULL
  204. * @off: offset into the buffer
  205. * @v: value to be stored
  206. *
  207. * Number format defined by gcc: numbers are recorded in the 32 bit
  208. * unsigned binary form of the endianness of the machine generating the
  209. * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
  210. * store anything.
  211. */
  212. size_t store_gcov_u32(void *buffer, size_t off, u32 v)
  213. {
  214. u32 *data;
  215. if (buffer) {
  216. data = buffer + off;
  217. *data = v;
  218. }
  219. return sizeof(*data);
  220. }
  221. /**
  222. * store_gcov_u64 - store 64 bit number in gcov format to buffer
  223. * @buffer: target buffer or NULL
  224. * @off: offset into the buffer
  225. * @v: value to be stored
  226. *
  227. * Number format defined by gcc: numbers are recorded in the 32 bit
  228. * unsigned binary form of the endianness of the machine generating the
  229. * file. 64 bit numbers are stored as two 32 bit numbers, the low part
  230. * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
  231. * anything.
  232. */
  233. size_t store_gcov_u64(void *buffer, size_t off, u64 v)
  234. {
  235. u32 *data;
  236. if (buffer) {
  237. data = buffer + off;
  238. data[0] = (v & 0xffffffffUL);
  239. data[1] = (v >> 32);
  240. }
  241. return sizeof(*data) * 2;
  242. }
  243. /**
  244. * convert_to_gcda - convert coverage info set to gcda file format
  245. * @buffer: the buffer to store file data or %NULL if no data should be stored
  246. * @info: coverage info set to be converted
  247. *
  248. * Returns the number of bytes that were/would have been stored into the buffer.
  249. */
  250. size_t convert_to_gcda(char *buffer, struct gcov_info *info)
  251. {
  252. struct gcov_fn_info *fi_ptr;
  253. struct gcov_ctr_info *ci_ptr;
  254. unsigned int fi_idx;
  255. unsigned int ct_idx;
  256. unsigned int cv_idx;
  257. size_t pos = 0;
  258. /* File header. */
  259. pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
  260. pos += store_gcov_u32(buffer, pos, info->version);
  261. pos += store_gcov_u32(buffer, pos, info->stamp);
  262. #if (__GNUC__ >= 12)
  263. /* Use zero as checksum of the compilation unit. */
  264. pos += store_gcov_u32(buffer, pos, 0);
  265. #endif
  266. for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
  267. fi_ptr = info->functions[fi_idx];
  268. /* Function record. */
  269. pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
  270. pos += store_gcov_u32(buffer, pos,
  271. GCOV_TAG_FUNCTION_LENGTH * GCOV_UNIT_SIZE);
  272. pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
  273. pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
  274. pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
  275. ci_ptr = fi_ptr->ctrs;
  276. for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
  277. if (!counter_active(info, ct_idx))
  278. continue;
  279. /* Counter record. */
  280. pos += store_gcov_u32(buffer, pos,
  281. GCOV_TAG_FOR_COUNTER(ct_idx));
  282. pos += store_gcov_u32(buffer, pos,
  283. ci_ptr->num * 2 * GCOV_UNIT_SIZE);
  284. for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
  285. pos += store_gcov_u64(buffer, pos,
  286. ci_ptr->values[cv_idx]);
  287. }
  288. ci_ptr++;
  289. }
  290. }
  291. return pos;
  292. }
  293. /*
  294. * These functions may be referenced by gcc-generated profiling code but serve
  295. * no function for kernel profiling.
  296. */
  297. void __gcov_flush(void)
  298. {
  299. /* Unused. */
  300. }
  301. void __gcov_merge_add(gcov_type *counters, unsigned int n_counters)
  302. {
  303. /* Unused. */
  304. }
  305. void __gcov_merge_single(gcov_type *counters, unsigned int n_counters)
  306. {
  307. /* Unused. */
  308. }
  309. void __gcov_merge_delta(gcov_type *counters, unsigned int n_counters)
  310. {
  311. /* Unused. */
  312. }
  313. void __gcov_merge_ior(gcov_type *counters, unsigned int n_counters)
  314. {
  315. /* Unused. */
  316. }
  317. void __gcov_merge_time_profile(gcov_type *counters, unsigned int n_counters)
  318. {
  319. /* Unused. */
  320. }
  321. void __gcov_merge_icall_topn(gcov_type *counters, unsigned int n_counters)
  322. {
  323. /* Unused. */
  324. }
  325. void __gcov_exit(void)
  326. {
  327. /* Unused. */
  328. }
  329. /*
  330. * __gcov_init is called by gcc-generated constructor code for each object
  331. * file compiled with -fprofile-arcs.
  332. */
  333. void __gcov_init(struct gcov_info *info)
  334. {
  335. if (info) {
  336. gcov_info_link(info);
  337. }
  338. }
  339. #define NUM_OCTETS_PER_LINE 20
  340. #define FLUSH_OUTPUT() fflush(stdout)
  341. static void hexdumpbuf(char *buf, unsigned long sz)
  342. {
  343. unsigned long rem, cur = 0, i = 0;
  344. FLUSH_OUTPUT();
  345. while (cur < sz) {
  346. rem = ((sz - cur) < NUM_OCTETS_PER_LINE) ? (sz - cur) : NUM_OCTETS_PER_LINE;
  347. for (i = 0; i < rem; i++) {
  348. printf("%02x", buf[cur + i]);
  349. }
  350. printf("\n");
  351. FLUSH_OUTPUT();
  352. cur += rem;
  353. }
  354. }
  355. // Modified based on https://github.com/astarasikov/lk/commit/2a4af09a894194dfaff3e05f6fd505241d54d074
  356. static void dump_gcov_info(struct gcov_info *info)
  357. {
  358. size_t sz = 0;
  359. char *bufptr = NULL;
  360. if (!info) {
  361. return;
  362. }
  363. sz = convert_to_gcda(NULL, info);
  364. bufptr = (char *)malloc(sz);
  365. if (bufptr == NULL) {
  366. printf("ERROR: Can't allocate gcda buffer for %s\n", info->filename);
  367. return;
  368. }
  369. sz = convert_to_gcda(bufptr, info);
  370. hexdumpbuf(bufptr, sz);
  371. free(bufptr);
  372. printf("\nCREATE: %s\n", info->filename);
  373. }
  374. void gcov_dump(void)
  375. {
  376. struct gcov_info *info;
  377. if (!gcov_info_head) {
  378. return;
  379. }
  380. printf("\nDump coverage data start\n");
  381. fflush(stdout);
  382. for (info = gcov_info_head; info != NULL; info = info->next) {
  383. dump_gcov_info(info);
  384. }
  385. printf("\nDump coverage data finish\n");
  386. fflush(stdout);
  387. }
  388. /**
  389. * gcov_free - free all coverage data allocated memory
  390. *
  391. */
  392. void gcov_free(void)
  393. {
  394. struct gcov_data *data;
  395. if (gcov_data_head == NULL) {
  396. return;
  397. }
  398. for (data = gcov_data_head; data != NULL; data = data->next) {
  399. if (data->buffer) {
  400. free(data->buffer);
  401. }
  402. free(data);
  403. }
  404. gcov_data_head = NULL;
  405. }
  406. /**
  407. * gcov_collect - collect and convert coverage data from gcov_info_head
  408. * It need to malloc buffer from heap, so it may fail if your heap is not big enough
  409. *
  410. * Return 0, if all coverage data is collected and converted
  411. */
  412. int gcov_collect(unsigned long interface)
  413. {
  414. struct gcov_info *info;
  415. struct gcov_data *data;
  416. size_t sz = 0, count = 0;
  417. char *bufptr = NULL;
  418. // Make sure there are coverage information in it
  419. if (!gcov_info_head) {
  420. return -1;
  421. }
  422. // if you want to dump in console, just call gcov_dump() function
  423. if (interface > 1) {
  424. gcov_dump();
  425. return 0;
  426. }
  427. // Free coverage data if present before
  428. // to make sure collect latest coverage data
  429. gcov_free();
  430. for (info = gcov_info_head; info != NULL; info = info->next) {
  431. sz = convert_to_gcda(NULL, info);
  432. bufptr = (char *)malloc(sz);
  433. data = (struct gcov_data *)malloc(sizeof(struct gcov_data));
  434. if ((bufptr == NULL) || (data == NULL)) {
  435. printf("Can't allocate gcda buffer for %s\n", info->filename);
  436. return -1;
  437. }
  438. data->filename = info->filename;
  439. data->buffer = bufptr;
  440. data->size = sz;
  441. convert_to_gcda(bufptr, info);
  442. gcov_data_link(data);
  443. if (interface == 1) {
  444. FILE *fp = fopen(data->filename, "wb");
  445. if (fp != NULL) {
  446. printf("Create and store coverage data in %s file\n", data->filename);
  447. fwrite(data->buffer, 1, (size_t)data->size, fp);
  448. fclose(fp);
  449. } else {
  450. printf("Unable to open %s file\n", data->filename);
  451. }
  452. }
  453. count += 1;
  454. }
  455. if (count) {
  456. printf("%u files coverage data collected, see gcov_data_head=0x%x\n", count, gcov_data_head);
  457. }
  458. return 0;
  459. }