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/slab.h>
55 typedef void (*llvm_gcov_callback)(void);
58 struct list_head head;
64 struct list_head functions;
68 struct list_head head;
78 static struct gcov_info *current_info;
80 static LIST_HEAD(clang_gcov_list);
82 void llvm_gcov_init(llvm_gcov_callback writeout, llvm_gcov_callback flush)
84 struct gcov_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
89 INIT_LIST_HEAD(&info->head);
90 INIT_LIST_HEAD(&info->functions);
92 mutex_lock(&gcov_lock);
94 list_add_tail(&info->head, &clang_gcov_list);
98 if (gcov_events_enabled)
99 gcov_event(GCOV_ADD, info);
101 mutex_unlock(&gcov_lock);
103 EXPORT_SYMBOL(llvm_gcov_init);
105 void llvm_gcda_start_file(const char *orig_filename, u32 version, u32 checksum)
107 current_info->filename = orig_filename;
108 current_info->version = version;
109 current_info->checksum = checksum;
111 EXPORT_SYMBOL(llvm_gcda_start_file);
113 void llvm_gcda_emit_function(u32 ident, u32 func_checksum, u32 cfg_checksum)
115 struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
120 INIT_LIST_HEAD(&info->head);
122 info->checksum = func_checksum;
123 info->cfg_checksum = cfg_checksum;
124 list_add_tail(&info->head, ¤t_info->functions);
126 EXPORT_SYMBOL(llvm_gcda_emit_function);
128 void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
130 struct gcov_fn_info *info = list_last_entry(¤t_info->functions,
131 struct gcov_fn_info, head);
133 info->num_counters = num_counters;
134 info->counters = counters;
136 EXPORT_SYMBOL(llvm_gcda_emit_arcs);
138 void llvm_gcda_summary_info(void)
141 EXPORT_SYMBOL(llvm_gcda_summary_info);
143 void llvm_gcda_end_file(void)
146 EXPORT_SYMBOL(llvm_gcda_end_file);
149 * gcov_info_filename - return info filename
150 * @info: profiling data set
152 const char *gcov_info_filename(struct gcov_info *info)
154 return info->filename;
158 * gcov_info_version - return info version
159 * @info: profiling data set
161 unsigned int gcov_info_version(struct gcov_info *info)
163 return info->version;
167 * gcov_info_next - return next profiling data set
168 * @info: profiling data set
170 * Returns next gcov_info following @info or first gcov_info in the chain if
173 struct gcov_info *gcov_info_next(struct gcov_info *info)
176 return list_first_entry_or_null(&clang_gcov_list,
177 struct gcov_info, head);
178 if (list_is_last(&info->head, &clang_gcov_list))
180 return list_next_entry(info, head);
184 * gcov_info_link - link/add profiling data set to the list
185 * @info: profiling data set
187 void gcov_info_link(struct gcov_info *info)
189 list_add_tail(&info->head, &clang_gcov_list);
193 * gcov_info_unlink - unlink/remove profiling data set from the list
194 * @prev: previous profiling data set
195 * @info: profiling data set
197 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
199 /* Generic code unlinks while iterating. */
200 __list_del_entry(&info->head);
204 * gcov_info_within_module - check if a profiling data set belongs to a module
205 * @info: profiling data set
208 * Returns true if profiling data belongs module, false otherwise.
210 bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
212 return within_module((unsigned long)info->filename, mod);
215 /* Symbolic links to be created for each profiling data file. */
216 const struct gcov_link gcov_link[] = {
217 { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
222 * gcov_info_reset - reset profiling data to zero
223 * @info: profiling data set
225 void gcov_info_reset(struct gcov_info *info)
227 struct gcov_fn_info *fn;
229 list_for_each_entry(fn, &info->functions, head)
230 memset(fn->counters, 0,
231 sizeof(fn->counters[0]) * fn->num_counters);
235 * gcov_info_is_compatible - check if profiling data can be added
236 * @info1: first profiling data set
237 * @info2: second profiling data set
239 * Returns non-zero if profiling data can be added, zero otherwise.
241 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
243 struct gcov_fn_info *fn_ptr1 = list_first_entry_or_null(
244 &info1->functions, struct gcov_fn_info, head);
245 struct gcov_fn_info *fn_ptr2 = list_first_entry_or_null(
246 &info2->functions, struct gcov_fn_info, head);
248 if (info1->checksum != info2->checksum)
251 return fn_ptr1 == fn_ptr2;
252 while (!list_is_last(&fn_ptr1->head, &info1->functions) &&
253 !list_is_last(&fn_ptr2->head, &info2->functions)) {
254 if (fn_ptr1->checksum != fn_ptr2->checksum)
256 if (fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
258 fn_ptr1 = list_next_entry(fn_ptr1, head);
259 fn_ptr2 = list_next_entry(fn_ptr2, head);
261 return list_is_last(&fn_ptr1->head, &info1->functions) &&
262 list_is_last(&fn_ptr2->head, &info2->functions);
266 * gcov_info_add - add up profiling data
267 * @dest: profiling data set to which data is added
268 * @source: profiling data set which is added
270 * Adds profiling counts of @source to @dest.
272 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
274 struct gcov_fn_info *dfn_ptr;
275 struct gcov_fn_info *sfn_ptr = list_first_entry_or_null(&src->functions,
276 struct gcov_fn_info, head);
278 list_for_each_entry(dfn_ptr, &dst->functions, head) {
281 for (i = 0; i < sfn_ptr->num_counters; i++)
282 dfn_ptr->counters[i] += sfn_ptr->counters[i];
286 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
288 size_t cv_size; /* counter values size */
289 struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
293 INIT_LIST_HEAD(&fn_dup->head);
295 cv_size = fn->num_counters * sizeof(fn->counters[0]);
296 fn_dup->counters = kvmalloc(cv_size, GFP_KERNEL);
297 if (!fn_dup->counters) {
302 memcpy(fn_dup->counters, fn->counters, cv_size);
308 * gcov_info_dup - duplicate profiling data set
309 * @info: profiling data set to duplicate
311 * Return newly allocated duplicate on success, %NULL on error.
313 struct gcov_info *gcov_info_dup(struct gcov_info *info)
315 struct gcov_info *dup;
316 struct gcov_fn_info *fn;
318 dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
321 INIT_LIST_HEAD(&dup->head);
322 INIT_LIST_HEAD(&dup->functions);
323 dup->filename = kstrdup(info->filename, GFP_KERNEL);
327 list_for_each_entry(fn, &info->functions, head) {
328 struct gcov_fn_info *fn_dup = gcov_fn_info_dup(fn);
332 list_add_tail(&fn_dup->head, &dup->functions);
343 * gcov_info_free - release memory for profiling data set duplicate
344 * @info: profiling data set duplicate to free
346 void gcov_info_free(struct gcov_info *info)
348 struct gcov_fn_info *fn, *tmp;
350 list_for_each_entry_safe(fn, tmp, &info->functions, head) {
351 kvfree(fn->counters);
355 kfree(info->filename);
360 * convert_to_gcda - convert profiling data set to gcda file format
361 * @buffer: the buffer to store file data or %NULL if no data should be stored
362 * @info: profiling data set to be converted
364 * Returns the number of bytes that were/would have been stored into the buffer.
366 size_t convert_to_gcda(char *buffer, struct gcov_info *info)
368 struct gcov_fn_info *fi_ptr;
372 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
373 pos += store_gcov_u32(buffer, pos, info->version);
374 pos += store_gcov_u32(buffer, pos, info->checksum);
376 list_for_each_entry(fi_ptr, &info->functions, head) {
379 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
380 pos += store_gcov_u32(buffer, pos, 3);
381 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
382 pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
383 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
384 pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
385 pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
386 for (i = 0; i < fi_ptr->num_counters; i++)
387 pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);