BPF List
 help / color / mirror / Atom feed
From: Alan Maguire <alan.maguire@oracle.com>
To: acme@kernel.org
Cc: cavok@debian.org, jolsa@kernel.org, ben@decadent.org.uk,
	dwarves@vger.kernel.org, bpf@vger.kernel.org,
	Alan Maguire <alan.maguire@oracle.com>
Subject: [PATCH dwarves] btf_encoder: log libbpf errors when they cause encoding errors
Date: Thu, 25 Jul 2024 08:55:20 +0100	[thread overview]
Message-ID: <20240725075520.1281905-1-alan.maguire@oracle.com> (raw)

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(-)

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


             reply	other threads:[~2024-07-25  7:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-25  7:55 Alan Maguire [this message]
2024-07-25 12:48 ` [PATCH dwarves] btf_encoder: log libbpf errors when they cause encoding errors Jiri Olsa
2024-07-26 12:31 ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240725075520.1281905-1-alan.maguire@oracle.com \
    --to=alan.maguire@oracle.com \
    --cc=acme@kernel.org \
    --cc=ben@decadent.org.uk \
    --cc=bpf@vger.kernel.org \
    --cc=cavok@debian.org \
    --cc=dwarves@vger.kernel.org \
    --cc=jolsa@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox