# HG changeset patch # User Thomas Wuerthinger # Date 1372016160 -7200 # Node ID 3e6f538829ce25efaf077543735136439b9e714e # Parent 3e34b0318de6f20ce0fa38735f409ec2063af752 Add decompiler debug handler. diff -r 3e34b0318de6 -r 3e6f538829ce graal/com.oracle.graal.printer/src/com/oracle/graal/printer/DecompilerDebugDumpHandler.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.printer/src/com/oracle/graal/printer/DecompilerDebugDumpHandler.java Sun Jun 23 21:36:00 2013 +0200 @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.printer; + +import java.io.*; +import java.util.concurrent.atomic.*; + +import static com.oracle.graal.phases.GraalOptions.*; + +import com.oracle.graal.debug.*; +import com.oracle.graal.java.decompiler.*; +import com.oracle.graal.nodes.*; +import com.oracle.graal.phases.schedule.*; + +public class DecompilerDebugDumpHandler implements DebugDumpHandler { + + private final PrintStream infoPrintStream = System.out; + private File file; + private FileOutputStream fos; + private PrintStream printStream; + private String fileName; + private static final AtomicInteger uniqueId = new AtomicInteger(); + + @Override + public void dump(Object object, final String message) { + if (object instanceof StructuredGraph) { + if (Debug.currentScope().contains("LowTier")) { + // no decompilation after high / mid tier + return; + } + final StructuredGraph graph = (StructuredGraph) object; + String filter = DecompileAfterPhase.getValue(); + if (filter.endsWith("Phase")) { + filter = filter.substring(0, filter.indexOf("Phase")); + } + + if (printStream == null) { + fileName = "decompilerDump_" + uniqueId.incrementAndGet() + "_" + System.currentTimeMillis() + ".txt"; + file = new File(fileName); + try { + fos = new FileOutputStream(file, true); + } catch (FileNotFoundException e) { + throw new IllegalStateException(e); + } + printStream = new PrintStream(fos); + infoPrintStream.println("Dump Decompiler Output to " + file.getAbsolutePath()); + } + + final String currentScope = Debug.currentScope(); + if (currentScope.endsWith(filter) && graph.method() != null) { + final String methodName = graph.method().getName(); + Debug.sandbox("Printing Decompiler Output", null, new Runnable() { + + @Override + public void run() { + printStream.println(); + printStream.println("Object: " + methodName); + printStream.println("Message: " + message); + new Decompiler(graph, getPredefinedSchedule(), printStream, infoPrintStream, currentScope).decompile(); + printStream.flush(); + } + }); + + } + } + } + + private static SchedulePhase getPredefinedSchedule() { + SchedulePhase result = null; + for (Object o : Debug.context()) { + if (o instanceof SchedulePhase) { + result = (SchedulePhase) o; + } + } + return result; + } + + @Override + public void close() { + try { + printStream.close(); + fos.close(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + +}