1 /* 2 * Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.asmtools.jdis; 24 25 import org.openjdk.asmtools.common.Tool; 26 import org.openjdk.asmtools.util.I18NResourceBundle; 27 import org.openjdk.asmtools.util.ProductInfo; 28 29 import java.io.DataInputStream; 30 import java.io.PrintStream; 31 import java.io.PrintWriter; 32 import java.util.ArrayList; 33 34 /** 35 * Main program of the Java Disassembler :: class to jasm 36 */ 37 public class Main extends Tool { 38 39 private Options options; 40 41 public static final I18NResourceBundle i18n 42 = I18NResourceBundle.getBundleForClass(Main.class); 43 44 public Main(PrintWriter out, PrintWriter err, String programName) { 45 super(out, err, programName); 46 // tool specific initialization 47 options = Options.OptionObject(); 48 DebugFlag = () -> options.contains(Options.PR.DEBUG); 49 printCannotReadMsg = (fname) -> error( i18n.getString("jdis.error.cannot_read", fname)); 50 } 51 52 public Main(PrintStream out, String program) { 53 this(new PrintWriter(out), new PrintWriter(System.err), program); 54 } 55 56 @Override 57 public void usage() { 58 println(i18n.getString("jdis.usage")); 59 println(i18n.getString("jdis.opt.g")); 60 println(i18n.getString("jdis.opt.sl")); 61 println(i18n.getString("jdis.opt.hx")); 62 println(i18n.getString("jdis.opt.v")); 63 println(i18n.getString("jdis.opt.version")); 64 } 65 66 /** 67 * Run the disassembler 68 */ 69 public synchronized boolean disasm(String argv[]) { 70 ArrayList<String> files = new ArrayList<>(); 71 72 // Parse arguments 73 for (int i = 0; i < argv.length; i++) { 74 String arg = argv[i]; 75 switch (arg) { 76 case "-g": 77 options.setCodeOptions(); 78 break; 79 case "-v": 80 options.set(Options.PR.DEBUG); 81 break; 82 case "-sl": 83 options.set(Options.PR.SRC); 84 break; 85 case "-hx": 86 options.set(Options.PR.HEX); 87 break; 88 case "-version": 89 out.println(ProductInfo.FULL_VERSION); 90 break; 91 default: 92 if (arg.startsWith("-")) { 93 error(i18n.getString("jdis.error.invalid_option", arg)); 94 usage(); 95 return false; 96 } else { 97 files.add(arg); 98 } 99 break; 100 } 101 } 102 103 if (files.isEmpty()) { 104 usage(); 105 return false; 106 } 107 108 for (String fname : files) { 109 if (fname == null) { 110 continue; 111 } // cross out by CompilerChoice.compile 112 try { 113 ClassData cc = new ClassData(out, this); 114 cc.read(fname); 115 cc.print(); 116 out.flush(); 117 continue; 118 } catch (Error ee) { 119 if (DebugFlag.getAsBoolean()) 120 ee.printStackTrace(); 121 error(i18n.getString("jdis.error.fatal_error", fname)); 122 } catch (Exception ee) { 123 if (DebugFlag.getAsBoolean()) 124 ee.printStackTrace(); 125 error(i18n.getString("jdis.error.fatal_exception", fname)); 126 } 127 return false; 128 } 129 return true; 130 } 131 132 /** 133 * Main program 134 */ 135 public static void main(String argv[]) { 136 Main disassembler = new Main(new PrintWriter(new uEscWriter(System.out)), new PrintWriter(System.err), "jdis"); 137 boolean result = disassembler.disasm(argv); 138 System.exit(result ? 0 : 1); 139 } 140 }