gcc_3_4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This code provides functions to handle gcc's profiling data format
  4. * introduced with gcc 3.4. Future versions of gcc may change the gcov
  5. * format (as happened before), so all format-specific information needs
  6. * to be kept modular and easily exchangeable.
  7. *
  8. * This file is based on gcc-internal definitions. Functions and data
  9. * structures are defined to be compatible with gcc counterparts.
  10. * For a better understanding, refer to gcc source: gcc/gcov-io.h.
  11. *
  12. * Copyright IBM Corp. 2009
  13. * Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
  14. *
  15. * Uses gcc-internal data definitions.
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/string.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/vmalloc.h>
  22. #include "gcov.h"
  23. #define GCOV_COUNTERS 5
  24. static struct gcov_info *gcov_info_head;
  25. /**
  26. * struct gcov_fn_info - profiling meta data per function
  27. * @ident: object file-unique function identifier
  28. * @checksum: function checksum
  29. * @n_ctrs: number of values per counter type belonging to this function
  30. *
  31. * This data is generated by gcc during compilation and doesn't change
  32. * at run-time.
  33. */
  34. struct gcov_fn_info {
  35. unsigned int ident;
  36. unsigned int checksum;
  37. unsigned int n_ctrs[0];
  38. };
  39. /**
  40. * struct gcov_ctr_info - profiling data per counter type
  41. * @num: number of counter values for this type
  42. * @values: array of counter values for this type
  43. * @merge: merge function for counter values of this type (unused)
  44. *
  45. * This data is generated by gcc during compilation and doesn't change
  46. * at run-time with the exception of the values array.
  47. */
  48. struct gcov_ctr_info {
  49. unsigned int num;
  50. gcov_type *values;
  51. void (*merge)(gcov_type *, unsigned int);
  52. };
  53. /**
  54. * struct gcov_info - profiling data per object file
  55. * @version: gcov version magic indicating the gcc version used for compilation
  56. * @next: list head for a singly-linked list
  57. * @stamp: time stamp
  58. * @filename: name of the associated gcov data file
  59. * @n_functions: number of instrumented functions
  60. * @functions: function data
  61. * @ctr_mask: mask specifying which counter types are active
  62. * @counts: counter data per counter type
  63. *
  64. * This data is generated by gcc during compilation and doesn't change
  65. * at run-time with the exception of the next pointer.
  66. */
  67. struct gcov_info {
  68. unsigned int version;
  69. struct gcov_info *next;
  70. unsigned int stamp;
  71. const char *filename;
  72. unsigned int n_functions;
  73. const struct gcov_fn_info *functions;
  74. unsigned int ctr_mask;
  75. struct gcov_ctr_info counts[0];
  76. };
  77. /**
  78. * gcov_info_filename - return info filename
  79. * @info: profiling data set
  80. */
  81. const char *gcov_info_filename(struct gcov_info *info)
  82. {
  83. return info->filename;
  84. }
  85. /**
  86. * gcov_info_version - return info version
  87. * @info: profiling data set
  88. */
  89. unsigned int gcov_info_version(struct gcov_info *info)
  90. {
  91. return info->version;
  92. }
  93. /**
  94. * gcov_info_next - return next profiling data set
  95. * @info: profiling data set
  96. *
  97. * Returns next gcov_info following @info or first gcov_info in the chain if
  98. * @info is %NULL.
  99. */
  100. struct gcov_info *gcov_info_next(struct gcov_info *info)
  101. {
  102. if (!info)
  103. return gcov_info_head;
  104. return info->next;
  105. }
  106. /**
  107. * gcov_info_link - link/add profiling data set to the list
  108. * @info: profiling data set
  109. */
  110. void gcov_info_link(struct gcov_info *info)
  111. {
  112. info->next = gcov_info_head;
  113. gcov_info_head = info;
  114. }
  115. /**
  116. * gcov_info_unlink - unlink/remove profiling data set from the list
  117. * @prev: previous profiling data set
  118. * @info: profiling data set
  119. */
  120. void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
  121. {
  122. if (prev)
  123. prev->next = info->next;
  124. else
  125. gcov_info_head = info->next;
  126. }
  127. /* Symbolic links to be created for each profiling data file. */
  128. const struct gcov_link gcov_link[] = {
  129. { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
  130. { 0, NULL},
  131. };
  132. /*
  133. * Determine whether a counter is active. Based on gcc magic. Doesn't change
  134. * at run-time.
  135. */
  136. static int counter_active(struct gcov_info *info, unsigned int type)
  137. {
  138. return (1 << type) & info->ctr_mask;
  139. }
  140. /* Determine number of active counters. Based on gcc magic. */
  141. static unsigned int num_counter_active(struct gcov_info *info)
  142. {
  143. unsigned int i;
  144. unsigned int result = 0;
  145. for (i = 0; i < GCOV_COUNTERS; i++) {
  146. if (counter_active(info, i))
  147. result++;
  148. }
  149. return result;
  150. }
  151. /**
  152. * gcov_info_reset - reset profiling data to zero
  153. * @info: profiling data set
  154. */
  155. void gcov_info_reset(struct gcov_info *info)
  156. {
  157. unsigned int active = num_counter_active(info);
  158. unsigned int i;
  159. for (i = 0; i < active; i++) {
  160. memset(info->counts[i].values, 0,
  161. info->counts[i].num * sizeof(gcov_type));
  162. }
  163. }
  164. /**
  165. * gcov_info_is_compatible - check if profiling data can be added
  166. * @info1: first profiling data set
  167. * @info2: second profiling data set
  168. *
  169. * Returns non-zero if profiling data can be added, zero otherwise.
  170. */
  171. int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
  172. {
  173. return (info1->stamp == info2->stamp);
  174. }
  175. /**
  176. * gcov_info_add - add up profiling data
  177. * @dest: profiling data set to which data is added
  178. * @source: profiling data set which is added
  179. *
  180. * Adds profiling counts of @source to @dest.
  181. */
  182. void gcov_info_add(struct gcov_info *dest, struct gcov_info *source)
  183. {
  184. unsigned int i;
  185. unsigned int j;
  186. for (i = 0; i < num_counter_active(dest); i++) {
  187. for (j = 0; j < dest->counts[i].num; j++) {
  188. dest->counts[i].values[j] +=
  189. source->counts[i].values[j];
  190. }
  191. }
  192. }
  193. /* Get size of function info entry. Based on gcc magic. */
  194. static size_t get_fn_size(struct gcov_info *info)
  195. {
  196. size_t size;
  197. size = sizeof(struct gcov_fn_info) + num_counter_active(info) *
  198. sizeof(unsigned int);
  199. if (__alignof__(struct gcov_fn_info) > sizeof(unsigned int))
  200. size = ALIGN(size, __alignof__(struct gcov_fn_info));
  201. return size;
  202. }
  203. /* Get address of function info entry. Based on gcc magic. */
  204. static struct gcov_fn_info *get_fn_info(struct gcov_info *info, unsigned int fn)
  205. {
  206. return (struct gcov_fn_info *)
  207. ((char *) info->functions + fn * get_fn_size(info));
  208. }
  209. /**
  210. * gcov_info_dup - duplicate profiling data set
  211. * @info: profiling data set to duplicate
  212. *
  213. * Return newly allocated duplicate on success, %NULL on error.
  214. */
  215. struct gcov_info *gcov_info_dup(struct gcov_info *info)
  216. {
  217. struct gcov_info *dup;
  218. unsigned int i;
  219. unsigned int active;
  220. /* Duplicate gcov_info. */
  221. active = num_counter_active(info);
  222. dup = kzalloc(sizeof(struct gcov_info) +
  223. sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);
  224. if (!dup)
  225. return NULL;
  226. dup->version = info->version;
  227. dup->stamp = info->stamp;
  228. dup->n_functions = info->n_functions;
  229. dup->ctr_mask = info->ctr_mask;
  230. /* Duplicate filename. */
  231. dup->filename = kstrdup(info->filename, GFP_KERNEL);
  232. if (!dup->filename)
  233. goto err_free;
  234. /* Duplicate table of functions. */
  235. dup->functions = kmemdup(info->functions, info->n_functions *
  236. get_fn_size(info), GFP_KERNEL);
  237. if (!dup->functions)
  238. goto err_free;
  239. /* Duplicate counter arrays. */
  240. for (i = 0; i < active ; i++) {
  241. struct gcov_ctr_info *ctr = &info->counts[i];
  242. size_t size = ctr->num * sizeof(gcov_type);
  243. dup->counts[i].num = ctr->num;
  244. dup->counts[i].merge = ctr->merge;
  245. dup->counts[i].values = vmalloc(size);
  246. if (!dup->counts[i].values)
  247. goto err_free;
  248. memcpy(dup->counts[i].values, ctr->values, size);
  249. }
  250. return dup;
  251. err_free:
  252. gcov_info_free(dup);
  253. return NULL;
  254. }
  255. /**
  256. * gcov_info_free - release memory for profiling data set duplicate
  257. * @info: profiling data set duplicate to free
  258. */
  259. void gcov_info_free(struct gcov_info *info)
  260. {
  261. unsigned int active = num_counter_active(info);
  262. unsigned int i;
  263. for (i = 0; i < active ; i++)
  264. vfree(info->counts[i].values);
  265. kfree(info->functions);
  266. kfree(info->filename);
  267. kfree(info);
  268. }
  269. /**
  270. * struct type_info - iterator helper array
  271. * @ctr_type: counter type
  272. * @offset: index of the first value of the current function for this type
  273. *
  274. * This array is needed to convert the in-memory data format into the in-file
  275. * data format:
  276. *
  277. * In-memory:
  278. * for each counter type
  279. * for each function
  280. * values
  281. *
  282. * In-file:
  283. * for each function
  284. * for each counter type
  285. * values
  286. *
  287. * See gcc source gcc/gcov-io.h for more information on data organization.
  288. */
  289. struct type_info {
  290. int ctr_type;
  291. unsigned int offset;
  292. };
  293. /**
  294. * struct gcov_iterator - specifies current file position in logical records
  295. * @info: associated profiling data
  296. * @record: record type
  297. * @function: function number
  298. * @type: counter type
  299. * @count: index into values array
  300. * @num_types: number of counter types
  301. * @type_info: helper array to get values-array offset for current function
  302. */
  303. struct gcov_iterator {
  304. struct gcov_info *info;
  305. int record;
  306. unsigned int function;
  307. unsigned int type;
  308. unsigned int count;
  309. int num_types;
  310. struct type_info type_info[0];
  311. };
  312. static struct gcov_fn_info *get_func(struct gcov_iterator *iter)
  313. {
  314. return get_fn_info(iter->info, iter->function);
  315. }
  316. static struct type_info *get_type(struct gcov_iterator *iter)
  317. {
  318. return &iter->type_info[iter->type];
  319. }
  320. /**
  321. * gcov_iter_new - allocate and initialize profiling data iterator
  322. * @info: profiling data set to be iterated
  323. *
  324. * Return file iterator on success, %NULL otherwise.
  325. */
  326. struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
  327. {
  328. struct gcov_iterator *iter;
  329. iter = kzalloc(sizeof(struct gcov_iterator) +
  330. num_counter_active(info) * sizeof(struct type_info),
  331. GFP_KERNEL);
  332. if (iter)
  333. iter->info = info;
  334. return iter;
  335. }
  336. /**
  337. * gcov_iter_free - release memory for iterator
  338. * @iter: file iterator to free
  339. */
  340. void gcov_iter_free(struct gcov_iterator *iter)
  341. {
  342. kfree(iter);
  343. }
  344. /**
  345. * gcov_iter_get_info - return profiling data set for given file iterator
  346. * @iter: file iterator
  347. */
  348. struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
  349. {
  350. return iter->info;
  351. }
  352. /**
  353. * gcov_iter_start - reset file iterator to starting position
  354. * @iter: file iterator
  355. */
  356. void gcov_iter_start(struct gcov_iterator *iter)
  357. {
  358. int i;
  359. iter->record = 0;
  360. iter->function = 0;
  361. iter->type = 0;
  362. iter->count = 0;
  363. iter->num_types = 0;
  364. for (i = 0; i < GCOV_COUNTERS; i++) {
  365. if (counter_active(iter->info, i)) {
  366. iter->type_info[iter->num_types].ctr_type = i;
  367. iter->type_info[iter->num_types++].offset = 0;
  368. }
  369. }
  370. }
  371. /* Mapping of logical record number to actual file content. */
  372. #define RECORD_FILE_MAGIC 0
  373. #define RECORD_GCOV_VERSION 1
  374. #define RECORD_TIME_STAMP 2
  375. #define RECORD_FUNCTION_TAG 3
  376. #define RECORD_FUNCTON_TAG_LEN 4
  377. #define RECORD_FUNCTION_IDENT 5
  378. #define RECORD_FUNCTION_CHECK 6
  379. #define RECORD_COUNT_TAG 7
  380. #define RECORD_COUNT_LEN 8
  381. #define RECORD_COUNT 9
  382. /**
  383. * gcov_iter_next - advance file iterator to next logical record
  384. * @iter: file iterator
  385. *
  386. * Return zero if new position is valid, non-zero if iterator has reached end.
  387. */
  388. int gcov_iter_next(struct gcov_iterator *iter)
  389. {
  390. switch (iter->record) {
  391. case RECORD_FILE_MAGIC:
  392. case RECORD_GCOV_VERSION:
  393. case RECORD_FUNCTION_TAG:
  394. case RECORD_FUNCTON_TAG_LEN:
  395. case RECORD_FUNCTION_IDENT:
  396. case RECORD_COUNT_TAG:
  397. /* Advance to next record */
  398. iter->record++;
  399. break;
  400. case RECORD_COUNT:
  401. /* Advance to next count */
  402. iter->count++;
  403. /* fall through */
  404. case RECORD_COUNT_LEN:
  405. if (iter->count < get_func(iter)->n_ctrs[iter->type]) {
  406. iter->record = 9;
  407. break;
  408. }
  409. /* Advance to next counter type */
  410. get_type(iter)->offset += iter->count;
  411. iter->count = 0;
  412. iter->type++;
  413. /* fall through */
  414. case RECORD_FUNCTION_CHECK:
  415. if (iter->type < iter->num_types) {
  416. iter->record = 7;
  417. break;
  418. }
  419. /* Advance to next function */
  420. iter->type = 0;
  421. iter->function++;
  422. /* fall through */
  423. case RECORD_TIME_STAMP:
  424. if (iter->function < iter->info->n_functions)
  425. iter->record = 3;
  426. else
  427. iter->record = -1;
  428. break;
  429. }
  430. /* Check for EOF. */
  431. if (iter->record == -1)
  432. return -EINVAL;
  433. else
  434. return 0;
  435. }
  436. /**
  437. * seq_write_gcov_u32 - write 32 bit number in gcov format to seq_file
  438. * @seq: seq_file handle
  439. * @v: value to be stored
  440. *
  441. * Number format defined by gcc: numbers are recorded in the 32 bit
  442. * unsigned binary form of the endianness of the machine generating the
  443. * file.
  444. */
  445. static int seq_write_gcov_u32(struct seq_file *seq, u32 v)
  446. {
  447. return seq_write(seq, &v, sizeof(v));
  448. }
  449. /**
  450. * seq_write_gcov_u64 - write 64 bit number in gcov format to seq_file
  451. * @seq: seq_file handle
  452. * @v: value to be stored
  453. *
  454. * Number format defined by gcc: numbers are recorded in the 32 bit
  455. * unsigned binary form of the endianness of the machine generating the
  456. * file. 64 bit numbers are stored as two 32 bit numbers, the low part
  457. * first.
  458. */
  459. static int seq_write_gcov_u64(struct seq_file *seq, u64 v)
  460. {
  461. u32 data[2];
  462. data[0] = (v & 0xffffffffUL);
  463. data[1] = (v >> 32);
  464. return seq_write(seq, data, sizeof(data));
  465. }
  466. /**
  467. * gcov_iter_write - write data for current pos to seq_file
  468. * @iter: file iterator
  469. * @seq: seq_file handle
  470. *
  471. * Return zero on success, non-zero otherwise.
  472. */
  473. int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
  474. {
  475. int rc = -EINVAL;
  476. switch (iter->record) {
  477. case RECORD_FILE_MAGIC:
  478. rc = seq_write_gcov_u32(seq, GCOV_DATA_MAGIC);
  479. break;
  480. case RECORD_GCOV_VERSION:
  481. rc = seq_write_gcov_u32(seq, iter->info->version);
  482. break;
  483. case RECORD_TIME_STAMP:
  484. rc = seq_write_gcov_u32(seq, iter->info->stamp);
  485. break;
  486. case RECORD_FUNCTION_TAG:
  487. rc = seq_write_gcov_u32(seq, GCOV_TAG_FUNCTION);
  488. break;
  489. case RECORD_FUNCTON_TAG_LEN:
  490. rc = seq_write_gcov_u32(seq, 2);
  491. break;
  492. case RECORD_FUNCTION_IDENT:
  493. rc = seq_write_gcov_u32(seq, get_func(iter)->ident);
  494. break;
  495. case RECORD_FUNCTION_CHECK:
  496. rc = seq_write_gcov_u32(seq, get_func(iter)->checksum);
  497. break;
  498. case RECORD_COUNT_TAG:
  499. rc = seq_write_gcov_u32(seq,
  500. GCOV_TAG_FOR_COUNTER(get_type(iter)->ctr_type));
  501. break;
  502. case RECORD_COUNT_LEN:
  503. rc = seq_write_gcov_u32(seq,
  504. get_func(iter)->n_ctrs[iter->type] * 2);
  505. break;
  506. case RECORD_COUNT:
  507. rc = seq_write_gcov_u64(seq,
  508. iter->info->counts[iter->type].
  509. values[iter->count + get_type(iter)->offset]);
  510. break;
  511. }
  512. return rc;
  513. }