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 | ec78c78 | 2016-05-13 14:19:37 -0700 | [diff] [blame] | 19 | import com.android.tools.perflib.heap.ProguardMap; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 20 | import com.sun.net.httpserver.HttpServer; |
| 21 | import java.io.File; |
| 22 | import java.io.IOException; |
| 23 | import java.io.PrintStream; |
| 24 | import java.net.InetAddress; |
| 25 | import java.net.InetSocketAddress; |
Richard Uhler | ec78c78 | 2016-05-13 14:19:37 -0700 | [diff] [blame] | 26 | import java.text.ParseException; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 27 | import java.util.concurrent.Executors; |
| 28 | |
| 29 | public class Main { |
| 30 | |
| 31 | public static void help(PrintStream out) { |
Richard Uhler | ec78c78 | 2016-05-13 14:19:37 -0700 | [diff] [blame] | 32 | out.println("java -jar ahat.jar [-p port] [--proguard-map FILE] FILE"); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 33 | out.println(" Launch an http server for viewing " |
| 34 | + "the given Android heap-dump FILE."); |
| 35 | out.println(""); |
| 36 | out.println("Options:"); |
| 37 | out.println(" -p <port>"); |
| 38 | out.println(" Serve pages on the given port. Defaults to 7100."); |
Richard Uhler | ec78c78 | 2016-05-13 14:19:37 -0700 | [diff] [blame] | 39 | out.println(" --proguard-map FILE"); |
| 40 | out.println(" Use the proguard map FILE to deobfuscate the heap dump."); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 41 | out.println(""); |
| 42 | } |
| 43 | |
| 44 | public static void main(String[] args) throws IOException { |
| 45 | int port = 7100; |
| 46 | for (String arg : args) { |
| 47 | if (arg.equals("--help")) { |
| 48 | help(System.out); |
| 49 | return; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | File hprof = null; |
Richard Uhler | ec78c78 | 2016-05-13 14:19:37 -0700 | [diff] [blame] | 54 | ProguardMap map = new ProguardMap(); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 55 | for (int i = 0; i < args.length; i++) { |
| 56 | if ("-p".equals(args[i]) && i + 1 < args.length) { |
| 57 | i++; |
| 58 | port = Integer.parseInt(args[i]); |
Richard Uhler | ec78c78 | 2016-05-13 14:19:37 -0700 | [diff] [blame] | 59 | } else if ("--proguard-map".equals(args[i]) && i + 1 < args.length) { |
| 60 | i++; |
| 61 | try { |
| 62 | map.readFromFile(new File(args[i])); |
| 63 | } catch (IOException|ParseException ex) { |
| 64 | System.out.println("Unable to read proguard map: " + ex); |
| 65 | System.out.println("The proguard map will not be used."); |
| 66 | } |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 67 | } else { |
| 68 | if (hprof != null) { |
| 69 | System.err.println("multiple input files."); |
| 70 | help(System.err); |
| 71 | return; |
| 72 | } |
| 73 | hprof = new File(args[i]); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (hprof == null) { |
| 78 | System.err.println("no input file."); |
| 79 | help(System.err); |
| 80 | return; |
| 81 | } |
| 82 | |
Richard Uhler | 46e476b | 2016-07-21 13:52:48 -0700 | [diff] [blame] | 83 | // Launch the server before parsing the hprof file so we get |
| 84 | // BindExceptions quickly. |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 85 | InetAddress loopback = InetAddress.getLoopbackAddress(); |
| 86 | InetSocketAddress addr = new InetSocketAddress(loopback, port); |
| 87 | HttpServer server = HttpServer.create(addr, 0); |
Richard Uhler | 46e476b | 2016-07-21 13:52:48 -0700 | [diff] [blame] | 88 | |
| 89 | System.out.println("Processing hprof file..."); |
Richard Uhler | ec78c78 | 2016-05-13 14:19:37 -0700 | [diff] [blame] | 90 | AhatSnapshot ahat = AhatSnapshot.fromHprof(hprof, map); |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 91 | server.createContext("/", new AhatHttpHandler(new OverviewHandler(ahat, hprof))); |
Richard Uhler | 7a16adb | 2015-11-11 09:13:23 -0800 | [diff] [blame] | 92 | server.createContext("/rooted", new AhatHttpHandler(new RootedHandler(ahat))); |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 93 | server.createContext("/object", new AhatHttpHandler(new ObjectHandler(ahat))); |
| 94 | server.createContext("/objects", new AhatHttpHandler(new ObjectsHandler(ahat))); |
| 95 | server.createContext("/site", new AhatHttpHandler(new SiteHandler(ahat))); |
Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 96 | server.createContext("/native", new AhatHttpHandler(new NativeAllocationsHandler(ahat))); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 97 | server.createContext("/bitmap", new BitmapHandler(ahat)); |
Richard Uhler | a7f46cb | 2015-12-21 14:34:59 -0800 | [diff] [blame] | 98 | server.createContext("/help", new HelpHandler()); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 99 | server.createContext("/style.css", new StaticHandler("style.css", "text/css")); |
| 100 | server.setExecutor(Executors.newFixedThreadPool(1)); |
| 101 | System.out.println("Server started on localhost:" + port); |
| 102 | server.start(); |
| 103 | } |
| 104 | } |
| 105 | |