blob: 091820f7fc9db997391e07b797bb8f9fa5073e2c [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 Uhlerb730b782015-07-15 16:01:58 -070019import com.sun.net.httpserver.HttpServer;
20import java.io.File;
21import java.io.IOException;
22import java.io.PrintStream;
23import java.net.InetAddress;
24import java.net.InetSocketAddress;
25import java.util.concurrent.Executors;
26
27public 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 Uhler35244722015-09-10 16:45:54 -070070 System.out.println("Processing hprof file...");
71 AhatSnapshot ahat = AhatSnapshot.fromHprof(hprof);
Richard Uhlerb730b782015-07-15 16:01:58 -070072
73 InetAddress loopback = InetAddress.getLoopbackAddress();
74 InetSocketAddress addr = new InetSocketAddress(loopback, port);
75 HttpServer server = HttpServer.create(addr, 0);
Richard Uhler1af86f12015-10-29 14:55:00 -070076 server.createContext("/", new AhatHttpHandler(new OverviewHandler(ahat, hprof)));
Richard Uhler7a16adb2015-11-11 09:13:23 -080077 server.createContext("/rooted", new AhatHttpHandler(new RootedHandler(ahat)));
Richard Uhler1af86f12015-10-29 14:55:00 -070078 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 Uhlerb730b782015-07-15 16:01:58 -070081 server.createContext("/bitmap", new BitmapHandler(ahat));
Richard Uhlera7f46cb2015-12-21 14:34:59 -080082 server.createContext("/help", new HelpHandler());
Richard Uhlerb730b782015-07-15 16:01:58 -070083 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