Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 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 | |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 70 | System.out.println("Processing hprof file..."); |
| 71 | AhatSnapshot ahat = AhatSnapshot.fromHprof(hprof); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 72 | |
| 73 | InetAddress loopback = InetAddress.getLoopbackAddress(); |
| 74 | InetSocketAddress addr = new InetSocketAddress(loopback, port); |
| 75 | HttpServer server = HttpServer.create(addr, 0); |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 76 | server.createContext("/", new AhatHttpHandler(new OverviewHandler(ahat, hprof))); |
Richard Uhler | 7a16adb | 2015-11-11 09:13:23 -0800 | [diff] [blame] | 77 | server.createContext("/rooted", new AhatHttpHandler(new RootedHandler(ahat))); |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 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))); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 81 | server.createContext("/bitmap", new BitmapHandler(ahat)); |
Richard Uhler | a7f46cb | 2015-12-21 14:34:59 -0800 | [diff] [blame] | 82 | server.createContext("/help", new HelpHandler()); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 83 | server.createContext("/style.css", new StaticHandler("style.css", "text/css")); |
| 84 | server.setExecutor(Executors.newFixedThreadPool(1)); |
| 85 | System.out.println("Server started on localhost:" + port); |
| 86 | server.start(); |
| 87 | } |
| 88 | } |
| 89 | |