Blob Blame History Raw
From: Andrii Nakryiko <andriin@fb.com>
Date: Tue, 18 Aug 2020 15:39:13 -0700
Subject: libbpf: Improve error logging for mismatched BTF kind cases
Patch-mainline: v5.10-rc1
Git-commit: 81ba0889027505b7d5136319117e473a69a923c4
References: bsc#1177028

Instead of printing out integer value of BTF kind, print out a string
representation of a kind.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200818223921.2911963-2-andriin@fb.com
Acked-by: Gary Lin <glin@suse.com>
---
 tools/lib/bpf/libbpf.c |   59 ++++++++++++++++++++++++++++++++++---------------
 1 file changed, 41 insertions(+), 18 deletions(-)

--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1884,6 +1884,29 @@ resolve_func_ptr(const struct btf *btf,
 	return btf_is_func_proto(t) ? t : NULL;
 }
 
+static const char *btf_kind_str(const struct btf_type *t)
+{
+	switch (btf_kind(t)) {
+	case BTF_KIND_UNKN: return "void";
+	case BTF_KIND_INT: return "int";
+	case BTF_KIND_PTR: return "ptr";
+	case BTF_KIND_ARRAY: return "array";
+	case BTF_KIND_STRUCT: return "struct";
+	case BTF_KIND_UNION: return "union";
+	case BTF_KIND_ENUM: return "enum";
+	case BTF_KIND_FWD: return "fwd";
+	case BTF_KIND_TYPEDEF: return "typedef";
+	case BTF_KIND_VOLATILE: return "volatile";
+	case BTF_KIND_CONST: return "const";
+	case BTF_KIND_RESTRICT: return "restrict";
+	case BTF_KIND_FUNC: return "func";
+	case BTF_KIND_FUNC_PROTO: return "func_proto";
+	case BTF_KIND_VAR: return "var";
+	case BTF_KIND_DATASEC: return "datasec";
+	default: return "unknown";
+	}
+}
+
 /*
  * Fetch integer attribute of BTF map definition. Such attributes are
  * represented using a pointer to an array, in which dimensionality of array
@@ -1900,8 +1923,8 @@ static bool get_map_field_int(const char
 	const struct btf_type *arr_t;
 
 	if (!btf_is_ptr(t)) {
-		pr_warning("map '%s': attr '%s': expected PTR, got %u.\n",
-			   map_name, name, btf_kind(t));
+		pr_warning("map '%s': attr '%s': expected PTR, got %s.\n",
+			   map_name, name, btf_kind_str(t));
 		return false;
 	}
 
@@ -1912,8 +1935,8 @@ static bool get_map_field_int(const char
 		return false;
 	}
 	if (!btf_is_array(arr_t)) {
-		pr_warning("map '%s': attr '%s': expected ARRAY, got %u.\n",
-			   map_name, name, btf_kind(arr_t));
+		pr_warning("map '%s': attr '%s': expected ARRAY, got %s.\n",
+			   map_name, name, btf_kind_str(arr_t));
 		return false;
 	}
 	arr_info = btf_array(arr_t);
@@ -2007,8 +2030,8 @@ static int parse_btf_map_def(struct bpf_
 				return -EINVAL;
 			}
 			if (!btf_is_ptr(t)) {
-				pr_warning("map '%s': key spec is not PTR: %u.\n",
-					   map->name, btf_kind(t));
+				pr_warning("map '%s': key spec is not PTR: %s.\n",
+					   map->name, btf_kind_str(t));
 				return -EINVAL;
 			}
 			sz = btf__resolve_size(obj->btf, t->type);
@@ -2049,8 +2072,8 @@ static int parse_btf_map_def(struct bpf_
 				return -EINVAL;
 			}
 			if (!btf_is_ptr(t)) {
-				pr_warning("map '%s': value spec is not PTR: %u.\n",
-					   map->name, btf_kind(t));
+				pr_warning("map '%s': value spec is not PTR: %s.\n",
+					   map->name, btf_kind_str(t));
 				return -EINVAL;
 			}
 			sz = btf__resolve_size(obj->btf, t->type);
@@ -2107,14 +2130,14 @@ static int parse_btf_map_def(struct bpf_
 			t = skip_mods_and_typedefs(obj->btf, btf_array(t)->type,
 						   NULL);
 			if (!btf_is_ptr(t)) {
-				pr_warning("map '%s': map-in-map inner def is of unexpected kind %u.\n",
-					   map->name, btf_kind(t));
+				pr_warning("map '%s': map-in-map inner def is of unexpected kind %s.\n",
+					   map->name, btf_kind_str(t));
 				return -EINVAL;
 			}
 			t = skip_mods_and_typedefs(obj->btf, t->type, NULL);
 			if (!btf_is_struct(t)) {
-				pr_warning("map '%s': map-in-map inner def is of unexpected kind %u.\n",
-					   map->name, btf_kind(t));
+				pr_warning("map '%s': map-in-map inner def is of unexpected kind %s.\n",
+					   map->name, btf_kind_str(t));
 				return -EINVAL;
 			}
 
@@ -2205,8 +2228,8 @@ static int bpf_object__init_user_btf_map
 		return -EINVAL;
 	}
 	if (!btf_is_var(var)) {
-		pr_warning("map '%s': unexpected var kind %u.\n",
-			   map_name, btf_kind(var));
+		pr_warning("map '%s': unexpected var kind %s.\n",
+			   map_name, btf_kind_str(var));
 		return -EINVAL;
 	}
 	if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED &&
@@ -2218,8 +2241,8 @@ static int bpf_object__init_user_btf_map
 
 	def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
 	if (!btf_is_struct(def)) {
-		pr_warning("map '%s': unexpected def kind %u.\n",
-			   map_name, btf_kind(var));
+		pr_warning("map '%s': unexpected def kind %s.\n",
+			   map_name, btf_kind_str(var));
 		return -EINVAL;
 	}
 	if (def->size > vi->size) {
@@ -4185,8 +4208,8 @@ static int bpf_core_spec_parse(const str
 				return sz;
 			spec->bit_offset += access_idx * sz * 8;
 		} else {
-			pr_warning("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %d\n",
-				   type_id, spec_str, i, id, btf_kind(t));
+			pr_warning("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %s\n",
+				   type_id, spec_str, i, id, btf_kind_str(t));
 			return -EINVAL;
 		}
 	}