changeset 4361:18b52fec79f1

Completed exception interception.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Fri, 20 Jan 2012 16:19:41 +0100
parents d203d8b25721
children ed69fd347566
files graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/alloc/LinearScan.java graal/com.oracle.max.graal.debug/src/com/oracle/max/graal/debug/Debug.java graal/com.oracle.max.graal.debug/src/com/oracle/max/graal/debug/DebugConfig.java graal/com.oracle.max.graal.debug/src/com/oracle/max/graal/debug/internal/DebugScope.java graal/com.oracle.max.graal.hotspot/src/com/oracle/max/graal/hotspot/HotSpotDebugConfig.java
diffstat 5 files changed, 91 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/alloc/LinearScan.java	Fri Jan 20 15:43:15 2012 +0100
+++ b/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/alloc/LinearScan.java	Fri Jan 20 16:19:41 2012 +0100
@@ -131,7 +131,7 @@
 
         this.registers = target.arch.registers;
         this.firstVariableNumber = registers.length;
-        this.variables = new ArrayList<>(ir.numVariables() * 3 / 2);
+        this.variables = new ArrayList<>(ir.numVariables() * 3 / 2); throw new NullPointerException();
     }
 
     public static boolean isVariableOrRegister(CiValue value) {
--- a/graal/com.oracle.max.graal.debug/src/com/oracle/max/graal/debug/Debug.java	Fri Jan 20 15:43:15 2012 +0100
+++ b/graal/com.oracle.max.graal.debug/src/com/oracle/max/graal/debug/Debug.java	Fri Jan 20 16:19:41 2012 +0100
@@ -25,7 +25,7 @@
 import com.oracle.max.graal.debug.internal.DebugScope;
 import com.oracle.max.graal.debug.internal.MetricImpl;
 import com.oracle.max.graal.debug.internal.TimerImpl;
-import java.util.Collections;
+import java.util.*;
 import java.util.concurrent.*;
 
 
@@ -97,6 +97,18 @@
         }
     }
 
+    public static List<Object> contextSnapshot() {
+        if (ENABLED) {
+            List<Object> result = new ArrayList<>();
+            for (Object o : context()) {
+                result.add(o);
+            }
+            return result;
+        } else {
+            return Collections.emptyList();
+        }
+    }
+
     public static DebugMetric metric(String name) {
         if (ENABLED && DebugScope.getInstance().isMeterEnabled()) {
             return new MetricImpl(name);
@@ -111,6 +123,36 @@
         }
     }
 
+    public static DebugConfig fixedConfix(final boolean isLogEnabled, final boolean isDumpEnabled, final boolean isMeterEnabled, final boolean isTimerEnabled) {
+        return new DebugConfig() {
+
+            @Override
+            public boolean isLogEnabled() {
+                return isLogEnabled;
+            }
+
+            @Override
+            public boolean isMeterEnabled() {
+                return isMeterEnabled;
+            }
+
+            @Override
+            public boolean isDumpEnabled() {
+                return isDumpEnabled;
+            }
+
+            @Override
+            public boolean isTimerEnabled() {
+                return isTimerEnabled;
+            }
+
+            @Override
+            public RuntimeException interceptException(RuntimeException e) {
+                return e;
+            }
+        };
+    }
+
     private static final DebugMetric VOID_METRIC = new DebugMetric() {
         public void increment() { }
         public void add(int value) { }
--- a/graal/com.oracle.max.graal.debug/src/com/oracle/max/graal/debug/DebugConfig.java	Fri Jan 20 15:43:15 2012 +0100
+++ b/graal/com.oracle.max.graal.debug/src/com/oracle/max/graal/debug/DebugConfig.java	Fri Jan 20 16:19:41 2012 +0100
@@ -28,4 +28,5 @@
     boolean isMeterEnabled();
     boolean isDumpEnabled();
     boolean isTimerEnabled();
+    RuntimeException interceptException(RuntimeException e);
 }
--- a/graal/com.oracle.max.graal.debug/src/com/oracle/max/graal/debug/internal/DebugScope.java	Fri Jan 20 15:43:15 2012 +0100
+++ b/graal/com.oracle.max.graal.debug/src/com/oracle/max/graal/debug/internal/DebugScope.java	Fri Jan 20 16:19:41 2012 +0100
@@ -31,8 +31,9 @@
 
 public final class DebugScope {
 
-    private static ThreadLocal<DebugScope> instance = new ThreadLocal<>();
-    private static ThreadLocal<DebugConfig> config = new ThreadLocal<>();
+    private static ThreadLocal<DebugScope> instanceTL = new ThreadLocal<>();
+    private static ThreadLocal<DebugConfig> configTL = new ThreadLocal<>();
+    private static ThreadLocal<RuntimeException> lastExceptionThrownTL = new ThreadLocal<>();
 
     private final String name;
     private final DebugScope parent;
@@ -52,17 +53,17 @@
     private boolean dumpEnabled;
 
     public static DebugScope getInstance() {
-        DebugScope result = instance.get();
+        DebugScope result = instanceTL.get();
         if (result == null) {
-            instance.set(new DebugScope("DEFAULT", "DEFAULT", null));
-            return instance.get();
+            instanceTL.set(new DebugScope("DEFAULT", "DEFAULT", null));
+            return instanceTL.get();
         } else {
             return result;
         }
     }
 
     public static DebugConfig getConfig() {
-        return config.get();
+        return configTL.get();
     }
 
     private DebugScope(String name, String qualifiedName, DebugScope parent, Object... context) {
@@ -107,7 +108,7 @@
         } else {
             newChild = oldContext.createChild(newName, newContext);
         }
-        instance.set(newChild);
+        instanceTL.set(newChild);
         T result = null;
         updateFlags();
         log("Starting scope %s", newChild.getQualifiedName());
@@ -119,10 +120,16 @@
                 result = call(callable);
             }
         } catch (RuntimeException e) {
-            throw interceptException(e);
+            if (e == lastExceptionThrownTL.get()) {
+                throw e;
+            } else {
+                RuntimeException newException = interceptException(e);
+                lastExceptionThrownTL.set(newException);
+                throw newException;
+            }
         } finally {
             newChild.deactivate();
-            instance.set(oldContext);
+            instanceTL.set(oldContext);
             setConfig(oldConfig);
         }
         return result;
@@ -147,7 +154,19 @@
         context = null;
     }
 
-    private RuntimeException interceptException(RuntimeException e) {
+    private RuntimeException interceptException(final RuntimeException e) {
+        final DebugConfig config = getConfig();
+        if (config != null) {
+            return scope("InterceptException", null, new Callable<RuntimeException>(){
+                @Override
+                public RuntimeException call() throws Exception {
+                    try {
+                        return config.interceptException(e);
+                    } catch (Throwable t) {
+                        return e;
+                    }
+                }}, false, new Object[]{e});
+        }
         return e;
     }
 
@@ -230,7 +249,7 @@
     }
 
     public void setConfig(DebugConfig newConfig) {
-        config.set(newConfig);
+        configTL.set(newConfig);
         updateFlags();
     }
 
--- a/graal/com.oracle.max.graal.hotspot/src/com/oracle/max/graal/hotspot/HotSpotDebugConfig.java	Fri Jan 20 15:43:15 2012 +0100
+++ b/graal/com.oracle.max.graal.hotspot/src/com/oracle/max/graal/hotspot/HotSpotDebugConfig.java	Fri Jan 20 16:19:41 2012 +0100
@@ -24,6 +24,7 @@
 
 import com.oracle.max.cri.ri.*;
 import com.oracle.max.graal.debug.*;
+import com.oracle.max.graal.graph.*;
 
 
 public class HotSpotDebugConfig implements DebugConfig {
@@ -98,4 +99,19 @@
             sb.append(filter);
         }
     }
+
+    @Override
+    public RuntimeException interceptException(RuntimeException e) {
+        Debug.setConfig(Debug.fixedConfix(true, true, false, false));
+        Debug.log(String.format("Exception occured in scope: %s", Debug.currentScope()));
+        for (Object o : Debug.context()) {
+            Debug.log("Context obj %s", o);
+            if (o instanceof Graph) {
+                Graph graph = (Graph) o;
+                Debug.log("Found graph in context: ", graph);
+                Debug.dump(o, "Exception graph");
+            }
+        }
+        return e;
+    }
 }