gcc_4_7.c 13 KB

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