• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.dx.command.dump;
18 
19 import com.android.dex.util.FileUtils;
20 import com.android.dx.cf.iface.ParseException;
21 import com.android.dx.util.HexParser;
22 import java.io.UnsupportedEncodingException;
23 
24 /**
25  * Main class for the class file dumper.
26  */
27 public class Main {
28 
29     static Args parsedArgs = new Args();
30 
31     /**
32      * This class is uninstantiable.
33      */
Main()34     private Main() {
35         // This space intentionally left blank.
36     }
37 
38     /**
39      * Run!
40      */
main(String[] args)41     public static void main(String[] args) {
42         int at = 0;
43 
44         for (/*at*/; at < args.length; at++) {
45             String arg = args[at];
46             if (arg.equals("--") || !arg.startsWith("--")) {
47                 break;
48             } else if (arg.equals("--bytes")) {
49                 parsedArgs.rawBytes = true;
50             } else if (arg.equals("--basic-blocks")) {
51                 parsedArgs.basicBlocks = true;
52             } else if (arg.equals("--rop-blocks")) {
53                 parsedArgs.ropBlocks = true;
54             } else if (arg.equals("--optimize")) {
55                 parsedArgs.optimize = true;
56             } else if (arg.equals("--ssa-blocks")) {
57                 parsedArgs.ssaBlocks = true;
58             } else if (arg.startsWith("--ssa-step=")) {
59                 parsedArgs.ssaStep = arg.substring(arg.indexOf('=') + 1);
60             } else if (arg.equals("--debug")) {
61                 parsedArgs.debug = true;
62             } else if (arg.equals("--dot")) {
63                 parsedArgs.dotDump = true;
64             } else if (arg.equals("--strict")) {
65                 parsedArgs.strictParse = true;
66             } else if (arg.startsWith("--width=")) {
67                 arg = arg.substring(arg.indexOf('=') + 1);
68                 parsedArgs.width = Integer.parseInt(arg);
69             } else if (arg.startsWith("--method=")) {
70                 arg = arg.substring(arg.indexOf('=') + 1);
71                 parsedArgs.method = arg;
72             } else {
73                 System.err.println("unknown option: " + arg);
74                 throw new RuntimeException("usage");
75             }
76         }
77 
78         if (at == args.length) {
79             System.err.println("no input files specified");
80             throw new RuntimeException("usage");
81         }
82 
83         for (/*at*/; at < args.length; at++) {
84             try {
85                 String name = args[at];
86                 System.out.println("reading " + name + "...");
87                 byte[] bytes = FileUtils.readFile(name);
88                 if (!name.endsWith(".class")) {
89                     String src;
90                     try {
91                         src = new String(bytes, "utf-8");
92                     } catch (UnsupportedEncodingException ex) {
93                         throw new RuntimeException("shouldn't happen", ex);
94                     }
95                     bytes = HexParser.parse(src);
96                 }
97                 processOne(name, bytes);
98             } catch (ParseException ex) {
99                 System.err.println("\ntrouble parsing:");
100                 if (parsedArgs.debug) {
101                     ex.printStackTrace();
102                 } else {
103                     ex.printContext(System.err);
104                 }
105             }
106         }
107     }
108 
109     /**
110      * Processes one file.
111      *
112      * @param name {@code non-null;} name of the file
113      * @param bytes {@code non-null;} contents of the file
114      */
processOne(String name, byte[] bytes)115     private static void processOne(String name, byte[] bytes) {
116         if (parsedArgs.dotDump) {
117             DotDumper.dump(bytes, name, parsedArgs);
118         } else if (parsedArgs.basicBlocks) {
119             BlockDumper.dump(bytes, System.out, name, false, parsedArgs);
120         } else if (parsedArgs.ropBlocks) {
121             BlockDumper.dump(bytes, System.out, name, true, parsedArgs);
122         } else if (parsedArgs.ssaBlocks) {
123             // --optimize ignored with --ssa-blocks
124             parsedArgs.optimize = false;
125             SsaDumper.dump(bytes, System.out, name, parsedArgs);
126         } else {
127             ClassDumper.dump(bytes, System.out, name, parsedArgs);
128         }
129     }
130 }
131