1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Google, Inc.
4 * modified from kernel/gcov/gcc_4_7.c
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 * LLVM uses profiling data that's deliberately similar to GCC, but has a
17 * very different way of exporting that data. LLVM calls llvm_gcov_init() once
18 * per module, and provides a couple of callbacks that we can use to ask for
21 * We care about the "writeout" callback, which in turn calls back into
22 * compiler-rt/this module to dump all the gathered coverage data to disk:
24 * llvm_gcda_start_file()
25 * llvm_gcda_emit_function()
26 * llvm_gcda_emit_arcs()
27 * llvm_gcda_emit_function()
28 * llvm_gcda_emit_arcs()
29 * [... repeats for each function ...]
30 * llvm_gcda_summary_info()
31 * llvm_gcda_end_file()
33 * This design is much more stateless and unstructured than gcc's, and is
34 * intended to run at process exit. This forces us to keep some local state
35 * about which module we're dealing with at the moment. On the other hand, it
36 * also means we don't depend as much on how LLVM represents profiling data
39 * See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more
40 * details on how this works, particularly GCOVProfiler::emitProfileArcs(),
41 * GCOVProfiler::insertCounterWriteout(), and
42 * GCOVProfiler::insertFlush().
45 #define pr_fmt(fmt) "gcov: " fmt
47 #include <linux/kernel.h>
48 #include <linux/list.h>
49 #include <linux/printk.h>
50 #include <linux/ratelimit.h>
51 #include <linux/seq_file.h>
52 #include <linux/slab.h>
53 #include <linux/vmalloc.h>
56 typedef void (*llvm_gcov_callback)(void);
59 struct list_head head;
65 struct list_head functions;
69 struct list_head head;
73 #if CONFIG_CLANG_VERSION < 110000
74 u8 use_extra_checksum;
80 #if CONFIG_CLANG_VERSION < 110000
81 const char *function_name;
85 static struct gcov_info *current_info;
87 static LIST_HEAD(clang_gcov_list);
89 void llvm_gcov_init(llvm_gcov_callback writeout, llvm_gcov_callback flush)
91 struct gcov_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
96 INIT_LIST_HEAD(&info->head);
97 INIT_LIST_HEAD(&info->functions);
99 mutex_lock(&gcov_lock);
101 list_add_tail(&info->head, &clang_gcov_list);
105 if (gcov_events_enabled)
106 gcov_event(GCOV_ADD, info);
108 mutex_unlock(&gcov_lock);
110 EXPORT_SYMBOL(llvm_gcov_init);
112 #if CONFIG_CLANG_VERSION < 110000
113 void llvm_gcda_start_file(const char *orig_filename, const char version[4],
116 current_info->filename = orig_filename;
117 memcpy(¤t_info->version, version, sizeof(current_info->version));
118 current_info->checksum = checksum;
120 EXPORT_SYMBOL(llvm_gcda_start_file);
122 void llvm_gcda_start_file(const char *orig_filename, u32 version, u32 checksum)
124 current_info->filename = orig_filename;
125 current_info->version = version;
126 current_info->checksum = checksum;
128 EXPORT_SYMBOL(llvm_gcda_start_file);
131 #if CONFIG_CLANG_VERSION < 110000
132 void llvm_gcda_emit_function(u32 ident, const char *function_name,
133 u32 func_checksum, u8 use_extra_checksum, u32 cfg_checksum)
135 struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
140 INIT_LIST_HEAD(&info->head);
142 info->checksum = func_checksum;
143 info->use_extra_checksum = use_extra_checksum;
144 info->cfg_checksum = cfg_checksum;
146 info->function_name = kstrdup(function_name, GFP_KERNEL);
148 list_add_tail(&info->head, ¤t_info->functions);
151 void llvm_gcda_emit_function(u32 ident, u32 func_checksum, u32 cfg_checksum)
153 struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
158 INIT_LIST_HEAD(&info->head);
160 info->checksum = func_checksum;
161 info->cfg_checksum = cfg_checksum;
162 list_add_tail(&info->head, ¤t_info->functions);
165 EXPORT_SYMBOL(llvm_gcda_emit_function);
167 void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
169 struct gcov_fn_info *info = list_last_entry(¤t_info->functions,
170 struct gcov_fn_info, head);
172 info->num_counters = num_counters;
173 info->counters = counters;
175 EXPORT_SYMBOL(llvm_gcda_emit_arcs);
177 void llvm_gcda_summary_info(void)
180 EXPORT_SYMBOL(llvm_gcda_summary_info);
182 void llvm_gcda_end_file(void)
185 EXPORT_SYMBOL(llvm_gcda_end_file);
188 * gcov_info_filename - return info filename
189 * @info: profiling data set
191 const char *gcov_info_filename(struct gcov_info *info)
193 return info->filename;
197 * gcov_info_version - return info version
198 * @info: profiling data set
200 unsigned int gcov_info_version(struct gcov_info *info)
202 return info->version;
206 * gcov_info_next - return next profiling data set
207 * @info: profiling data set
209 * Returns next gcov_info following @info or first gcov_info in the chain if
212 struct gcov_info *gcov_info_next(struct gcov_info *info)
215 return list_first_entry_or_null(&clang_gcov_list,
216 struct gcov_info, head);
217 if (list_is_last(&info->head, &clang_gcov_list))
219 return list_next_entry(info, head);
223 * gcov_info_link - link/add profiling data set to the list
224 * @info: profiling data set
226 void gcov_info_link(struct gcov_info *info)
228 list_add_tail(&info->head, &clang_gcov_list);
232 * gcov_info_unlink - unlink/remove profiling data set from the list
233 * @prev: previous profiling data set
234 * @info: profiling data set
236 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
238 /* Generic code unlinks while iterating. */
239 __list_del_entry(&info->head);
243 * gcov_info_within_module - check if a profiling data set belongs to a module
244 * @info: profiling data set
247 * Returns true if profiling data belongs module, false otherwise.
249 bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
251 return within_module((unsigned long)info->filename, mod);
254 /* Symbolic links to be created for each profiling data file. */
255 const struct gcov_link gcov_link[] = {
256 { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
261 * gcov_info_reset - reset profiling data to zero
262 * @info: profiling data set
264 void gcov_info_reset(struct gcov_info *info)
266 struct gcov_fn_info *fn;
268 list_for_each_entry(fn, &info->functions, head)
269 memset(fn->counters, 0,
270 sizeof(fn->counters[0]) * fn->num_counters);
274 * gcov_info_is_compatible - check if profiling data can be added
275 * @info1: first profiling data set
276 * @info2: second profiling data set
278 * Returns non-zero if profiling data can be added, zero otherwise.
280 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
282 struct gcov_fn_info *fn_ptr1 = list_first_entry_or_null(
283 &info1->functions, struct gcov_fn_info, head);
284 struct gcov_fn_info *fn_ptr2 = list_first_entry_or_null(
285 &info2->functions, struct gcov_fn_info, head);
287 if (info1->checksum != info2->checksum)
290 return fn_ptr1 == fn_ptr2;
291 while (!list_is_last(&fn_ptr1->head, &info1->functions) &&
292 !list_is_last(&fn_ptr2->head, &info2->functions)) {
293 if (fn_ptr1->checksum != fn_ptr2->checksum)
295 #if CONFIG_CLANG_VERSION < 110000
296 if (fn_ptr1->use_extra_checksum != fn_ptr2->use_extra_checksum)
298 if (fn_ptr1->use_extra_checksum &&
299 fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
302 if (fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
305 fn_ptr1 = list_next_entry(fn_ptr1, head);
306 fn_ptr2 = list_next_entry(fn_ptr2, head);
308 return list_is_last(&fn_ptr1->head, &info1->functions) &&
309 list_is_last(&fn_ptr2->head, &info2->functions);
313 * gcov_info_add - add up profiling data
314 * @dest: profiling data set to which data is added
315 * @source: profiling data set which is added
317 * Adds profiling counts of @source to @dest.
319 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
321 struct gcov_fn_info *dfn_ptr;
322 struct gcov_fn_info *sfn_ptr = list_first_entry_or_null(&src->functions,
323 struct gcov_fn_info, head);
325 list_for_each_entry(dfn_ptr, &dst->functions, head) {
328 for (i = 0; i < sfn_ptr->num_counters; i++)
329 dfn_ptr->counters[i] += sfn_ptr->counters[i];
331 sfn_ptr = list_next_entry(sfn_ptr, head);
335 #if CONFIG_CLANG_VERSION < 110000
336 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
338 size_t cv_size; /* counter values size */
339 struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
343 INIT_LIST_HEAD(&fn_dup->head);
345 fn_dup->function_name = kstrdup(fn->function_name, GFP_KERNEL);
346 if (!fn_dup->function_name)
349 cv_size = fn->num_counters * sizeof(fn->counters[0]);
350 fn_dup->counters = vmalloc(cv_size);
351 if (!fn_dup->counters)
353 memcpy(fn_dup->counters, fn->counters, cv_size);
358 kfree(fn_dup->function_name);
364 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
366 size_t cv_size; /* counter values size */
367 struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
371 INIT_LIST_HEAD(&fn_dup->head);
373 cv_size = fn->num_counters * sizeof(fn->counters[0]);
374 fn_dup->counters = vmalloc(cv_size);
375 if (!fn_dup->counters) {
380 memcpy(fn_dup->counters, fn->counters, cv_size);
387 * gcov_info_dup - duplicate profiling data set
388 * @info: profiling data set to duplicate
390 * Return newly allocated duplicate on success, %NULL on error.
392 struct gcov_info *gcov_info_dup(struct gcov_info *info)
394 struct gcov_info *dup;
395 struct gcov_fn_info *fn;
397 dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
400 INIT_LIST_HEAD(&dup->head);
401 INIT_LIST_HEAD(&dup->functions);
402 dup->filename = kstrdup(info->filename, GFP_KERNEL);
406 list_for_each_entry(fn, &info->functions, head) {
407 struct gcov_fn_info *fn_dup = gcov_fn_info_dup(fn);
411 list_add_tail(&fn_dup->head, &dup->functions);
422 * gcov_info_free - release memory for profiling data set duplicate
423 * @info: profiling data set duplicate to free
425 #if CONFIG_CLANG_VERSION < 110000
426 void gcov_info_free(struct gcov_info *info)
428 struct gcov_fn_info *fn, *tmp;
430 list_for_each_entry_safe(fn, tmp, &info->functions, head) {
431 kfree(fn->function_name);
436 kfree(info->filename);
440 void gcov_info_free(struct gcov_info *info)
442 struct gcov_fn_info *fn, *tmp;
444 list_for_each_entry_safe(fn, tmp, &info->functions, head) {
449 kfree(info->filename);
454 #define ITER_STRIDE PAGE_SIZE
457 * struct gcov_iterator - specifies current file position in logical records
458 * @info: associated profiling data
459 * @buffer: buffer containing file data
460 * @size: size of buffer
461 * @pos: current position in file
463 struct gcov_iterator {
464 struct gcov_info *info;
471 * store_gcov_u32 - store 32 bit number in gcov format to buffer
472 * @buffer: target buffer or NULL
473 * @off: offset into the buffer
474 * @v: value to be stored
476 * Number format defined by gcc: numbers are recorded in the 32 bit
477 * unsigned binary form of the endianness of the machine generating the
478 * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
481 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
490 return sizeof(*data);
494 * store_gcov_u64 - store 64 bit number in gcov format to buffer
495 * @buffer: target buffer or NULL
496 * @off: offset into the buffer
497 * @v: value to be stored
499 * Number format defined by gcc: numbers are recorded in the 32 bit
500 * unsigned binary form of the endianness of the machine generating the
501 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
502 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
505 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
512 data[0] = (v & 0xffffffffUL);
516 return sizeof(*data) * 2;
520 * convert_to_gcda - convert profiling data set to gcda file format
521 * @buffer: the buffer to store file data or %NULL if no data should be stored
522 * @info: profiling data set to be converted
524 * Returns the number of bytes that were/would have been stored into the buffer.
526 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
528 struct gcov_fn_info *fi_ptr;
532 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
533 pos += store_gcov_u32(buffer, pos, info->version);
534 pos += store_gcov_u32(buffer, pos, info->checksum);
536 list_for_each_entry(fi_ptr, &info->functions, head) {
539 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
540 #if CONFIG_CLANG_VERSION < 110000
541 pos += store_gcov_u32(buffer, pos,
542 fi_ptr->use_extra_checksum ? 3 : 2);
544 pos += store_gcov_u32(buffer, pos, 3);
546 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
547 pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
548 #if CONFIG_CLANG_VERSION < 110000
549 if (fi_ptr->use_extra_checksum)
550 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
552 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
555 pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
556 pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
557 for (i = 0; i < fi_ptr->num_counters; i++)
558 pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);
565 * gcov_iter_new - allocate and initialize profiling data iterator
566 * @info: profiling data set to be iterated
568 * Return file iterator on success, %NULL otherwise.
570 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
572 struct gcov_iterator *iter;
574 iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
579 /* Dry-run to get the actual buffer size. */
580 iter->size = convert_to_gcda(NULL, info);
581 iter->buffer = vmalloc(iter->size);
585 convert_to_gcda(iter->buffer, info);
596 * gcov_iter_get_info - return profiling data set for given file iterator
597 * @iter: file iterator
599 void gcov_iter_free(struct gcov_iterator *iter)
606 * gcov_iter_get_info - return profiling data set for given file iterator
607 * @iter: file iterator
609 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
615 * gcov_iter_start - reset file iterator to starting position
616 * @iter: file iterator
618 void gcov_iter_start(struct gcov_iterator *iter)
624 * gcov_iter_next - advance file iterator to next logical record
625 * @iter: file iterator
627 * Return zero if new position is valid, non-zero if iterator has reached end.
629 int gcov_iter_next(struct gcov_iterator *iter)
631 if (iter->pos < iter->size)
632 iter->pos += ITER_STRIDE;
634 if (iter->pos >= iter->size)
641 * gcov_iter_write - write data for current pos to seq_file
642 * @iter: file iterator
643 * @seq: seq_file handle
645 * Return zero on success, non-zero otherwise.
647 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
651 if (iter->pos >= iter->size)
655 if (iter->pos + len > iter->size)
656 len = iter->size - iter->pos;
658 seq_write(seq, iter->buffer + iter->pos, len);