gcc_4_7.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This code provides functions to handle gcc's profiling data 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. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/vmalloc.h>
  19. #include "gcov.h"
  20. #if (__GNUC__ >= 7)
  21. #define GCOV_COUNTERS 9
  22. #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
  23. #define GCOV_COUNTERS 10
  24. #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
  25. #define GCOV_COUNTERS 9
  26. #else
  27. #define GCOV_COUNTERS 8
  28. #endif
  29. #define GCOV_TAG_FUNCTION_LENGTH 3
  30. static struct gcov_info *gcov_info_head;
  31. /**
  32. * struct gcov_ctr_info - information about counters for a single function
  33. * @num: number of counter values for this type
  34. * @values: array of counter values for this type
  35. *
  36. * This data is generated by gcc during compilation and doesn't change
  37. * at run-time with the exception of the values array.
  38. */
  39. struct gcov_ctr_info {
  40. unsigned int num;
  41. gcov_type *values;
  42. };
  43. /**
  44. * struct gcov_fn_info - profiling meta data per function
  45. * @key: comdat key
  46. * @ident: unique ident of function
  47. * @lineno_checksum: function lineo_checksum
  48. * @cfg_checksum: function cfg checksum
  49. * @ctrs: instrumented counters
  50. *
  51. * This data is generated by gcc during compilation and doesn't change
  52. * at run-time.
  53. *
  54. * Information about a single function. This uses the trailing array
  55. * idiom. The number of counters is determined from the merge pointer
  56. * array in gcov_info. The key is used to detect which of a set of
  57. * comdat functions was selected -- it points to the gcov_info object
  58. * of the object file containing the selected comdat function.
  59. */
  60. struct gcov_fn_info {
  61. const struct gcov_info *key;
  62. unsigned int ident;
  63. unsigned int lineno_checksum;
  64. unsigned int cfg_checksum;
  65. struct gcov_ctr_info ctrs[0];
  66. };
  67. /**
  68. * struct gcov_info - profiling data per object file
  69. * @version: gcov version magic indicating the gcc version used for compilation
  70. * @next: list head for a singly-linked list
  71. * @stamp: uniquifying time stamp
  72. * @filename: name of the associated gcov data file
  73. * @merge: merge functions (null for unused counter type)
  74. * @n_functions: number of instrumented functions
  75. * @functions: pointer to pointers to function information
  76. *
  77. * This data is generated by gcc during compilation and doesn't change
  78. * at run-time with the exception of the next pointer.
  79. */
  80. struct gcov_info {
  81. unsigned int version;
  82. struct gcov_info *next;
  83. unsigned int stamp;
  84. const char *filename;
  85. void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
  86. unsigned int n_functions;
  87. struct gcov_fn_info **functions;
  88. };
  89. /**
  90. * gcov_info_filename - return info filename
  91. * @info: profiling data set
  92. */
  93. const char *gcov_info_filename(struct gcov_info *info)
  94. {
  95. return info->filename;
  96. }
  97. /**
  98. * gcov_info_version - return info version
  99. * @info: profiling data set
  100. */
  101. unsigned int gcov_info_version(struct gcov_info *info)
  102. {
  103. return info->version;
  104. }
  105. /**
  106. * gcov_info_next - return next profiling data set
  107. * @info: profiling data set
  108. *
  109. * Returns next gcov_info following @info or first gcov_info in the chain if
  110. * @info is %NULL.
  111. */
  112. struct gcov_info *gcov_info_next(struct gcov_info *info)
  113. {
  114. if (!info)
  115. return gcov_info_head;
  116. return info->next;
  117. }
  118. /**
  119. * gcov_info_link - link/add profiling data set to the list
  120. * @info: profiling data set
  121. */
  122. void gcov_info_link(struct gcov_info *info)
  123. {
  124. info->next = gcov_info_head;
  125. gcov_info_head = info;
  126. }
  127. /**
  128. * gcov_info_unlink - unlink/remove profiling data set from the list
  129. * @prev: previous profiling data set
  130. * @info: profiling data set
  131. */
  132. void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
  133. {
  134. if (prev)
  135. prev->next = info->next;
  136. else
  137. gcov_info_head = info->next;
  138. }
  139. /* Symbolic links to be created for each profiling data file. */
  140. const struct gcov_link gcov_link[] = {
  141. { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
  142. { 0, NULL},
  143. };
  144. /*
  145. * Determine whether a counter is active. Doesn't change at run-time.
  146. */
  147. static int counter_active(struct gcov_info *info, unsigned int type)
  148. {
  149. return info->merge[type] ? 1 : 0;
  150. }
  151. /* Determine number of active counters. Based on gcc magic. */
  152. static unsigned int num_counter_active(struct gcov_info *info)
  153. {
  154. unsigned int i;
  155. unsigned int result = 0;
  156. for (i = 0; i < GCOV_COUNTERS; i++) {
  157. if (counter_active(info, i))
  158. result++;
  159. }
  160. return result;
  161. }
  162. /**
  163. * gcov_info_reset - reset profiling data to zero
  164. * @info: profiling data set
  165. */
  166. void gcov_info_reset(struct gcov_info *info)
  167. {
  168. struct gcov_ctr_info *ci_ptr;
  169. unsigned int fi_idx;
  170. unsigned int ct_idx;
  171. for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
  172. ci_ptr = info->functions[fi_idx]->ctrs;
  173. for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
  174. if (!counter_active(info, ct_idx))
  175. continue;
  176. memset(ci_ptr->values, 0,
  177. sizeof(gcov_type) * ci_ptr->num);
  178. ci_ptr++;
  179. }
  180. }
  181. }
  182. /**
  183. * gcov_info_is_compatible - check if profiling data can be added
  184. * @info1: first profiling data set
  185. * @info2: second profiling data set
  186. *
  187. * Returns non-zero if profiling data can be added, zero otherwise.
  188. */
  189. int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
  190. {
  191. return (info1->stamp == info2->stamp);
  192. }
  193. /**
  194. * gcov_info_add - add up profiling data
  195. * @dest: profiling data set to which data is added
  196. * @source: profiling data set which is added
  197. *
  198. * Adds profiling counts of @source to @dest.
  199. */
  200. void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
  201. {
  202. struct gcov_ctr_info *dci_ptr;
  203. struct gcov_ctr_info *sci_ptr;
  204. unsigned int fi_idx;
  205. unsigned int ct_idx;
  206. unsigned int val_idx;
  207. for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) {
  208. dci_ptr = dst->functions[fi_idx]->ctrs;
  209. sci_ptr = src->functions[fi_idx]->ctrs;
  210. for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
  211. if (!counter_active(src, ct_idx))
  212. continue;
  213. for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
  214. dci_ptr->values[val_idx] +=
  215. sci_ptr->values[val_idx];
  216. dci_ptr++;
  217. sci_ptr++;
  218. }
  219. }
  220. }
  221. /**
  222. * gcov_info_dup - duplicate profiling data set
  223. * @info: profiling data set to duplicate
  224. *
  225. * Return newly allocated duplicate on success, %NULL on error.
  226. */
  227. struct gcov_info *gcov_info_dup(struct gcov_info *info)
  228. {
  229. struct gcov_info *dup;
  230. struct gcov_ctr_info *dci_ptr; /* dst counter info */
  231. struct gcov_ctr_info *sci_ptr; /* src counter info */
  232. unsigned int active;
  233. unsigned int fi_idx; /* function info idx */
  234. unsigned int ct_idx; /* counter type idx */
  235. size_t fi_size; /* function info size */
  236. size_t cv_size; /* counter values size */
  237. dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
  238. if (!dup)
  239. return NULL;
  240. dup->next = NULL;
  241. dup->filename = NULL;
  242. dup->functions = NULL;
  243. dup->filename = kstrdup(info->filename, GFP_KERNEL);
  244. if (!dup->filename)
  245. goto err_free;
  246. dup->functions = kcalloc(info->n_functions,
  247. sizeof(struct gcov_fn_info *), GFP_KERNEL);
  248. if (!dup->functions)
  249. goto err_free;
  250. active = num_counter_active(info);
  251. fi_size = sizeof(struct gcov_fn_info);
  252. fi_size += sizeof(struct gcov_ctr_info) * active;
  253. for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
  254. dup->functions[fi_idx] = kzalloc(fi_size, GFP_KERNEL);
  255. if (!dup->functions[fi_idx])
  256. goto err_free;
  257. *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
  258. sci_ptr = info->functions[fi_idx]->ctrs;
  259. dci_ptr = dup->functions[fi_idx]->ctrs;
  260. for (ct_idx = 0; ct_idx < active; ct_idx++) {
  261. cv_size = sizeof(gcov_type) * sci_ptr->num;
  262. dci_ptr->values = vmalloc(cv_size);
  263. if (!dci_ptr->values)
  264. goto err_free;
  265. dci_ptr->num = sci_ptr->num;
  266. memcpy(dci_ptr->values, sci_ptr->values, cv_size);
  267. sci_ptr++;
  268. dci_ptr++;
  269. }
  270. }
  271. return dup;
  272. err_free:
  273. gcov_info_free(dup);
  274. return NULL;
  275. }
  276. /**
  277. * gcov_info_free - release memory for profiling data set duplicate
  278. * @info: profiling data set duplicate to free
  279. */
  280. void gcov_info_free(struct gcov_info *info)
  281. {
  282. unsigned int active;
  283. unsigned int fi_idx;
  284. unsigned int ct_idx;
  285. struct gcov_ctr_info *ci_ptr;
  286. if (!info->functions)
  287. goto free_info;
  288. active = num_counter_active(info);
  289. for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
  290. if (!info->functions[fi_idx])
  291. continue;
  292. ci_ptr = info->functions[fi_idx]->ctrs;
  293. for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
  294. vfree(ci_ptr->values);
  295. kfree(info->functions[fi_idx]);
  296. }
  297. free_info:
  298. kfree(info->functions);
  299. kfree(info->filename);
  300. kfree(info);
  301. }
  302. #define ITER_STRIDE PAGE_SIZE
  303. /**
  304. * struct gcov_iterator - specifies current file position in logical records
  305. * @info: associated profiling data
  306. * @buffer: buffer containing file data
  307. * @size: size of buffer
  308. * @pos: current position in file
  309. */
  310. struct gcov_iterator {
  311. struct gcov_info *info;
  312. void *buffer;
  313. size_t size;
  314. loff_t pos;
  315. };
  316. /**
  317. * store_gcov_u32 - store 32 bit number in gcov format to buffer
  318. * @buffer: target buffer or NULL
  319. * @off: offset into the buffer
  320. * @v: value to be stored
  321. *
  322. * Number format defined by gcc: numbers are recorded in the 32 bit
  323. * unsigned binary form of the endianness of the machine generating the
  324. * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
  325. * store anything.
  326. */
  327. static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
  328. {
  329. u32 *data;
  330. if (buffer) {
  331. data = buffer + off;
  332. *data = v;
  333. }
  334. return sizeof(*data);
  335. }
  336. /**
  337. * store_gcov_u64 - store 64 bit number in gcov format to buffer
  338. * @buffer: target buffer or NULL
  339. * @off: offset into the buffer
  340. * @v: value to be stored
  341. *
  342. * Number format defined by gcc: numbers are recorded in the 32 bit
  343. * unsigned binary form of the endianness of the machine generating the
  344. * file. 64 bit numbers are stored as two 32 bit numbers, the low part
  345. * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
  346. * anything.
  347. */
  348. static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
  349. {
  350. u32 *data;
  351. if (buffer) {
  352. data = buffer + off;
  353. data[0] = (v & 0xffffffffUL);
  354. data[1] = (v >> 32);
  355. }
  356. return sizeof(*data) * 2;
  357. }
  358. /**
  359. * convert_to_gcda - convert profiling data set to gcda file format
  360. * @buffer: the buffer to store file data or %NULL if no data should be stored
  361. * @info: profiling data set to be converted
  362. *
  363. * Returns the number of bytes that were/would have been stored into the buffer.
  364. */
  365. static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
  366. {
  367. struct gcov_fn_info *fi_ptr;
  368. struct gcov_ctr_info *ci_ptr;
  369. unsigned int fi_idx;
  370. unsigned int ct_idx;
  371. unsigned int cv_idx;
  372. size_t pos = 0;
  373. /* File header. */
  374. pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
  375. pos += store_gcov_u32(buffer, pos, info->version);
  376. pos += store_gcov_u32(buffer, pos, info->stamp);
  377. for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
  378. fi_ptr = info->functions[fi_idx];
  379. /* Function record. */
  380. pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
  381. pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION_LENGTH);
  382. pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
  383. pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
  384. pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
  385. ci_ptr = fi_ptr->ctrs;
  386. for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
  387. if (!counter_active(info, ct_idx))
  388. continue;
  389. /* Counter record. */
  390. pos += store_gcov_u32(buffer, pos,
  391. GCOV_TAG_FOR_COUNTER(ct_idx));
  392. pos += store_gcov_u32(buffer, pos, ci_ptr->num * 2);
  393. for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
  394. pos += store_gcov_u64(buffer, pos,
  395. ci_ptr->values[cv_idx]);
  396. }
  397. ci_ptr++;
  398. }
  399. }
  400. return pos;
  401. }
  402. /**
  403. * gcov_iter_new - allocate and initialize profiling data iterator
  404. * @info: profiling data set to be iterated
  405. *
  406. * Return file iterator on success, %NULL otherwise.
  407. */
  408. struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
  409. {
  410. struct gcov_iterator *iter;
  411. iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
  412. if (!iter)
  413. goto err_free;
  414. iter->info = info;
  415. /* Dry-run to get the actual buffer size. */
  416. iter->size = convert_to_gcda(NULL, info);
  417. iter->buffer = vmalloc(iter->size);
  418. if (!iter->buffer)
  419. goto err_free;
  420. convert_to_gcda(iter->buffer, info);
  421. return iter;
  422. err_free:
  423. kfree(iter);
  424. return NULL;
  425. }
  426. /**
  427. * gcov_iter_get_info - return profiling data set for given file iterator
  428. * @iter: file iterator
  429. */
  430. void gcov_iter_free(struct gcov_iterator *iter)
  431. {
  432. vfree(iter->buffer);
  433. kfree(iter);
  434. }
  435. /**
  436. * gcov_iter_get_info - return profiling data set for given file iterator
  437. * @iter: file iterator
  438. */
  439. struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
  440. {
  441. return iter->info;
  442. }
  443. /**
  444. * gcov_iter_start - reset file iterator to starting position
  445. * @iter: file iterator
  446. */
  447. void gcov_iter_start(struct gcov_iterator *iter)
  448. {
  449. iter->pos = 0;
  450. }
  451. /**
  452. * gcov_iter_next - advance file iterator to next logical record
  453. * @iter: file iterator
  454. *
  455. * Return zero if new position is valid, non-zero if iterator has reached end.
  456. */
  457. int gcov_iter_next(struct gcov_iterator *iter)
  458. {
  459. if (iter->pos < iter->size)
  460. iter->pos += ITER_STRIDE;
  461. if (iter->pos >= iter->size)
  462. return -EINVAL;
  463. return 0;
  464. }
  465. /**
  466. * gcov_iter_write - write data for current pos to seq_file
  467. * @iter: file iterator
  468. * @seq: seq_file handle
  469. *
  470. * Return zero on success, non-zero otherwise.
  471. */
  472. int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
  473. {
  474. size_t len;
  475. if (iter->pos >= iter->size)
  476. return -EINVAL;
  477. len = ITER_STRIDE;
  478. if (iter->pos + len > iter->size)
  479. len = iter->size - iter->pos;
  480. seq_write(seq, iter->buffer + iter->pos, len);
  481. return 0;
  482. }