Commit 81ba0889 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: Improve error logging for mismatched BTF kind cases

Instead of printing out integer value of BTF kind, print out a string
representation of a kind.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200818223921.2911963-2-andriin@fb.com
parent a12a625c
...@@ -1886,6 +1886,29 @@ resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id) ...@@ -1886,6 +1886,29 @@ resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
return btf_is_func_proto(t) ? t : NULL; 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 * Fetch integer attribute of BTF map definition. Such attributes are
* represented using a pointer to an array, in which dimensionality of array * represented using a pointer to an array, in which dimensionality of array
...@@ -1902,8 +1925,8 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf, ...@@ -1902,8 +1925,8 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf,
const struct btf_type *arr_t; const struct btf_type *arr_t;
if (!btf_is_ptr(t)) { if (!btf_is_ptr(t)) {
pr_warn("map '%s': attr '%s': expected PTR, got %u.\n", pr_warn("map '%s': attr '%s': expected PTR, got %s.\n",
map_name, name, btf_kind(t)); map_name, name, btf_kind_str(t));
return false; return false;
} }
...@@ -1914,8 +1937,8 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf, ...@@ -1914,8 +1937,8 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf,
return false; return false;
} }
if (!btf_is_array(arr_t)) { if (!btf_is_array(arr_t)) {
pr_warn("map '%s': attr '%s': expected ARRAY, got %u.\n", pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n",
map_name, name, btf_kind(arr_t)); map_name, name, btf_kind_str(arr_t));
return false; return false;
} }
arr_info = btf_array(arr_t); arr_info = btf_array(arr_t);
...@@ -2009,8 +2032,8 @@ static int parse_btf_map_def(struct bpf_object *obj, ...@@ -2009,8 +2032,8 @@ static int parse_btf_map_def(struct bpf_object *obj,
return -EINVAL; return -EINVAL;
} }
if (!btf_is_ptr(t)) { if (!btf_is_ptr(t)) {
pr_warn("map '%s': key spec is not PTR: %u.\n", pr_warn("map '%s': key spec is not PTR: %s.\n",
map->name, btf_kind(t)); map->name, btf_kind_str(t));
return -EINVAL; return -EINVAL;
} }
sz = btf__resolve_size(obj->btf, t->type); sz = btf__resolve_size(obj->btf, t->type);
...@@ -2051,8 +2074,8 @@ static int parse_btf_map_def(struct bpf_object *obj, ...@@ -2051,8 +2074,8 @@ static int parse_btf_map_def(struct bpf_object *obj,
return -EINVAL; return -EINVAL;
} }
if (!btf_is_ptr(t)) { if (!btf_is_ptr(t)) {
pr_warn("map '%s': value spec is not PTR: %u.\n", pr_warn("map '%s': value spec is not PTR: %s.\n",
map->name, btf_kind(t)); map->name, btf_kind_str(t));
return -EINVAL; return -EINVAL;
} }
sz = btf__resolve_size(obj->btf, t->type); sz = btf__resolve_size(obj->btf, t->type);
...@@ -2109,14 +2132,14 @@ static int parse_btf_map_def(struct bpf_object *obj, ...@@ -2109,14 +2132,14 @@ static int parse_btf_map_def(struct bpf_object *obj,
t = skip_mods_and_typedefs(obj->btf, btf_array(t)->type, t = skip_mods_and_typedefs(obj->btf, btf_array(t)->type,
NULL); NULL);
if (!btf_is_ptr(t)) { if (!btf_is_ptr(t)) {
pr_warn("map '%s': map-in-map inner def is of unexpected kind %u.\n", pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
map->name, btf_kind(t)); map->name, btf_kind_str(t));
return -EINVAL; return -EINVAL;
} }
t = skip_mods_and_typedefs(obj->btf, t->type, NULL); t = skip_mods_and_typedefs(obj->btf, t->type, NULL);
if (!btf_is_struct(t)) { if (!btf_is_struct(t)) {
pr_warn("map '%s': map-in-map inner def is of unexpected kind %u.\n", pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
map->name, btf_kind(t)); map->name, btf_kind_str(t));
return -EINVAL; return -EINVAL;
} }
...@@ -2207,8 +2230,8 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj, ...@@ -2207,8 +2230,8 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj,
return -EINVAL; return -EINVAL;
} }
if (!btf_is_var(var)) { if (!btf_is_var(var)) {
pr_warn("map '%s': unexpected var kind %u.\n", pr_warn("map '%s': unexpected var kind %s.\n",
map_name, btf_kind(var)); map_name, btf_kind_str(var));
return -EINVAL; return -EINVAL;
} }
if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED && if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED &&
...@@ -2220,8 +2243,8 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj, ...@@ -2220,8 +2243,8 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj,
def = skip_mods_and_typedefs(obj->btf, var->type, NULL); def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
if (!btf_is_struct(def)) { if (!btf_is_struct(def)) {
pr_warn("map '%s': unexpected def kind %u.\n", pr_warn("map '%s': unexpected def kind %s.\n",
map_name, btf_kind(var)); map_name, btf_kind_str(var));
return -EINVAL; return -EINVAL;
} }
if (def->size > vi->size) { if (def->size > vi->size) {
...@@ -4212,8 +4235,8 @@ static int bpf_core_spec_parse(const struct btf *btf, ...@@ -4212,8 +4235,8 @@ static int bpf_core_spec_parse(const struct btf *btf,
return sz; return sz;
spec->bit_offset += access_idx * sz * 8; spec->bit_offset += access_idx * sz * 8;
} else { } else {
pr_warn("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %d\n", pr_warn("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %s\n",
type_id, spec_str, i, id, btf_kind(t)); type_id, spec_str, i, id, btf_kind_str(t));
return -EINVAL; return -EINVAL;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment