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];
333 #if CONFIG_CLANG_VERSION < 110000
334 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
336 size_t cv_size; /* counter values size */
337 struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
341 INIT_LIST_HEAD(&fn_dup->head);
343 fn_dup->function_name = kstrdup(fn->function_name, GFP_KERNEL);
344 if (!fn_dup->function_name)
347 cv_size = fn->num_counters * sizeof(fn->counters[0]);
348 fn_dup->counters = vmalloc(cv_size);
349 if (!fn_dup->counters)
351 memcpy(fn_dup->counters, fn->counters, cv_size);
356 kfree(fn_dup->function_name);
362 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
364 size_t cv_size; /* counter values size */
365 struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
369 INIT_LIST_HEAD(&fn_dup->head);
371 cv_size = fn->num_counters * sizeof(fn->counters[0]);
372 fn_dup->counters = kvmalloc(cv_size, GFP_KERNEL);
373 if (!fn_dup->counters) {
378 memcpy(fn_dup->counters, fn->counters, cv_size);
385 * gcov_info_dup - duplicate profiling data set
386 * @info: profiling data set to duplicate
388 * Return newly allocated duplicate on success, %NULL on error.
390 struct gcov_info *gcov_info_dup(struct gcov_info *info)
392 struct gcov_info *dup;
393 struct gcov_fn_info *fn;
395 dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
398 INIT_LIST_HEAD(&dup->head);
399 INIT_LIST_HEAD(&dup->functions);
400 dup->filename = kstrdup(info->filename, GFP_KERNEL);
404 list_for_each_entry(fn, &info->functions, head) {
405 struct gcov_fn_info *fn_dup = gcov_fn_info_dup(fn);
409 list_add_tail(&fn_dup->head, &dup->functions);
420 * gcov_info_free - release memory for profiling data set duplicate
421 * @info: profiling data set duplicate to free
423 #if CONFIG_CLANG_VERSION < 110000
424 void gcov_info_free(struct gcov_info *info)
426 struct gcov_fn_info *fn, *tmp;
428 list_for_each_entry_safe(fn, tmp, &info->functions, head) {
429 kfree(fn->function_name);
434 kfree(info->filename);
438 void gcov_info_free(struct gcov_info *info)
440 struct gcov_fn_info *fn, *tmp;
442 list_for_each_entry_safe(fn, tmp, &info->functions, head) {
447 kfree(info->filename);
452 #define ITER_STRIDE PAGE_SIZE
455 * struct gcov_iterator - specifies current file position in logical records
456 * @info: associated profiling data
457 * @buffer: buffer containing file data
458 * @size: size of buffer
459 * @pos: current position in file
461 struct gcov_iterator {
462 struct gcov_info *info;
469 * store_gcov_u32 - store 32 bit number in gcov format to buffer
470 * @buffer: target buffer or NULL
471 * @off: offset into the buffer
472 * @v: value to be stored
474 * Number format defined by gcc: numbers are recorded in the 32 bit
475 * unsigned binary form of the endianness of the machine generating the
476 * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
479 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
488 return sizeof(*data);
492 * store_gcov_u64 - store 64 bit number in gcov format to buffer
493 * @buffer: target buffer or NULL
494 * @off: offset into the buffer
495 * @v: value to be stored
497 * Number format defined by gcc: numbers are recorded in the 32 bit
498 * unsigned binary form of the endianness of the machine generating the
499 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
500 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
503 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
510 data[0] = (v & 0xffffffffUL);
514 return sizeof(*data) * 2;
518 * convert_to_gcda - convert profiling data set to gcda file format
519 * @buffer: the buffer to store file data or %NULL if no data should be stored
520 * @info: profiling data set to be converted
522 * Returns the number of bytes that were/would have been stored into the buffer.
524 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
526 struct gcov_fn_info *fi_ptr;
530 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
531 pos += store_gcov_u32(buffer, pos, info->version);
532 pos += store_gcov_u32(buffer, pos, info->checksum);
534 list_for_each_entry(fi_ptr, &info->functions, head) {
537 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
538 #if CONFIG_CLANG_VERSION < 110000
539 pos += store_gcov_u32(buffer, pos,
540 fi_ptr->use_extra_checksum ? 3 : 2);
542 pos += store_gcov_u32(buffer, pos, 3);
544 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
545 pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
546 #if CONFIG_CLANG_VERSION < 110000
547 if (fi_ptr->use_extra_checksum)
548 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
550 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
553 pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
554 pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
555 for (i = 0; i < fi_ptr->num_counters; i++)
556 pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);
563 * gcov_iter_new - allocate and initialize profiling data iterator
564 * @info: profiling data set to be iterated
566 * Return file iterator on success, %NULL otherwise.
568 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
570 struct gcov_iterator *iter;
572 iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
577 /* Dry-run to get the actual buffer size. */
578 iter->size = convert_to_gcda(NULL, info);
579 iter->buffer = vmalloc(iter->size);
583 convert_to_gcda(iter->buffer, info);
594 * gcov_iter_get_info - return profiling data set for given file iterator
595 * @iter: file iterator
597 void gcov_iter_free(struct gcov_iterator *iter)
604 * gcov_iter_get_info - return profiling data set for given file iterator
605 * @iter: file iterator
607 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
613 * gcov_iter_start - reset file iterator to starting position
614 * @iter: file iterator
616 void gcov_iter_start(struct gcov_iterator *iter)
622 * gcov_iter_next - advance file iterator to next logical record
623 * @iter: file iterator
625 * Return zero if new position is valid, non-zero if iterator has reached end.
627 int gcov_iter_next(struct gcov_iterator *iter)
629 if (iter->pos < iter->size)
630 iter->pos += ITER_STRIDE;
632 if (iter->pos >= iter->size)
639 * gcov_iter_write - write data for current pos to seq_file
640 * @iter: file iterator
641 * @seq: seq_file handle
643 * Return zero on success, non-zero otherwise.
645 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
649 if (iter->pos >= iter->size)
653 if (iter->pos + len > iter->size)
654 len = iter->size - iter->pos;
656 seq_write(seq, iter->buffer + iter->pos, len);