1 // SPDX-License-Identifier: GPL-2.0
3 * This code provides functions to handle gcc's profiling data format
4 * introduced with gcc 4.7.
6 * This file is based heavily on gcc_3_4.c file.
8 * For a better understanding, refer to gcc source:
12 * Uses gcc-internal data definitions.
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/seq_file.h>
19 #include <linux/vmalloc.h>
23 #define GCOV_COUNTERS 8
25 #define GCOV_COUNTERS 9
26 #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
27 #define GCOV_COUNTERS 10
28 #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
29 #define GCOV_COUNTERS 9
31 #define GCOV_COUNTERS 8
34 #define GCOV_TAG_FUNCTION_LENGTH 3
36 /* Since GCC 12.1 sizes are in BYTES and not in WORDS (4B). */
38 #define GCOV_UNIT_SIZE 4
40 #define GCOV_UNIT_SIZE 1
43 static struct gcov_info *gcov_info_head;
46 * struct gcov_ctr_info - information about counters for a single function
47 * @num: number of counter values for this type
48 * @values: array of counter values for this type
50 * This data is generated by gcc during compilation and doesn't change
51 * at run-time with the exception of the values array.
53 struct gcov_ctr_info {
59 * struct gcov_fn_info - profiling meta data per function
61 * @ident: unique ident of function
62 * @lineno_checksum: function lineo_checksum
63 * @cfg_checksum: function cfg checksum
64 * @ctrs: instrumented counters
66 * This data is generated by gcc during compilation and doesn't change
69 * Information about a single function. This uses the trailing array
70 * idiom. The number of counters is determined from the merge pointer
71 * array in gcov_info. The key is used to detect which of a set of
72 * comdat functions was selected -- it points to the gcov_info object
73 * of the object file containing the selected comdat function.
76 const struct gcov_info *key;
78 unsigned int lineno_checksum;
79 unsigned int cfg_checksum;
80 struct gcov_ctr_info ctrs[0];
84 * struct gcov_info - profiling data per object file
85 * @version: gcov version magic indicating the gcc version used for compilation
86 * @next: list head for a singly-linked list
87 * @stamp: uniquifying time stamp
88 * @filename: name of the associated gcov data file
89 * @merge: merge functions (null for unused counter type)
90 * @n_functions: number of instrumented functions
91 * @functions: pointer to pointers to function information
93 * This data is generated by gcc during compilation and doesn't change
94 * at run-time with the exception of the next pointer.
98 struct gcov_info *next;
100 const char *filename;
101 void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
102 unsigned int n_functions;
103 struct gcov_fn_info **functions;
107 * gcov_info_filename - return info filename
108 * @info: profiling data set
110 const char *gcov_info_filename(struct gcov_info *info)
112 return info->filename;
116 * gcov_info_version - return info version
117 * @info: profiling data set
119 unsigned int gcov_info_version(struct gcov_info *info)
121 return info->version;
125 * gcov_info_next - return next profiling data set
126 * @info: profiling data set
128 * Returns next gcov_info following @info or first gcov_info in the chain if
131 struct gcov_info *gcov_info_next(struct gcov_info *info)
134 return gcov_info_head;
140 * gcov_info_link - link/add profiling data set to the list
141 * @info: profiling data set
143 void gcov_info_link(struct gcov_info *info)
145 info->next = gcov_info_head;
146 gcov_info_head = info;
150 * gcov_info_unlink - unlink/remove profiling data set from the list
151 * @prev: previous profiling data set
152 * @info: profiling data set
154 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
157 prev->next = info->next;
159 gcov_info_head = info->next;
162 /* Symbolic links to be created for each profiling data file. */
163 const struct gcov_link gcov_link[] = {
164 { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
169 * Determine whether a counter is active. Doesn't change at run-time.
171 static int counter_active(struct gcov_info *info, unsigned int type)
173 return info->merge[type] ? 1 : 0;
176 /* Determine number of active counters. Based on gcc magic. */
177 static unsigned int num_counter_active(struct gcov_info *info)
180 unsigned int result = 0;
182 for (i = 0; i < GCOV_COUNTERS; i++) {
183 if (counter_active(info, i))
190 * gcov_info_reset - reset profiling data to zero
191 * @info: profiling data set
193 void gcov_info_reset(struct gcov_info *info)
195 struct gcov_ctr_info *ci_ptr;
199 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
200 ci_ptr = info->functions[fi_idx]->ctrs;
202 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
203 if (!counter_active(info, ct_idx))
206 memset(ci_ptr->values, 0,
207 sizeof(gcov_type) * ci_ptr->num);
214 * gcov_info_is_compatible - check if profiling data can be added
215 * @info1: first profiling data set
216 * @info2: second profiling data set
218 * Returns non-zero if profiling data can be added, zero otherwise.
220 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
222 return (info1->stamp == info2->stamp);
226 * gcov_info_add - add up profiling data
227 * @dest: profiling data set to which data is added
228 * @source: profiling data set which is added
230 * Adds profiling counts of @source to @dest.
232 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
234 struct gcov_ctr_info *dci_ptr;
235 struct gcov_ctr_info *sci_ptr;
238 unsigned int val_idx;
240 for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) {
241 dci_ptr = dst->functions[fi_idx]->ctrs;
242 sci_ptr = src->functions[fi_idx]->ctrs;
244 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
245 if (!counter_active(src, ct_idx))
248 for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
249 dci_ptr->values[val_idx] +=
250 sci_ptr->values[val_idx];
259 * gcov_info_dup - duplicate profiling data set
260 * @info: profiling data set to duplicate
262 * Return newly allocated duplicate on success, %NULL on error.
264 struct gcov_info *gcov_info_dup(struct gcov_info *info)
266 struct gcov_info *dup;
267 struct gcov_ctr_info *dci_ptr; /* dst counter info */
268 struct gcov_ctr_info *sci_ptr; /* src counter info */
270 unsigned int fi_idx; /* function info idx */
271 unsigned int ct_idx; /* counter type idx */
272 size_t fi_size; /* function info size */
273 size_t cv_size; /* counter values size */
275 dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
280 dup->filename = NULL;
281 dup->functions = NULL;
283 dup->filename = kstrdup(info->filename, GFP_KERNEL);
287 dup->functions = kcalloc(info->n_functions,
288 sizeof(struct gcov_fn_info *), GFP_KERNEL);
292 active = num_counter_active(info);
293 fi_size = sizeof(struct gcov_fn_info);
294 fi_size += sizeof(struct gcov_ctr_info) * active;
296 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
297 dup->functions[fi_idx] = kzalloc(fi_size, GFP_KERNEL);
298 if (!dup->functions[fi_idx])
301 *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
303 sci_ptr = info->functions[fi_idx]->ctrs;
304 dci_ptr = dup->functions[fi_idx]->ctrs;
306 for (ct_idx = 0; ct_idx < active; ct_idx++) {
308 cv_size = sizeof(gcov_type) * sci_ptr->num;
310 dci_ptr->values = vmalloc(cv_size);
312 if (!dci_ptr->values)
315 dci_ptr->num = sci_ptr->num;
316 memcpy(dci_ptr->values, sci_ptr->values, cv_size);
330 * gcov_info_free - release memory for profiling data set duplicate
331 * @info: profiling data set duplicate to free
333 void gcov_info_free(struct gcov_info *info)
338 struct gcov_ctr_info *ci_ptr;
340 if (!info->functions)
343 active = num_counter_active(info);
345 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
346 if (!info->functions[fi_idx])
349 ci_ptr = info->functions[fi_idx]->ctrs;
351 for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
352 vfree(ci_ptr->values);
354 kfree(info->functions[fi_idx]);
358 kfree(info->functions);
359 kfree(info->filename);
363 #define ITER_STRIDE PAGE_SIZE
366 * struct gcov_iterator - specifies current file position in logical records
367 * @info: associated profiling data
368 * @buffer: buffer containing file data
369 * @size: size of buffer
370 * @pos: current position in file
372 struct gcov_iterator {
373 struct gcov_info *info;
380 * store_gcov_u32 - store 32 bit number in gcov format to buffer
381 * @buffer: target buffer or NULL
382 * @off: offset into the buffer
383 * @v: value to be stored
385 * Number format defined by gcc: numbers are recorded in the 32 bit
386 * unsigned binary form of the endianness of the machine generating the
387 * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
390 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
399 return sizeof(*data);
403 * store_gcov_u64 - store 64 bit number in gcov format to buffer
404 * @buffer: target buffer or NULL
405 * @off: offset into the buffer
406 * @v: value to be stored
408 * Number format defined by gcc: numbers are recorded in the 32 bit
409 * unsigned binary form of the endianness of the machine generating the
410 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
411 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
414 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
421 data[0] = (v & 0xffffffffUL);
425 return sizeof(*data) * 2;
429 * convert_to_gcda - convert profiling data set to gcda file format
430 * @buffer: the buffer to store file data or %NULL if no data should be stored
431 * @info: profiling data set to be converted
433 * Returns the number of bytes that were/would have been stored into the buffer.
435 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
437 struct gcov_fn_info *fi_ptr;
438 struct gcov_ctr_info *ci_ptr;
445 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
446 pos += store_gcov_u32(buffer, pos, info->version);
447 pos += store_gcov_u32(buffer, pos, info->stamp);
450 /* Use zero as checksum of the compilation unit. */
451 pos += store_gcov_u32(buffer, pos, 0);
454 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
455 fi_ptr = info->functions[fi_idx];
457 /* Function record. */
458 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
459 pos += store_gcov_u32(buffer, pos,
460 GCOV_TAG_FUNCTION_LENGTH * GCOV_UNIT_SIZE);
461 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
462 pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
463 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
465 ci_ptr = fi_ptr->ctrs;
467 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
468 if (!counter_active(info, ct_idx))
471 /* Counter record. */
472 pos += store_gcov_u32(buffer, pos,
473 GCOV_TAG_FOR_COUNTER(ct_idx));
474 pos += store_gcov_u32(buffer, pos,
475 ci_ptr->num * 2 * GCOV_UNIT_SIZE);
477 for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
478 pos += store_gcov_u64(buffer, pos,
479 ci_ptr->values[cv_idx]);
490 * gcov_iter_new - allocate and initialize profiling data iterator
491 * @info: profiling data set to be iterated
493 * Return file iterator on success, %NULL otherwise.
495 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
497 struct gcov_iterator *iter;
499 iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
504 /* Dry-run to get the actual buffer size. */
505 iter->size = convert_to_gcda(NULL, info);
506 iter->buffer = vmalloc(iter->size);
510 convert_to_gcda(iter->buffer, info);
521 * gcov_iter_get_info - return profiling data set for given file iterator
522 * @iter: file iterator
524 void gcov_iter_free(struct gcov_iterator *iter)
531 * gcov_iter_get_info - return profiling data set for given file iterator
532 * @iter: file iterator
534 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
540 * gcov_iter_start - reset file iterator to starting position
541 * @iter: file iterator
543 void gcov_iter_start(struct gcov_iterator *iter)
549 * gcov_iter_next - advance file iterator to next logical record
550 * @iter: file iterator
552 * Return zero if new position is valid, non-zero if iterator has reached end.
554 int gcov_iter_next(struct gcov_iterator *iter)
556 if (iter->pos < iter->size)
557 iter->pos += ITER_STRIDE;
559 if (iter->pos >= iter->size)
566 * gcov_iter_write - write data for current pos to seq_file
567 * @iter: file iterator
568 * @seq: seq_file handle
570 * Return zero on success, non-zero otherwise.
572 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
576 if (iter->pos >= iter->size)
580 if (iter->pos + len > iter->size)
581 len = iter->size - iter->pos;
583 seq_write(seq, iter->buffer + iter->pos, len);