gcov.c 14 KB

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