diff mbox series

[dwarves] btf_encoder: log libbpf errors when they cause encoding errors

Message ID 20240725075520.1281905-1-alan.maguire@oracle.com (mailing list archive)
State Not Applicable
Headers show
Series [dwarves] btf_encoder: log libbpf errors when they cause encoding errors | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Alan Maguire July 25, 2024, 7:55 a.m. UTC
libbpf returns a negative error code when adding types fails.
Pass it into btf__log_err() and display the libbpf error when
negative.  Nearly all use cases of btf__log_err() happen as a
result of a libbpf-returned error, pass 0 for the exceptions.

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 btf_encoder.c | 45 +++++++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 20 deletions(-)

Comments

Jiri Olsa July 25, 2024, 12:48 p.m. UTC | #1
On Thu, Jul 25, 2024 at 08:55:20AM +0100, Alan Maguire wrote:
> libbpf returns a negative error code when adding types fails.
> Pass it into btf__log_err() and display the libbpf error when
> negative.  Nearly all use cases of btf__log_err() happen as a
> result of a libbpf-returned error, pass 0 for the exceptions.

the more info the better ;-) lgtm

Reviewed-by: Jiri Olsa <jolsa@kernel.org>

jirka

> 
> Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
> ---
>  btf_encoder.c | 45 +++++++++++++++++++++++++--------------------
>  1 file changed, 25 insertions(+), 20 deletions(-)
> 
> diff --git a/btf_encoder.c b/btf_encoder.c
> index c2df2bc..adc38c3 100644
> --- a/btf_encoder.c
> +++ b/btf_encoder.c
> @@ -237,9 +237,9 @@ static const char * btf__int_encoding_str(uint8_t encoding)
>  		return "UNKN";
>  }
>  
> -__attribute ((format (printf, 5, 6)))
> +__attribute ((format (printf, 6, 7)))
>  static void btf__log_err(const struct btf *btf, int kind, const char *name,
> -			 bool output_cr, const char *fmt, ...)
> +			 bool output_cr, int libbpf_err, const char *fmt, ...)
>  {
>  	fprintf(stderr, "[%u] %s %s", btf__type_cnt(btf),
>  		btf_kind_str[kind], name ?: "(anon)");
> @@ -253,6 +253,9 @@ static void btf__log_err(const struct btf *btf, int kind, const char *name,
>  		va_end(ap);
>  	}
>  
> +	if (libbpf_err < 0)
> +		fprintf(stderr, " (libbpf error %d)", libbpf_err);
> +
>  	if (output_cr)
>  		fprintf(stderr, "\n");
>  }
> @@ -355,7 +358,8 @@ static int32_t btf_encoder__add_float(struct btf_encoder *encoder, const struct
>  	int32_t id = btf__add_float(encoder->btf, name, BITS_ROUNDUP_BYTES(bt->bit_size));
>  
>  	if (id < 0) {
> -		btf__log_err(encoder->btf, BTF_KIND_FLOAT, name, true, "Error emitting BTF type");
> +		btf__log_err(encoder->btf, BTF_KIND_FLOAT, name, true, id,
> +			     "Error emitting BTF type");
>  	} else {
>  		const struct btf_type *t;
>  
> @@ -429,7 +433,7 @@ static int32_t btf_encoder__add_base_type(struct btf_encoder *encoder, const str
>  
>  	id = btf__add_int(encoder->btf, name, byte_sz, encoding);
>  	if (id < 0) {
> -		btf__log_err(encoder->btf, BTF_KIND_INT, name, true, "Error emitting BTF type");
> +		btf__log_err(encoder->btf, BTF_KIND_INT, name, true, id, "Error emitting BTF type");
>  	} else {
>  		t = btf__type_by_id(encoder->btf, id);
>  		btf_encoder__log_type(encoder, t, false, true, "size=%u nr_bits=%u encoding=%s%s",
> @@ -473,7 +477,7 @@ static int32_t btf_encoder__add_ref_type(struct btf_encoder *encoder, uint16_t k
>  		id = btf__add_func(btf, name, BTF_FUNC_STATIC, type);
>  		break;
>  	default:
> -		btf__log_err(btf, kind, name, true, "Unexpected kind for reference");
> +		btf__log_err(btf, kind, name, true, 0, "Unexpected kind for reference");
>  		return -1;
>  	}
>  
> @@ -484,7 +488,7 @@ static int32_t btf_encoder__add_ref_type(struct btf_encoder *encoder, uint16_t k
>  		else
>  			btf_encoder__log_type(encoder, t, false, true, "type_id=%u", t->type);
>  	} else {
> -		btf__log_err(btf, kind, name, true, "Error emitting BTF type");
> +		btf__log_err(btf, kind, name, true, id, "Error emitting BTF type");
>  	}
>  	return id;
>  }
> @@ -503,7 +507,7 @@ static int32_t btf_encoder__add_array(struct btf_encoder *encoder, uint32_t type
>  		btf_encoder__log_type(encoder, t, false, true, "type_id=%u index_type_id=%u nr_elems=%u",
>  				      array->type, array->index_type, array->nelems);
>  	} else {
> -		btf__log_err(btf, BTF_KIND_ARRAY, NULL, true,
> +		btf__log_err(btf, BTF_KIND_ARRAY, NULL, true, id,
>  			      "type_id=%u index_type_id=%u nr_elems=%u Error emitting BTF type",
>  			      type, index_type, nelems);
>  	}
> @@ -545,12 +549,12 @@ static int32_t btf_encoder__add_struct(struct btf_encoder *encoder, uint8_t kind
>  		id = btf__add_union(btf, name, size);
>  		break;
>  	default:
> -		btf__log_err(btf, kind, name, true, "Unexpected kind of struct");
> +		btf__log_err(btf, kind, name, true, 0, "Unexpected kind of struct");
>  		return -1;
>  	}
>  
>  	if (id < 0) {
> -		btf__log_err(btf, kind, name, true, "Error emitting BTF type");
> +		btf__log_err(btf, kind, name, true, id, "Error emitting BTF type");
>  	} else {
>  		t = btf__type_by_id(btf, id);
>  		btf_encoder__log_type(encoder, t, false, true, "size=%u", t->size);
> @@ -600,7 +604,7 @@ static int32_t btf_encoder__add_enum(struct btf_encoder *encoder, const char *na
>  		t = btf__type_by_id(btf, id);
>  		btf_encoder__log_type(encoder, t, false, true, "size=%u", t->size);
>  	} else {
> -		btf__log_err(btf, is_enum32 ? BTF_KIND_ENUM : BTF_KIND_ENUM64, name, true,
> +		btf__log_err(btf, is_enum32 ? BTF_KIND_ENUM : BTF_KIND_ENUM64, name, true, id,
>  			      "size=%u Error emitting BTF type", size);
>  	}
>  	return id;
> @@ -682,9 +686,9 @@ static int32_t btf_encoder__add_func_proto(struct btf_encoder *encoder, struct f
>  		t = btf__type_by_id(btf, id);
>  		btf_encoder__log_type(encoder, t, false, false, "return=%u args=(%s", t->type, !nr_params ? "void)\n" : "");
>  	} else {
> -		btf__log_err(btf, BTF_KIND_FUNC_PROTO, NULL, true,
> -			      "return=%u vlen=%u Error emitting BTF type",
> -			      type_id, nr_params);
> +		btf__log_err(btf, BTF_KIND_FUNC_PROTO, NULL, true, id,
> +			     "return=%u vlen=%u Error emitting BTF type",
> +			     type_id, nr_params);
>  		return id;
>  	}
>  
> @@ -718,9 +722,9 @@ static int32_t btf_encoder__add_var(struct btf_encoder *encoder, uint32_t type,
>  		t = btf__type_by_id(btf, id);
>  		btf_encoder__log_type(encoder, t, false, true, "type=%u linkage=%u", t->type, btf_var(t)->linkage);
>  	} else {
> -		btf__log_err(btf, BTF_KIND_VAR, name, true,
> -			      "type=%u linkage=%u Error emitting BTF type",
> -			      type, linkage);
> +		btf__log_err(btf, BTF_KIND_VAR, name, true, id,
> +			     "type=%u linkage=%u Error emitting BTF type",
> +			     type, linkage);
>  	}
>  	return id;
>  }
> @@ -781,9 +785,9 @@ static int32_t btf_encoder__add_datasec(struct btf_encoder *encoder, const char
>  
>  	id = btf__add_datasec(btf, section_name, datasec_sz);
>  	if (id < 0) {
> -		btf__log_err(btf, BTF_KIND_DATASEC, section_name, true,
> -				 "size=%u vlen=%u Error emitting BTF type",
> -				 datasec_sz, nr_var_secinfo);
> +		btf__log_err(btf, BTF_KIND_DATASEC, section_name, true, id,
> +			     "size=%u vlen=%u Error emitting BTF type",
> +			     datasec_sz, nr_var_secinfo);
>  	} else {
>  		t = btf__type_by_id(btf, id);
>  		btf_encoder__log_type(encoder, t, false, true, "size=%u vlen=%u", t->size, nr_var_secinfo);
> @@ -819,7 +823,8 @@ static int32_t btf_encoder__add_decl_tag(struct btf_encoder *encoder, const char
>  		btf_encoder__log_type(encoder, t, false, true, "type_id=%u component_idx=%d",
>  				      t->type, component_idx);
>  	} else {
> -		btf__log_err(btf, BTF_KIND_DECL_TAG, value, true, "component_idx=%d Error emitting BTF type",
> +		btf__log_err(btf, BTF_KIND_DECL_TAG, value, true, id,
> +			     "component_idx=%d Error emitting BTF type",
>  			     component_idx);
>  	}
>  
> -- 
> 2.43.5
>
Arnaldo Carvalho de Melo July 26, 2024, 12:31 p.m. UTC | #2
On Thu, Jul 25, 2024 at 08:55:20AM +0100, Alan Maguire wrote:
> libbpf returns a negative error code when adding types fails.
> Pass it into btf__log_err() and display the libbpf error when
> negative.  Nearly all use cases of btf__log_err() happen as a
> result of a libbpf-returned error, pass 0 for the exceptions.

Thanks, applied.

- Arnaldo
 
> Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
> ---
>  btf_encoder.c | 45 +++++++++++++++++++++++++--------------------
>  1 file changed, 25 insertions(+), 20 deletions(-)
> 
> diff --git a/btf_encoder.c b/btf_encoder.c
> index c2df2bc..adc38c3 100644
> --- a/btf_encoder.c
> +++ b/btf_encoder.c
> @@ -237,9 +237,9 @@ static const char * btf__int_encoding_str(uint8_t encoding)
>  		return "UNKN";
>  }
>  
> -__attribute ((format (printf, 5, 6)))
> +__attribute ((format (printf, 6, 7)))
>  static void btf__log_err(const struct btf *btf, int kind, const char *name,
> -			 bool output_cr, const char *fmt, ...)
> +			 bool output_cr, int libbpf_err, const char *fmt, ...)
>  {
>  	fprintf(stderr, "[%u] %s %s", btf__type_cnt(btf),
>  		btf_kind_str[kind], name ?: "(anon)");
> @@ -253,6 +253,9 @@ static void btf__log_err(const struct btf *btf, int kind, const char *name,
>  		va_end(ap);
>  	}
>  
> +	if (libbpf_err < 0)
> +		fprintf(stderr, " (libbpf error %d)", libbpf_err);
> +
>  	if (output_cr)
>  		fprintf(stderr, "\n");
>  }
> @@ -355,7 +358,8 @@ static int32_t btf_encoder__add_float(struct btf_encoder *encoder, const struct
>  	int32_t id = btf__add_float(encoder->btf, name, BITS_ROUNDUP_BYTES(bt->bit_size));
>  
>  	if (id < 0) {
> -		btf__log_err(encoder->btf, BTF_KIND_FLOAT, name, true, "Error emitting BTF type");
> +		btf__log_err(encoder->btf, BTF_KIND_FLOAT, name, true, id,
> +			     "Error emitting BTF type");
>  	} else {
>  		const struct btf_type *t;
>  
> @@ -429,7 +433,7 @@ static int32_t btf_encoder__add_base_type(struct btf_encoder *encoder, const str
>  
>  	id = btf__add_int(encoder->btf, name, byte_sz, encoding);
>  	if (id < 0) {
> -		btf__log_err(encoder->btf, BTF_KIND_INT, name, true, "Error emitting BTF type");
> +		btf__log_err(encoder->btf, BTF_KIND_INT, name, true, id, "Error emitting BTF type");
>  	} else {
>  		t = btf__type_by_id(encoder->btf, id);
>  		btf_encoder__log_type(encoder, t, false, true, "size=%u nr_bits=%u encoding=%s%s",
> @@ -473,7 +477,7 @@ static int32_t btf_encoder__add_ref_type(struct btf_encoder *encoder, uint16_t k
>  		id = btf__add_func(btf, name, BTF_FUNC_STATIC, type);
>  		break;
>  	default:
> -		btf__log_err(btf, kind, name, true, "Unexpected kind for reference");
> +		btf__log_err(btf, kind, name, true, 0, "Unexpected kind for reference");
>  		return -1;
>  	}
>  
> @@ -484,7 +488,7 @@ static int32_t btf_encoder__add_ref_type(struct btf_encoder *encoder, uint16_t k
>  		else
>  			btf_encoder__log_type(encoder, t, false, true, "type_id=%u", t->type);
>  	} else {
> -		btf__log_err(btf, kind, name, true, "Error emitting BTF type");
> +		btf__log_err(btf, kind, name, true, id, "Error emitting BTF type");
>  	}
>  	return id;
>  }
> @@ -503,7 +507,7 @@ static int32_t btf_encoder__add_array(struct btf_encoder *encoder, uint32_t type
>  		btf_encoder__log_type(encoder, t, false, true, "type_id=%u index_type_id=%u nr_elems=%u",
>  				      array->type, array->index_type, array->nelems);
>  	} else {
> -		btf__log_err(btf, BTF_KIND_ARRAY, NULL, true,
> +		btf__log_err(btf, BTF_KIND_ARRAY, NULL, true, id,
>  			      "type_id=%u index_type_id=%u nr_elems=%u Error emitting BTF type",
>  			      type, index_type, nelems);
>  	}
> @@ -545,12 +549,12 @@ static int32_t btf_encoder__add_struct(struct btf_encoder *encoder, uint8_t kind
>  		id = btf__add_union(btf, name, size);
>  		break;
>  	default:
> -		btf__log_err(btf, kind, name, true, "Unexpected kind of struct");
> +		btf__log_err(btf, kind, name, true, 0, "Unexpected kind of struct");
>  		return -1;
>  	}
>  
>  	if (id < 0) {
> -		btf__log_err(btf, kind, name, true, "Error emitting BTF type");
> +		btf__log_err(btf, kind, name, true, id, "Error emitting BTF type");
>  	} else {
>  		t = btf__type_by_id(btf, id);
>  		btf_encoder__log_type(encoder, t, false, true, "size=%u", t->size);
> @@ -600,7 +604,7 @@ static int32_t btf_encoder__add_enum(struct btf_encoder *encoder, const char *na
>  		t = btf__type_by_id(btf, id);
>  		btf_encoder__log_type(encoder, t, false, true, "size=%u", t->size);
>  	} else {
> -		btf__log_err(btf, is_enum32 ? BTF_KIND_ENUM : BTF_KIND_ENUM64, name, true,
> +		btf__log_err(btf, is_enum32 ? BTF_KIND_ENUM : BTF_KIND_ENUM64, name, true, id,
>  			      "size=%u Error emitting BTF type", size);
>  	}
>  	return id;
> @@ -682,9 +686,9 @@ static int32_t btf_encoder__add_func_proto(struct btf_encoder *encoder, struct f
>  		t = btf__type_by_id(btf, id);
>  		btf_encoder__log_type(encoder, t, false, false, "return=%u args=(%s", t->type, !nr_params ? "void)\n" : "");
>  	} else {
> -		btf__log_err(btf, BTF_KIND_FUNC_PROTO, NULL, true,
> -			      "return=%u vlen=%u Error emitting BTF type",
> -			      type_id, nr_params);
> +		btf__log_err(btf, BTF_KIND_FUNC_PROTO, NULL, true, id,
> +			     "return=%u vlen=%u Error emitting BTF type",
> +			     type_id, nr_params);
>  		return id;
>  	}
>  
> @@ -718,9 +722,9 @@ static int32_t btf_encoder__add_var(struct btf_encoder *encoder, uint32_t type,
>  		t = btf__type_by_id(btf, id);
>  		btf_encoder__log_type(encoder, t, false, true, "type=%u linkage=%u", t->type, btf_var(t)->linkage);
>  	} else {
> -		btf__log_err(btf, BTF_KIND_VAR, name, true,
> -			      "type=%u linkage=%u Error emitting BTF type",
> -			      type, linkage);
> +		btf__log_err(btf, BTF_KIND_VAR, name, true, id,
> +			     "type=%u linkage=%u Error emitting BTF type",
> +			     type, linkage);
>  	}
>  	return id;
>  }
> @@ -781,9 +785,9 @@ static int32_t btf_encoder__add_datasec(struct btf_encoder *encoder, const char
>  
>  	id = btf__add_datasec(btf, section_name, datasec_sz);
>  	if (id < 0) {
> -		btf__log_err(btf, BTF_KIND_DATASEC, section_name, true,
> -				 "size=%u vlen=%u Error emitting BTF type",
> -				 datasec_sz, nr_var_secinfo);
> +		btf__log_err(btf, BTF_KIND_DATASEC, section_name, true, id,
> +			     "size=%u vlen=%u Error emitting BTF type",
> +			     datasec_sz, nr_var_secinfo);
>  	} else {
>  		t = btf__type_by_id(btf, id);
>  		btf_encoder__log_type(encoder, t, false, true, "size=%u vlen=%u", t->size, nr_var_secinfo);
> @@ -819,7 +823,8 @@ static int32_t btf_encoder__add_decl_tag(struct btf_encoder *encoder, const char
>  		btf_encoder__log_type(encoder, t, false, true, "type_id=%u component_idx=%d",
>  				      t->type, component_idx);
>  	} else {
> -		btf__log_err(btf, BTF_KIND_DECL_TAG, value, true, "component_idx=%d Error emitting BTF type",
> +		btf__log_err(btf, BTF_KIND_DECL_TAG, value, true, id,
> +			     "component_idx=%d Error emitting BTF type",
>  			     component_idx);
>  	}
>  
> -- 
> 2.43.5
diff mbox series

Patch

diff --git a/btf_encoder.c b/btf_encoder.c
index c2df2bc..adc38c3 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -237,9 +237,9 @@  static const char * btf__int_encoding_str(uint8_t encoding)
 		return "UNKN";
 }
 
-__attribute ((format (printf, 5, 6)))
+__attribute ((format (printf, 6, 7)))
 static void btf__log_err(const struct btf *btf, int kind, const char *name,
-			 bool output_cr, const char *fmt, ...)
+			 bool output_cr, int libbpf_err, const char *fmt, ...)
 {
 	fprintf(stderr, "[%u] %s %s", btf__type_cnt(btf),
 		btf_kind_str[kind], name ?: "(anon)");
@@ -253,6 +253,9 @@  static void btf__log_err(const struct btf *btf, int kind, const char *name,
 		va_end(ap);
 	}
 
+	if (libbpf_err < 0)
+		fprintf(stderr, " (libbpf error %d)", libbpf_err);
+
 	if (output_cr)
 		fprintf(stderr, "\n");
 }
@@ -355,7 +358,8 @@  static int32_t btf_encoder__add_float(struct btf_encoder *encoder, const struct
 	int32_t id = btf__add_float(encoder->btf, name, BITS_ROUNDUP_BYTES(bt->bit_size));
 
 	if (id < 0) {
-		btf__log_err(encoder->btf, BTF_KIND_FLOAT, name, true, "Error emitting BTF type");
+		btf__log_err(encoder->btf, BTF_KIND_FLOAT, name, true, id,
+			     "Error emitting BTF type");
 	} else {
 		const struct btf_type *t;
 
@@ -429,7 +433,7 @@  static int32_t btf_encoder__add_base_type(struct btf_encoder *encoder, const str
 
 	id = btf__add_int(encoder->btf, name, byte_sz, encoding);
 	if (id < 0) {
-		btf__log_err(encoder->btf, BTF_KIND_INT, name, true, "Error emitting BTF type");
+		btf__log_err(encoder->btf, BTF_KIND_INT, name, true, id, "Error emitting BTF type");
 	} else {
 		t = btf__type_by_id(encoder->btf, id);
 		btf_encoder__log_type(encoder, t, false, true, "size=%u nr_bits=%u encoding=%s%s",
@@ -473,7 +477,7 @@  static int32_t btf_encoder__add_ref_type(struct btf_encoder *encoder, uint16_t k
 		id = btf__add_func(btf, name, BTF_FUNC_STATIC, type);
 		break;
 	default:
-		btf__log_err(btf, kind, name, true, "Unexpected kind for reference");
+		btf__log_err(btf, kind, name, true, 0, "Unexpected kind for reference");
 		return -1;
 	}
 
@@ -484,7 +488,7 @@  static int32_t btf_encoder__add_ref_type(struct btf_encoder *encoder, uint16_t k
 		else
 			btf_encoder__log_type(encoder, t, false, true, "type_id=%u", t->type);
 	} else {
-		btf__log_err(btf, kind, name, true, "Error emitting BTF type");
+		btf__log_err(btf, kind, name, true, id, "Error emitting BTF type");
 	}
 	return id;
 }
@@ -503,7 +507,7 @@  static int32_t btf_encoder__add_array(struct btf_encoder *encoder, uint32_t type
 		btf_encoder__log_type(encoder, t, false, true, "type_id=%u index_type_id=%u nr_elems=%u",
 				      array->type, array->index_type, array->nelems);
 	} else {
-		btf__log_err(btf, BTF_KIND_ARRAY, NULL, true,
+		btf__log_err(btf, BTF_KIND_ARRAY, NULL, true, id,
 			      "type_id=%u index_type_id=%u nr_elems=%u Error emitting BTF type",
 			      type, index_type, nelems);
 	}
@@ -545,12 +549,12 @@  static int32_t btf_encoder__add_struct(struct btf_encoder *encoder, uint8_t kind
 		id = btf__add_union(btf, name, size);
 		break;
 	default:
-		btf__log_err(btf, kind, name, true, "Unexpected kind of struct");
+		btf__log_err(btf, kind, name, true, 0, "Unexpected kind of struct");
 		return -1;
 	}
 
 	if (id < 0) {
-		btf__log_err(btf, kind, name, true, "Error emitting BTF type");
+		btf__log_err(btf, kind, name, true, id, "Error emitting BTF type");
 	} else {
 		t = btf__type_by_id(btf, id);
 		btf_encoder__log_type(encoder, t, false, true, "size=%u", t->size);
@@ -600,7 +604,7 @@  static int32_t btf_encoder__add_enum(struct btf_encoder *encoder, const char *na
 		t = btf__type_by_id(btf, id);
 		btf_encoder__log_type(encoder, t, false, true, "size=%u", t->size);
 	} else {
-		btf__log_err(btf, is_enum32 ? BTF_KIND_ENUM : BTF_KIND_ENUM64, name, true,
+		btf__log_err(btf, is_enum32 ? BTF_KIND_ENUM : BTF_KIND_ENUM64, name, true, id,
 			      "size=%u Error emitting BTF type", size);
 	}
 	return id;
@@ -682,9 +686,9 @@  static int32_t btf_encoder__add_func_proto(struct btf_encoder *encoder, struct f
 		t = btf__type_by_id(btf, id);
 		btf_encoder__log_type(encoder, t, false, false, "return=%u args=(%s", t->type, !nr_params ? "void)\n" : "");
 	} else {
-		btf__log_err(btf, BTF_KIND_FUNC_PROTO, NULL, true,
-			      "return=%u vlen=%u Error emitting BTF type",
-			      type_id, nr_params);
+		btf__log_err(btf, BTF_KIND_FUNC_PROTO, NULL, true, id,
+			     "return=%u vlen=%u Error emitting BTF type",
+			     type_id, nr_params);
 		return id;
 	}
 
@@ -718,9 +722,9 @@  static int32_t btf_encoder__add_var(struct btf_encoder *encoder, uint32_t type,
 		t = btf__type_by_id(btf, id);
 		btf_encoder__log_type(encoder, t, false, true, "type=%u linkage=%u", t->type, btf_var(t)->linkage);
 	} else {
-		btf__log_err(btf, BTF_KIND_VAR, name, true,
-			      "type=%u linkage=%u Error emitting BTF type",
-			      type, linkage);
+		btf__log_err(btf, BTF_KIND_VAR, name, true, id,
+			     "type=%u linkage=%u Error emitting BTF type",
+			     type, linkage);
 	}
 	return id;
 }
@@ -781,9 +785,9 @@  static int32_t btf_encoder__add_datasec(struct btf_encoder *encoder, const char
 
 	id = btf__add_datasec(btf, section_name, datasec_sz);
 	if (id < 0) {
-		btf__log_err(btf, BTF_KIND_DATASEC, section_name, true,
-				 "size=%u vlen=%u Error emitting BTF type",
-				 datasec_sz, nr_var_secinfo);
+		btf__log_err(btf, BTF_KIND_DATASEC, section_name, true, id,
+			     "size=%u vlen=%u Error emitting BTF type",
+			     datasec_sz, nr_var_secinfo);
 	} else {
 		t = btf__type_by_id(btf, id);
 		btf_encoder__log_type(encoder, t, false, true, "size=%u vlen=%u", t->size, nr_var_secinfo);
@@ -819,7 +823,8 @@  static int32_t btf_encoder__add_decl_tag(struct btf_encoder *encoder, const char
 		btf_encoder__log_type(encoder, t, false, true, "type_id=%u component_idx=%d",
 				      t->type, component_idx);
 	} else {
-		btf__log_err(btf, BTF_KIND_DECL_TAG, value, true, "component_idx=%d Error emitting BTF type",
+		btf__log_err(btf, BTF_KIND_DECL_TAG, value, true, id,
+			     "component_idx=%d Error emitting BTF type",
 			     component_idx);
 	}