gcc_3_4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. /**
  128. * gcov_info_within_module - check if a profiling data set belongs to a module
  129. * @info: profiling data set
  130. * @mod: module
  131. *
  132. * Returns true if profiling data belongs module, false otherwise.
  133. */
  134. bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
  135. {
  136. return within_module((unsigned long)info, mod);
  137. }
  138. /* Symbolic links to be created for each profiling data file. */
  139. const struct gcov_link gcov_link[] = {
  140. { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
  141. { 0, NULL},
  142. };
  143. /*
  144. * Determine whether a counter is active. Based on gcc magic. Doesn't change
  145. * at run-time.
  146. */
  147. static int counter_active(struct gcov_info *info, unsigned int type)
  148. {
  149. return (1 << type) & info->ctr_mask;
  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. unsigned int active = num_counter_active(info);
  169. unsigned int i;
  170. for (i = 0; i < active; i++) {
  171. memset(info->counts[i].values, 0,
  172. info->counts[i].num * sizeof(gcov_type));
  173. }
  174. }
  175. /**
  176. * gcov_info_is_compatible - check if profiling data can be added
  177. * @info1: first profiling data set
  178. * @info2: second profiling data set
  179. *
  180. * Returns non-zero if profiling data can be added, zero otherwise.
  181. */
  182. int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
  183. {
  184. return (info1->stamp == info2->stamp);
  185. }
  186. /**
  187. * gcov_info_add - add up profiling data
  188. * @dest: profiling data set to which data is added
  189. * @source: profiling data set which is added
  190. *
  191. * Adds profiling counts of @source to @dest.
  192. */
  193. void gcov_info_add(struct gcov_info *dest, struct gcov_info *source)
  194. {
  195. unsigned int i;
  196. unsigned int j;
  197. for (i = 0; i < num_counter_active(dest); i++) {
  198. for (j = 0; j < dest->counts[i].num; j++) {
  199. dest->counts[i].values[j] +=
  200. source->counts[i].values[j];
  201. }
  202. }
  203. }
  204. /* Get size of function info entry. Based on gcc magic. */
  205. static size_t get_fn_size(struct gcov_info *info)
  206. {
  207. size_t size;
  208. size = sizeof(struct gcov_fn_info) + num_counter_active(info) *
  209. sizeof(unsigned int);
  210. if (__alignof__(struct gcov_fn_info) > sizeof(unsigned int))
  211. size = ALIGN(size, __alignof__(struct gcov_fn_info));
  212. return size;
  213. }
  214. /* Get address of function info entry. Based on gcc magic. */
  215. static struct gcov_fn_info *get_fn_info(struct gcov_info *info, unsigned int fn)
  216. {
  217. return (struct gcov_fn_info *)
  218. ((char *) info->functions + fn * get_fn_size(info));
  219. }
  220. /**
  221. * gcov_info_dup - duplicate profiling data set
  222. * @info: profiling data set to duplicate
  223. *
  224. * Return newly allocated duplicate on success, %NULL on error.
  225. */
  226. struct gcov_info *gcov_info_dup(struct gcov_info *info)
  227. {
  228. struct gcov_info *dup;
  229. unsigned int i;
  230. unsigned int active;
  231. /* Duplicate gcov_info. */
  232. active = num_counter_active(info);
  233. dup = kzalloc(sizeof(struct gcov_info) +
  234. sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);
  235. if (!dup)
  236. return NULL;
  237. dup->version = info->version;
  238. dup->stamp = info->stamp;
  239. dup->n_functions = info->n_functions;
  240. dup->ctr_mask = info->ctr_mask;
  241. /* Duplicate filename. */
  242. dup->filename = kstrdup(info->filename, GFP_KERNEL);
  243. if (!dup->filename)
  244. goto err_free;
  245. /* Duplicate table of functions. */
  246. dup->functions = kmemdup(info->functions, info->n_functions *
  247. get_fn_size(info), GFP_KERNEL);
  248. if (!dup->functions)
  249. goto err_free;
  250. /* Duplicate counter arrays. */
  251. for (i = 0; i < active ; i++) {
  252. struct gcov_ctr_info *ctr = &info->counts[i];
  253. size_t size = ctr->num * sizeof(gcov_type);
  254. dup->counts[i].num = ctr->num;
  255. dup->counts[i].merge = ctr->merge;
  256. dup->counts[i].values = vmalloc(size);
  257. if (!dup->counts[i].values)
  258. goto err_free;
  259. memcpy(dup->counts[i].values, ctr->values, size);
  260. }
  261. return dup;
  262. err_free:
  263. gcov_info_free(dup);
  264. return NULL;
  265. }
  266. /**
  267. * gcov_info_free - release memory for profiling data set duplicate
  268. * @info: profiling data set duplicate to free
  269. */
  270. void gcov_info_free(struct gcov_info *info)
  271. {
  272. unsigned int active = num_counter_active(info);
  273. unsigned int i;
  274. for (i = 0; i < active ; i++)
  275. vfree(info->counts[i].values);
  276. kfree(info->functions);
  277. kfree(info->filename);
  278. kfree(info);
  279. }
  280. /**
  281. * struct type_info - iterator helper array
  282. * @ctr_type: counter type
  283. * @offset: index of the first value of the current function for this type
  284. *
  285. * This array is needed to convert the in-memory data format into the in-file
  286. * data format:
  287. *
  288. * In-memory:
  289. * for each counter type
  290. * for each function
  291. * values
  292. *
  293. * In-file:
  294. * for each function
  295. * for each counter type
  296. * values
  297. *
  298. * See gcc source gcc/gcov-io.h for more information on data organization.
  299. */
  300. struct type_info {
  301. int ctr_type;
  302. unsigned int offset;
  303. };
  304. /**
  305. * struct gcov_iterator - specifies current file position in logical records
  306. * @info: associated profiling data
  307. * @record: record type
  308. * @function: function number
  309. * @type: counter type
  310. * @count: index into values array
  311. * @num_types: number of counter types
  312. * @type_info: helper array to get values-array offset for current function
  313. */
  314. struct gcov_iterator {
  315. struct gcov_info *info;
  316. int record;
  317. unsigned int function;
  318. unsigned int type;
  319. unsigned int count;
  320. int num_types;
  321. struct type_info type_info[0];
  322. };
  323. static struct gcov_fn_info *get_func(struct gcov_iterator *iter)
  324. {
  325. return get_fn_info(iter->info, iter->function);
  326. }
  327. static struct type_info *get_type(struct gcov_iterator *iter)
  328. {
  329. return &iter->type_info[iter->type];
  330. }
  331. /**
  332. * gcov_iter_new - allocate and initialize profiling data iterator
  333. * @info: profiling data set to be iterated
  334. *
  335. * Return file iterator on success, %NULL otherwise.
  336. */
  337. struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
  338. {
  339. struct gcov_iterator *iter;
  340. iter = kzalloc(sizeof(struct gcov_iterator) +
  341. num_counter_active(info) * sizeof(struct type_info),
  342. GFP_KERNEL);
  343. if (iter)
  344. iter->info = info;
  345. return iter;
  346. }
  347. /**
  348. * gcov_iter_free - release memory for iterator
  349. * @iter: file iterator to free
  350. */
  351. void gcov_iter_free(struct gcov_iterator *iter)
  352. {
  353. kfree(iter);
  354. }
  355. /**
  356. * gcov_iter_get_info - return profiling data set for given file iterator
  357. * @iter: file iterator
  358. */
  359. struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
  360. {
  361. return iter->info;
  362. }
  363. /**
  364. * gcov_iter_start - reset file iterator to starting position
  365. * @iter: file iterator
  366. */
  367. void gcov_iter_start(struct gcov_iterator *iter)
  368. {
  369. int i;
  370. iter->record = 0;
  371. iter->function = 0;
  372. iter->type = 0;
  373. iter->count = 0;
  374. iter->num_types = 0;
  375. for (i = 0; i < GCOV_COUNTERS; i++) {
  376. if (counter_active(iter->info, i)) {
  377. iter->type_info[iter->num_types].ctr_type = i;
  378. iter->type_info[iter->num_types++].offset = 0;
  379. }
  380. }
  381. }
  382. /* Mapping of logical record number to actual file content. */
  383. #define RECORD_FILE_MAGIC 0
  384. #define RECORD_GCOV_VERSION 1
  385. #define RECORD_TIME_STAMP 2
  386. #define RECORD_FUNCTION_TAG 3
  387. #define RECORD_FUNCTON_TAG_LEN 4
  388. #define RECORD_FUNCTION_IDENT 5
  389. #define RECORD_FUNCTION_CHECK 6
  390. #define RECORD_COUNT_TAG 7
  391. #define RECORD_COUNT_LEN 8
  392. #define RECORD_COUNT 9
  393. /**
  394. * gcov_iter_next - advance file iterator to next logical record
  395. * @iter: file iterator
  396. *
  397. * Return zero if new position is valid, non-zero if iterator has reached end.
  398. */
  399. int gcov_iter_next(struct gcov_iterator *iter)
  400. {
  401. switch (iter->record) {
  402. case RECORD_FILE_MAGIC:
  403. case RECORD_GCOV_VERSION:
  404. case RECORD_FUNCTION_TAG:
  405. case RECORD_FUNCTON_TAG_LEN:
  406. case RECORD_FUNCTION_IDENT:
  407. case RECORD_COUNT_TAG:
  408. /* Advance to next record */
  409. iter->record++;
  410. break;
  411. case RECORD_COUNT:
  412. /* Advance to next count */
  413. iter->count++;
  414. /* fall through */
  415. case RECORD_COUNT_LEN:
  416. if (iter->count < get_func(iter)->n_ctrs[iter->type]) {
  417. iter->record = 9;
  418. break;
  419. }
  420. /* Advance to next counter type */
  421. get_type(iter)->offset += iter->count;
  422. iter->count = 0;
  423. iter->type++;
  424. /* fall through */
  425. case RECORD_FUNCTION_CHECK:
  426. if (iter->type < iter->num_types) {
  427. iter->record = 7;
  428. break;
  429. }
  430. /* Advance to next function */
  431. iter->type = 0;
  432. iter->function++;
  433. /* fall through */
  434. case RECORD_TIME_STAMP:
  435. if (iter->function < iter->info->n_functions)
  436. iter->record = 3;
  437. else
  438. iter->record = -1;
  439. break;
  440. }
  441. /* Check for EOF. */
  442. if (iter->record == -1)
  443. return -EINVAL;
  444. else
  445. return 0;
  446. }
  447. /**
  448. * seq_write_gcov_u32 - write 32 bit number in gcov format to seq_file
  449. * @seq: seq_file handle
  450. * @v: value to be stored
  451. *
  452. * Number format defined by gcc: numbers are recorded in the 32 bit
  453. * unsigned binary form of the endianness of the machine generating the
  454. * file.
  455. */
  456. static int seq_write_gcov_u32(struct seq_file *seq, u32 v)
  457. {
  458. return seq_write(seq, &v, sizeof(v));
  459. }
  460. /**
  461. * seq_write_gcov_u64 - write 64 bit number in gcov format to seq_file
  462. * @seq: seq_file handle
  463. * @v: value to be stored
  464. *
  465. * Number format defined by gcc: numbers are recorded in the 32 bit
  466. * unsigned binary form of the endianness of the machine generating the
  467. * file. 64 bit numbers are stored as two 32 bit numbers, the low part
  468. * first.
  469. */
  470. static int seq_write_gcov_u64(struct seq_file *seq, u64 v)
  471. {
  472. u32 data[2];
  473. data[0] = (v & 0xffffffffUL);
  474. data[1] = (v >> 32);
  475. return seq_write(seq, data, sizeof(data));
  476. }
  477. /**
  478. * gcov_iter_write - write data for current pos to seq_file
  479. * @iter: file iterator
  480. * @seq: seq_file handle
  481. *
  482. * Return zero on success, non-zero otherwise.
  483. */
  484. int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
  485. {
  486. int rc = -EINVAL;
  487. switch (iter->record) {
  488. case RECORD_FILE_MAGIC:
  489. rc = seq_write_gcov_u32(seq, GCOV_DATA_MAGIC);
  490. break;
  491. case RECORD_GCOV_VERSION:
  492. rc = seq_write_gcov_u32(seq, iter->info->version);
  493. break;
  494. case RECORD_TIME_STAMP:
  495. rc = seq_write_gcov_u32(seq, iter->info->stamp);
  496. break;
  497. case RECORD_FUNCTION_TAG:
  498. rc = seq_write_gcov_u32(seq, GCOV_TAG_FUNCTION);
  499. break;
  500. case RECORD_FUNCTON_TAG_LEN:
  501. rc = seq_write_gcov_u32(seq, 2);
  502. break;
  503. case RECORD_FUNCTION_IDENT:
  504. rc = seq_write_gcov_u32(seq, get_func(iter)->ident);
  505. break;
  506. case RECORD_FUNCTION_CHECK:
  507. rc = seq_write_gcov_u32(seq, get_func(iter)->checksum);
  508. break;
  509. case RECORD_COUNT_TAG:
  510. rc = seq_write_gcov_u32(seq,
  511. GCOV_TAG_FOR_COUNTER(get_type(iter)->ctr_type));
  512. break;
  513. case RECORD_COUNT_LEN:
  514. rc = seq_write_gcov_u32(seq,
  515. get_func(iter)->n_ctrs[iter->type] * 2);
  516. break;
  517. case RECORD_COUNT:
  518. rc = seq_write_gcov_u64(seq,
  519. iter->info->counts[iter->type].
  520. values[iter->count + get_type(iter)->offset]);
  521. break;
  522. }
  523. return rc;
  524. }