2 * This code provides functions to handle gcc's profiling data format
3 * introduced with gcc 3.4. Future versions of gcc may change the gcov
4 * format (as happened before), so all format-specific information needs
5 * to be kept modular and easily exchangeable.
7 * This file is based on gcc-internal definitions. Functions and data
8 * structures are defined to be compatible with gcc counterparts.
9 * For a better understanding, refer to gcc source: gcc/gcov-io.h.
11 * Copyright IBM Corp. 2009
12 * Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
14 * Uses gcc-internal data definitions.
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/seq_file.h>
21 #include <linux/vmalloc.h>
24 #define GCOV_COUNTERS 5
26 static struct gcov_info
*gcov_info_head
;
29 * struct gcov_fn_info - profiling meta data per function
30 * @ident: object file-unique function identifier
31 * @checksum: function checksum
32 * @n_ctrs: number of values per counter type belonging to this function
34 * This data is generated by gcc during compilation and doesn't change
39 unsigned int checksum
;
40 unsigned int n_ctrs
[0];
44 * struct gcov_ctr_info - profiling data per counter type
45 * @num: number of counter values for this type
46 * @values: array of counter values for this type
47 * @merge: merge function for counter values of this type (unused)
49 * This data is generated by gcc during compilation and doesn't change
50 * at run-time with the exception of the values array.
52 struct gcov_ctr_info
{
55 void (*merge
)(gcov_type
*, unsigned int);
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
63 * @filename: name of the associated gcov data file
64 * @n_functions: number of instrumented functions
65 * @functions: function data
66 * @ctr_mask: mask specifying which counter types are active
67 * @counts: counter data per counter type
69 * This data is generated by gcc during compilation and doesn't change
70 * at run-time with the exception of the next pointer.
74 struct gcov_info
*next
;
77 unsigned int n_functions
;
78 const struct gcov_fn_info
*functions
;
79 unsigned int ctr_mask
;
80 struct gcov_ctr_info counts
[0];
84 * gcov_info_filename - return info filename
85 * @info: profiling data set
87 const char *gcov_info_filename(struct gcov_info
*info
)
89 return info
->filename
;
93 * gcov_info_version - return info version
94 * @info: profiling data set
96 unsigned int gcov_info_version(struct gcov_info
*info
)
102 * gcov_info_next - return next profiling data set
103 * @info: profiling data set
105 * Returns next gcov_info following @info or first gcov_info in the chain if
108 struct gcov_info
*gcov_info_next(struct gcov_info
*info
)
111 return gcov_info_head
;
117 * gcov_info_link - link/add profiling data set to the list
118 * @info: profiling data set
120 void gcov_info_link(struct gcov_info
*info
)
122 info
->next
= gcov_info_head
;
123 gcov_info_head
= info
;
127 * gcov_info_unlink - unlink/remove profiling data set from the list
128 * @prev: previous profiling data set
129 * @info: profiling data set
131 void gcov_info_unlink(struct gcov_info
*prev
, struct gcov_info
*info
)
134 prev
->next
= info
->next
;
136 gcov_info_head
= info
->next
;
139 /* Symbolic links to be created for each profiling data file. */
140 const struct gcov_link gcov_link
[] = {
141 { OBJ_TREE
, "gcno" }, /* Link to .gcno file in $(objtree). */
146 * Determine whether a counter is active. Based on gcc magic. Doesn't change
149 static int counter_active(struct gcov_info
*info
, unsigned int type
)
151 return (1 << type
) & info
->ctr_mask
;
154 /* Determine number of active counters. Based on gcc magic. */
155 static unsigned int num_counter_active(struct gcov_info
*info
)
158 unsigned int result
= 0;
160 for (i
= 0; i
< GCOV_COUNTERS
; i
++) {
161 if (counter_active(info
, i
))
168 * gcov_info_reset - reset profiling data to zero
169 * @info: profiling data set
171 void gcov_info_reset(struct gcov_info
*info
)
173 unsigned int active
= num_counter_active(info
);
176 for (i
= 0; i
< active
; i
++) {
177 memset(info
->counts
[i
].values
, 0,
178 info
->counts
[i
].num
* sizeof(gcov_type
));
183 * gcov_info_is_compatible - check if profiling data can be added
184 * @info1: first profiling data set
185 * @info2: second profiling data set
187 * Returns non-zero if profiling data can be added, zero otherwise.
189 int gcov_info_is_compatible(struct gcov_info
*info1
, struct gcov_info
*info2
)
191 return (info1
->stamp
== info2
->stamp
);
195 * gcov_info_add - add up profiling data
196 * @dest: profiling data set to which data is added
197 * @source: profiling data set which is added
199 * Adds profiling counts of @source to @dest.
201 void gcov_info_add(struct gcov_info
*dest
, struct gcov_info
*source
)
206 for (i
= 0; i
< num_counter_active(dest
); i
++) {
207 for (j
= 0; j
< dest
->counts
[i
].num
; j
++) {
208 dest
->counts
[i
].values
[j
] +=
209 source
->counts
[i
].values
[j
];
214 /* Get size of function info entry. Based on gcc magic. */
215 static size_t get_fn_size(struct gcov_info
*info
)
219 size
= sizeof(struct gcov_fn_info
) + num_counter_active(info
) *
220 sizeof(unsigned int);
221 if (__alignof__(struct gcov_fn_info
) > sizeof(unsigned int))
222 size
= ALIGN(size
, __alignof__(struct gcov_fn_info
));
226 /* Get address of function info entry. Based on gcc magic. */
227 static struct gcov_fn_info
*get_fn_info(struct gcov_info
*info
, unsigned int fn
)
229 return (struct gcov_fn_info
*)
230 ((char *) info
->functions
+ fn
* get_fn_size(info
));
234 * gcov_info_dup - duplicate profiling data set
235 * @info: profiling data set to duplicate
237 * Return newly allocated duplicate on success, %NULL on error.
239 struct gcov_info
*gcov_info_dup(struct gcov_info
*info
)
241 struct gcov_info
*dup
;
245 /* Duplicate gcov_info. */
246 active
= num_counter_active(info
);
247 dup
= kzalloc(sizeof(struct gcov_info
) +
248 sizeof(struct gcov_ctr_info
) * active
, GFP_KERNEL
);
251 dup
->version
= info
->version
;
252 dup
->stamp
= info
->stamp
;
253 dup
->n_functions
= info
->n_functions
;
254 dup
->ctr_mask
= info
->ctr_mask
;
255 /* Duplicate filename. */
256 dup
->filename
= kstrdup(info
->filename
, GFP_KERNEL
);
259 /* Duplicate table of functions. */
260 dup
->functions
= kmemdup(info
->functions
, info
->n_functions
*
261 get_fn_size(info
), GFP_KERNEL
);
264 /* Duplicate counter arrays. */
265 for (i
= 0; i
< active
; i
++) {
266 struct gcov_ctr_info
*ctr
= &info
->counts
[i
];
267 size_t size
= ctr
->num
* sizeof(gcov_type
);
269 dup
->counts
[i
].num
= ctr
->num
;
270 dup
->counts
[i
].merge
= ctr
->merge
;
271 dup
->counts
[i
].values
= vmalloc(size
);
272 if (!dup
->counts
[i
].values
)
274 memcpy(dup
->counts
[i
].values
, ctr
->values
, size
);
284 * gcov_info_free - release memory for profiling data set duplicate
285 * @info: profiling data set duplicate to free
287 void gcov_info_free(struct gcov_info
*info
)
289 unsigned int active
= num_counter_active(info
);
292 for (i
= 0; i
< active
; i
++)
293 vfree(info
->counts
[i
].values
);
294 kfree(info
->functions
);
295 kfree(info
->filename
);
300 * struct type_info - iterator helper array
301 * @ctr_type: counter type
302 * @offset: index of the first value of the current function for this type
304 * This array is needed to convert the in-memory data format into the in-file
308 * for each counter type
314 * for each counter type
317 * See gcc source gcc/gcov-io.h for more information on data organization.
325 * struct gcov_iterator - specifies current file position in logical records
326 * @info: associated profiling data
327 * @record: record type
328 * @function: function number
329 * @type: counter type
330 * @count: index into values array
331 * @num_types: number of counter types
332 * @type_info: helper array to get values-array offset for current function
334 struct gcov_iterator
{
335 struct gcov_info
*info
;
338 unsigned int function
;
343 struct type_info type_info
[0];
346 static struct gcov_fn_info
*get_func(struct gcov_iterator
*iter
)
348 return get_fn_info(iter
->info
, iter
->function
);
351 static struct type_info
*get_type(struct gcov_iterator
*iter
)
353 return &iter
->type_info
[iter
->type
];
357 * gcov_iter_new - allocate and initialize profiling data iterator
358 * @info: profiling data set to be iterated
360 * Return file iterator on success, %NULL otherwise.
362 struct gcov_iterator
*gcov_iter_new(struct gcov_info
*info
)
364 struct gcov_iterator
*iter
;
366 iter
= kzalloc(sizeof(struct gcov_iterator
) +
367 num_counter_active(info
) * sizeof(struct type_info
),
376 * gcov_iter_free - release memory for iterator
377 * @iter: file iterator to free
379 void gcov_iter_free(struct gcov_iterator
*iter
)
385 * gcov_iter_get_info - return profiling data set for given file iterator
386 * @iter: file iterator
388 struct gcov_info
*gcov_iter_get_info(struct gcov_iterator
*iter
)
394 * gcov_iter_start - reset file iterator to starting position
395 * @iter: file iterator
397 void gcov_iter_start(struct gcov_iterator
*iter
)
406 for (i
= 0; i
< GCOV_COUNTERS
; i
++) {
407 if (counter_active(iter
->info
, i
)) {
408 iter
->type_info
[iter
->num_types
].ctr_type
= i
;
409 iter
->type_info
[iter
->num_types
++].offset
= 0;
414 /* Mapping of logical record number to actual file content. */
415 #define RECORD_FILE_MAGIC 0
416 #define RECORD_GCOV_VERSION 1
417 #define RECORD_TIME_STAMP 2
418 #define RECORD_FUNCTION_TAG 3
419 #define RECORD_FUNCTON_TAG_LEN 4
420 #define RECORD_FUNCTION_IDENT 5
421 #define RECORD_FUNCTION_CHECK 6
422 #define RECORD_COUNT_TAG 7
423 #define RECORD_COUNT_LEN 8
424 #define RECORD_COUNT 9
427 * gcov_iter_next - advance file iterator to next logical record
428 * @iter: file iterator
430 * Return zero if new position is valid, non-zero if iterator has reached end.
432 int gcov_iter_next(struct gcov_iterator
*iter
)
434 switch (iter
->record
) {
435 case RECORD_FILE_MAGIC
:
436 case RECORD_GCOV_VERSION
:
437 case RECORD_FUNCTION_TAG
:
438 case RECORD_FUNCTON_TAG_LEN
:
439 case RECORD_FUNCTION_IDENT
:
440 case RECORD_COUNT_TAG
:
441 /* Advance to next record */
445 /* Advance to next count */
448 case RECORD_COUNT_LEN
:
449 if (iter
->count
< get_func(iter
)->n_ctrs
[iter
->type
]) {
453 /* Advance to next counter type */
454 get_type(iter
)->offset
+= iter
->count
;
458 case RECORD_FUNCTION_CHECK
:
459 if (iter
->type
< iter
->num_types
) {
463 /* Advance to next function */
467 case RECORD_TIME_STAMP
:
468 if (iter
->function
< iter
->info
->n_functions
)
475 if (iter
->record
== -1)
482 * seq_write_gcov_u32 - write 32 bit number in gcov format to seq_file
483 * @seq: seq_file handle
484 * @v: value to be stored
486 * Number format defined by gcc: numbers are recorded in the 32 bit
487 * unsigned binary form of the endianness of the machine generating the
490 static int seq_write_gcov_u32(struct seq_file
*seq
, u32 v
)
492 return seq_write(seq
, &v
, sizeof(v
));
496 * seq_write_gcov_u64 - write 64 bit number in gcov format to seq_file
497 * @seq: seq_file handle
498 * @v: value to be stored
500 * Number format defined by gcc: numbers are recorded in the 32 bit
501 * unsigned binary form of the endianness of the machine generating the
502 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
505 static int seq_write_gcov_u64(struct seq_file
*seq
, u64 v
)
509 data
[0] = (v
& 0xffffffffUL
);
511 return seq_write(seq
, data
, sizeof(data
));
515 * gcov_iter_write - write data for current pos to seq_file
516 * @iter: file iterator
517 * @seq: seq_file handle
519 * Return zero on success, non-zero otherwise.
521 int gcov_iter_write(struct gcov_iterator
*iter
, struct seq_file
*seq
)
525 switch (iter
->record
) {
526 case RECORD_FILE_MAGIC
:
527 rc
= seq_write_gcov_u32(seq
, GCOV_DATA_MAGIC
);
529 case RECORD_GCOV_VERSION
:
530 rc
= seq_write_gcov_u32(seq
, iter
->info
->version
);
532 case RECORD_TIME_STAMP
:
533 rc
= seq_write_gcov_u32(seq
, iter
->info
->stamp
);
535 case RECORD_FUNCTION_TAG
:
536 rc
= seq_write_gcov_u32(seq
, GCOV_TAG_FUNCTION
);
538 case RECORD_FUNCTON_TAG_LEN
:
539 rc
= seq_write_gcov_u32(seq
, 2);
541 case RECORD_FUNCTION_IDENT
:
542 rc
= seq_write_gcov_u32(seq
, get_func(iter
)->ident
);
544 case RECORD_FUNCTION_CHECK
:
545 rc
= seq_write_gcov_u32(seq
, get_func(iter
)->checksum
);
547 case RECORD_COUNT_TAG
:
548 rc
= seq_write_gcov_u32(seq
,
549 GCOV_TAG_FOR_COUNTER(get_type(iter
)->ctr_type
));
551 case RECORD_COUNT_LEN
:
552 rc
= seq_write_gcov_u32(seq
,
553 get_func(iter
)->n_ctrs
[iter
->type
] * 2);
556 rc
= seq_write_gcov_u64(seq
,
557 iter
->info
->counts
[iter
->type
].
558 values
[iter
->count
+ get_type(iter
)->offset
]);