^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2019 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * modified from kernel/gcov/gcc_4_7.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This software is licensed under the terms of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * License version 2, as published by the Free Software Foundation, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * may be copied, distributed, and modified under those terms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * LLVM uses profiling data that's deliberately similar to GCC, but has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * very different way of exporting that data. LLVM calls llvm_gcov_init() once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * per module, and provides a couple of callbacks that we can use to ask for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * more data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * We care about the "writeout" callback, which in turn calls back into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * compiler-rt/this module to dump all the gathered coverage data to disk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * llvm_gcda_start_file()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * llvm_gcda_emit_function()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * llvm_gcda_emit_arcs()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * llvm_gcda_emit_function()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * llvm_gcda_emit_arcs()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * [... repeats for each function ...]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * llvm_gcda_summary_info()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * llvm_gcda_end_file()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * This design is much more stateless and unstructured than gcc's, and is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * intended to run at process exit. This forces us to keep some local state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * about which module we're dealing with at the moment. On the other hand, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * also means we don't depend as much on how LLVM represents profiling data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * internally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * details on how this works, particularly GCOVProfiler::emitProfileArcs(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * GCOVProfiler::insertCounterWriteout(), and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * GCOVProfiler::insertFlush().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define pr_fmt(fmt) "gcov: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include "gcov.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) typedef void (*llvm_gcov_callback)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct gcov_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct list_head head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) const char *filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned int version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) u32 checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct list_head functions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct gcov_fn_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct list_head head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u32 ident;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u32 checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #if CONFIG_CLANG_VERSION < 110000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) u8 use_extra_checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u32 cfg_checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) u32 num_counters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) u64 *counters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #if CONFIG_CLANG_VERSION < 110000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) const char *function_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static struct gcov_info *current_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static LIST_HEAD(clang_gcov_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void llvm_gcov_init(llvm_gcov_callback writeout, llvm_gcov_callback flush)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct gcov_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) INIT_LIST_HEAD(&info->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) INIT_LIST_HEAD(&info->functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) mutex_lock(&gcov_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) list_add_tail(&info->head, &clang_gcov_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) current_info = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) writeout();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) current_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (gcov_events_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) gcov_event(GCOV_ADD, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) mutex_unlock(&gcov_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) EXPORT_SYMBOL(llvm_gcov_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #if CONFIG_CLANG_VERSION < 110000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) void llvm_gcda_start_file(const char *orig_filename, const char version[4],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) u32 checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) current_info->filename = orig_filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) memcpy(¤t_info->version, version, sizeof(current_info->version));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) current_info->checksum = checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) EXPORT_SYMBOL(llvm_gcda_start_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) void llvm_gcda_start_file(const char *orig_filename, u32 version, u32 checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) current_info->filename = orig_filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) current_info->version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) current_info->checksum = checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) EXPORT_SYMBOL(llvm_gcda_start_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #if CONFIG_CLANG_VERSION < 110000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) void llvm_gcda_emit_function(u32 ident, const char *function_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) u32 func_checksum, u8 use_extra_checksum, u32 cfg_checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) INIT_LIST_HEAD(&info->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) info->ident = ident;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) info->checksum = func_checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) info->use_extra_checksum = use_extra_checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) info->cfg_checksum = cfg_checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (function_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) info->function_name = kstrdup(function_name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) list_add_tail(&info->head, ¤t_info->functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void llvm_gcda_emit_function(u32 ident, u32 func_checksum, u32 cfg_checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) INIT_LIST_HEAD(&info->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) info->ident = ident;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) info->checksum = func_checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) info->cfg_checksum = cfg_checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) list_add_tail(&info->head, ¤t_info->functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) EXPORT_SYMBOL(llvm_gcda_emit_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct gcov_fn_info *info = list_last_entry(¤t_info->functions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct gcov_fn_info, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) info->num_counters = num_counters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) info->counters = counters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) EXPORT_SYMBOL(llvm_gcda_emit_arcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) void llvm_gcda_summary_info(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) EXPORT_SYMBOL(llvm_gcda_summary_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) void llvm_gcda_end_file(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) EXPORT_SYMBOL(llvm_gcda_end_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * gcov_info_filename - return info filename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * @info: profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) const char *gcov_info_filename(struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return info->filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * gcov_info_version - return info version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @info: profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) unsigned int gcov_info_version(struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return info->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * gcov_info_next - return next profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * @info: profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * Returns next gcov_info following @info or first gcov_info in the chain if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * @info is %NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct gcov_info *gcov_info_next(struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return list_first_entry_or_null(&clang_gcov_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct gcov_info, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (list_is_last(&info->head, &clang_gcov_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return list_next_entry(info, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * gcov_info_link - link/add profiling data set to the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * @info: profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) void gcov_info_link(struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) list_add_tail(&info->head, &clang_gcov_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * gcov_info_unlink - unlink/remove profiling data set from the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * @prev: previous profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * @info: profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /* Generic code unlinks while iterating. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) __list_del_entry(&info->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * gcov_info_within_module - check if a profiling data set belongs to a module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * @info: profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * @mod: module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * Returns true if profiling data belongs module, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return within_module((unsigned long)info->filename, mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /* Symbolic links to be created for each profiling data file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) const struct gcov_link gcov_link[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) { 0, NULL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * gcov_info_reset - reset profiling data to zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * @info: profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) void gcov_info_reset(struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct gcov_fn_info *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) list_for_each_entry(fn, &info->functions, head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) memset(fn->counters, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) sizeof(fn->counters[0]) * fn->num_counters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * gcov_info_is_compatible - check if profiling data can be added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * @info1: first profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * @info2: second profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * Returns non-zero if profiling data can be added, zero otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct gcov_fn_info *fn_ptr1 = list_first_entry_or_null(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) &info1->functions, struct gcov_fn_info, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct gcov_fn_info *fn_ptr2 = list_first_entry_or_null(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) &info2->functions, struct gcov_fn_info, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (info1->checksum != info2->checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (!fn_ptr1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return fn_ptr1 == fn_ptr2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) while (!list_is_last(&fn_ptr1->head, &info1->functions) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) !list_is_last(&fn_ptr2->head, &info2->functions)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (fn_ptr1->checksum != fn_ptr2->checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) #if CONFIG_CLANG_VERSION < 110000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (fn_ptr1->use_extra_checksum != fn_ptr2->use_extra_checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (fn_ptr1->use_extra_checksum &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) fn_ptr1 = list_next_entry(fn_ptr1, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) fn_ptr2 = list_next_entry(fn_ptr2, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return list_is_last(&fn_ptr1->head, &info1->functions) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) list_is_last(&fn_ptr2->head, &info2->functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * gcov_info_add - add up profiling data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * @dest: profiling data set to which data is added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * @source: profiling data set which is added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * Adds profiling counts of @source to @dest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct gcov_fn_info *dfn_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct gcov_fn_info *sfn_ptr = list_first_entry_or_null(&src->functions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) struct gcov_fn_info, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) list_for_each_entry(dfn_ptr, &dst->functions, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) for (i = 0; i < sfn_ptr->num_counters; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) dfn_ptr->counters[i] += sfn_ptr->counters[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) #if CONFIG_CLANG_VERSION < 110000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) size_t cv_size; /* counter values size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (!fn_dup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) INIT_LIST_HEAD(&fn_dup->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) fn_dup->function_name = kstrdup(fn->function_name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (!fn_dup->function_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) goto err_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) cv_size = fn->num_counters * sizeof(fn->counters[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) fn_dup->counters = vmalloc(cv_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (!fn_dup->counters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) goto err_counters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) memcpy(fn_dup->counters, fn->counters, cv_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return fn_dup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) err_counters:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) kfree(fn_dup->function_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) err_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) kfree(fn_dup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) size_t cv_size; /* counter values size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (!fn_dup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) INIT_LIST_HEAD(&fn_dup->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) cv_size = fn->num_counters * sizeof(fn->counters[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) fn_dup->counters = vmalloc(cv_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (!fn_dup->counters) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) kfree(fn_dup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) memcpy(fn_dup->counters, fn->counters, cv_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return fn_dup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * gcov_info_dup - duplicate profiling data set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * @info: profiling data set to duplicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * Return newly allocated duplicate on success, %NULL on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) struct gcov_info *gcov_info_dup(struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) struct gcov_info *dup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct gcov_fn_info *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (!dup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) INIT_LIST_HEAD(&dup->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) INIT_LIST_HEAD(&dup->functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) dup->filename = kstrdup(info->filename, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (!dup->filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) list_for_each_entry(fn, &info->functions, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) struct gcov_fn_info *fn_dup = gcov_fn_info_dup(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (!fn_dup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) list_add_tail(&fn_dup->head, &dup->functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return dup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) gcov_info_free(dup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * gcov_info_free - release memory for profiling data set duplicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * @info: profiling data set duplicate to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) #if CONFIG_CLANG_VERSION < 110000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) void gcov_info_free(struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) struct gcov_fn_info *fn, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) list_for_each_entry_safe(fn, tmp, &info->functions, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) kfree(fn->function_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) vfree(fn->counters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) list_del(&fn->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) kfree(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) kfree(info->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) void gcov_info_free(struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) struct gcov_fn_info *fn, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) list_for_each_entry_safe(fn, tmp, &info->functions, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) vfree(fn->counters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) list_del(&fn->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) kfree(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) kfree(info->filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) #define ITER_STRIDE PAGE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * struct gcov_iterator - specifies current file position in logical records
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * @info: associated profiling data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * @buffer: buffer containing file data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * @size: size of buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * @pos: current position in file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) struct gcov_iterator {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) struct gcov_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) void *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) loff_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * store_gcov_u32 - store 32 bit number in gcov format to buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * @buffer: target buffer or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * @off: offset into the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * @v: value to be stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * Number format defined by gcc: numbers are recorded in the 32 bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * unsigned binary form of the endianness of the machine generating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * store anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) u32 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) data = buffer + off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) *data = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return sizeof(*data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) * store_gcov_u64 - store 64 bit number in gcov format to buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * @buffer: target buffer or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * @off: offset into the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * @v: value to be stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * Number format defined by gcc: numbers are recorded in the 32 bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) * unsigned binary form of the endianness of the machine generating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * file. 64 bit numbers are stored as two 32 bit numbers, the low part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) u32 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) data = buffer + off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) data[0] = (v & 0xffffffffUL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) data[1] = (v >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) return sizeof(*data) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * convert_to_gcda - convert profiling data set to gcda file format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * @buffer: the buffer to store file data or %NULL if no data should be stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) * @info: profiling data set to be converted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * Returns the number of bytes that were/would have been stored into the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct gcov_fn_info *fi_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) size_t pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /* File header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) pos += store_gcov_u32(buffer, pos, info->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) pos += store_gcov_u32(buffer, pos, info->checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) list_for_each_entry(fi_ptr, &info->functions, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) #if CONFIG_CLANG_VERSION < 110000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) pos += store_gcov_u32(buffer, pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) fi_ptr->use_extra_checksum ? 3 : 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) pos += store_gcov_u32(buffer, pos, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) #if CONFIG_CLANG_VERSION < 110000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (fi_ptr->use_extra_checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) for (i = 0; i < fi_ptr->num_counters; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) * gcov_iter_new - allocate and initialize profiling data iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * @info: profiling data set to be iterated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * Return file iterator on success, %NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) struct gcov_iterator *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (!iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) iter->info = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) /* Dry-run to get the actual buffer size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) iter->size = convert_to_gcda(NULL, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) iter->buffer = vmalloc(iter->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (!iter->buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) convert_to_gcda(iter->buffer, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) kfree(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) * gcov_iter_get_info - return profiling data set for given file iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) * @iter: file iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) void gcov_iter_free(struct gcov_iterator *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) vfree(iter->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) kfree(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) * gcov_iter_get_info - return profiling data set for given file iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * @iter: file iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) return iter->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * gcov_iter_start - reset file iterator to starting position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * @iter: file iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) void gcov_iter_start(struct gcov_iterator *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) iter->pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * gcov_iter_next - advance file iterator to next logical record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * @iter: file iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * Return zero if new position is valid, non-zero if iterator has reached end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) int gcov_iter_next(struct gcov_iterator *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (iter->pos < iter->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) iter->pos += ITER_STRIDE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) if (iter->pos >= iter->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) * gcov_iter_write - write data for current pos to seq_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * @iter: file iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * @seq: seq_file handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * Return zero on success, non-zero otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (iter->pos >= iter->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) len = ITER_STRIDE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (iter->pos + len > iter->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) len = iter->size - iter->pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) seq_write(seq, iter->buffer + iter->pos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) }