bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii@kernel.org>
To: <dwarves@vger.kernel.org>, <acme@kernel.org>, <bpf@vger.kernel.org>
Cc: <andrii@kernel.org>, <kernel-team@fb.com>
Subject: [PATCH dwarves 2/4] libbtf: improve variable naming and error reporting when writing out BTF
Date: Thu, 5 Nov 2020 21:25:47 -0800	[thread overview]
Message-ID: <20201106052549.3782099-3-andrii@kernel.org> (raw)
In-Reply-To: <20201106052549.3782099-1-andrii@kernel.org>

Rename few local variables to reflects the purpose a bit better. Also separate
writing out BTF raw data and objcopy invocation into two separate steps and
improve error reporting for each.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 libbtf.c | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)

diff --git a/libbtf.c b/libbtf.c
index babf4fe8cd9e..b6ddd7599395 100644
--- a/libbtf.c
+++ b/libbtf.c
@@ -679,11 +679,11 @@ static int btf_elf__write(const char *filename, struct btf *btf)
 {
 	GElf_Shdr shdr_mem, *shdr;
 	GElf_Ehdr ehdr_mem, *ehdr;
-	Elf_Data *btf_elf = NULL;
+	Elf_Data *btf_data = NULL;
 	Elf_Scn *scn = NULL;
 	Elf *elf = NULL;
-	const void *btf_data;
-	uint32_t btf_size;
+	const void *raw_btf_data;
+	uint32_t raw_btf_size;
 	int fd, err = -1;
 	size_t strndx;
 
@@ -735,18 +735,18 @@ static int btf_elf__write(const char *filename, struct btf *btf)
 			continue;
 		char *secname = elf_strptr(elf, strndx, shdr->sh_name);
 		if (strcmp(secname, ".BTF") == 0) {
-			btf_elf = elf_getdata(scn, btf_elf);
+			btf_data = elf_getdata(scn, btf_data);
 			break;
 		}
 	}
 
-	btf_data = btf__get_raw_data(btf, &btf_size);
+	raw_btf_data = btf__get_raw_data(btf, &raw_btf_size);
 
-	if (btf_elf) {
+	if (btf_data) {
 		/* Exisiting .BTF section found */
-		btf_elf->d_buf = (void *)btf_data;
-		btf_elf->d_size = btf_size;
-		elf_flagdata(btf_elf, ELF_C_SET, ELF_F_DIRTY);
+		btf_data->d_buf = (void *)raw_btf_data;
+		btf_data->d_size = raw_btf_size;
+		elf_flagdata(btf_data, ELF_C_SET, ELF_F_DIRTY);
 
 		if (elf_update(elf, ELF_C_NULL) >= 0 &&
 		    elf_update(elf, ELF_C_WRITE) >= 0)
@@ -770,12 +770,21 @@ static int btf_elf__write(const char *filename, struct btf *btf)
 			goto out;
 		}
 
+		if (write(fd, raw_btf_data, raw_btf_size) != raw_btf_size) {
+			fprintf(stderr, "%s: write of %d bytes to '%s' failed: %d!\n",
+				__func__, raw_btf_size, tmp_fn, errno);
+			goto out;
+		}
+
 		snprintf(cmd, sizeof(cmd), "%s --add-section .BTF=%s %s",
 			 llvm_objcopy, tmp_fn, filename);
+		if (system(cmd)) {
+			fprintf(stderr, "%s: failed to add .BTF section to '%s': %d!\n",
+				__func__, tmp_fn, errno);
+			goto out;
+		}
 
-		if (write(fd, btf_data, btf_size) == btf_size && !system(cmd))
-			err = 0;
-
+		err = 0;
 		unlink(tmp_fn);
 	}
 
-- 
2.24.1


  parent reply	other threads:[~2020-11-06  5:26 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-06  5:25 [PATCH dwarves 0/4] Add split BTF support to pahole Andrii Nakryiko
2020-11-06  5:25 ` [PATCH dwarves 1/4] btf_encoder: fix array index type numbering Andrii Nakryiko
2020-11-06  5:25 ` Andrii Nakryiko [this message]
2020-11-06  5:25 ` [PATCH dwarves 3/4] libbpf: update libbpf submodlue reference to latest master Andrii Nakryiko
2020-11-06  5:25 ` [PATCH dwarves 4/4] btf: add support for split BTF loading and encoding Andrii Nakryiko
2020-11-11 11:56   ` Arnaldo Carvalho de Melo
2020-11-11 12:19     ` Arnaldo Carvalho de Melo
2020-11-11 18:29       ` Andrii Nakryiko
2020-11-11 18:32         ` Arnaldo
2020-11-13 11:35         ` Arnaldo Carvalho de Melo
2020-11-11 18:27     ` Andrii Nakryiko
2020-11-11 18:34       ` Arnaldo
2020-11-11 18:50         ` Andrii Nakryiko
2020-11-10 23:34 ` [PATCH dwarves 0/4] Add split BTF support to pahole Andrii Nakryiko
2020-11-11  0:29   ` Arnaldo
2020-11-11  0:35     ` Andrii Nakryiko
2020-11-11  0:38       ` Arnaldo

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=20201106052549.3782099-3-andrii@kernel.org \
    --to=andrii@kernel.org \
    --cc=acme@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dwarves@vger.kernel.org \
    --cc=kernel-team@fb.com \
    /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;
as well as URLs for NNTP newsgroup(s).