1 /* 2 * Copyright (C) 2015 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.ahat; 18 19 import com.sun.net.httpserver.HttpServer; 20 import java.io.File; 21 import java.io.IOException; 22 import java.io.PrintStream; 23 import java.net.InetAddress; 24 import java.net.InetSocketAddress; 25 import java.util.concurrent.Executors; 26 27 public class Main { 28 help(PrintStream out)29 public static void help(PrintStream out) { 30 out.println("java -jar ahat.jar [-p port] FILE"); 31 out.println(" Launch an http server for viewing " 32 + "the given Android heap-dump FILE."); 33 out.println(""); 34 out.println("Options:"); 35 out.println(" -p <port>"); 36 out.println(" Serve pages on the given port. Defaults to 7100."); 37 out.println(""); 38 } 39 main(String[] args)40 public static void main(String[] args) throws IOException { 41 int port = 7100; 42 for (String arg : args) { 43 if (arg.equals("--help")) { 44 help(System.out); 45 return; 46 } 47 } 48 49 File hprof = null; 50 for (int i = 0; i < args.length; i++) { 51 if ("-p".equals(args[i]) && i + 1 < args.length) { 52 i++; 53 port = Integer.parseInt(args[i]); 54 } else { 55 if (hprof != null) { 56 System.err.println("multiple input files."); 57 help(System.err); 58 return; 59 } 60 hprof = new File(args[i]); 61 } 62 } 63 64 if (hprof == null) { 65 System.err.println("no input file."); 66 help(System.err); 67 return; 68 } 69 70 System.out.println("Processing hprof file..."); 71 AhatSnapshot ahat = AhatSnapshot.fromHprof(hprof); 72 73 InetAddress loopback = InetAddress.getLoopbackAddress(); 74 InetSocketAddress addr = new InetSocketAddress(loopback, port); 75 HttpServer server = HttpServer.create(addr, 0); 76 server.createContext("/", new AhatHttpHandler(new OverviewHandler(ahat, hprof))); 77 server.createContext("/rooted", new AhatHttpHandler(new RootedHandler(ahat))); 78 server.createContext("/object", new AhatHttpHandler(new ObjectHandler(ahat))); 79 server.createContext("/objects", new AhatHttpHandler(new ObjectsHandler(ahat))); 80 server.createContext("/site", new AhatHttpHandler(new SiteHandler(ahat))); 81 server.createContext("/native", new AhatHttpHandler(new NativeAllocationsHandler(ahat))); 82 server.createContext("/bitmap", new BitmapHandler(ahat)); 83 server.createContext("/help", new HelpHandler()); 84 server.createContext("/style.css", new StaticHandler("style.css", "text/css")); 85 server.setExecutor(Executors.newFixedThreadPool(1)); 86 System.out.println("Server started on localhost:" + port); 87 server.start(); 88 } 89 } 90 91