blob: 88e128c1221bb8b9c3a506291054a404bff9dd47 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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#include <linux/errno.h>
16#include <linux/slab.h>
17#include <linux/string.h>
18#include <linux/seq_file.h>
19#include <linux/vmalloc.h>
20#include "gcov.h"
21
22#if (__GNUC__ >= 14)
23#define GCOV_COUNTERS 9
24#elif (__GNUC__ >= 10)
25#define GCOV_COUNTERS 8
26#elif (__GNUC__ >= 7)
27#define GCOV_COUNTERS 9
28#elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
29#define GCOV_COUNTERS 10
30#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
31#define GCOV_COUNTERS 9
32#else
33#define GCOV_COUNTERS 8
34#endif
35
36#define GCOV_TAG_FUNCTION_LENGTH 3
37
38/* Since GCC 12.1 sizes are in BYTES and not in WORDS (4B). */
39#if (__GNUC__ >= 12)
40#define GCOV_UNIT_SIZE 4
41#else
42#define GCOV_UNIT_SIZE 1
43#endif
44
45static struct gcov_info *gcov_info_head;
46
47/**
48 * struct gcov_ctr_info - information about counters for a single function
49 * @num: number of counter values for this type
50 * @values: array of counter values for this type
51 *
52 * This data is generated by gcc during compilation and doesn't change
53 * at run-time with the exception of the values array.
54 */
55struct gcov_ctr_info {
56 unsigned int num;
57 gcov_type *values;
58};
59
60/**
61 * struct gcov_fn_info - profiling meta data per function
62 * @key: comdat key
63 * @ident: unique ident of function
64 * @lineno_checksum: function lineo_checksum
65 * @cfg_checksum: function cfg checksum
66 * @ctrs: instrumented counters
67 *
68 * This data is generated by gcc during compilation and doesn't change
69 * at run-time.
70 *
71 * Information about a single function. This uses the trailing array
72 * idiom. The number of counters is determined from the merge pointer
73 * array in gcov_info. The key is used to detect which of a set of
74 * comdat functions was selected -- it points to the gcov_info object
75 * of the object file containing the selected comdat function.
76 */
77struct gcov_fn_info {
78 const struct gcov_info *key;
79 unsigned int ident;
80 unsigned int lineno_checksum;
81 unsigned int cfg_checksum;
82 struct gcov_ctr_info ctrs[0];
83};
84
85/**
86 * struct gcov_info - profiling data per object file
87 * @version: gcov version magic indicating the gcc version used for compilation
88 * @next: list head for a singly-linked list
89 * @stamp: uniquifying time stamp
90 * @checksum: unique object checksum
91 * @filename: name of the associated gcov data file
92 * @merge: merge functions (null for unused counter type)
93 * @n_functions: number of instrumented functions
94 * @functions: pointer to pointers to function information
95 *
96 * This data is generated by gcc during compilation and doesn't change
97 * at run-time with the exception of the next pointer.
98 */
99struct gcov_info {
100 unsigned int version;
101 struct gcov_info *next;
102 unsigned int stamp;
103 /* Since GCC 12.1 a checksum field is added. */
104#if (__GNUC__ >= 12)
105 unsigned int checksum;
106#endif
107 const char *filename;
108 void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
109 unsigned int n_functions;
110 struct gcov_fn_info **functions;
111};
112
113/**
114 * gcov_info_filename - return info filename
115 * @info: profiling data set
116 */
117const char *gcov_info_filename(struct gcov_info *info)
118{
119 return info->filename;
120}
121
122/**
123 * gcov_info_version - return info version
124 * @info: profiling data set
125 */
126unsigned int gcov_info_version(struct gcov_info *info)
127{
128 return info->version;
129}
130
131/**
132 * gcov_info_next - return next profiling data set
133 * @info: profiling data set
134 *
135 * Returns next gcov_info following @info or first gcov_info in the chain if
136 * @info is %NULL.
137 */
138struct gcov_info *gcov_info_next(struct gcov_info *info)
139{
140 if (!info)
141 return gcov_info_head;
142
143 return info->next;
144}
145
146/**
147 * gcov_info_link - link/add profiling data set to the list
148 * @info: profiling data set
149 */
150void gcov_info_link(struct gcov_info *info)
151{
152 info->next = gcov_info_head;
153 gcov_info_head = info;
154}
155
156/**
157 * gcov_info_unlink - unlink/remove profiling data set from the list
158 * @prev: previous profiling data set
159 * @info: profiling data set
160 */
161void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
162{
163 if (prev)
164 prev->next = info->next;
165 else
166 gcov_info_head = info->next;
167}
168
169/**
170 * gcov_info_within_module - check if a profiling data set belongs to a module
171 * @info: profiling data set
172 * @mod: module
173 *
174 * Returns true if profiling data belongs module, false otherwise.
175 */
176bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
177{
178 return within_module((unsigned long)info, mod);
179}
180
181/* Symbolic links to be created for each profiling data file. */
182const struct gcov_link gcov_link[] = {
183 { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
184 { 0, NULL},
185};
186
187/*
188 * Determine whether a counter is active. Doesn't change at run-time.
189 */
190static int counter_active(struct gcov_info *info, unsigned int type)
191{
192 return info->merge[type] ? 1 : 0;
193}
194
195/* Determine number of active counters. Based on gcc magic. */
196static unsigned int num_counter_active(struct gcov_info *info)
197{
198 unsigned int i;
199 unsigned int result = 0;
200
201 for (i = 0; i < GCOV_COUNTERS; i++) {
202 if (counter_active(info, i))
203 result++;
204 }
205 return result;
206}
207
208/**
209 * gcov_info_reset - reset profiling data to zero
210 * @info: profiling data set
211 */
212void 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 */
239int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
240{
241 return (info1->stamp == info2->stamp);
242}
243
244/**
245 * gcov_info_add - add up profiling data
246 * @dest: profiling data set to which data is added
247 * @source: profiling data set which is added
248 *
249 * Adds profiling counts of @source to @dest.
250 */
251void 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 */
283struct gcov_info *gcov_info_dup(struct gcov_info *info)
284{
285 struct gcov_info *dup;
286 struct gcov_ctr_info *dci_ptr; /* dst counter info */
287 struct gcov_ctr_info *sci_ptr; /* src counter info */
288 unsigned int active;
289 unsigned int fi_idx; /* function info idx */
290 unsigned int ct_idx; /* counter type idx */
291 size_t fi_size; /* function info size */
292 size_t cv_size; /* counter values size */
293
294 dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
295 if (!dup)
296 return NULL;
297
298 dup->next = NULL;
299 dup->filename = NULL;
300 dup->functions = NULL;
301
302 dup->filename = kstrdup(info->filename, GFP_KERNEL);
303 if (!dup->filename)
304 goto err_free;
305
306 dup->functions = kcalloc(info->n_functions,
307 sizeof(struct gcov_fn_info *), GFP_KERNEL);
308 if (!dup->functions)
309 goto err_free;
310
311 active = num_counter_active(info);
312 fi_size = sizeof(struct gcov_fn_info);
313 fi_size += sizeof(struct gcov_ctr_info) * active;
314
315 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
316 dup->functions[fi_idx] = kzalloc(fi_size, GFP_KERNEL);
317 if (!dup->functions[fi_idx])
318 goto err_free;
319
320 *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
321
322 sci_ptr = info->functions[fi_idx]->ctrs;
323 dci_ptr = dup->functions[fi_idx]->ctrs;
324
325 for (ct_idx = 0; ct_idx < active; ct_idx++) {
326
327 cv_size = sizeof(gcov_type) * sci_ptr->num;
328
329 dci_ptr->values = vmalloc(cv_size);
330
331 if (!dci_ptr->values)
332 goto err_free;
333
334 dci_ptr->num = sci_ptr->num;
335 memcpy(dci_ptr->values, sci_ptr->values, cv_size);
336
337 sci_ptr++;
338 dci_ptr++;
339 }
340 }
341
342 return dup;
343err_free:
344 gcov_info_free(dup);
345 return NULL;
346}
347
348/**
349 * gcov_info_free - release memory for profiling data set duplicate
350 * @info: profiling data set duplicate to free
351 */
352void 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 vfree(ci_ptr->values);
372
373 kfree(info->functions[fi_idx]);
374 }
375
376free_info:
377 kfree(info->functions);
378 kfree(info->filename);
379 kfree(info);
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 */
391struct gcov_iterator {
392 struct gcov_info *info;
393 void *buffer;
394 size_t size;
395 loff_t pos;
396};
397
398/**
399 * store_gcov_u32 - 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 */
409static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
410{
411 u32 *data;
412
413 if (buffer) {
414 data = buffer + off;
415 *data = v;
416 }
417
418 return sizeof(*data);
419}
420
421/**
422 * store_gcov_u64 - store 64 bit number in gcov format to buffer
423 * @buffer: target buffer or NULL
424 * @off: offset into the buffer
425 * @v: value to be stored
426 *
427 * Number format defined by gcc: numbers are recorded in the 32 bit
428 * unsigned binary form of the endianness of the machine generating the
429 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
430 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
431 * anything.
432 */
433static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
434{
435 u32 *data;
436
437 if (buffer) {
438 data = buffer + off;
439
440 data[0] = (v & 0xffffffffUL);
441 data[1] = (v >> 32);
442 }
443
444 return sizeof(*data) * 2;
445}
446
447/**
448 * convert_to_gcda - convert profiling data set to gcda file format
449 * @buffer: the buffer to store file data or %NULL if no data should be stored
450 * @info: profiling data set to be converted
451 *
452 * Returns the number of bytes that were/would have been stored into the buffer.
453 */
454static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
455{
456 struct gcov_fn_info *fi_ptr;
457 struct gcov_ctr_info *ci_ptr;
458 unsigned int fi_idx;
459 unsigned int ct_idx;
460 unsigned int cv_idx;
461 size_t pos = 0;
462
463 /* File header. */
464 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
465 pos += store_gcov_u32(buffer, pos, info->version);
466 pos += store_gcov_u32(buffer, pos, info->stamp);
467
468#if (__GNUC__ >= 12)
469 /* Use zero as checksum of the compilation unit. */
470 pos += store_gcov_u32(buffer, pos, 0);
471#endif
472
473 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
474 fi_ptr = info->functions[fi_idx];
475
476 /* Function record. */
477 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
478 pos += store_gcov_u32(buffer, pos,
479 GCOV_TAG_FUNCTION_LENGTH * GCOV_UNIT_SIZE);
480 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
481 pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
482 pos += store_gcov_u32(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_u32(buffer, pos,
492 GCOV_TAG_FOR_COUNTER(ct_idx));
493 pos += store_gcov_u32(buffer, pos,
494 ci_ptr->num * 2 * GCOV_UNIT_SIZE);
495
496 for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
497 pos += store_gcov_u64(buffer, pos,
498 ci_ptr->values[cv_idx]);
499 }
500
501 ci_ptr++;
502 }
503 }
504
505 return pos;
506}
507
508/**
509 * gcov_iter_new - allocate and initialize profiling data iterator
510 * @info: profiling data set to be iterated
511 *
512 * Return file iterator on success, %NULL otherwise.
513 */
514struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
515{
516 struct gcov_iterator *iter;
517
518 iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
519 if (!iter)
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 = vmalloc(iter->size);
526 if (!iter->buffer)
527 goto err_free;
528
529 convert_to_gcda(iter->buffer, info);
530
531 return iter;
532
533err_free:
534 kfree(iter);
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 */
543void gcov_iter_free(struct gcov_iterator *iter)
544{
545 vfree(iter->buffer);
546 kfree(iter);
547}
548
549/**
550 * gcov_iter_get_info - return profiling data set for given file iterator
551 * @iter: file iterator
552 */
553struct gcov_info *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 */
562void gcov_iter_start(struct gcov_iterator *iter)
563{
564 iter->pos = 0;
565}
566
567/**
568 * gcov_iter_next - advance file iterator to next logical record
569 * @iter: file iterator
570 *
571 * Return zero if new position is valid, non-zero if iterator has reached end.
572 */
573int gcov_iter_next(struct gcov_iterator *iter)
574{
575 if (iter->pos < iter->size)
576 iter->pos += ITER_STRIDE;
577
578 if (iter->pos >= iter->size)
579 return -EINVAL;
580
581 return 0;
582}
583
584/**
585 * gcov_iter_write - write data for current pos to seq_file
586 * @iter: file iterator
587 * @seq: seq_file handle
588 *
589 * Return zero on success, non-zero otherwise.
590 */
591int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
592{
593 size_t len;
594
595 if (iter->pos >= iter->size)
596 return -EINVAL;
597
598 len = ITER_STRIDE;
599 if (iter->pos + len > iter->size)
600 len = iter->size - iter->pos;
601
602 seq_write(seq, iter->buffer + iter->pos, len);
603
604 return 0;
605}