disassemble.js revision 802:235d22ccfd24
155682Smarkm/* 272445Sassar * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3142403Snectar * 4233294Sstas * Redistribution and use in source and binary forms, with or without 5142403Snectar * modification, are permitted provided that the following conditions 6233294Sstas * are met: 7233294Sstas * 872445Sassar * - Redistributions of source code must retain the above copyright 972445Sassar * notice, this list of conditions and the following disclaimer. 1072445Sassar * 1172445Sassar * - Redistributions in binary form must reproduce the above copyright 1272445Sassar * notice, this list of conditions and the following disclaimer in the 1372445Sassar * documentation and/or other materials provided with the distribution. 1472445Sassar * 1572445Sassar * - Neither the name of Oracle nor the names of its 1672445Sassar * contributors may be used to endorse or promote products derived 1772445Sassar * from this software without specific prior written permission. 1872445Sassar * 1972445Sassar * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 2072445Sassar * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21233294Sstas * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2272445Sassar * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23102644Snectar * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24102644Snectar * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25102644Snectar * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26102644Snectar * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27102644Snectar * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2872445Sassar * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 2972445Sassar * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3072445Sassar */ 3172445Sassar 3272445Sassar// Usage: jjs disassemble.js -- <.class-file-path> 3372445Sassar 34178825Sdfr// Simple .class disassembler that uses bundled ObjectWeb ASM 35178825Sdfr// classes in jdk8. WARNING: Bundled ObjectWeb ASM classes are 3672445Sassar// not part of official jdk8 API. It can be changed/removed 37102644Snectar// without notice. So, this script is brittle by design! 38102644Snectar 39102644Snectar// This example demonstrates passing arguments to script 40102644Snectar// from jjs command line, nio and ASM usage. 41102644Snectar 42102644Snectar// classes used 43102644Snectarvar FileSystems = Java.type("java.nio.file.FileSystems"); 44102644Snectarvar Files = Java.type("java.nio.file.Files"); 45142403Snectarvar System = Java.type("java.lang.System"); 46142403Snectarvar PrintWriter = Java.type("java.io.PrintWriter"); 47178825Sdfr 4872445Sassar// WARNING: uses non-API classes of jdk8! 4972445Sassarvar ClassReader = Java.type("jdk.internal.org.objectweb.asm.ClassReader"); 5072445Sassarvar TraceClassVisitor = Java.type("jdk.internal.org.objectweb.asm.util.TraceClassVisitor"); 5172445Sassar 5272445Sassar// convert file name to Path instance 53142403Snectarfunction path(file) { 54142403Snectar return FileSystems.default.getPath(file); 55142403Snectar} 56142403Snectar 57142403Snectar// read all file content as a byte[] 58142403Snectarfunction readAllBytes(file) { 59142403Snectar return Files.readAllBytes(path(file)); 60142403Snectar} 61142403Snectar 6272445Sassar// disassemble .class byte[] and prints output to stdout 6372445Sassarfunction disassemble(bytecode) { 6472445Sassar var pw = new PrintWriter(System.out); 6572445Sassar new ClassReader(bytecode).accept(new TraceClassVisitor(pw), 0); 6672445Sassar} 6772445Sassar 6872445Sassar// check for command line arg (for .class file name) 6972445Sassarif (arguments.length == 0 || !arguments[0].endsWith('.class')) { 7072445Sassar print("Usage: jjs disassemble -- <.class file>"); 7172445Sassar exit(1); 7272445Sassar} 7372445Sassar 7472445Sassar// disassemble the given .class file 7572445Sassardisassemble(readAllBytes(arguments[0])); 7672445Sassar