gcc_4_7.c 13 KB

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