Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753802AbdDKVbD (ORCPT ); Tue, 11 Apr 2017 17:31:03 -0400 Received: from mail-sn1nam02on0072.outbound.protection.outlook.com ([104.47.36.72]:9152 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753522AbdDKVbA (ORCPT ); Tue, 11 Apr 2017 17:31:00 -0400 Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=cavium.com; From: David Daney To: Alexei Starovoitov , netdev@vger.kernel.org, "David S. Miller" Cc: linux-kernel@vger.kernel.org, David Daney Subject: [PATCH] tools: bpf_jit_disasm: Add option to dump JIT image to a file. Date: Tue, 11 Apr 2017 14:30:52 -0700 Message-Id: <20170411213052.7337-1-david.daney@cavium.com> X-Mailer: git-send-email 2.9.3 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [50.233.148.156] X-ClientProxiedBy: SN2PR07CA010.namprd07.prod.outlook.com (10.255.174.27) To MWHPR07MB3502.namprd07.prod.outlook.com (10.164.192.29) X-MS-Office365-Filtering-Correlation-Id: 26aa65b0-91ba-480e-2de9-08d481220b00 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(201703131423075)(201703031133081);SRVR:MWHPR07MB3502; X-Microsoft-Exchange-Diagnostics: 1;MWHPR07MB3502;3:0SAlz60HM+qhG6xqE1rjCdmo+rS4OQ9syOh46OO/5b5jweTtmDOTHh2ET85LorMG6lkpahQwUujsIz7Qd3HVFmkLmM+oUcPwQio0BImWwhnJCpPcVPs6etyHmiapjWOxWepl8x3l0iXXvZxxjBNDPTQZGlO3iRRWdJApcpo9o8cCYUCqh2yX/UwXvBbsebZ7jM5kc8Qe+Hz5BF/ISzelyt2jvEjeA2JXVLfFBP/7UF71iC10xgctWjSA4mowrZQC1zFVcLf6DT3B+JmsZCjqqT4y8G9LxX3IiZm/43ev2q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g2c3pMy0dBpFoQibfAYCEVjvk0qKj8QzlnVR3/qXGaauH5NkWX64HJvr8LjXuPUwGQ1NvryRqWvTxwzFq+SyTiuHvlCzRe810e3jKdJyGjX+qMXrk3TD81w2e9yBPPCmBL77rpb0g/y4Q4MVLvihnTzZuJzIEEiA+YTsFCBd40aGBx7rzkPzBH/2peuLVE+eCAc6nQXtfTfcHoGQbGMCQ7adeLMdBJEG1vjgewbRE58QJAgX9miz7 X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(5005006)(8121501046)(93006095)(93001095)(10201501046)(3002001)(6041248)(20161123560025)(20161123564025)(20161123562025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(6072148);SRVR:MWHPR07MB3502;BCL:0;PCL:0;RULEID:;SRVR:MWHPR07MB3502; X-Microsoft-Exchange-Diagnostics: 1;MWHPR07MB3502;4:ujXVN/DTWrhi8nz6Wl7AQtAKECx89ZCq5Ys79cdWVyRH6PLZV9kgXa6+Eyp3isnSQ0AKD/9GPnH7f3t9Bq0sUMkAn4qJvo5KkghbLOJCINlsgYRsUxMTtsDCFQi4gN9TCSlMa2eQmmotOJ0l4hPw/vIDGCxysDiSBMK3RdH9ZXM3oxX6lwKFVkldJ6rM2M22XHyfkvke3qai74rHfC/VRWmS1HHy3nxHQwvZ65RqLJ+GYFMpz2DHdswIpYQsezVQTSWJmwNPSelE+0AVPqQsnL5xV2LFajf/+NY/QupE9EOiKbNZUgOylkyAek1zMbrKsErpZGCdYnDc9Is+ZEXrduWsNHYA621oQ4UFmYskM3C2ck6mOIT50vMZPHjqM2/jKmW+lGY4DiRt3lCOMfmrR4bCdqEOGkCcH8MyCzI5Fyldnrla9mS9CHOpNYRp7p9Z8PisblDrtUo0Lj152jtoJ5l2BMLwuS0RV7EOpwpGRK1xeMjzcETGvzVplrQ3j/adgMwvhsra+h3cRM+mfU/Fq7vBGaKcSrvIZu0L5BosbkB/BpV32ekBjjiESzZERI2fiyQsfbg1rfDFxOkigSpYTZO5Op29LQx5/2AvuJDclzKAGSm5QLQba2mRocml3X/3qziDMJhRPg5PEg4VxaWp4+OoPRrkAb2G7bbIS/ualDIFEwj+G0HEprCSK1CdSyyYlDssD+bB+okRWRO9JaNt8g== X-Forefront-PRVS: 0274272F87 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39400400002)(39410400002)(39450400003)(39840400002)(39850400002)(6512007)(81166006)(50466002)(6666003)(50226002)(53936002)(25786009)(6506006)(7736002)(48376002)(6486002)(8676002)(2906002)(53416004)(50986999)(33646002)(36756003)(42186005)(305945005)(5660300001)(3846002)(6116002)(1076002)(107886003)(38730400002)(47776003)(66066001)(189998001)(4326008)(5003940100001)(86362001);DIR:OUT;SFP:1101;SCL:1;SRVR:MWHPR07MB3502;H:ddl.caveonetworks.com;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;MWHPR07MB3502;23:PUrTiM2I9LU2vSsQ83eU8TNzT7jY3AnXT9JsEYTAu?= =?us-ascii?Q?TjEk5j4MQ5krv9YwP7FMlM12uRfhyNMVjioRWf743SOo+or1ZwiQgUJEU53r?= =?us-ascii?Q?zBpFxhAsYde1PWfmlf6FYkcr7uNkMMCsaloNNDQ2m/A/b6IxXh1PscANPufo?= =?us-ascii?Q?5L0R2z0g9yE7FaQfO4Is0cCC+R2EP3OSQ7ub06+KDXRQ5lu0IpzZ2Lb9iTJV?= =?us-ascii?Q?bEEOeb/YyXgxHGkXTaSfaabO86Nkg3BJL9WAjU58knz/TfZ1pyysBRT2YpqI?= =?us-ascii?Q?l5b6fj0pHq6FJ1Qd4Oq58RpSfxpGJCd12I6Vg8HLAL0ywr04LsvKA9ICOvtO?= =?us-ascii?Q?dnWVZ3vCVs5jivdvsTOnfYmWZ+ch2+LKGLRzkdRg2o29UO0u6WIZY4CeO7SC?=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3VSmWUq+cR78MD0NNAAGGmsJ7v9aeb0PPE3TUv6lm43krKBj1PO7IGnJ0cg1G1PcoqPwnSEw8Y87ffP1X97CJNO0WTFxRyj1xjYcEvsQr9sVa5AUgyWS1GMbW4rf7RTGkcQhjyVjQQ5PPUxiOJ7Sz8nfh6+3BQ==;5:d+TXbR6kY1VtDyIWXxm8A5WSMJX8QvLaz0Ol0fOyNlIIozgOgKssn5XN1Fo8U5VZ8R3ETsS7JK7Z5XrRd7wx7ScH0IX6pJdqUZfk0MijdhqKP9y1hnmljHUd+elnoY5FcM7DvjduUnhtX+32iaDkdw==;24:cS4nKi+nTZXae/D5hLZRKUOVmYoE9fHKb2uh5rtz2ZVKohaH+l3uIJhVB4zMiX9K9dEX6UAEVsI5Ne1xo05ghxggM05ZRP2WBj4jp7uV4Rk= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;MWHPR07MB3502;7:D1w/1GShUe43cN+nX1l37ULz7XX8En7K322H5g4sD1ElBrr5cK9TwgeOrhlpZ6OidXlM9u2MSkZwB3rLafHipkpjhAWXditGHzACgAAnH9cbDNM8uvr2iIa2QziXMWaU0qsbsXYxuwabAlTD7RltUKUzFoHyKxs5kIHft9OgmnxF9IE+WYBbKDWighTubhMKWzHJDhy/5Sq1m4o6pJx4tTKIxyzWGV7Ms3KIqMuxB0jS6j+v5FmvyAOa+GisRqWvpNaRIQTG0ZSpmnN2eycvAghqUyYLB6oMFYUjfJUTBaG2hGbwo6PkpTxBI5AhIo0MZzapbC2sRkxp1lfhutBq/Q== X-OriginatorOrg: cavium.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2017 21:30:57.3410 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR07MB3502 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2527 Lines: 91 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 --- 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 Write binary image of code to file, don't disassemble to stdout.\n"); printf(" -f 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