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
;
72 #if CONFIG_CLANG_VERSION < 110000
73 u8 use_extra_checksum
;
79 #if CONFIG_CLANG_VERSION < 110000
80 const char *function_name
;
84 static struct gcov_info
*current_info
;
86 static LIST_HEAD(clang_gcov_list
);
88 void llvm_gcov_init(llvm_gcov_callback writeout
, llvm_gcov_callback flush
)
90 struct gcov_info
*info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
95 INIT_LIST_HEAD(&info
->head
);
96 INIT_LIST_HEAD(&info
->functions
);
98 mutex_lock(&gcov_lock
);
100 list_add_tail(&info
->head
, &clang_gcov_list
);
104 if (gcov_events_enabled
)
105 gcov_event(GCOV_ADD
, info
);
107 mutex_unlock(&gcov_lock
);
109 EXPORT_SYMBOL(llvm_gcov_init
);
111 #if CONFIG_CLANG_VERSION < 110000
112 void llvm_gcda_start_file(const char *orig_filename
, const char version
[4],
115 current_info
->filename
= orig_filename
;
116 memcpy(¤t_info
->version
, version
, sizeof(current_info
->version
));
117 current_info
->checksum
= checksum
;
119 EXPORT_SYMBOL(llvm_gcda_start_file
);
121 void llvm_gcda_start_file(const char *orig_filename
, u32 version
, u32 checksum
)
123 current_info
->filename
= orig_filename
;
124 current_info
->version
= version
;
125 current_info
->checksum
= checksum
;
127 EXPORT_SYMBOL(llvm_gcda_start_file
);
130 #if CONFIG_CLANG_VERSION < 110000
131 void llvm_gcda_emit_function(u32 ident
, const char *function_name
,
132 u32 func_checksum
, u8 use_extra_checksum
, u32 cfg_checksum
)
134 struct gcov_fn_info
*info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
139 INIT_LIST_HEAD(&info
->head
);
141 info
->checksum
= func_checksum
;
142 info
->use_extra_checksum
= use_extra_checksum
;
143 info
->cfg_checksum
= cfg_checksum
;
145 info
->function_name
= kstrdup(function_name
, GFP_KERNEL
);
147 list_add_tail(&info
->head
, ¤t_info
->functions
);
150 void llvm_gcda_emit_function(u32 ident
, u32 func_checksum
, u32 cfg_checksum
)
152 struct gcov_fn_info
*info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
157 INIT_LIST_HEAD(&info
->head
);
159 info
->checksum
= func_checksum
;
160 info
->cfg_checksum
= cfg_checksum
;
161 list_add_tail(&info
->head
, ¤t_info
->functions
);
164 EXPORT_SYMBOL(llvm_gcda_emit_function
);
166 void llvm_gcda_emit_arcs(u32 num_counters
, u64
*counters
)
168 struct gcov_fn_info
*info
= list_last_entry(¤t_info
->functions
,
169 struct gcov_fn_info
, head
);
171 info
->num_counters
= num_counters
;
172 info
->counters
= counters
;
174 EXPORT_SYMBOL(llvm_gcda_emit_arcs
);
176 void llvm_gcda_summary_info(void)
179 EXPORT_SYMBOL(llvm_gcda_summary_info
);
181 void llvm_gcda_end_file(void)
184 EXPORT_SYMBOL(llvm_gcda_end_file
);
187 * gcov_info_filename - return info filename
188 * @info: profiling data set
190 const char *gcov_info_filename(struct gcov_info
*info
)
192 return info
->filename
;
196 * gcov_info_version - return info version
197 * @info: profiling data set
199 unsigned int gcov_info_version(struct gcov_info
*info
)
201 return info
->version
;
205 * gcov_info_next - return next profiling data set
206 * @info: profiling data set
208 * Returns next gcov_info following @info or first gcov_info in the chain if
211 struct gcov_info
*gcov_info_next(struct gcov_info
*info
)
214 return list_first_entry_or_null(&clang_gcov_list
,
215 struct gcov_info
, head
);
216 if (list_is_last(&info
->head
, &clang_gcov_list
))
218 return list_next_entry(info
, head
);
222 * gcov_info_link - link/add profiling data set to the list
223 * @info: profiling data set
225 void gcov_info_link(struct gcov_info
*info
)
227 list_add_tail(&info
->head
, &clang_gcov_list
);
231 * gcov_info_unlink - unlink/remove profiling data set from the list
232 * @prev: previous profiling data set
233 * @info: profiling data set
235 void gcov_info_unlink(struct gcov_info
*prev
, struct gcov_info
*info
)
237 /* Generic code unlinks while iterating. */
238 __list_del_entry(&info
->head
);
242 * gcov_info_within_module - check if a profiling data set belongs to a module
243 * @info: profiling data set
246 * Returns true if profiling data belongs module, false otherwise.
248 bool gcov_info_within_module(struct gcov_info
*info
, struct module
*mod
)
250 return within_module((unsigned long)info
->filename
, mod
);
253 /* Symbolic links to be created for each profiling data file. */
254 const struct gcov_link gcov_link
[] = {
255 { OBJ_TREE
, "gcno" }, /* Link to .gcno file in $(objtree). */
260 * gcov_info_reset - reset profiling data to zero
261 * @info: profiling data set
263 void gcov_info_reset(struct gcov_info
*info
)
265 struct gcov_fn_info
*fn
;
267 list_for_each_entry(fn
, &info
->functions
, head
)
268 memset(fn
->counters
, 0,
269 sizeof(fn
->counters
[0]) * fn
->num_counters
);
273 * gcov_info_is_compatible - check if profiling data can be added
274 * @info1: first profiling data set
275 * @info2: second profiling data set
277 * Returns non-zero if profiling data can be added, zero otherwise.
279 int gcov_info_is_compatible(struct gcov_info
*info1
, struct gcov_info
*info2
)
281 struct gcov_fn_info
*fn_ptr1
= list_first_entry_or_null(
282 &info1
->functions
, struct gcov_fn_info
, head
);
283 struct gcov_fn_info
*fn_ptr2
= list_first_entry_or_null(
284 &info2
->functions
, struct gcov_fn_info
, head
);
286 if (info1
->checksum
!= info2
->checksum
)
289 return fn_ptr1
== fn_ptr2
;
290 while (!list_is_last(&fn_ptr1
->head
, &info1
->functions
) &&
291 !list_is_last(&fn_ptr2
->head
, &info2
->functions
)) {
292 if (fn_ptr1
->checksum
!= fn_ptr2
->checksum
)
294 #if CONFIG_CLANG_VERSION < 110000
295 if (fn_ptr1
->use_extra_checksum
!= fn_ptr2
->use_extra_checksum
)
297 if (fn_ptr1
->use_extra_checksum
&&
298 fn_ptr1
->cfg_checksum
!= fn_ptr2
->cfg_checksum
)
301 if (fn_ptr1
->cfg_checksum
!= fn_ptr2
->cfg_checksum
)
304 fn_ptr1
= list_next_entry(fn_ptr1
, head
);
305 fn_ptr2
= list_next_entry(fn_ptr2
, head
);
307 return list_is_last(&fn_ptr1
->head
, &info1
->functions
) &&
308 list_is_last(&fn_ptr2
->head
, &info2
->functions
);
312 * gcov_info_add - add up profiling data
313 * @dest: profiling data set to which data is added
314 * @source: profiling data set which is added
316 * Adds profiling counts of @source to @dest.
318 void gcov_info_add(struct gcov_info
*dst
, struct gcov_info
*src
)
320 struct gcov_fn_info
*dfn_ptr
;
321 struct gcov_fn_info
*sfn_ptr
= list_first_entry_or_null(&src
->functions
,
322 struct gcov_fn_info
, head
);
324 list_for_each_entry(dfn_ptr
, &dst
->functions
, head
) {
327 for (i
= 0; i
< sfn_ptr
->num_counters
; i
++)
328 dfn_ptr
->counters
[i
] += sfn_ptr
->counters
[i
];
332 #if CONFIG_CLANG_VERSION < 110000
333 static struct gcov_fn_info
*gcov_fn_info_dup(struct gcov_fn_info
*fn
)
335 size_t cv_size
; /* counter values size */
336 struct gcov_fn_info
*fn_dup
= kmemdup(fn
, sizeof(*fn
), GFP_KERNEL
);
340 INIT_LIST_HEAD(&fn_dup
->head
);
342 fn_dup
->function_name
= kstrdup(fn
->function_name
, GFP_KERNEL
);
343 if (!fn_dup
->function_name
)
346 cv_size
= fn
->num_counters
* sizeof(fn
->counters
[0]);
347 fn_dup
->counters
= kvmalloc(cv_size
, GFP_KERNEL
);
348 if (!fn_dup
->counters
)
350 memcpy(fn_dup
->counters
, fn
->counters
, cv_size
);
355 kfree(fn_dup
->function_name
);
361 static struct gcov_fn_info
*gcov_fn_info_dup(struct gcov_fn_info
*fn
)
363 size_t cv_size
; /* counter values size */
364 struct gcov_fn_info
*fn_dup
= kmemdup(fn
, sizeof(*fn
),
368 INIT_LIST_HEAD(&fn_dup
->head
);
370 cv_size
= fn
->num_counters
* sizeof(fn
->counters
[0]);
371 fn_dup
->counters
= kvmalloc(cv_size
, GFP_KERNEL
);
372 if (!fn_dup
->counters
) {
377 memcpy(fn_dup
->counters
, fn
->counters
, cv_size
);
384 * gcov_info_dup - duplicate profiling data set
385 * @info: profiling data set to duplicate
387 * Return newly allocated duplicate on success, %NULL on error.
389 struct gcov_info
*gcov_info_dup(struct gcov_info
*info
)
391 struct gcov_info
*dup
;
392 struct gcov_fn_info
*fn
;
394 dup
= kmemdup(info
, sizeof(*dup
), GFP_KERNEL
);
397 INIT_LIST_HEAD(&dup
->head
);
398 INIT_LIST_HEAD(&dup
->functions
);
399 dup
->filename
= kstrdup(info
->filename
, GFP_KERNEL
);
403 list_for_each_entry(fn
, &info
->functions
, head
) {
404 struct gcov_fn_info
*fn_dup
= gcov_fn_info_dup(fn
);
408 list_add_tail(&fn_dup
->head
, &dup
->functions
);
419 * gcov_info_free - release memory for profiling data set duplicate
420 * @info: profiling data set duplicate to free
422 #if CONFIG_CLANG_VERSION < 110000
423 void gcov_info_free(struct gcov_info
*info
)
425 struct gcov_fn_info
*fn
, *tmp
;
427 list_for_each_entry_safe(fn
, tmp
, &info
->functions
, head
) {
428 kfree(fn
->function_name
);
433 kfree(info
->filename
);
437 void gcov_info_free(struct gcov_info
*info
)
439 struct gcov_fn_info
*fn
, *tmp
;
441 list_for_each_entry_safe(fn
, tmp
, &info
->functions
, head
) {
442 kvfree(fn
->counters
);
446 kfree(info
->filename
);
452 * convert_to_gcda - convert profiling data set to gcda file format
453 * @buffer: the buffer to store file data or %NULL if no data should be stored
454 * @info: profiling data set to be converted
456 * Returns the number of bytes that were/would have been stored into the buffer.
458 size_t convert_to_gcda(char *buffer
, struct gcov_info
*info
)
460 struct gcov_fn_info
*fi_ptr
;
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
->checksum
);
468 list_for_each_entry(fi_ptr
, &info
->functions
, head
) {
471 pos
+= store_gcov_u32(buffer
, pos
, GCOV_TAG_FUNCTION
);
472 #if CONFIG_CLANG_VERSION < 110000
473 pos
+= store_gcov_u32(buffer
, pos
,
474 fi_ptr
->use_extra_checksum
? 3 : 2);
476 pos
+= store_gcov_u32(buffer
, pos
, 3);
478 pos
+= store_gcov_u32(buffer
, pos
, fi_ptr
->ident
);
479 pos
+= store_gcov_u32(buffer
, pos
, fi_ptr
->checksum
);
480 #if CONFIG_CLANG_VERSION < 110000
481 if (fi_ptr
->use_extra_checksum
)
482 pos
+= store_gcov_u32(buffer
, pos
, fi_ptr
->cfg_checksum
);
484 pos
+= store_gcov_u32(buffer
, pos
, fi_ptr
->cfg_checksum
);
487 pos
+= store_gcov_u32(buffer
, pos
, GCOV_TAG_COUNTER_BASE
);
488 pos
+= store_gcov_u32(buffer
, pos
, fi_ptr
->num_counters
* 2);
489 for (i
= 0; i
< fi_ptr
->num_counters
; i
++)
490 pos
+= store_gcov_u64(buffer
, pos
, fi_ptr
->counters
[i
]);