diff options
| author | 2017-09-29 08:15:49 +0000 | |
|---|---|---|
| committer | 2017-09-29 08:15:49 +0000 | |
| commit | cdf68b16a212e71120ecccbf9f4d798b61259ed2 (patch) | |
| tree | fff027f87d46ba8b2d05569634b582a7eed58af1 | |
| parent | b880a8a5926662bb49d9e66ceaa84be1f7f0d5f5 (diff) | |
| parent | 5316bb897d5b66a9cf1eb38b28c562d48a8d2863 (diff) | |
Merge "Give better error messages on failure to launch ahat."
| -rw-r--r-- | tools/ahat/src/Main.java | 55 | ||||
| -rw-r--r-- | tools/ahat/src/heapdump/HprofFormatException.java | 4 | ||||
| -rw-r--r-- | tools/ahat/src/heapdump/Parser.java | 15 |
3 files changed, 47 insertions, 27 deletions
diff --git a/tools/ahat/src/Main.java b/tools/ahat/src/Main.java index 623a865785..31c485d851 100644 --- a/tools/ahat/src/Main.java +++ b/tools/ahat/src/Main.java @@ -18,6 +18,7 @@ package com.android.ahat; import com.android.ahat.heapdump.AhatSnapshot; import com.android.ahat.heapdump.Diff; +import com.android.ahat.heapdump.HprofFormatException; import com.android.ahat.heapdump.Parser; import com.android.ahat.proguard.ProguardMap; import com.sun.net.httpserver.HttpServer; @@ -47,7 +48,7 @@ public class Main { out.println(""); } - public static void main(String[] args) throws Exception { + public static void main(String[] args) { int port = 7100; for (String arg : args) { if (arg.equals("--help")) { @@ -104,34 +105,40 @@ public class Main { return; } - // Launch the server before parsing the hprof file so we get - // BindExceptions quickly. - InetAddress loopback = InetAddress.getLoopbackAddress(); - InetSocketAddress addr = new InetSocketAddress(loopback, port); - HttpServer server = HttpServer.create(addr, 0); + try { + // Launch the server before parsing the hprof file so we get + // BindExceptions quickly. + InetAddress loopback = InetAddress.getLoopbackAddress(); + InetSocketAddress addr = new InetSocketAddress(loopback, port); + System.out.println("Preparing " + addr + " ..."); + HttpServer server = HttpServer.create(addr, 0); - System.out.println("Processing hprof file..."); - AhatSnapshot ahat = Parser.parseHeapDump(hprof, map); + System.out.println("Processing '" + hprof + "' ..."); + AhatSnapshot ahat = Parser.parseHeapDump(hprof, map); - if (hprofbase != null) { - System.out.println("Processing baseline hprof file..."); - AhatSnapshot base = Parser.parseHeapDump(hprofbase, mapbase); + if (hprofbase != null) { + System.out.println("Processing '" + hprofbase + "' ..."); + AhatSnapshot base = Parser.parseHeapDump(hprofbase, mapbase); - System.out.println("Diffing hprof files..."); - Diff.snapshots(ahat, base); - } + System.out.println("Diffing heap dumps ..."); + Diff.snapshots(ahat, base); + } - server.createContext("/", new AhatHttpHandler(new OverviewHandler(ahat, hprof, hprofbase))); - server.createContext("/rooted", new AhatHttpHandler(new RootedHandler(ahat))); - server.createContext("/object", new AhatHttpHandler(new ObjectHandler(ahat))); - server.createContext("/objects", new AhatHttpHandler(new ObjectsHandler(ahat))); - server.createContext("/site", new AhatHttpHandler(new SiteHandler(ahat))); - server.createContext("/bitmap", new BitmapHandler(ahat)); - server.createContext("/style.css", new StaticHandler("style.css", "text/css")); - server.setExecutor(Executors.newFixedThreadPool(1)); - System.out.println("Server started on localhost:" + port); + server.createContext("/", new AhatHttpHandler(new OverviewHandler(ahat, hprof, hprofbase))); + server.createContext("/rooted", new AhatHttpHandler(new RootedHandler(ahat))); + server.createContext("/object", new AhatHttpHandler(new ObjectHandler(ahat))); + server.createContext("/objects", new AhatHttpHandler(new ObjectsHandler(ahat))); + server.createContext("/site", new AhatHttpHandler(new SiteHandler(ahat))); + server.createContext("/bitmap", new BitmapHandler(ahat)); + server.createContext("/style.css", new StaticHandler("style.css", "text/css")); + server.setExecutor(Executors.newFixedThreadPool(1)); + System.out.println("Server started on localhost:" + port); - server.start(); + server.start(); + } catch (HprofFormatException|IOException e) { + System.err.println("Unable to launch ahat:"); + e.printStackTrace(); + } } } diff --git a/tools/ahat/src/heapdump/HprofFormatException.java b/tools/ahat/src/heapdump/HprofFormatException.java index 55e8958c43..0e128cd50a 100644 --- a/tools/ahat/src/heapdump/HprofFormatException.java +++ b/tools/ahat/src/heapdump/HprofFormatException.java @@ -20,4 +20,8 @@ public class HprofFormatException extends Exception { public HprofFormatException(String msg) { super(msg); } + + public HprofFormatException(String msg, Exception cause) { + super(msg, cause); + } } diff --git a/tools/ahat/src/heapdump/Parser.java b/tools/ahat/src/heapdump/Parser.java index 9dc4eb4a01..756b7d2554 100644 --- a/tools/ahat/src/heapdump/Parser.java +++ b/tools/ahat/src/heapdump/Parser.java @@ -19,6 +19,7 @@ package com.android.ahat.heapdump; import com.android.ahat.proguard.ProguardMap; import java.io.File; import java.io.IOException; +import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.StandardCharsets; @@ -48,7 +49,11 @@ public class Parser { */ public static AhatSnapshot parseHeapDump(File hprof, ProguardMap map) throws IOException, HprofFormatException { - return parseHeapDump(new HprofBuffer(hprof), map); + try { + return parseHeapDump(new HprofBuffer(hprof), map); + } catch (BufferUnderflowException e) { + throw new HprofFormatException("Unexpected end of file", e); + } } /** @@ -56,11 +61,15 @@ public class Parser { */ public static AhatSnapshot parseHeapDump(ByteBuffer hprof, ProguardMap map) throws IOException, HprofFormatException { - return parseHeapDump(new HprofBuffer(hprof), map); + try { + return parseHeapDump(new HprofBuffer(hprof), map); + } catch (BufferUnderflowException e) { + throw new HprofFormatException("Unexpected end of file", e); + } } private static AhatSnapshot parseHeapDump(HprofBuffer hprof, ProguardMap map) - throws IOException, HprofFormatException { + throws IOException, HprofFormatException, BufferUnderflowException { // Read, and mostly ignore, the hprof header info. { StringBuilder format = new StringBuilder(); |