diff --git a/core/src/main/java/arthas.properties b/core/src/main/java/arthas.properties index 99d9f256a..64cb21762 100644 --- a/core/src/main/java/arthas.properties +++ b/core/src/main/java/arthas.properties @@ -11,3 +11,5 @@ arthas.enhanceLoaders=java.lang.ClassLoader #arthas.appName=demoapp #arthas.tunnelServer=ws://127.0.0.1:7777/ws #arthas.agentId=mmmmmmyiddddd + +#arthas.outputPath=arthas-output \ No newline at end of file diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/ProfilerCommand.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/ProfilerCommand.java index c41cdd67f..6e38585a7 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/ProfilerCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/ProfilerCommand.java @@ -433,10 +433,16 @@ public class ProfilerCommand extends AnnotatedCommand { return profilerModel; } - private String outputFile() { + private String outputFile() throws IOException { if (this.file == null) { - this.file = new File("arthas-output", - new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + "." + this.format).getAbsolutePath(); + File outputPath = ArthasBootstrap.getInstance().getOutputPath(); + if (outputPath != null) { + this.file = new File(outputPath, + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + "." + this.format) + .getAbsolutePath(); + } else { + this.file = File.createTempFile("arthas-output", "." + this.format).getAbsolutePath(); + } } return file; } diff --git a/core/src/main/java/com/taobao/arthas/core/config/Configure.java b/core/src/main/java/com/taobao/arthas/core/config/Configure.java index 1ebe0db8b..cec4e16cb 100644 --- a/core/src/main/java/com/taobao/arthas/core/config/Configure.java +++ b/core/src/main/java/com/taobao/arthas/core/config/Configure.java @@ -28,6 +28,11 @@ public class Configure { private String tunnelServer; private String agentId; + /** + * @see com.taobao.arthas.common.ArthasConstants#ARTHAS_OUTPUT + */ + private String outputPath; + /** * 需要被增强的ClassLoader的全类名,多个用英文 , 分隔 */ @@ -148,6 +153,14 @@ public class Configure { this.enhanceLoaders = enhanceLoaders; } + public String getOutputPath() { + return outputPath; + } + + public void setOutputPath(String outputPath) { + this.outputPath = outputPath; + } + /** * 序列化成字符串 * diff --git a/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java b/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java index 32607379d..2b5164630 100644 --- a/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java +++ b/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java @@ -104,7 +104,7 @@ public class ArthasBootstrap { private SessionManager sessionManager; private TunnelClient tunnelClient; - private File arthasOutputDir; + private File outputPath; private static LoggerContext loggerContext; private EventExecutorGroup workerGroup; @@ -122,15 +122,20 @@ public class ArthasBootstrap { private ArthasBootstrap(Instrumentation instrumentation, Map args) throws Throwable { this.instrumentation = instrumentation; - String outputPath = System.getProperty("arthas.output.dir", "arthas-output"); - arthasOutputDir = new File(outputPath); - arthasOutputDir.mkdirs(); initFastjson(); // 1. initSpy() initSpy(); // 2. ArthasEnvironment initArthasEnvironment(args); + + String outputPathStr = configure.getOutputPath(); + if (outputPathStr == null) { + outputPathStr = ArthasConstants.ARTHAS_OUTPUT; + } + outputPath = new File(outputPathStr); + outputPath.mkdirs(); + // 3. init logger loggerContext = LogUtil.initLooger(arthasEnvironment); @@ -622,4 +627,9 @@ public class ArthasBootstrap { public HttpApiHandler getHttpApiHandler() { return httpApiHandler; } + + public File getOutputPath() { + return outputPath; + } + } diff --git a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/DirectoryBrowser.java b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/DirectoryBrowser.java index 5ab46822a..216cd8717 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/DirectoryBrowser.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/DirectoryBrowser.java @@ -55,7 +55,7 @@ public class DirectoryBrowser { private static String linePart1Str = ""; private static String linePart2Str = "%-60s"; - private static String renderDir(File dir) { + static String renderDir(File dir) { File[] listFiles = dir.listFiles(); StringBuilder sb = new StringBuilder(8192); @@ -112,7 +112,9 @@ public class DirectoryBrowser { if (path.startsWith("/")) { path = path.substring(1, path.length()); } - File file = new File(path); + // path maybe: arthas-output/20201225-203454.svg + // 需要取 dir的parent来去掉前缀 + File file = new File(dir.getParent(), path); if (isSubFile(dir, file)) { DefaultFullHttpResponse fullResp = new DefaultFullHttpResponse(version, HttpResponseStatus.OK); diff --git a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/HttpRequestHandler.java b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/HttpRequestHandler.java index b38cba88b..4d683de63 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/HttpRequestHandler.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/HttpRequestHandler.java @@ -46,6 +46,10 @@ public class HttpRequestHandler extends SimpleChannelInboundHandler pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new HttpObjectAggregator(ArthasConstants.MAX_HTTP_CONTENT_LENGTH)); - pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler("/ws", new File(ArthasConstants.ARTHAS_OUTPUT))); + pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler("/ws")); pipeline.addLast(new WebSocketServerProtocolHandler("/ws")); pipeline.addLast(new TtyWebSocketFrameHandler(group, handler)); } diff --git a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/TtyServerInitializer.java b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/TtyServerInitializer.java index b2b419d1d..6693a7dc8 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/TtyServerInitializer.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/TtyServerInitializer.java @@ -39,7 +39,7 @@ public class TtyServerInitializer extends ChannelInitializer { pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new HttpObjectAggregator(ArthasConstants.MAX_HTTP_CONTENT_LENGTH)); - pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler("/ws", new File(ArthasConstants.ARTHAS_OUTPUT))); + pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler("/ws")); pipeline.addLast(new WebSocketServerProtocolHandler("/ws")); pipeline.addLast(new TtyWebSocketFrameHandler(group, handler)); } diff --git a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/httptelnet/ProtocolDetectHandler.java b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/httptelnet/ProtocolDetectHandler.java index 81892b7a7..09fbf2e18 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/httptelnet/ProtocolDetectHandler.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/httptelnet/ProtocolDetectHandler.java @@ -86,7 +86,7 @@ public class ProtocolDetectHandler extends ChannelInboundHandlerAdapter { pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new HttpObjectAggregator(ArthasConstants.MAX_HTTP_CONTENT_LENGTH)); - pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler("/ws", new File(ArthasConstants.ARTHAS_OUTPUT))); + pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler("/ws")); pipeline.addLast(new WebSocketServerProtocolHandler("/ws")); pipeline.addLast(new TtyWebSocketFrameHandler(channelGroup, ttyConnectionFactory)); ctx.fireChannelActive();