All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tools: bpf_jit_disasm: Add option to dump JIT image to a file.
@ 2017-04-11 21:30 David Daney
  2017-04-11 21:54 ` Daniel Borkmann
  2017-04-13 17:04 ` David Miller
  0 siblings, 2 replies; 4+ messages in thread
From: David Daney @ 2017-04-11 21:30 UTC (permalink / raw)
  To: Alexei Starovoitov, netdev, David S. Miller; +Cc: linux-kernel, David Daney

When debugging the JIT on an embedded platform or cross build
environment, libbfd may not be available, making it impossible to run
bpf_jit_disasm natively.

Add an option to emit a binary image of the JIT code to a file.  This
file can then be disassembled off line.  Typical usage in this case
might be (pasting mips64 dmesg output to cat command):

   $ cat > jit.raw
   $ bpf_jit_disasm -f jit.raw -O jit.bin
   $ mips64-linux-gnu-objdump -D -b binary -m mips:isa64r2 -EB jit.bin

Signed-off-by: David Daney <david.daney@cavium.com>
---
 tools/net/bpf_jit_disasm.c | 40 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 4 deletions(-)

diff --git a/tools/net/bpf_jit_disasm.c b/tools/net/bpf_jit_disasm.c
index 544b05a..ad572e6 100644
--- a/tools/net/bpf_jit_disasm.c
+++ b/tools/net/bpf_jit_disasm.c
@@ -229,6 +229,7 @@ static void usage(void)
 {
 	printf("Usage: bpf_jit_disasm [...]\n");
 	printf("       -o          Also display related opcodes (default: off).\n");
+	printf("       -O <file>   Write binary image of code to file, don't disassemble to stdout.\n");
 	printf("       -f <file>   Read last image dump from file or stdin (default: klog).\n");
 	printf("       -h          Display this help.\n");
 }
@@ -238,12 +239,19 @@ int main(int argc, char **argv)
 	unsigned int len, klen, opt, opcodes = 0;
 	static uint8_t image[32768];
 	char *kbuff, *file = NULL;
+	char *ofile = NULL;
+	int ofd;
+	ssize_t nr;
+	uint8_t *pos;
 
-	while ((opt = getopt(argc, argv, "of:")) != -1) {
+	while ((opt = getopt(argc, argv, "of:O:")) != -1) {
 		switch (opt) {
 		case 'o':
 			opcodes = 1;
 			break;
+		case 'O':
+			ofile = optarg;
+			break;
 		case 'f':
 			file = optarg;
 			break;
@@ -263,11 +271,35 @@ int main(int argc, char **argv)
 	}
 
 	len = get_last_jit_image(kbuff, klen, image, sizeof(image));
-	if (len > 0)
-		get_asm_insns(image, len, opcodes);
-	else
+	if (len <= 0) {
 		fprintf(stderr, "No JIT image found!\n");
+		goto done;
+	}
+	if (!ofile) {
+		get_asm_insns(image, len, opcodes);
+		goto done;
+	}
+
+	ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);
+	if (ofd < 0) {
+		fprintf(stderr, "Could not open file %s for writing: ", ofile);
+		perror(NULL);
+		goto done;
+	}
+	pos = image;
+	do {
+		nr = write(ofd, pos, len);
+		if (nr < 0) {
+			fprintf(stderr, "Could not write data to %s: ", ofile);
+			perror(NULL);
+			goto done;
+		}
+		len -= nr;
+		pos += nr;
+	} while (len);
+	close(ofd);
 
+done:
 	put_log_buff(kbuff);
 	return 0;
 }
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools: bpf_jit_disasm: Add option to dump JIT image to a file.
  2017-04-11 21:30 [PATCH] tools: bpf_jit_disasm: Add option to dump JIT image to a file David Daney
@ 2017-04-11 21:54 ` Daniel Borkmann
  2017-04-11 23:11   ` David Daney
  2017-04-13 17:04 ` David Miller
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel Borkmann @ 2017-04-11 21:54 UTC (permalink / raw)
  To: David Daney, Alexei Starovoitov, netdev, David S. Miller; +Cc: linux-kernel

On 04/11/2017 11:30 PM, David Daney wrote:
> When debugging the JIT on an embedded platform or cross build
> environment, libbfd may not be available, making it impossible to run
> bpf_jit_disasm natively.
>
> Add an option to emit a binary image of the JIT code to a file.  This
> file can then be disassembled off line.  Typical usage in this case
> might be (pasting mips64 dmesg output to cat command):
>
>     $ cat > jit.raw
>     $ bpf_jit_disasm -f jit.raw -O jit.bin
>     $ mips64-linux-gnu-objdump -D -b binary -m mips:isa64r2 -EB jit.bin
>
> Signed-off-by: David Daney <david.daney@cavium.com>

Seems good, we could at some point also add an -I jit.bin option
if needed for offline analysis/comparison instead of using -f
plain text.

Thanks!

Acked-by: Daniel Borkmann <daniel@iogearbox.net>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools: bpf_jit_disasm: Add option to dump JIT image to a file.
  2017-04-11 21:54 ` Daniel Borkmann
@ 2017-04-11 23:11   ` David Daney
  0 siblings, 0 replies; 4+ messages in thread
From: David Daney @ 2017-04-11 23:11 UTC (permalink / raw)
  To: Daniel Borkmann, David Daney, Alexei Starovoitov, netdev,
	David S. Miller
  Cc: linux-kernel

On 04/11/2017 02:54 PM, Daniel Borkmann wrote:
> On 04/11/2017 11:30 PM, David Daney wrote:
>> When debugging the JIT on an embedded platform or cross build
>> environment, libbfd may not be available, making it impossible to run
>> bpf_jit_disasm natively.
>>
>> Add an option to emit a binary image of the JIT code to a file.  This
>> file can then be disassembled off line.  Typical usage in this case
>> might be (pasting mips64 dmesg output to cat command):
>>
>>     $ cat > jit.raw
>>     $ bpf_jit_disasm -f jit.raw -O jit.bin
>>     $ mips64-linux-gnu-objdump -D -b binary -m mips:isa64r2 -EB jit.bin
>>
>> Signed-off-by: David Daney <david.daney@cavium.com>
>
> Seems good, we could at some point also add an -I jit.bin option
> if needed for offline analysis/comparison instead of using -f
> plain text.
>

FWIW:  The objdump incantation in the changelog does just about the same 
disassembly as the bpf_jit_disasm internal implementation.

> Thanks!
>
> Acked-by: Daniel Borkmann <daniel@iogearbox.net>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools: bpf_jit_disasm: Add option to dump JIT image to a file.
  2017-04-11 21:30 [PATCH] tools: bpf_jit_disasm: Add option to dump JIT image to a file David Daney
  2017-04-11 21:54 ` Daniel Borkmann
@ 2017-04-13 17:04 ` David Miller
  1 sibling, 0 replies; 4+ messages in thread
From: David Miller @ 2017-04-13 17:04 UTC (permalink / raw)
  To: david.daney; +Cc: ast, netdev, linux-kernel

From: David Daney <david.daney@cavium.com>
Date: Tue, 11 Apr 2017 14:30:52 -0700

> When debugging the JIT on an embedded platform or cross build
> environment, libbfd may not be available, making it impossible to run
> bpf_jit_disasm natively.
> 
> Add an option to emit a binary image of the JIT code to a file.  This
> file can then be disassembled off line.  Typical usage in this case
> might be (pasting mips64 dmesg output to cat command):
> 
>    $ cat > jit.raw
>    $ bpf_jit_disasm -f jit.raw -O jit.bin
>    $ mips64-linux-gnu-objdump -D -b binary -m mips:isa64r2 -EB jit.bin
> 
> Signed-off-by: David Daney <david.daney@cavium.com>

Applied, thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-04-13 17:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-11 21:30 [PATCH] tools: bpf_jit_disasm: Add option to dump JIT image to a file David Daney
2017-04-11 21:54 ` Daniel Borkmann
2017-04-11 23:11   ` David Daney
2017-04-13 17:04 ` David Miller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.