1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * This code provides functions to handle gcc's profiling data format 4 * introduced with gcc 4.7. 5 * 6 * This file is based heavily on gcc_3_4.c file. 7 * 8 * For a better understanding, refer to gcc source: 9 * gcc/gcov-io.h 10 * libgcc/libgcov.c 11 * 12 * Uses gcc-internal data definitions. 13 */ 14 15 16 #include <sys/cdefs.h> 17 __FBSDID("$FreeBSD$"); 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/types.h> 22 #include <sys/systm.h> 23 #include <sys/sbuf.h> 24 #include <sys/malloc.h> 25 #include <sys/module.h> 26 #include <gnu/gcov/gcov.h> 27 28 29 #if (__GNUC__ >= 7) 30 #define GCOV_COUNTERS 9 31 #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1) 32 #define GCOV_COUNTERS 10 33 #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9 34 #define GCOV_COUNTERS 9 35 #else 36 #define GCOV_COUNTERS 8 37 #endif 38 39 #define GCOV_TAG_FUNCTION_LENGTH 3 40 41 static struct gcov_info *gcov_info_head; 42 43 /** 44 * struct gcov_ctr_info - information about counters for a single function 45 * @num: number of counter values for this type 46 * @values: array of counter values for this type 47 * 48 * This data is generated by gcc during compilation and doesn't change 49 * at run-time with the exception of the values array. 50 */ 51 struct gcov_ctr_info { 52 unsigned int num; 53 gcov_type *values; 54 }; 55 56 /** 57 * struct gcov_fn_info - profiling meta data per function 58 * @key: comdat key 59 * @ident: unique ident of function 60 * @lineno_checksum: function lineo_checksum 61 * @cfg_checksum: function cfg checksum 62 * @ctrs: instrumented counters 63 * 64 * This data is generated by gcc during compilation and doesn't change 65 * at run-time. 66 * 67 * Information about a single function. This uses the trailing array 68 * idiom. The number of counters is determined from the merge pointer 69 * array in gcov_info. The key is used to detect which of a set of 70 * comdat functions was selected -- it points to the gcov_info object 71 * of the object file containing the selected comdat function. 72 */ 73 struct gcov_fn_info { 74 const struct gcov_info *key; 75 unsigned int ident; 76 unsigned int lineno_checksum; 77 unsigned int cfg_checksum; 78 struct gcov_ctr_info ctrs[0]; 79 }; 80 81 /** 82 * struct gcov_info - profiling data per object file 83 * @version: gcov version magic indicating the gcc version used for compilation 84 * @next: list head for a singly-linked list 85 * @stamp: uniquifying time stamp 86 * @filename: name of the associated gcov data file 87 * @merge: merge functions (null for unused counter type) 88 * @n_functions: number of instrumented functions 89 * @functions: pointer to pointers to function information 90 * 91 * This data is generated by gcc during compilation and doesn't change 92 * at run-time with the exception of the next pointer. 93 */ 94 struct gcov_info { 95 unsigned int version; 96 struct gcov_info *next; 97 unsigned int stamp; 98 const char *filename; 99 void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int); 100 unsigned int n_functions; 101 struct gcov_fn_info **functions; 102 }; 103 104 /** 105 * gcov_info_filename - return info filename 106 * @info: profiling data set 107 */ 108 const char * 109 gcov_info_filename(struct gcov_info *info) 110 { 111 return (info->filename); 112 } 113 114 /** 115 * gcov_info_version - return info version 116 * @info: profiling data set 117 */ 118 unsigned int 119 gcov_info_version(struct gcov_info *info) 120 { 121 return (info->version); 122 } 123 124 /** 125 * gcov_info_next - return next profiling data set 126 * @info: profiling data set 127 * 128 * Returns next gcov_info following @info or first gcov_info in the chain if 129 * @info is %NULL. 130 */ 131 struct gcov_info * 132 gcov_info_next(struct gcov_info *info) 133 { 134 if (!info) 135 return gcov_info_head; 136 137 return (info->next); 138 } 139 140 /** 141 * gcov_info_link - link/add profiling data set to the list 142 * @info: profiling data set 143 */ 144 void 145 gcov_info_link(struct gcov_info *info) 146 { 147 info->next = gcov_info_head; 148 gcov_info_head = info; 149 } 150 151 /** 152 * gcov_info_unlink - unlink/remove profiling data set from the list 153 * @prev: previous profiling data set 154 * @info: profiling data set 155 */ 156 void 157 gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info) 158 { 159 if (prev) 160 prev->next = info->next; 161 else 162 gcov_info_head = info->next; 163 } 164 165 /* Symbolic links to be created for each profiling data file. */ 166 const struct gcov_link gcov_link[] = { 167 { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */ 168 { 0, NULL}, 169 }; 170 171 /* 172 * Determine whether a counter is active. Doesn't change at run-time. 173 */ 174 static int 175 counter_active(struct gcov_info *info, unsigned int type) 176 { 177 return (info->merge[type] ? 1 : 0); 178 } 179 180 /* Determine number of active counters. Based on gcc magic. */ 181 static unsigned int 182 num_counter_active(struct gcov_info *info) 183 { 184 unsigned int i; 185 unsigned int result = 0; 186 187 for (i = 0; i < GCOV_COUNTERS; i++) { 188 if (counter_active(info, i)) 189 result++; 190 } 191 return (result); 192 } 193 194 /** 195 * gcov_info_reset - reset profiling data to zero 196 * @info: profiling data set 197 */ 198 void 199 gcov_info_reset(struct gcov_info *info) 200 { 201 struct gcov_ctr_info *ci_ptr; 202 unsigned int fi_idx; 203 unsigned int ct_idx; 204 205 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) { 206 ci_ptr = info->functions[fi_idx]->ctrs; 207 208 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) { 209 if (!counter_active(info, ct_idx)) 210 continue; 211 212 memset(ci_ptr->values, 0, 213 sizeof(gcov_type) * ci_ptr->num); 214 ci_ptr++; 215 } 216 } 217 } 218 219 /** 220 * gcov_info_is_compatible - check if profiling data can be added 221 * @info1: first profiling data set 222 * @info2: second profiling data set 223 * 224 * Returns non-zero if profiling data can be added, zero otherwise. 225 */ 226 int 227 gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2) 228 { 229 return (info1->stamp == info2->stamp); 230 } 231 232 /** 233 * gcov_info_add - add up profiling data 234 * @dest: profiling data set to which data is added 235 * @source: profiling data set which is added 236 * 237 * Adds profiling counts of @source to @dest. 238 */ 239 void 240 gcov_info_add(struct gcov_info *dst, struct gcov_info *src) 241 { 242 struct gcov_ctr_info *dci_ptr; 243 struct gcov_ctr_info *sci_ptr; 244 unsigned int fi_idx; 245 unsigned int ct_idx; 246 unsigned int val_idx; 247 248 for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) { 249 dci_ptr = dst->functions[fi_idx]->ctrs; 250 sci_ptr = src->functions[fi_idx]->ctrs; 251 252 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) { 253 if (!counter_active(src, ct_idx)) 254 continue; 255 256 for (val_idx = 0; val_idx < sci_ptr->num; val_idx++) 257 dci_ptr->values[val_idx] += 258 sci_ptr->values[val_idx]; 259 260 dci_ptr++; 261 sci_ptr++; 262 } 263 } 264 } 265 266 /** 267 * gcov_info_dup - duplicate profiling data set 268 * @info: profiling data set to duplicate 269 * 270 * Return newly allocated duplicate on success, %NULL on error. 271 */ 272 struct gcov_info * 273 gcov_info_dup(struct gcov_info *info) 274 { 275 struct gcov_info *dup; 276 struct gcov_ctr_info *dci_ptr; /* dst counter info */ 277 struct gcov_ctr_info *sci_ptr; /* src counter info */ 278 unsigned int active; 279 unsigned int fi_idx; /* function info idx */ 280 unsigned int ct_idx; /* counter type idx */ 281 size_t fi_size; /* function info size */ 282 size_t cv_size; /* counter values size */ 283 284 if ((dup = malloc(sizeof(*dup), M_GCOV, M_NOWAIT|M_ZERO)) == NULL) 285 return (NULL); 286 memcpy(dup, info, sizeof(*dup)); 287 288 dup->next = NULL; 289 dup->filename = NULL; 290 dup->functions = NULL; 291 292 dup->filename = strdup_flags(info->filename, M_GCOV, M_NOWAIT); 293 if (dup->filename == NULL) 294 goto err_free; 295 296 dup->functions = malloc(info->n_functions * sizeof(struct gcov_fn_info *), M_GCOV, M_NOWAIT|M_ZERO); 297 if (dup->functions == NULL) 298 goto err_free; 299 active = num_counter_active(info); 300 fi_size = sizeof(struct gcov_fn_info); 301 fi_size += sizeof(struct gcov_ctr_info) * active; 302 303 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) { 304 dup->functions[fi_idx] = malloc(fi_size, M_GCOV, M_NOWAIT|M_ZERO); 305 if (!dup->functions[fi_idx]) 306 goto err_free; 307 308 *(dup->functions[fi_idx]) = *(info->functions[fi_idx]); 309 310 sci_ptr = info->functions[fi_idx]->ctrs; 311 dci_ptr = dup->functions[fi_idx]->ctrs; 312 313 for (ct_idx = 0; ct_idx < active; ct_idx++) { 314 315 cv_size = sizeof(gcov_type) * sci_ptr->num; 316 317 dci_ptr->values = malloc(cv_size, M_GCOV, M_NOWAIT); 318 319 if (!dci_ptr->values) 320 goto err_free; 321 322 dci_ptr->num = sci_ptr->num; 323 memcpy(dci_ptr->values, sci_ptr->values, cv_size); 324 325 sci_ptr++; 326 dci_ptr++; 327 } 328 } 329 330 return (dup); 331 err_free: 332 gcov_info_free(dup); 333 return (NULL); 334 } 335 336 /** 337 * gcov_info_free - release memory for profiling data set duplicate 338 * @info: profiling data set duplicate to free 339 */ 340 void 341 gcov_info_free(struct gcov_info *info) 342 { 343 unsigned int active; 344 unsigned int fi_idx; 345 unsigned int ct_idx; 346 struct gcov_ctr_info *ci_ptr; 347 348 if (!info->functions) 349 goto free_info; 350 351 active = num_counter_active(info); 352 353 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) { 354 if (!info->functions[fi_idx]) 355 continue; 356 357 ci_ptr = info->functions[fi_idx]->ctrs; 358 359 for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++) 360 free(ci_ptr->values, M_GCOV); 361 362 free(info->functions[fi_idx], M_GCOV); 363 } 364 365 free_info: 366 free(info->functions, M_GCOV); 367 free(__DECONST(char *, info->filename), M_GCOV); 368 free(info, M_GCOV); 369 } 370 371 #define ITER_STRIDE PAGE_SIZE 372 373 /** 374 * struct gcov_iterator - specifies current file position in logical records 375 * @info: associated profiling data 376 * @buffer: buffer containing file data 377 * @size: size of buffer 378 * @pos: current position in file 379 */ 380 struct gcov_iterator { 381 struct gcov_info *info; 382 caddr_t buffer; 383 size_t size; 384 off_t pos; 385 }; 386 387 /** 388 * store_gcov_uint32 - store 32 bit number in gcov format to buffer 389 * @buffer: target buffer or NULL 390 * @off: offset into the buffer 391 * @v: value to be stored 392 * 393 * Number format defined by gcc: numbers are recorded in the 32 bit 394 * unsigned binary form of the endianness of the machine generating the 395 * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't 396 * store anything. 397 */ 398 static size_t 399 store_gcov_uint32(void *buffer, size_t off, uint32_t v) 400 { 401 uint32_t *data; 402 403 if (buffer) { 404 data = (void*)((caddr_t)buffer + off); 405 *data = v; 406 } 407 408 return sizeof(*data); 409 } 410 411 /** 412 * store_gcov_uint64 - store 64 bit number in gcov format to buffer 413 * @buffer: target buffer or NULL 414 * @off: offset into the buffer 415 * @v: value to be stored 416 * 417 * Number format defined by gcc: numbers are recorded in the 32 bit 418 * unsigned binary form of the endianness of the machine generating the 419 * file. 64 bit numbers are stored as two 32 bit numbers, the low part 420 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store 421 * anything. 422 */ 423 424 static size_t 425 store_gcov_uint64(void *buffer, size_t off, uint64_t v) 426 { 427 uint32_t *data; 428 429 if (buffer) { 430 data = (void*)((caddr_t)buffer + off); 431 432 data[0] = (v & 0xffffffffUL); 433 data[1] = (v >> 32); 434 } 435 436 return sizeof(*data) * 2; 437 } 438 439 /** 440 * convert_to_gcda - convert profiling data set to gcda file format 441 * @buffer: the buffer to store file data or %NULL if no data should be stored 442 * @info: profiling data set to be converted 443 * 444 * Returns the number of bytes that were/would have been stored into the buffer. 445 */ 446 static size_t 447 convert_to_gcda(char *buffer, struct gcov_info *info) 448 { 449 struct gcov_fn_info *fi_ptr; 450 struct gcov_ctr_info *ci_ptr; 451 unsigned int fi_idx; 452 unsigned int ct_idx; 453 unsigned int cv_idx; 454 size_t pos = 0; 455 456 /* File header. */ 457 pos += store_gcov_uint32(buffer, pos, GCOV_DATA_MAGIC); 458 pos += store_gcov_uint32(buffer, pos, info->version); 459 pos += store_gcov_uint32(buffer, pos, info->stamp); 460 461 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) { 462 fi_ptr = info->functions[fi_idx]; 463 464 /* Function record. */ 465 pos += store_gcov_uint32(buffer, pos, GCOV_TAG_FUNCTION); 466 pos += store_gcov_uint32(buffer, pos, GCOV_TAG_FUNCTION_LENGTH); 467 pos += store_gcov_uint32(buffer, pos, fi_ptr->ident); 468 pos += store_gcov_uint32(buffer, pos, fi_ptr->lineno_checksum); 469 pos += store_gcov_uint32(buffer, pos, fi_ptr->cfg_checksum); 470 471 ci_ptr = fi_ptr->ctrs; 472 473 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) { 474 if (!counter_active(info, ct_idx)) 475 continue; 476 477 /* Counter record. */ 478 pos += store_gcov_uint32(buffer, pos, 479 GCOV_TAG_FOR_COUNTER(ct_idx)); 480 pos += store_gcov_uint32(buffer, pos, ci_ptr->num * 2); 481 482 for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) { 483 pos += store_gcov_uint64(buffer, pos, 484 ci_ptr->values[cv_idx]); 485 } 486 487 ci_ptr++; 488 } 489 } 490 491 return (pos); 492 } 493 494 /** 495 * gcov_iter_new - allocate and initialize profiling data iterator 496 * @info: profiling data set to be iterated 497 * 498 * Return file iterator on success, %NULL otherwise. 499 */ 500 struct gcov_iterator * 501 gcov_iter_new(struct gcov_info *info) 502 { 503 struct gcov_iterator *iter; 504 505 iter = malloc(sizeof(struct gcov_iterator), M_GCOV, M_NOWAIT|M_ZERO); 506 if (iter == NULL) 507 goto err_free; 508 509 iter->info = info; 510 /* Dry-run to get the actual buffer size. */ 511 iter->size = convert_to_gcda(NULL, info); 512 iter->buffer = malloc(iter->size, M_GCOV, M_NOWAIT); 513 if (!iter->buffer) 514 goto err_free; 515 516 convert_to_gcda(iter->buffer, info); 517 518 return iter; 519 520 err_free: 521 free(iter, M_GCOV); 522 return (NULL); 523 } 524 525 526 /** 527 * gcov_iter_get_info - return profiling data set for given file iterator 528 * @iter: file iterator 529 */ 530 void 531 gcov_iter_free(struct gcov_iterator *iter) 532 { 533 free(iter->buffer, M_GCOV); 534 free(iter, M_GCOV); 535 } 536 537 /** 538 * gcov_iter_get_info - return profiling data set for given file iterator 539 * @iter: file iterator 540 */ 541 struct gcov_info * 542 gcov_iter_get_info(struct gcov_iterator *iter) 543 { 544 return (iter->info); 545 } 546 547 /** 548 * gcov_iter_start - reset file iterator to starting position 549 * @iter: file iterator 550 */ 551 void 552 gcov_iter_start(struct gcov_iterator *iter) 553 { 554 iter->pos = 0; 555 } 556 557 /** 558 * gcov_iter_next - advance file iterator to next logical record 559 * @iter: file iterator 560 * 561 * Return zero if new position is valid, non-zero if iterator has reached end. 562 */ 563 int 564 gcov_iter_next(struct gcov_iterator *iter) 565 { 566 if (iter->pos < iter->size) 567 iter->pos += ITER_STRIDE; 568 569 if (iter->pos >= iter->size) 570 return (EINVAL); 571 572 return 0; 573 } 574 575 /** 576 * gcov_iter_write - write data for current pos to seq_file 577 * @iter: file iterator 578 * @seq: seq_file handle 579 * 580 * Return zero on success, non-zero otherwise. 581 */ 582 int 583 gcov_iter_write(struct gcov_iterator *iter, struct sbuf *sbuf) 584 { 585 size_t len; 586 587 if (iter->pos >= iter->size) 588 return (EINVAL); 589 590 len = ITER_STRIDE; 591 if (iter->pos + len > iter->size) 592 len = iter->size - iter->pos; 593 594 sbuf_bcat(sbuf, iter->buffer + iter->pos, len); 595 596 return (0); 597 } 598