gcc_4_7.c 13 KB

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