[PATCH v2][makedumpfile 10/14] Implement kernel module's btf resolving
Tao Liu
ltao at redhat.com
Mon Oct 20 15:24:06 PDT 2025
Signed-off-by: Tao Liu <ltao at redhat.com>
---
btf.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
btf.h | 2 +
2 files changed, 123 insertions(+)
diff --git a/btf.c b/btf.c
index ba376cf..5dd2047 100644
--- a/btf.c
+++ b/btf.c
@@ -796,3 +796,124 @@ int init_kernel_btf(void)
return ret;
}
+int init_module_btf(void)
+{
+ uint64_t btf_modules, list;
+ struct member_info mi;
+ uint64_t btf = 0, data = 0, module = 0;
+ int data_size = 0;
+ char *btf_buf = NULL;
+ char *modname = NULL;
+
+ btf_modules = get_kallsyms_value_by_name("btf_modules");
+ if (!btf_modules)
+ /* Maybe module is not enabled, this is not an error */
+ return 0;
+
+ INIT_MEMBER_OFF_SIZE(btf_module, list);
+ INIT_MEMBER_OFF_SIZE(btf_module, btf);
+ INIT_MEMBER_OFF_SIZE(btf_module, module);
+ INIT_MEMBER_OFF_SIZE(module, name);
+ INIT_MEMBER_OFF_SIZE(btf, data);
+ INIT_MEMBER_OFF_SIZE(btf, data_size);
+ modname = (char *)malloc(GET_MEMBER_SIZE(module, name));
+ if (!modname) {
+ fprintf(stderr, "%s: Not enough memory!\n", __func__);
+ goto out;
+ }
+
+ for (list = next_list(btf_modules); list != btf_modules; list = next_list(list)) {
+ readmem(VADDR, list - GET_MEMBER_OFF(btf_module, list) +
+ GET_MEMBER_OFF(btf_module, btf),
+ &btf, GET_MEMBER_SIZE(btf_module, btf));
+ readmem(VADDR, list - GET_MEMBER_OFF(btf_module, list) +
+ GET_MEMBER_OFF(btf_module, module),
+ &module, GET_MEMBER_SIZE(btf_module, module));
+ readmem(VADDR, module + GET_MEMBER_OFF(module, name),
+ modname, GET_MEMBER_SIZE(module, name));
+ readmem(VADDR, btf + GET_MEMBER_OFF(btf, data),
+ &data, GET_MEMBER_SIZE(btf, data));
+ readmem(VADDR, btf + GET_MEMBER_OFF(btf, data_size),
+ &data_size, GET_MEMBER_SIZE(btf, data_size));
+ btf_buf = (char *)malloc(data_size);
+ if (!btf_buf) {
+ fprintf(stderr, "%s: Not enough memory!\n", __func__);
+ goto out;
+ }
+ readmem(VADDR, data, btf_buf, data_size);
+ if (init_btf_from_buf(modname, btf_buf, data_size))
+ goto out;
+ free(btf_buf);
+ }
+ return 0;
+out:
+ if (modname)
+ free(modname);
+ if (btf_buf)
+ free(btf_buf);
+ return -1;
+}
+
+void cleanup_btf(void)
+{
+ int bf_index, i;
+ struct btf_file *bf;
+ struct name_entry *en, *en_tmp;
+ DIR *temp_dir;
+ struct dirent *entry;
+ char path[512];
+
+ /* Free no-hashtable-installed elements */
+ for (bf_index = 0; bf_index < btf_files_array_index; bf_index++) {
+ bf = btf_files_array[bf_index];
+ if (bf && bf->array) {
+ for (i = 0; i < bf->array_len; i++) {
+ if (!bf->array[i]->name || !bf->array[i]->name[0])
+ free(bf->array[i]);
+ }
+ }
+ }
+
+ /* Free the hashtable-installed elements */
+ for (i = 0; i < NAME_HASH; i++) {
+ for (en = name_hash_table[i]; en;) {
+ en_tmp = en;
+ en = en->name_hash_next;
+ free(en_tmp);
+ }
+ }
+
+ /* Cleanup anything else */
+ for (bf_index = 0; bf_index < btf_files_array_index; bf_index++) {
+ bf = btf_files_array[bf_index];
+ if (bf && bf->array) {
+ free(bf->array);
+ }
+ if (bf && bf->file_name)
+ free(bf->file_name);
+ if (bf && bf->str_cache)
+ free(bf->str_cache);
+ if (bf)
+ free(bf);
+ }
+
+ if (btf_files_array)
+ free(btf_files_array);
+
+ /* Cleanup the dir /tmp/btf_XXXXXX */
+ temp_dir = opendir(temp);
+ if (!temp_dir)
+ return;
+ while ((entry = readdir(temp_dir)) != NULL) {
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+ continue;
+ snprintf(path, sizeof(path), "%s/%s", temp, entry->d_name);
+ if (remove(path)) {
+ fprintf(stderr, "%s: fail del %s!\n", __func__, path);
+ /* In case too many fail messages of files in the dir */
+ return;
+ }
+ }
+ closedir(temp_dir);
+ rmdir(temp);
+}
\ No newline at end of file
diff --git a/btf.h b/btf.h
index 424e9e0..2fcdfe1 100644
--- a/btf.h
+++ b/btf.h
@@ -166,6 +166,8 @@ void resolve_typedef(struct name_entry *, struct name_entry **, struct btf_type
int get_btf_type_by_type_id(struct btf_file *, uint32_t,
struct btf_type *, struct name_entry **);
uint32_t id_to_uniq_id(uint32_t, struct btf_file *);
+int init_module_btf(void);
+void cleanup_btf(void);
static inline uint32_t btf_kind(uint32_t info)
{
--
2.47.0
More information about the kexec
mailing list