1 // SPDX-License-Identifier: GPL-2.0
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; version 2.
6 // This program is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // GNU General Public License for more details.
11 // You should have received a copy of the GNU General Public License
12 // along with this program; if not, write to the Free Software
13 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * This code provides functions to handle gcc's profiling data format
17 * introduced with gcc 4.7.
19 * This file is based heavily on gcc_3_4.c file.
21 * For a better understanding, refer to gcc source:
25 * Uses gcc-internal data definitions.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <gnu/gcov/gcov.h>
43 #define GCOV_COUNTERS 9
44 #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
45 #define GCOV_COUNTERS 10
46 #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
47 #define GCOV_COUNTERS 9
49 #define GCOV_COUNTERS 8
52 #define GCOV_TAG_FUNCTION_LENGTH 3
54 static struct gcov_info *gcov_info_head;
57 * struct gcov_ctr_info - information about counters for a single function
58 * @num: number of counter values for this type
59 * @values: array of counter values for this type
61 * This data is generated by gcc during compilation and doesn't change
62 * at run-time with the exception of the values array.
64 struct gcov_ctr_info {
70 * struct gcov_fn_info - profiling meta data per function
72 * @ident: unique ident of function
73 * @lineno_checksum: function lineo_checksum
74 * @cfg_checksum: function cfg checksum
75 * @ctrs: instrumented counters
77 * This data is generated by gcc during compilation and doesn't change
80 * Information about a single function. This uses the trailing array
81 * idiom. The number of counters is determined from the merge pointer
82 * array in gcov_info. The key is used to detect which of a set of
83 * comdat functions was selected -- it points to the gcov_info object
84 * of the object file containing the selected comdat function.
87 const struct gcov_info *key;
89 unsigned int lineno_checksum;
90 unsigned int cfg_checksum;
91 struct gcov_ctr_info ctrs[0];
95 * struct gcov_info - profiling data per object file
96 * @version: gcov version magic indicating the gcc version used for compilation
97 * @next: list head for a singly-linked list
98 * @stamp: uniquifying time stamp
99 * @filename: name of the associated gcov data file
100 * @merge: merge functions (null for unused counter type)
101 * @n_functions: number of instrumented functions
102 * @functions: pointer to pointers to function information
104 * This data is generated by gcc during compilation and doesn't change
105 * at run-time with the exception of the next pointer.
108 unsigned int version;
109 struct gcov_info *next;
111 const char *filename;
112 void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
113 unsigned int n_functions;
114 struct gcov_fn_info **functions;
118 * gcov_info_filename - return info filename
119 * @info: profiling data set
122 gcov_info_filename(struct gcov_info *info)
124 return (info->filename);
128 * gcov_info_version - return info version
129 * @info: profiling data set
132 gcov_info_version(struct gcov_info *info)
134 return (info->version);
138 * gcov_info_next - return next profiling data set
139 * @info: profiling data set
141 * Returns next gcov_info following @info or first gcov_info in the chain if
145 gcov_info_next(struct gcov_info *info)
148 return gcov_info_head;
154 * gcov_info_link - link/add profiling data set to the list
155 * @info: profiling data set
158 gcov_info_link(struct gcov_info *info)
160 info->next = gcov_info_head;
161 gcov_info_head = info;
165 * gcov_info_unlink - unlink/remove profiling data set from the list
166 * @prev: previous profiling data set
167 * @info: profiling data set
170 gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
173 prev->next = info->next;
175 gcov_info_head = info->next;
178 /* Symbolic links to be created for each profiling data file. */
179 const struct gcov_link gcov_link[] = {
180 { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
185 * Determine whether a counter is active. Doesn't change at run-time.
188 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. */
195 num_counter_active(struct gcov_info *info)
198 unsigned int result = 0;
200 for (i = 0; i < GCOV_COUNTERS; i++) {
201 if (counter_active(info, i))
208 * gcov_info_reset - reset profiling data to zero
209 * @info: profiling data set
212 gcov_info_reset(struct gcov_info *info)
214 struct gcov_ctr_info *ci_ptr;
218 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
219 ci_ptr = info->functions[fi_idx]->ctrs;
221 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
222 if (!counter_active(info, ct_idx))
225 memset(ci_ptr->values, 0,
226 sizeof(gcov_type) * ci_ptr->num);
233 * gcov_info_is_compatible - check if profiling data can be added
234 * @info1: first profiling data set
235 * @info2: second profiling data set
237 * Returns non-zero if profiling data can be added, zero otherwise.
240 gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
242 return (info1->stamp == info2->stamp);
246 * gcov_info_add - add up profiling data
247 * @dest: profiling data set to which data is added
248 * @source: profiling data set which is added
250 * Adds profiling counts of @source to @dest.
253 gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
255 struct gcov_ctr_info *dci_ptr;
256 struct gcov_ctr_info *sci_ptr;
259 unsigned int val_idx;
261 for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) {
262 dci_ptr = dst->functions[fi_idx]->ctrs;
263 sci_ptr = src->functions[fi_idx]->ctrs;
265 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
266 if (!counter_active(src, ct_idx))
269 for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
270 dci_ptr->values[val_idx] +=
271 sci_ptr->values[val_idx];
280 * gcov_info_dup - duplicate profiling data set
281 * @info: profiling data set to duplicate
283 * Return newly allocated duplicate on success, %NULL on error.
286 gcov_info_dup(struct gcov_info *info)
288 struct gcov_info *dup;
289 struct gcov_ctr_info *dci_ptr; /* dst counter info */
290 struct gcov_ctr_info *sci_ptr; /* src counter info */
292 unsigned int fi_idx; /* function info idx */
293 unsigned int ct_idx; /* counter type idx */
294 size_t fi_size; /* function info size */
295 size_t cv_size; /* counter values size */
297 if ((dup = malloc(sizeof(*dup), M_GCOV, M_NOWAIT|M_ZERO)) == NULL)
299 memcpy(dup, info, sizeof(*dup));
302 dup->filename = NULL;
303 dup->functions = NULL;
305 dup->filename = strdup_flags(info->filename, M_GCOV, M_NOWAIT);
306 if (dup->filename == NULL)
309 dup->functions = malloc(info->n_functions * sizeof(struct gcov_fn_info *), M_GCOV, M_NOWAIT|M_ZERO);
310 if (dup->functions == NULL)
312 active = num_counter_active(info);
313 fi_size = sizeof(struct gcov_fn_info);
314 fi_size += sizeof(struct gcov_ctr_info) * active;
316 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
317 dup->functions[fi_idx] = malloc(fi_size, M_GCOV, M_NOWAIT|M_ZERO);
318 if (!dup->functions[fi_idx])
321 *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
323 sci_ptr = info->functions[fi_idx]->ctrs;
324 dci_ptr = dup->functions[fi_idx]->ctrs;
326 for (ct_idx = 0; ct_idx < active; ct_idx++) {
328 cv_size = sizeof(gcov_type) * sci_ptr->num;
330 dci_ptr->values = malloc(cv_size, M_GCOV, M_NOWAIT);
332 if (!dci_ptr->values)
335 dci_ptr->num = sci_ptr->num;
336 memcpy(dci_ptr->values, sci_ptr->values, cv_size);
350 * gcov_info_free - release memory for profiling data set duplicate
351 * @info: profiling data set duplicate to free
354 gcov_info_free(struct gcov_info *info)
359 struct gcov_ctr_info *ci_ptr;
361 if (!info->functions)
364 active = num_counter_active(info);
366 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
367 if (!info->functions[fi_idx])
370 ci_ptr = info->functions[fi_idx]->ctrs;
372 for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
373 free(ci_ptr->values, M_GCOV);
375 free(info->functions[fi_idx], M_GCOV);
379 free(info->functions, M_GCOV);
380 free(__DECONST(char *, info->filename), M_GCOV);
384 #define ITER_STRIDE PAGE_SIZE
387 * struct gcov_iterator - specifies current file position in logical records
388 * @info: associated profiling data
389 * @buffer: buffer containing file data
390 * @size: size of buffer
391 * @pos: current position in file
393 struct gcov_iterator {
394 struct gcov_info *info;
401 * store_gcov_uint32 - store 32 bit number in gcov format to buffer
402 * @buffer: target buffer or NULL
403 * @off: offset into the buffer
404 * @v: value to be stored
406 * Number format defined by gcc: numbers are recorded in the 32 bit
407 * unsigned binary form of the endianness of the machine generating the
408 * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
412 store_gcov_uint32(void *buffer, size_t off, uint32_t v)
417 data = (void*)((caddr_t)buffer + off);
421 return sizeof(*data);
425 * store_gcov_uint64 - store 64 bit number in gcov format to buffer
426 * @buffer: target buffer or NULL
427 * @off: offset into the buffer
428 * @v: value to be stored
430 * Number format defined by gcc: numbers are recorded in the 32 bit
431 * unsigned binary form of the endianness of the machine generating the
432 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
433 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
438 store_gcov_uint64(void *buffer, size_t off, uint64_t v)
443 data = (void*)((caddr_t)buffer + off);
445 data[0] = (v & 0xffffffffUL);
449 return sizeof(*data) * 2;
453 * convert_to_gcda - convert profiling data set to gcda file format
454 * @buffer: the buffer to store file data or %NULL if no data should be stored
455 * @info: profiling data set to be converted
457 * Returns the number of bytes that were/would have been stored into the buffer.
460 convert_to_gcda(char *buffer, struct gcov_info *info)
462 struct gcov_fn_info *fi_ptr;
463 struct gcov_ctr_info *ci_ptr;
470 pos += store_gcov_uint32(buffer, pos, GCOV_DATA_MAGIC);
471 pos += store_gcov_uint32(buffer, pos, info->version);
472 pos += store_gcov_uint32(buffer, pos, info->stamp);
474 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
475 fi_ptr = info->functions[fi_idx];
477 /* Function record. */
478 pos += store_gcov_uint32(buffer, pos, GCOV_TAG_FUNCTION);
479 pos += store_gcov_uint32(buffer, pos, GCOV_TAG_FUNCTION_LENGTH);
480 pos += store_gcov_uint32(buffer, pos, fi_ptr->ident);
481 pos += store_gcov_uint32(buffer, pos, fi_ptr->lineno_checksum);
482 pos += store_gcov_uint32(buffer, pos, fi_ptr->cfg_checksum);
484 ci_ptr = fi_ptr->ctrs;
486 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
487 if (!counter_active(info, ct_idx))
490 /* Counter record. */
491 pos += store_gcov_uint32(buffer, pos,
492 GCOV_TAG_FOR_COUNTER(ct_idx));
493 pos += store_gcov_uint32(buffer, pos, ci_ptr->num * 2);
495 for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
496 pos += store_gcov_uint64(buffer, pos,
497 ci_ptr->values[cv_idx]);
508 * gcov_iter_new - allocate and initialize profiling data iterator
509 * @info: profiling data set to be iterated
511 * Return file iterator on success, %NULL otherwise.
513 struct gcov_iterator *
514 gcov_iter_new(struct gcov_info *info)
516 struct gcov_iterator *iter;
518 iter = malloc(sizeof(struct gcov_iterator), M_GCOV, M_NOWAIT|M_ZERO);
523 /* Dry-run to get the actual buffer size. */
524 iter->size = convert_to_gcda(NULL, info);
525 iter->buffer = malloc(iter->size, M_GCOV, M_NOWAIT);
529 convert_to_gcda(iter->buffer, info);
540 * gcov_iter_get_info - return profiling data set for given file iterator
541 * @iter: file iterator
544 gcov_iter_free(struct gcov_iterator *iter)
546 free(iter->buffer, M_GCOV);
551 * gcov_iter_get_info - return profiling data set for given file iterator
552 * @iter: file iterator
555 gcov_iter_get_info(struct gcov_iterator *iter)
561 * gcov_iter_start - reset file iterator to starting position
562 * @iter: file iterator
565 gcov_iter_start(struct gcov_iterator *iter)
571 * gcov_iter_next - advance file iterator to next logical record
572 * @iter: file iterator
574 * Return zero if new position is valid, non-zero if iterator has reached end.
577 gcov_iter_next(struct gcov_iterator *iter)
579 if (iter->pos < iter->size)
580 iter->pos += ITER_STRIDE;
582 if (iter->pos >= iter->size)
589 * gcov_iter_write - write data for current pos to seq_file
590 * @iter: file iterator
591 * @seq: seq_file handle
593 * Return zero on success, non-zero otherwise.
596 gcov_iter_write(struct gcov_iterator *iter, struct sbuf *sbuf)
600 if (iter->pos >= iter->size)
604 if (iter->pos + len > iter->size)
605 len = iter->size - iter->pos;
607 sbuf_bcat(sbuf, iter->buffer + iter->pos, len);