xref: /freebsd/sys/gnu/gcov/gcc_4_7.c (revision bda54b8f964b4035dcf5aba394d14ed24370a6f2)
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/sbuf.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <gnu/gcov/gcov.h>
40 
41 
42 #if (__GNUC__ >= 7)
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
48 #else
49 #define GCOV_COUNTERS			8
50 #endif
51 
52 #define GCOV_TAG_FUNCTION_LENGTH	3
53 
54 static struct gcov_info *gcov_info_head;
55 
56 /**
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
60  *
61  * This data is generated by gcc during compilation and doesn't change
62  * at run-time with the exception of the values array.
63  */
64 struct gcov_ctr_info {
65 	unsigned int num;
66 	gcov_type *values;
67 };
68 
69 /**
70  * struct gcov_fn_info - profiling meta data per function
71  * @key: comdat key
72  * @ident: unique ident of function
73  * @lineno_checksum: function lineo_checksum
74  * @cfg_checksum: function cfg checksum
75  * @ctrs: instrumented counters
76  *
77  * This data is generated by gcc during compilation and doesn't change
78  * at run-time.
79  *
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.
85  */
86 struct gcov_fn_info {
87 	const struct gcov_info *key;
88 	unsigned int ident;
89 	unsigned int lineno_checksum;
90 	unsigned int cfg_checksum;
91 	struct gcov_ctr_info ctrs[0];
92 };
93 
94 /**
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
103  *
104  * This data is generated by gcc during compilation and doesn't change
105  * at run-time with the exception of the next pointer.
106  */
107 struct gcov_info {
108 	unsigned int version;
109 	struct gcov_info *next;
110 	unsigned int stamp;
111 	const char *filename;
112 	void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
113 	unsigned int n_functions;
114 	struct gcov_fn_info **functions;
115 };
116 
117 /**
118  * gcov_info_filename - return info filename
119  * @info: profiling data set
120  */
121 const char *
122 gcov_info_filename(struct gcov_info *info)
123 {
124 	return (info->filename);
125 }
126 
127 /**
128  * gcov_info_version - return info version
129  * @info: profiling data set
130  */
131 unsigned int
132 gcov_info_version(struct gcov_info *info)
133 {
134 	return (info->version);
135 }
136 
137 /**
138  * gcov_info_next - return next profiling data set
139  * @info: profiling data set
140  *
141  * Returns next gcov_info following @info or first gcov_info in the chain if
142  * @info is %NULL.
143  */
144 struct gcov_info *
145 gcov_info_next(struct gcov_info *info)
146 {
147 	if (!info)
148 		return gcov_info_head;
149 
150 	return (info->next);
151 }
152 
153 /**
154  * gcov_info_link - link/add profiling data set to the list
155  * @info: profiling data set
156  */
157 void
158 gcov_info_link(struct gcov_info *info)
159 {
160 	info->next = gcov_info_head;
161 	gcov_info_head = info;
162 }
163 
164 /**
165  * gcov_info_unlink - unlink/remove profiling data set from the list
166  * @prev: previous profiling data set
167  * @info: profiling data set
168  */
169 void
170 gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
171 {
172 	if (prev)
173 		prev->next = info->next;
174 	else
175 		gcov_info_head = info->next;
176 }
177 
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). */
181 	{ 0, NULL},
182 };
183 
184 /*
185  * Determine whether a counter is active. Doesn't change at run-time.
186  */
187 static int
188 counter_active(struct gcov_info *info, unsigned int type)
189 {
190 	return (info->merge[type] ? 1 : 0);
191 }
192 
193 /* Determine number of active counters. Based on gcc magic. */
194 static unsigned int
195 num_counter_active(struct gcov_info *info)
196 {
197 	unsigned int i;
198 	unsigned int result = 0;
199 
200 	for (i = 0; i < GCOV_COUNTERS; i++) {
201 		if (counter_active(info, i))
202 			result++;
203 	}
204 	return (result);
205 }
206 
207 /**
208  * gcov_info_reset - reset profiling data to zero
209  * @info: profiling data set
210  */
211 void
212 gcov_info_reset(struct gcov_info *info)
213 {
214 	struct gcov_ctr_info *ci_ptr;
215 	unsigned int fi_idx;
216 	unsigned int ct_idx;
217 
218 	for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
219 		ci_ptr = info->functions[fi_idx]->ctrs;
220 
221 		for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
222 			if (!counter_active(info, ct_idx))
223 				continue;
224 
225 			memset(ci_ptr->values, 0,
226 					sizeof(gcov_type) * ci_ptr->num);
227 			ci_ptr++;
228 		}
229 	}
230 }
231 
232 /**
233  * gcov_info_is_compatible - check if profiling data can be added
234  * @info1: first profiling data set
235  * @info2: second profiling data set
236  *
237  * Returns non-zero if profiling data can be added, zero otherwise.
238  */
239 int
240 gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
241 {
242 	return (info1->stamp == info2->stamp);
243 }
244 
245 /**
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
249  *
250  * Adds profiling counts of @source to @dest.
251  */
252 void
253 gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
254 {
255 	struct gcov_ctr_info *dci_ptr;
256 	struct gcov_ctr_info *sci_ptr;
257 	unsigned int fi_idx;
258 	unsigned int ct_idx;
259 	unsigned int val_idx;
260 
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;
264 
265 		for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
266 			if (!counter_active(src, ct_idx))
267 				continue;
268 
269 			for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
270 				dci_ptr->values[val_idx] +=
271 					sci_ptr->values[val_idx];
272 
273 			dci_ptr++;
274 			sci_ptr++;
275 		}
276 	}
277 }
278 
279 /**
280  * gcov_info_dup - duplicate profiling data set
281  * @info: profiling data set to duplicate
282  *
283  * Return newly allocated duplicate on success, %NULL on error.
284  */
285 struct gcov_info *
286 gcov_info_dup(struct gcov_info *info)
287 {
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 */
291 	unsigned int active;
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 */
296 
297 	if ((dup = malloc(sizeof(*dup), M_GCOV, M_NOWAIT|M_ZERO)) == NULL)
298 		return (NULL);
299 	memcpy(dup, info, sizeof(*dup));
300 
301 	dup->next = NULL;
302 	dup->filename = NULL;
303 	dup->functions = NULL;
304 
305 	dup->filename = strdup_flags(info->filename, M_GCOV, M_NOWAIT);
306 	if (dup->filename == NULL)
307 		goto err_free;
308 
309 	dup->functions = malloc(info->n_functions * sizeof(struct gcov_fn_info *), M_GCOV, M_NOWAIT|M_ZERO);
310 	if (dup->functions == NULL)
311 		goto err_free;
312 	active = num_counter_active(info);
313 	fi_size = sizeof(struct gcov_fn_info);
314 	fi_size += sizeof(struct gcov_ctr_info) * active;
315 
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])
319 			goto err_free;
320 
321 		*(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
322 
323 		sci_ptr = info->functions[fi_idx]->ctrs;
324 		dci_ptr = dup->functions[fi_idx]->ctrs;
325 
326 		for (ct_idx = 0; ct_idx < active; ct_idx++) {
327 
328 			cv_size = sizeof(gcov_type) * sci_ptr->num;
329 
330 			dci_ptr->values = malloc(cv_size, M_GCOV, M_NOWAIT);
331 
332 			if (!dci_ptr->values)
333 				goto err_free;
334 
335 			dci_ptr->num = sci_ptr->num;
336 			memcpy(dci_ptr->values, sci_ptr->values, cv_size);
337 
338 			sci_ptr++;
339 			dci_ptr++;
340 		}
341 	}
342 
343 	return (dup);
344 err_free:
345 	gcov_info_free(dup);
346 	return (NULL);
347 }
348 
349 /**
350  * gcov_info_free - release memory for profiling data set duplicate
351  * @info: profiling data set duplicate to free
352  */
353 void
354 gcov_info_free(struct gcov_info *info)
355 {
356 	unsigned int active;
357 	unsigned int fi_idx;
358 	unsigned int ct_idx;
359 	struct gcov_ctr_info *ci_ptr;
360 
361 	if (!info->functions)
362 		goto free_info;
363 
364 	active = num_counter_active(info);
365 
366 	for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
367 		if (!info->functions[fi_idx])
368 			continue;
369 
370 		ci_ptr = info->functions[fi_idx]->ctrs;
371 
372 		for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
373 			free(ci_ptr->values, M_GCOV);
374 
375 		free(info->functions[fi_idx], M_GCOV);
376 	}
377 
378 free_info:
379 	free(info->functions, M_GCOV);
380 	free(__DECONST(char *, info->filename), M_GCOV);
381 	free(info, M_GCOV);
382  }
383 
384 #define ITER_STRIDE	PAGE_SIZE
385 
386 /**
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
392  */
393 struct gcov_iterator {
394 	struct gcov_info *info;
395 	caddr_t buffer;
396 	size_t size;
397 	off_t pos;
398 };
399 
400 /**
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
405  *
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
409  * store anything.
410  */
411 static size_t
412 store_gcov_uint32(void *buffer, size_t off, uint32_t v)
413 {
414 	uint32_t *data;
415 
416 	if (buffer) {
417 		data = (void*)((caddr_t)buffer + off);
418 		*data = v;
419 	}
420 
421 	return sizeof(*data);
422 }
423 
424 /**
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
429  *
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
434  * anything.
435  */
436 
437 static size_t
438 store_gcov_uint64(void *buffer, size_t off, uint64_t v)
439 {
440 	uint32_t *data;
441 
442 	if (buffer) {
443 		data = (void*)((caddr_t)buffer + off);
444 
445 		data[0] = (v & 0xffffffffUL);
446 		data[1] = (v >> 32);
447 	}
448 
449 	return sizeof(*data) * 2;
450 }
451 
452 /**
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
456  *
457  * Returns the number of bytes that were/would have been stored into the buffer.
458  */
459 static size_t
460 convert_to_gcda(char *buffer, struct gcov_info *info)
461 {
462 	struct gcov_fn_info *fi_ptr;
463 	struct gcov_ctr_info *ci_ptr;
464 	unsigned int fi_idx;
465 	unsigned int ct_idx;
466 	unsigned int cv_idx;
467 	size_t pos = 0;
468 
469 	/* File header. */
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);
473 
474 	for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
475 		fi_ptr = info->functions[fi_idx];
476 
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);
483 
484 		ci_ptr = fi_ptr->ctrs;
485 
486 		for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
487 			if (!counter_active(info, ct_idx))
488 				continue;
489 
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);
494 
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]);
498 			}
499 
500 			ci_ptr++;
501 		}
502 	}
503 
504 	return (pos);
505 }
506 
507 /**
508  * gcov_iter_new - allocate and initialize profiling data iterator
509  * @info: profiling data set to be iterated
510  *
511  * Return file iterator on success, %NULL otherwise.
512  */
513 struct gcov_iterator *
514 gcov_iter_new(struct gcov_info *info)
515 {
516 	struct gcov_iterator *iter;
517 
518 	iter = malloc(sizeof(struct gcov_iterator), M_GCOV, M_NOWAIT|M_ZERO);
519 	if (iter == NULL)
520 		goto err_free;
521 
522 	iter->info = info;
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);
526 	if (!iter->buffer)
527 		goto err_free;
528 
529 	convert_to_gcda(iter->buffer, info);
530 
531 	return iter;
532 
533 err_free:
534 	free(iter, M_GCOV);
535 	return (NULL);
536 }
537 
538 
539 /**
540  * gcov_iter_get_info - return profiling data set for given file iterator
541  * @iter: file iterator
542  */
543 void
544 gcov_iter_free(struct gcov_iterator *iter)
545 {
546 	free(iter->buffer, M_GCOV);
547 	free(iter, M_GCOV);
548 }
549 
550 /**
551  * gcov_iter_get_info - return profiling data set for given file iterator
552  * @iter: file iterator
553  */
554 struct gcov_info *
555 gcov_iter_get_info(struct gcov_iterator *iter)
556 {
557 	return (iter->info);
558 }
559 
560 /**
561  * gcov_iter_start - reset file iterator to starting position
562  * @iter: file iterator
563  */
564 void
565 gcov_iter_start(struct gcov_iterator *iter)
566 {
567 	iter->pos = 0;
568 }
569 
570 /**
571  * gcov_iter_next - advance file iterator to next logical record
572  * @iter: file iterator
573  *
574  * Return zero if new position is valid, non-zero if iterator has reached end.
575  */
576 int
577 gcov_iter_next(struct gcov_iterator *iter)
578 {
579 	if (iter->pos < iter->size)
580 		iter->pos += ITER_STRIDE;
581 
582 	if (iter->pos >= iter->size)
583 		return (EINVAL);
584 
585 	return 0;
586 }
587 
588 /**
589  * gcov_iter_write - write data for current pos to seq_file
590  * @iter: file iterator
591  * @seq: seq_file handle
592  *
593  * Return zero on success, non-zero otherwise.
594  */
595 int
596 gcov_iter_write(struct gcov_iterator *iter, struct sbuf *sbuf)
597 {
598 	size_t len;
599 
600 	if (iter->pos >= iter->size)
601 		return (EINVAL);
602 
603 	len = ITER_STRIDE;
604 	if (iter->pos + len > iter->size)
605 		len = iter->size - iter->pos;
606 
607 	sbuf_bcat(sbuf, iter->buffer + iter->pos, len);
608 
609 	return (0);
610 }
611