GNU Linux-libre 4.9.287-gnu1
[releases.git] / kernel / gcov / gcc_4_7.c
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
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"
20
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
32
33 #define GCOV_TAG_FUNCTION_LENGTH        3
34
35 static struct gcov_info *gcov_info_head;
36
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
41  *
42  * This data is generated by gcc during compilation and doesn't change
43  * at run-time with the exception of the values array.
44  */
45 struct gcov_ctr_info {
46         unsigned int num;
47         gcov_type *values;
48 };
49
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
57  *
58  * This data is generated by gcc during compilation and doesn't change
59  * at run-time.
60  *
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.
66  */
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];
73 };
74
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
84  *
85  * This data is generated by gcc during compilation and doesn't change
86  * at run-time with the exception of the next pointer.
87  */
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;
96 };
97
98 /**
99  * gcov_info_filename - return info filename
100  * @info: profiling data set
101  */
102 const char *gcov_info_filename(struct gcov_info *info)
103 {
104         return info->filename;
105 }
106
107 /**
108  * gcov_info_version - return info version
109  * @info: profiling data set
110  */
111 unsigned int gcov_info_version(struct gcov_info *info)
112 {
113         return info->version;
114 }
115
116 /**
117  * gcov_info_next - return next profiling data set
118  * @info: profiling data set
119  *
120  * Returns next gcov_info following @info or first gcov_info in the chain if
121  * @info is %NULL.
122  */
123 struct gcov_info *gcov_info_next(struct gcov_info *info)
124 {
125         if (!info)
126                 return gcov_info_head;
127
128         return info->next;
129 }
130
131 /**
132  * gcov_info_link - link/add profiling data set to the list
133  * @info: profiling data set
134  */
135 void gcov_info_link(struct gcov_info *info)
136 {
137         info->next = gcov_info_head;
138         gcov_info_head = info;
139 }
140
141 /**
142  * gcov_info_unlink - unlink/remove profiling data set from the list
143  * @prev: previous profiling data set
144  * @info: profiling data set
145  */
146 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
147 {
148         if (prev)
149                 prev->next = info->next;
150         else
151                 gcov_info_head = info->next;
152 }
153
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},
158 };
159
160 /*
161  * Determine whether a counter is active. Doesn't change at run-time.
162  */
163 static int counter_active(struct gcov_info *info, unsigned int type)
164 {
165         return info->merge[type] ? 1 : 0;
166 }
167
168 /* Determine number of active counters. Based on gcc magic. */
169 static unsigned int num_counter_active(struct gcov_info *info)
170 {
171         unsigned int i;
172         unsigned int result = 0;
173
174         for (i = 0; i < GCOV_COUNTERS; i++) {
175                 if (counter_active(info, i))
176                         result++;
177         }
178         return result;
179 }
180
181 /**
182  * gcov_info_reset - reset profiling data to zero
183  * @info: profiling data set
184  */
185 void gcov_info_reset(struct gcov_info *info)
186 {
187         struct gcov_ctr_info *ci_ptr;
188         unsigned int fi_idx;
189         unsigned int ct_idx;
190
191         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
192                 ci_ptr = info->functions[fi_idx]->ctrs;
193
194                 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
195                         if (!counter_active(info, ct_idx))
196                                 continue;
197
198                         memset(ci_ptr->values, 0,
199                                         sizeof(gcov_type) * ci_ptr->num);
200                         ci_ptr++;
201                 }
202         }
203 }
204
205 /**
206  * gcov_info_is_compatible - check if profiling data can be added
207  * @info1: first profiling data set
208  * @info2: second profiling data set
209  *
210  * Returns non-zero if profiling data can be added, zero otherwise.
211  */
212 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
213 {
214         return (info1->stamp == info2->stamp);
215 }
216
217 /**
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
221  *
222  * Adds profiling counts of @source to @dest.
223  */
224 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
225 {
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;
231
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;
235
236                 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
237                         if (!counter_active(src, ct_idx))
238                                 continue;
239
240                         for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
241                                 dci_ptr->values[val_idx] +=
242                                         sci_ptr->values[val_idx];
243
244                         dci_ptr++;
245                         sci_ptr++;
246                 }
247         }
248 }
249
250 /**
251  * gcov_info_dup - duplicate profiling data set
252  * @info: profiling data set to duplicate
253  *
254  * Return newly allocated duplicate on success, %NULL on error.
255  */
256 struct gcov_info *gcov_info_dup(struct gcov_info *info)
257 {
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 */
266
267         dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
268         if (!dup)
269                 return NULL;
270
271         dup->next = NULL;
272         dup->filename = NULL;
273         dup->functions = NULL;
274
275         dup->filename = kstrdup(info->filename, GFP_KERNEL);
276         if (!dup->filename)
277                 goto err_free;
278
279         dup->functions = kcalloc(info->n_functions,
280                                  sizeof(struct gcov_fn_info *), GFP_KERNEL);
281         if (!dup->functions)
282                 goto err_free;
283
284         active = num_counter_active(info);
285         fi_size = sizeof(struct gcov_fn_info);
286         fi_size += sizeof(struct gcov_ctr_info) * active;
287
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;
292
293                 *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
294
295                 sci_ptr = info->functions[fi_idx]->ctrs;
296                 dci_ptr = dup->functions[fi_idx]->ctrs;
297
298                 for (ct_idx = 0; ct_idx < active; ct_idx++) {
299
300                         cv_size = sizeof(gcov_type) * sci_ptr->num;
301
302                         dci_ptr->values = vmalloc(cv_size);
303
304                         if (!dci_ptr->values)
305                                 goto err_free;
306
307                         dci_ptr->num = sci_ptr->num;
308                         memcpy(dci_ptr->values, sci_ptr->values, cv_size);
309
310                         sci_ptr++;
311                         dci_ptr++;
312                 }
313         }
314
315         return dup;
316 err_free:
317         gcov_info_free(dup);
318         return NULL;
319 }
320
321 /**
322  * gcov_info_free - release memory for profiling data set duplicate
323  * @info: profiling data set duplicate to free
324  */
325 void gcov_info_free(struct gcov_info *info)
326 {
327         unsigned int active;
328         unsigned int fi_idx;
329         unsigned int ct_idx;
330         struct gcov_ctr_info *ci_ptr;
331
332         if (!info->functions)
333                 goto free_info;
334
335         active = num_counter_active(info);
336
337         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
338                 if (!info->functions[fi_idx])
339                         continue;
340
341                 ci_ptr = info->functions[fi_idx]->ctrs;
342
343                 for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
344                         vfree(ci_ptr->values);
345
346                 kfree(info->functions[fi_idx]);
347         }
348
349 free_info:
350         kfree(info->functions);
351         kfree(info->filename);
352         kfree(info);
353 }
354
355 #define ITER_STRIDE     PAGE_SIZE
356
357 /**
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
363  */
364 struct gcov_iterator {
365         struct gcov_info *info;
366         void *buffer;
367         size_t size;
368         loff_t pos;
369 };
370
371 /**
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
376  *
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.
381  */
382 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
383 {
384         u32 *data;
385
386         if (buffer) {
387                 data = buffer + off;
388                 *data = v;
389         }
390
391         return sizeof(*data);
392 }
393
394 /**
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
399  *
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.
405  */
406 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
407 {
408         u32 *data;
409
410         if (buffer) {
411                 data = buffer + off;
412
413                 data[0] = (v & 0xffffffffUL);
414                 data[1] = (v >> 32);
415         }
416
417         return sizeof(*data) * 2;
418 }
419
420 /**
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
424  *
425  * Returns the number of bytes that were/would have been stored into the buffer.
426  */
427 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
428 {
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;
435
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);
440
441         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
442                 fi_ptr = info->functions[fi_idx];
443
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);
450
451                 ci_ptr = fi_ptr->ctrs;
452
453                 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
454                         if (!counter_active(info, ct_idx))
455                                 continue;
456
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);
461
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]);
465                         }
466
467                         ci_ptr++;
468                 }
469         }
470
471         return pos;
472 }
473
474 /**
475  * gcov_iter_new - allocate and initialize profiling data iterator
476  * @info: profiling data set to be iterated
477  *
478  * Return file iterator on success, %NULL otherwise.
479  */
480 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
481 {
482         struct gcov_iterator *iter;
483
484         iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
485         if (!iter)
486                 goto err_free;
487
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;
494
495         convert_to_gcda(iter->buffer, info);
496
497         return iter;
498
499 err_free:
500         kfree(iter);
501         return NULL;
502 }
503
504
505 /**
506  * gcov_iter_get_info - return profiling data set for given file iterator
507  * @iter: file iterator
508  */
509 void gcov_iter_free(struct gcov_iterator *iter)
510 {
511         vfree(iter->buffer);
512         kfree(iter);
513 }
514
515 /**
516  * gcov_iter_get_info - return profiling data set for given file iterator
517  * @iter: file iterator
518  */
519 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
520 {
521         return iter->info;
522 }
523
524 /**
525  * gcov_iter_start - reset file iterator to starting position
526  * @iter: file iterator
527  */
528 void gcov_iter_start(struct gcov_iterator *iter)
529 {
530         iter->pos = 0;
531 }
532
533 /**
534  * gcov_iter_next - advance file iterator to next logical record
535  * @iter: file iterator
536  *
537  * Return zero if new position is valid, non-zero if iterator has reached end.
538  */
539 int gcov_iter_next(struct gcov_iterator *iter)
540 {
541         if (iter->pos < iter->size)
542                 iter->pos += ITER_STRIDE;
543
544         if (iter->pos >= iter->size)
545                 return -EINVAL;
546
547         return 0;
548 }
549
550 /**
551  * gcov_iter_write - write data for current pos to seq_file
552  * @iter: file iterator
553  * @seq: seq_file handle
554  *
555  * Return zero on success, non-zero otherwise.
556  */
557 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
558 {
559         size_t len;
560
561         if (iter->pos >= iter->size)
562                 return -EINVAL;
563
564         len = ITER_STRIDE;
565         if (iter->pos + len > iter->size)
566                 len = iter->size - iter->pos;
567
568         seq_write(seq, iter->buffer + iter->pos, len);
569
570         return 0;
571 }