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