blob: c79b57863e2b8c65e172288f269d7b0a1a37aa43 [file] [log] [blame]
Richard Uhlerb730b782015-07-15 16:01:58 -07001/*
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
17package com.android.ahat;
18
Richard Uhlerec78c782016-05-13 14:19:37 -070019import com.android.tools.perflib.heap.ProguardMap;
Richard Uhlerb730b782015-07-15 16:01:58 -070020import com.sun.net.httpserver.HttpServer;
21import java.io.File;
22import java.io.IOException;
23import java.io.PrintStream;
24import java.net.InetAddress;
25import java.net.InetSocketAddress;
Richard Uhlerec78c782016-05-13 14:19:37 -070026import java.text.ParseException;
Richard Uhlerb730b782015-07-15 16:01:58 -070027import java.util.concurrent.Executors;
28
29public class Main {
30
31 public static void help(PrintStream out) {
Richard Uhlerec78c782016-05-13 14:19:37 -070032 out.println("java -jar ahat.jar [-p port] [--proguard-map FILE] FILE");
Richard Uhlerb730b782015-07-15 16:01:58 -070033 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 Uhlerec78c782016-05-13 14:19:37 -070039 out.println(" --proguard-map FILE");
40 out.println(" Use the proguard map FILE to deobfuscate the heap dump.");
Richard Uhlerb730b782015-07-15 16:01:58 -070041 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 Uhlerec78c782016-05-13 14:19:37 -070054 ProguardMap map = new ProguardMap();
Richard Uhlerb730b782015-07-15 16:01:58 -070055 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 Uhlerec78c782016-05-13 14:19:37 -070059 } 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 Uhlerb730b782015-07-15 16:01:58 -070067 } 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 Uhler46e476b2016-07-21 13:52:48 -070083 // Launch the server before parsing the hprof file so we get
84 // BindExceptions quickly.
Richard Uhlerb730b782015-07-15 16:01:58 -070085 InetAddress loopback = InetAddress.getLoopbackAddress();
86 InetSocketAddress addr = new InetSocketAddress(loopback, port);
87 HttpServer server = HttpServer.create(addr, 0);
Richard Uhler46e476b2016-07-21 13:52:48 -070088
89 System.out.println("Processing hprof file...");
Richard Uhlerec78c782016-05-13 14:19:37 -070090 AhatSnapshot ahat = AhatSnapshot.fromHprof(hprof, map);
Richard Uhler1af86f12015-10-29 14:55:00 -070091 server.createContext("/", new AhatHttpHandler(new OverviewHandler(ahat, hprof)));
Richard Uhler7a16adb2015-11-11 09:13:23 -080092 server.createContext("/rooted", new AhatHttpHandler(new RootedHandler(ahat)));
Richard Uhler1af86f12015-10-29 14:55:00 -070093 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 Uhler1a5baaa2015-12-21 12:47:26 -080096 server.createContext("/native", new AhatHttpHandler(new NativeAllocationsHandler(ahat)));
Richard Uhlerb730b782015-07-15 16:01:58 -070097 server.createContext("/bitmap", new BitmapHandler(ahat));
Richard Uhlera7f46cb2015-12-21 14:34:59 -080098 server.createContext("/help", new HelpHandler());
Richard Uhlerb730b782015-07-15 16:01:58 -070099 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