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 * @checksum: unique object checksum
89 * @filename: name of the associated gcov data file
90 * @merge: merge functions (null for unused counter type)
91 * @n_functions: number of instrumented functions
92 * @functions: pointer to pointers to function information
94 * This data is generated by gcc during compilation and doesn't change
95 * at run-time with the exception of the next pointer.
99 struct gcov_info *next;
101 /* Since GCC 12.1 a checksum field is added. */
103 unsigned int checksum;
105 const char *filename;
106 void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
107 unsigned int n_functions;
108 struct gcov_fn_info **functions;
112 * gcov_info_filename - return info filename
113 * @info: profiling data set
115 const char *gcov_info_filename(struct gcov_info *info)
117 return info->filename;
121 * gcov_info_version - return info version
122 * @info: profiling data set
124 unsigned int gcov_info_version(struct gcov_info *info)
126 return info->version;
130 * gcov_info_next - return next profiling data set
131 * @info: profiling data set
133 * Returns next gcov_info following @info or first gcov_info in the chain if
136 struct gcov_info *gcov_info_next(struct gcov_info *info)
139 return gcov_info_head;
145 * gcov_info_link - link/add profiling data set to the list
146 * @info: profiling data set
148 void gcov_info_link(struct gcov_info *info)
150 info->next = gcov_info_head;
151 gcov_info_head = info;
155 * gcov_info_unlink - unlink/remove profiling data set from the list
156 * @prev: previous profiling data set
157 * @info: profiling data set
159 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
162 prev->next = info->next;
164 gcov_info_head = info->next;
168 * gcov_info_within_module - check if a profiling data set belongs to a module
169 * @info: profiling data set
172 * Returns true if profiling data belongs module, false otherwise.
174 bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
176 return within_module((unsigned long)info, mod);
179 /* Symbolic links to be created for each profiling data file. */
180 const struct gcov_link gcov_link[] = {
181 { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
186 * Determine whether a counter is active. Doesn't change at run-time.
188 static int counter_active(struct gcov_info *info, unsigned int type)
190 return info->merge[type] ? 1 : 0;
193 /* Determine number of active counters. Based on gcc magic. */
194 static unsigned int num_counter_active(struct gcov_info *info)
197 unsigned int result = 0;
199 for (i = 0; i < GCOV_COUNTERS; i++) {
200 if (counter_active(info, i))
207 * gcov_info_reset - reset profiling data to zero
208 * @info: profiling data set
210 void gcov_info_reset(struct gcov_info *info)
212 struct gcov_ctr_info *ci_ptr;
216 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
217 ci_ptr = info->functions[fi_idx]->ctrs;
219 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
220 if (!counter_active(info, ct_idx))
223 memset(ci_ptr->values, 0,
224 sizeof(gcov_type) * ci_ptr->num);
231 * gcov_info_is_compatible - check if profiling data can be added
232 * @info1: first profiling data set
233 * @info2: second profiling data set
235 * Returns non-zero if profiling data can be added, zero otherwise.
237 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
239 return (info1->stamp == info2->stamp);
243 * gcov_info_add - add up profiling data
244 * @dest: profiling data set to which data is added
245 * @source: profiling data set which is added
247 * Adds profiling counts of @source to @dest.
249 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
251 struct gcov_ctr_info *dci_ptr;
252 struct gcov_ctr_info *sci_ptr;
255 unsigned int val_idx;
257 for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) {
258 dci_ptr = dst->functions[fi_idx]->ctrs;
259 sci_ptr = src->functions[fi_idx]->ctrs;
261 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
262 if (!counter_active(src, ct_idx))
265 for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
266 dci_ptr->values[val_idx] +=
267 sci_ptr->values[val_idx];
276 * gcov_info_dup - duplicate profiling data set
277 * @info: profiling data set to duplicate
279 * Return newly allocated duplicate on success, %NULL on error.
281 struct gcov_info *gcov_info_dup(struct gcov_info *info)
283 struct gcov_info *dup;
284 struct gcov_ctr_info *dci_ptr; /* dst counter info */
285 struct gcov_ctr_info *sci_ptr; /* src counter info */
287 unsigned int fi_idx; /* function info idx */
288 unsigned int ct_idx; /* counter type idx */
289 size_t fi_size; /* function info size */
290 size_t cv_size; /* counter values size */
292 dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
297 dup->filename = NULL;
298 dup->functions = NULL;
300 dup->filename = kstrdup(info->filename, GFP_KERNEL);
304 dup->functions = kcalloc(info->n_functions,
305 sizeof(struct gcov_fn_info *), GFP_KERNEL);
309 active = num_counter_active(info);
310 fi_size = sizeof(struct gcov_fn_info);
311 fi_size += sizeof(struct gcov_ctr_info) * active;
313 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
314 dup->functions[fi_idx] = kzalloc(fi_size, GFP_KERNEL);
315 if (!dup->functions[fi_idx])
318 *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
320 sci_ptr = info->functions[fi_idx]->ctrs;
321 dci_ptr = dup->functions[fi_idx]->ctrs;
323 for (ct_idx = 0; ct_idx < active; ct_idx++) {
325 cv_size = sizeof(gcov_type) * sci_ptr->num;
327 dci_ptr->values = vmalloc(cv_size);
329 if (!dci_ptr->values)
332 dci_ptr->num = sci_ptr->num;
333 memcpy(dci_ptr->values, sci_ptr->values, cv_size);
347 * gcov_info_free - release memory for profiling data set duplicate
348 * @info: profiling data set duplicate to free
350 void gcov_info_free(struct gcov_info *info)
355 struct gcov_ctr_info *ci_ptr;
357 if (!info->functions)
360 active = num_counter_active(info);
362 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
363 if (!info->functions[fi_idx])
366 ci_ptr = info->functions[fi_idx]->ctrs;
368 for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
369 vfree(ci_ptr->values);
371 kfree(info->functions[fi_idx]);
375 kfree(info->functions);
376 kfree(info->filename);
380 #define ITER_STRIDE PAGE_SIZE
383 * struct gcov_iterator - specifies current file position in logical records
384 * @info: associated profiling data
385 * @buffer: buffer containing file data
386 * @size: size of buffer
387 * @pos: current position in file
389 struct gcov_iterator {
390 struct gcov_info *info;
397 * store_gcov_u32 - store 32 bit number in gcov format to buffer
398 * @buffer: target buffer or NULL
399 * @off: offset into the buffer
400 * @v: value to be stored
402 * Number format defined by gcc: numbers are recorded in the 32 bit
403 * unsigned binary form of the endianness of the machine generating the
404 * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
407 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
416 return sizeof(*data);
420 * store_gcov_u64 - store 64 bit number in gcov format to buffer
421 * @buffer: target buffer or NULL
422 * @off: offset into the buffer
423 * @v: value to be stored
425 * Number format defined by gcc: numbers are recorded in the 32 bit
426 * unsigned binary form of the endianness of the machine generating the
427 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
428 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
431 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
438 data[0] = (v & 0xffffffffUL);
442 return sizeof(*data) * 2;
446 * convert_to_gcda - convert profiling data set to gcda file format
447 * @buffer: the buffer to store file data or %NULL if no data should be stored
448 * @info: profiling data set to be converted
450 * Returns the number of bytes that were/would have been stored into the buffer.
452 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
454 struct gcov_fn_info *fi_ptr;
455 struct gcov_ctr_info *ci_ptr;
462 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
463 pos += store_gcov_u32(buffer, pos, info->version);
464 pos += store_gcov_u32(buffer, pos, info->stamp);
467 /* Use zero as checksum of the compilation unit. */
468 pos += store_gcov_u32(buffer, pos, 0);
471 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
472 fi_ptr = info->functions[fi_idx];
474 /* Function record. */
475 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
476 pos += store_gcov_u32(buffer, pos,
477 GCOV_TAG_FUNCTION_LENGTH * GCOV_UNIT_SIZE);
478 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
479 pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
480 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
482 ci_ptr = fi_ptr->ctrs;
484 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
485 if (!counter_active(info, ct_idx))
488 /* Counter record. */
489 pos += store_gcov_u32(buffer, pos,
490 GCOV_TAG_FOR_COUNTER(ct_idx));
491 pos += store_gcov_u32(buffer, pos,
492 ci_ptr->num * 2 * GCOV_UNIT_SIZE);
494 for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
495 pos += store_gcov_u64(buffer, pos,
496 ci_ptr->values[cv_idx]);
507 * gcov_iter_new - allocate and initialize profiling data iterator
508 * @info: profiling data set to be iterated
510 * Return file iterator on success, %NULL otherwise.
512 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
514 struct gcov_iterator *iter;
516 iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
521 /* Dry-run to get the actual buffer size. */
522 iter->size = convert_to_gcda(NULL, info);
523 iter->buffer = vmalloc(iter->size);
527 convert_to_gcda(iter->buffer, info);
538 * gcov_iter_get_info - return profiling data set for given file iterator
539 * @iter: file iterator
541 void gcov_iter_free(struct gcov_iterator *iter)
548 * gcov_iter_get_info - return profiling data set for given file iterator
549 * @iter: file iterator
551 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
557 * gcov_iter_start - reset file iterator to starting position
558 * @iter: file iterator
560 void gcov_iter_start(struct gcov_iterator *iter)
566 * gcov_iter_next - advance file iterator to next logical record
567 * @iter: file iterator
569 * Return zero if new position is valid, non-zero if iterator has reached end.
571 int gcov_iter_next(struct gcov_iterator *iter)
573 if (iter->pos < iter->size)
574 iter->pos += ITER_STRIDE;
576 if (iter->pos >= iter->size)
583 * gcov_iter_write - write data for current pos to seq_file
584 * @iter: file iterator
585 * @seq: seq_file handle
587 * Return zero on success, non-zero otherwise.
589 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
593 if (iter->pos >= iter->size)
597 if (iter->pos + len > iter->size)
598 len = iter->size - iter->pos;
600 seq_write(seq, iter->buffer + iter->pos, len);