0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
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
0037
0038
0039
0040
0041
0042
0043 struct gcov_ctr_info {
0044 unsigned int num;
0045 gcov_type *values;
0046 };
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
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
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
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
0098
0099
0100 const char *gcov_info_filename(struct gcov_info *info)
0101 {
0102 return info->filename;
0103 }
0104
0105
0106
0107
0108
0109 unsigned int gcov_info_version(struct gcov_info *info)
0110 {
0111 return info->version;
0112 }
0113
0114
0115
0116
0117
0118
0119
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
0131
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
0141
0142
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
0154
0155
0156
0157
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
0165 const struct gcov_link gcov_link[] = {
0166 { OBJ_TREE, "gcno" },
0167 { 0, NULL},
0168 };
0169
0170
0171
0172
0173 static int counter_active(struct gcov_info *info, unsigned int type)
0174 {
0175 return info->merge[type] ? 1 : 0;
0176 }
0177
0178
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
0193
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
0217
0218
0219
0220
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
0229
0230
0231
0232
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
0262
0263
0264
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;
0270 struct gcov_ctr_info *sci_ptr;
0271 unsigned int active;
0272 unsigned int fi_idx;
0273 unsigned int ct_idx;
0274 size_t fi_size;
0275 size_t cv_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
0333
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
0367
0368
0369
0370
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
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
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
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 }