Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  This code provides functions to handle gcc's profiling data format
0004  *  introduced with gcc 4.7.
0005  *
0006  *  This file is based heavily on gcc_3_4.c file.
0007  *
0008  *  For a better understanding, refer to gcc source:
0009  *  gcc/gcov-io.h
0010  *  libgcc/libgcov.c
0011  *
0012  *  Uses gcc-internal data definitions.
0013  */
0014 
0015 #include <linux/errno.h>
0016 #include <linux/slab.h>
0017 #include <linux/string.h>
0018 #include <linux/mm.h>
0019 #include "gcov.h"
0020 
0021 #if (__GNUC__ >= 10)
0022 #define GCOV_COUNTERS           8
0023 #elif (__GNUC__ >= 7)
0024 #define GCOV_COUNTERS           9
0025 #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
0026 #define GCOV_COUNTERS           10
0027 #else
0028 #define GCOV_COUNTERS           9
0029 #endif
0030 
0031 #define GCOV_TAG_FUNCTION_LENGTH    3
0032 
0033 static struct gcov_info *gcov_info_head;
0034 
0035 /**
0036  * struct gcov_ctr_info - information about counters for a single function
0037  * @num: number of counter values for this type
0038  * @values: array of counter values for this type
0039  *
0040  * This data is generated by gcc during compilation and doesn't change
0041  * at run-time with the exception of the values array.
0042  */
0043 struct gcov_ctr_info {
0044     unsigned int num;
0045     gcov_type *values;
0046 };
0047 
0048 /**
0049  * struct gcov_fn_info - profiling meta data per function
0050  * @key: comdat key
0051  * @ident: unique ident of function
0052  * @lineno_checksum: function lineo_checksum
0053  * @cfg_checksum: function cfg checksum
0054  * @ctrs: instrumented counters
0055  *
0056  * This data is generated by gcc during compilation and doesn't change
0057  * at run-time.
0058  *
0059  * Information about a single function.  This uses the trailing array
0060  * idiom. The number of counters is determined from the merge pointer
0061  * array in gcov_info.  The key is used to detect which of a set of
0062  * comdat functions was selected -- it points to the gcov_info object
0063  * of the object file containing the selected comdat function.
0064  */
0065 struct gcov_fn_info {
0066     const struct gcov_info *key;
0067     unsigned int ident;
0068     unsigned int lineno_checksum;
0069     unsigned int cfg_checksum;
0070     struct gcov_ctr_info ctrs[];
0071 };
0072 
0073 /**
0074  * struct gcov_info - profiling data per object file
0075  * @version: gcov version magic indicating the gcc version used for compilation
0076  * @next: list head for a singly-linked list
0077  * @stamp: uniquifying time stamp
0078  * @filename: name of the associated gcov data file
0079  * @merge: merge functions (null for unused counter type)
0080  * @n_functions: number of instrumented functions
0081  * @functions: pointer to pointers to function information
0082  *
0083  * This data is generated by gcc during compilation and doesn't change
0084  * at run-time with the exception of the next pointer.
0085  */
0086 struct gcov_info {
0087     unsigned int version;
0088     struct gcov_info *next;
0089     unsigned int stamp;
0090     const char *filename;
0091     void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
0092     unsigned int n_functions;
0093     struct gcov_fn_info **functions;
0094 };
0095 
0096 /**
0097  * gcov_info_filename - return info filename
0098  * @info: profiling data set
0099  */
0100 const char *gcov_info_filename(struct gcov_info *info)
0101 {
0102     return info->filename;
0103 }
0104 
0105 /**
0106  * gcov_info_version - return info version
0107  * @info: profiling data set
0108  */
0109 unsigned int gcov_info_version(struct gcov_info *info)
0110 {
0111     return info->version;
0112 }
0113 
0114 /**
0115  * gcov_info_next - return next profiling data set
0116  * @info: profiling data set
0117  *
0118  * Returns next gcov_info following @info or first gcov_info in the chain if
0119  * @info is %NULL.
0120  */
0121 struct gcov_info *gcov_info_next(struct gcov_info *info)
0122 {
0123     if (!info)
0124         return gcov_info_head;
0125 
0126     return info->next;
0127 }
0128 
0129 /**
0130  * gcov_info_link - link/add profiling data set to the list
0131  * @info: profiling data set
0132  */
0133 void gcov_info_link(struct gcov_info *info)
0134 {
0135     info->next = gcov_info_head;
0136     gcov_info_head = info;
0137 }
0138 
0139 /**
0140  * gcov_info_unlink - unlink/remove profiling data set from the list
0141  * @prev: previous profiling data set
0142  * @info: profiling data set
0143  */
0144 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
0145 {
0146     if (prev)
0147         prev->next = info->next;
0148     else
0149         gcov_info_head = info->next;
0150 }
0151 
0152 /**
0153  * gcov_info_within_module - check if a profiling data set belongs to a module
0154  * @info: profiling data set
0155  * @mod: module
0156  *
0157  * Returns true if profiling data belongs module, false otherwise.
0158  */
0159 bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
0160 {
0161     return within_module((unsigned long)info, mod);
0162 }
0163 
0164 /* Symbolic links to be created for each profiling data file. */
0165 const struct gcov_link gcov_link[] = {
0166     { OBJ_TREE, "gcno" },   /* Link to .gcno file in $(objtree). */
0167     { 0, NULL},
0168 };
0169 
0170 /*
0171  * Determine whether a counter is active. Doesn't change at run-time.
0172  */
0173 static int counter_active(struct gcov_info *info, unsigned int type)
0174 {
0175     return info->merge[type] ? 1 : 0;
0176 }
0177 
0178 /* Determine number of active counters. Based on gcc magic. */
0179 static unsigned int num_counter_active(struct gcov_info *info)
0180 {
0181     unsigned int i;
0182     unsigned int result = 0;
0183 
0184     for (i = 0; i < GCOV_COUNTERS; i++) {
0185         if (counter_active(info, i))
0186             result++;
0187     }
0188     return result;
0189 }
0190 
0191 /**
0192  * gcov_info_reset - reset profiling data to zero
0193  * @info: profiling data set
0194  */
0195 void gcov_info_reset(struct gcov_info *info)
0196 {
0197     struct gcov_ctr_info *ci_ptr;
0198     unsigned int fi_idx;
0199     unsigned int ct_idx;
0200 
0201     for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
0202         ci_ptr = info->functions[fi_idx]->ctrs;
0203 
0204         for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
0205             if (!counter_active(info, ct_idx))
0206                 continue;
0207 
0208             memset(ci_ptr->values, 0,
0209                     sizeof(gcov_type) * ci_ptr->num);
0210             ci_ptr++;
0211         }
0212     }
0213 }
0214 
0215 /**
0216  * gcov_info_is_compatible - check if profiling data can be added
0217  * @info1: first profiling data set
0218  * @info2: second profiling data set
0219  *
0220  * Returns non-zero if profiling data can be added, zero otherwise.
0221  */
0222 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
0223 {
0224     return (info1->stamp == info2->stamp);
0225 }
0226 
0227 /**
0228  * gcov_info_add - add up profiling data
0229  * @dst: profiling data set to which data is added
0230  * @src: profiling data set which is added
0231  *
0232  * Adds profiling counts of @src to @dst.
0233  */
0234 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
0235 {
0236     struct gcov_ctr_info *dci_ptr;
0237     struct gcov_ctr_info *sci_ptr;
0238     unsigned int fi_idx;
0239     unsigned int ct_idx;
0240     unsigned int val_idx;
0241 
0242     for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) {
0243         dci_ptr = dst->functions[fi_idx]->ctrs;
0244         sci_ptr = src->functions[fi_idx]->ctrs;
0245 
0246         for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
0247             if (!counter_active(src, ct_idx))
0248                 continue;
0249 
0250             for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
0251                 dci_ptr->values[val_idx] +=
0252                     sci_ptr->values[val_idx];
0253 
0254             dci_ptr++;
0255             sci_ptr++;
0256         }
0257     }
0258 }
0259 
0260 /**
0261  * gcov_info_dup - duplicate profiling data set
0262  * @info: profiling data set to duplicate
0263  *
0264  * Return newly allocated duplicate on success, %NULL on error.
0265  */
0266 struct gcov_info *gcov_info_dup(struct gcov_info *info)
0267 {
0268     struct gcov_info *dup;
0269     struct gcov_ctr_info *dci_ptr; /* dst counter info */
0270     struct gcov_ctr_info *sci_ptr; /* src counter info */
0271     unsigned int active;
0272     unsigned int fi_idx; /* function info idx */
0273     unsigned int ct_idx; /* counter type idx */
0274     size_t fi_size; /* function info size */
0275     size_t cv_size; /* counter values size */
0276 
0277     dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
0278     if (!dup)
0279         return NULL;
0280 
0281     dup->next = NULL;
0282     dup->filename = NULL;
0283     dup->functions = NULL;
0284 
0285     dup->filename = kstrdup(info->filename, GFP_KERNEL);
0286     if (!dup->filename)
0287         goto err_free;
0288 
0289     dup->functions = kcalloc(info->n_functions,
0290                  sizeof(struct gcov_fn_info *), GFP_KERNEL);
0291     if (!dup->functions)
0292         goto err_free;
0293 
0294     active = num_counter_active(info);
0295     fi_size = sizeof(struct gcov_fn_info);
0296     fi_size += sizeof(struct gcov_ctr_info) * active;
0297 
0298     for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
0299         dup->functions[fi_idx] = kzalloc(fi_size, GFP_KERNEL);
0300         if (!dup->functions[fi_idx])
0301             goto err_free;
0302 
0303         *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
0304 
0305         sci_ptr = info->functions[fi_idx]->ctrs;
0306         dci_ptr = dup->functions[fi_idx]->ctrs;
0307 
0308         for (ct_idx = 0; ct_idx < active; ct_idx++) {
0309 
0310             cv_size = sizeof(gcov_type) * sci_ptr->num;
0311 
0312             dci_ptr->values = kvmalloc(cv_size, GFP_KERNEL);
0313 
0314             if (!dci_ptr->values)
0315                 goto err_free;
0316 
0317             dci_ptr->num = sci_ptr->num;
0318             memcpy(dci_ptr->values, sci_ptr->values, cv_size);
0319 
0320             sci_ptr++;
0321             dci_ptr++;
0322         }
0323     }
0324 
0325     return dup;
0326 err_free:
0327     gcov_info_free(dup);
0328     return NULL;
0329 }
0330 
0331 /**
0332  * gcov_info_free - release memory for profiling data set duplicate
0333  * @info: profiling data set duplicate to free
0334  */
0335 void gcov_info_free(struct gcov_info *info)
0336 {
0337     unsigned int active;
0338     unsigned int fi_idx;
0339     unsigned int ct_idx;
0340     struct gcov_ctr_info *ci_ptr;
0341 
0342     if (!info->functions)
0343         goto free_info;
0344 
0345     active = num_counter_active(info);
0346 
0347     for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
0348         if (!info->functions[fi_idx])
0349             continue;
0350 
0351         ci_ptr = info->functions[fi_idx]->ctrs;
0352 
0353         for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
0354             kvfree(ci_ptr->values);
0355 
0356         kfree(info->functions[fi_idx]);
0357     }
0358 
0359 free_info:
0360     kfree(info->functions);
0361     kfree(info->filename);
0362     kfree(info);
0363 }
0364 
0365 /**
0366  * convert_to_gcda - convert profiling data set to gcda file format
0367  * @buffer: the buffer to store file data or %NULL if no data should be stored
0368  * @info: profiling data set to be converted
0369  *
0370  * Returns the number of bytes that were/would have been stored into the buffer.
0371  */
0372 size_t convert_to_gcda(char *buffer, struct gcov_info *info)
0373 {
0374     struct gcov_fn_info *fi_ptr;
0375     struct gcov_ctr_info *ci_ptr;
0376     unsigned int fi_idx;
0377     unsigned int ct_idx;
0378     unsigned int cv_idx;
0379     size_t pos = 0;
0380 
0381     /* File header. */
0382     pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
0383     pos += store_gcov_u32(buffer, pos, info->version);
0384     pos += store_gcov_u32(buffer, pos, info->stamp);
0385 
0386     for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
0387         fi_ptr = info->functions[fi_idx];
0388 
0389         /* Function record. */
0390         pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
0391         pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION_LENGTH);
0392         pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
0393         pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
0394         pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
0395 
0396         ci_ptr = fi_ptr->ctrs;
0397 
0398         for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
0399             if (!counter_active(info, ct_idx))
0400                 continue;
0401 
0402             /* Counter record. */
0403             pos += store_gcov_u32(buffer, pos,
0404                           GCOV_TAG_FOR_COUNTER(ct_idx));
0405             pos += store_gcov_u32(buffer, pos, ci_ptr->num * 2);
0406 
0407             for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
0408                 pos += store_gcov_u64(buffer, pos,
0409                               ci_ptr->values[cv_idx]);
0410             }
0411 
0412             ci_ptr++;
0413         }
0414     }
0415 
0416     return pos;
0417 }