Linux 4.9.251
[linux/fpc-iii.git] / kernel / gcov / gcc_4_7.c
blob6d5ef6220afe7dac29bb318d9862f23ded082d9a
1 /*
2 * This code provides functions to handle gcc's profiling data format
3 * introduced with gcc 4.7.
5 * This file is based heavily on gcc_3_4.c file.
7 * For a better understanding, refer to gcc source:
8 * gcc/gcov-io.h
9 * libgcc/libgcov.c
11 * Uses gcc-internal data definitions.
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/seq_file.h>
18 #include <linux/vmalloc.h>
19 #include "gcov.h"
21 #if (__GNUC__ >= 10)
22 #define GCOV_COUNTERS 8
23 #elif (__GNUC__ >= 7)
24 #define GCOV_COUNTERS 9
25 #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
26 #define GCOV_COUNTERS 10
27 #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
28 #define GCOV_COUNTERS 9
29 #else
30 #define GCOV_COUNTERS 8
31 #endif
33 #define GCOV_TAG_FUNCTION_LENGTH 3
35 static struct gcov_info *gcov_info_head;
37 /**
38 * struct gcov_ctr_info - information about counters for a single function
39 * @num: number of counter values for this type
40 * @values: array of counter values for this type
42 * This data is generated by gcc during compilation and doesn't change
43 * at run-time with the exception of the values array.
45 struct gcov_ctr_info {
46 unsigned int num;
47 gcov_type *values;
50 /**
51 * struct gcov_fn_info - profiling meta data per function
52 * @key: comdat key
53 * @ident: unique ident of function
54 * @lineno_checksum: function lineo_checksum
55 * @cfg_checksum: function cfg checksum
56 * @ctrs: instrumented counters
58 * This data is generated by gcc during compilation and doesn't change
59 * at run-time.
61 * Information about a single function. This uses the trailing array
62 * idiom. The number of counters is determined from the merge pointer
63 * array in gcov_info. The key is used to detect which of a set of
64 * comdat functions was selected -- it points to the gcov_info object
65 * of the object file containing the selected comdat function.
67 struct gcov_fn_info {
68 const struct gcov_info *key;
69 unsigned int ident;
70 unsigned int lineno_checksum;
71 unsigned int cfg_checksum;
72 struct gcov_ctr_info ctrs[0];
75 /**
76 * struct gcov_info - profiling data per object file
77 * @version: gcov version magic indicating the gcc version used for compilation
78 * @next: list head for a singly-linked list
79 * @stamp: uniquifying time stamp
80 * @filename: name of the associated gcov data file
81 * @merge: merge functions (null for unused counter type)
82 * @n_functions: number of instrumented functions
83 * @functions: pointer to pointers to function information
85 * This data is generated by gcc during compilation and doesn't change
86 * at run-time with the exception of the next pointer.
88 struct gcov_info {
89 unsigned int version;
90 struct gcov_info *next;
91 unsigned int stamp;
92 const char *filename;
93 void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
94 unsigned int n_functions;
95 struct gcov_fn_info **functions;
98 /**
99 * gcov_info_filename - return info filename
100 * @info: profiling data set
102 const char *gcov_info_filename(struct gcov_info *info)
104 return info->filename;
108 * gcov_info_version - return info version
109 * @info: profiling data set
111 unsigned int gcov_info_version(struct gcov_info *info)
113 return info->version;
117 * gcov_info_next - return next profiling data set
118 * @info: profiling data set
120 * Returns next gcov_info following @info or first gcov_info in the chain if
121 * @info is %NULL.
123 struct gcov_info *gcov_info_next(struct gcov_info *info)
125 if (!info)
126 return gcov_info_head;
128 return info->next;
132 * gcov_info_link - link/add profiling data set to the list
133 * @info: profiling data set
135 void gcov_info_link(struct gcov_info *info)
137 info->next = gcov_info_head;
138 gcov_info_head = info;
142 * gcov_info_unlink - unlink/remove profiling data set from the list
143 * @prev: previous profiling data set
144 * @info: profiling data set
146 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
148 if (prev)
149 prev->next = info->next;
150 else
151 gcov_info_head = info->next;
154 /* Symbolic links to be created for each profiling data file. */
155 const struct gcov_link gcov_link[] = {
156 { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
157 { 0, NULL},
161 * Determine whether a counter is active. Doesn't change at run-time.
163 static int counter_active(struct gcov_info *info, unsigned int type)
165 return info->merge[type] ? 1 : 0;
168 /* Determine number of active counters. Based on gcc magic. */
169 static unsigned int num_counter_active(struct gcov_info *info)
171 unsigned int i;
172 unsigned int result = 0;
174 for (i = 0; i < GCOV_COUNTERS; i++) {
175 if (counter_active(info, i))
176 result++;
178 return result;
182 * gcov_info_reset - reset profiling data to zero
183 * @info: profiling data set
185 void gcov_info_reset(struct gcov_info *info)
187 struct gcov_ctr_info *ci_ptr;
188 unsigned int fi_idx;
189 unsigned int ct_idx;
191 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
192 ci_ptr = info->functions[fi_idx]->ctrs;
194 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
195 if (!counter_active(info, ct_idx))
196 continue;
198 memset(ci_ptr->values, 0,
199 sizeof(gcov_type) * ci_ptr->num);
200 ci_ptr++;
206 * gcov_info_is_compatible - check if profiling data can be added
207 * @info1: first profiling data set
208 * @info2: second profiling data set
210 * Returns non-zero if profiling data can be added, zero otherwise.
212 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
214 return (info1->stamp == info2->stamp);
218 * gcov_info_add - add up profiling data
219 * @dest: profiling data set to which data is added
220 * @source: profiling data set which is added
222 * Adds profiling counts of @source to @dest.
224 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
226 struct gcov_ctr_info *dci_ptr;
227 struct gcov_ctr_info *sci_ptr;
228 unsigned int fi_idx;
229 unsigned int ct_idx;
230 unsigned int val_idx;
232 for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) {
233 dci_ptr = dst->functions[fi_idx]->ctrs;
234 sci_ptr = src->functions[fi_idx]->ctrs;
236 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
237 if (!counter_active(src, ct_idx))
238 continue;
240 for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
241 dci_ptr->values[val_idx] +=
242 sci_ptr->values[val_idx];
244 dci_ptr++;
245 sci_ptr++;
251 * gcov_info_dup - duplicate profiling data set
252 * @info: profiling data set to duplicate
254 * Return newly allocated duplicate on success, %NULL on error.
256 struct gcov_info *gcov_info_dup(struct gcov_info *info)
258 struct gcov_info *dup;
259 struct gcov_ctr_info *dci_ptr; /* dst counter info */
260 struct gcov_ctr_info *sci_ptr; /* src counter info */
261 unsigned int active;
262 unsigned int fi_idx; /* function info idx */
263 unsigned int ct_idx; /* counter type idx */
264 size_t fi_size; /* function info size */
265 size_t cv_size; /* counter values size */
267 dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
268 if (!dup)
269 return NULL;
271 dup->next = NULL;
272 dup->filename = NULL;
273 dup->functions = NULL;
275 dup->filename = kstrdup(info->filename, GFP_KERNEL);
276 if (!dup->filename)
277 goto err_free;
279 dup->functions = kcalloc(info->n_functions,
280 sizeof(struct gcov_fn_info *), GFP_KERNEL);
281 if (!dup->functions)
282 goto err_free;
284 active = num_counter_active(info);
285 fi_size = sizeof(struct gcov_fn_info);
286 fi_size += sizeof(struct gcov_ctr_info) * active;
288 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
289 dup->functions[fi_idx] = kzalloc(fi_size, GFP_KERNEL);
290 if (!dup->functions[fi_idx])
291 goto err_free;
293 *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
295 sci_ptr = info->functions[fi_idx]->ctrs;
296 dci_ptr = dup->functions[fi_idx]->ctrs;
298 for (ct_idx = 0; ct_idx < active; ct_idx++) {
300 cv_size = sizeof(gcov_type) * sci_ptr->num;
302 dci_ptr->values = vmalloc(cv_size);
304 if (!dci_ptr->values)
305 goto err_free;
307 dci_ptr->num = sci_ptr->num;
308 memcpy(dci_ptr->values, sci_ptr->values, cv_size);
310 sci_ptr++;
311 dci_ptr++;
315 return dup;
316 err_free:
317 gcov_info_free(dup);
318 return NULL;
322 * gcov_info_free - release memory for profiling data set duplicate
323 * @info: profiling data set duplicate to free
325 void gcov_info_free(struct gcov_info *info)
327 unsigned int active;
328 unsigned int fi_idx;
329 unsigned int ct_idx;
330 struct gcov_ctr_info *ci_ptr;
332 if (!info->functions)
333 goto free_info;
335 active = num_counter_active(info);
337 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
338 if (!info->functions[fi_idx])
339 continue;
341 ci_ptr = info->functions[fi_idx]->ctrs;
343 for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
344 vfree(ci_ptr->values);
346 kfree(info->functions[fi_idx]);
349 free_info:
350 kfree(info->functions);
351 kfree(info->filename);
352 kfree(info);
355 #define ITER_STRIDE PAGE_SIZE
358 * struct gcov_iterator - specifies current file position in logical records
359 * @info: associated profiling data
360 * @buffer: buffer containing file data
361 * @size: size of buffer
362 * @pos: current position in file
364 struct gcov_iterator {
365 struct gcov_info *info;
366 void *buffer;
367 size_t size;
368 loff_t pos;
372 * store_gcov_u32 - store 32 bit number in gcov format to buffer
373 * @buffer: target buffer or NULL
374 * @off: offset into the buffer
375 * @v: value to be stored
377 * Number format defined by gcc: numbers are recorded in the 32 bit
378 * unsigned binary form of the endianness of the machine generating the
379 * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
380 * store anything.
382 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
384 u32 *data;
386 if (buffer) {
387 data = buffer + off;
388 *data = v;
391 return sizeof(*data);
395 * store_gcov_u64 - store 64 bit number in gcov format to buffer
396 * @buffer: target buffer or NULL
397 * @off: offset into the buffer
398 * @v: value to be stored
400 * Number format defined by gcc: numbers are recorded in the 32 bit
401 * unsigned binary form of the endianness of the machine generating the
402 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
403 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
404 * anything.
406 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
408 u32 *data;
410 if (buffer) {
411 data = buffer + off;
413 data[0] = (v & 0xffffffffUL);
414 data[1] = (v >> 32);
417 return sizeof(*data) * 2;
421 * convert_to_gcda - convert profiling data set to gcda file format
422 * @buffer: the buffer to store file data or %NULL if no data should be stored
423 * @info: profiling data set to be converted
425 * Returns the number of bytes that were/would have been stored into the buffer.
427 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
429 struct gcov_fn_info *fi_ptr;
430 struct gcov_ctr_info *ci_ptr;
431 unsigned int fi_idx;
432 unsigned int ct_idx;
433 unsigned int cv_idx;
434 size_t pos = 0;
436 /* File header. */
437 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
438 pos += store_gcov_u32(buffer, pos, info->version);
439 pos += store_gcov_u32(buffer, pos, info->stamp);
441 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
442 fi_ptr = info->functions[fi_idx];
444 /* Function record. */
445 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
446 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION_LENGTH);
447 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
448 pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
449 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
451 ci_ptr = fi_ptr->ctrs;
453 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
454 if (!counter_active(info, ct_idx))
455 continue;
457 /* Counter record. */
458 pos += store_gcov_u32(buffer, pos,
459 GCOV_TAG_FOR_COUNTER(ct_idx));
460 pos += store_gcov_u32(buffer, pos, ci_ptr->num * 2);
462 for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
463 pos += store_gcov_u64(buffer, pos,
464 ci_ptr->values[cv_idx]);
467 ci_ptr++;
471 return pos;
475 * gcov_iter_new - allocate and initialize profiling data iterator
476 * @info: profiling data set to be iterated
478 * Return file iterator on success, %NULL otherwise.
480 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
482 struct gcov_iterator *iter;
484 iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
485 if (!iter)
486 goto err_free;
488 iter->info = info;
489 /* Dry-run to get the actual buffer size. */
490 iter->size = convert_to_gcda(NULL, info);
491 iter->buffer = vmalloc(iter->size);
492 if (!iter->buffer)
493 goto err_free;
495 convert_to_gcda(iter->buffer, info);
497 return iter;
499 err_free:
500 kfree(iter);
501 return NULL;
506 * gcov_iter_get_info - return profiling data set for given file iterator
507 * @iter: file iterator
509 void gcov_iter_free(struct gcov_iterator *iter)
511 vfree(iter->buffer);
512 kfree(iter);
516 * gcov_iter_get_info - return profiling data set for given file iterator
517 * @iter: file iterator
519 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
521 return iter->info;
525 * gcov_iter_start - reset file iterator to starting position
526 * @iter: file iterator
528 void gcov_iter_start(struct gcov_iterator *iter)
530 iter->pos = 0;
534 * gcov_iter_next - advance file iterator to next logical record
535 * @iter: file iterator
537 * Return zero if new position is valid, non-zero if iterator has reached end.
539 int gcov_iter_next(struct gcov_iterator *iter)
541 if (iter->pos < iter->size)
542 iter->pos += ITER_STRIDE;
544 if (iter->pos >= iter->size)
545 return -EINVAL;
547 return 0;
551 * gcov_iter_write - write data for current pos to seq_file
552 * @iter: file iterator
553 * @seq: seq_file handle
555 * Return zero on success, non-zero otherwise.
557 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
559 size_t len;
561 if (iter->pos >= iter->size)
562 return -EINVAL;
564 len = ITER_STRIDE;
565 if (iter->pos + len > iter->size)
566 len = iter->size - iter->pos;
568 seq_write(seq, iter->buffer + iter->pos, len);
570 return 0;