1 /*
  2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
  3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4 *
  5 * This code is free software; you can redistribute it and/or modify it
  6 * under the terms of the GNU General Public License version 2 only, as
  7 * published by the Free Software Foundation.
  8 *
  9 * This code is distributed in the hope that it will be useful, but WITHOUT
 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12 * version 2 for more details (a copy is included in the LICENSE file that
 13 * accompanied this code).
 14 *
 15 * You should have received a copy of the GNU General Public License version
 16 * 2 along with this work; if not, write to the Free Software Foundation,
 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18 *
 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20 * or visit www.oracle.com if you need additional information or have any
 21 * questions.
 22 */
 23 
 24 package java.lang;
 25 
 26 import java.lang.StackWalker.StackFrame;
 27 import java.lang.StackWalker.Option;
 28 import java.lang.LiveStackFrame.PrimitiveSlot;
 29 import java.lang.reflect.Method;
 30 import java.util.ArrayList;
 31 import java.util.Arrays;
 32 import java.util.EnumSet;
 33 import java.util.Set;
 34 import java.util.List;
 35 import java.util.Objects;
 36 import java.util.stream.Collectors;
 37 import jdk.internal.vm.Continuation;
 38 import jdk.internal.vm.ContinuationScope;
 39 
 40 public final class StackWalkerHelper {
 41     private static final Set<Option> OPTS = EnumSet.of(Option.SHOW_REFLECT_FRAMES); // EnumSet.noneOf(Option.class);
 42 
 43     public static StackWalker getInstance(ContinuationScope scope) {
 44         return StackWalker.getInstance(scope);
 45     }
 46 
 47     public static StackFrame[] getStackFrames(ContinuationScope scope)     { return getStackFrames(StackWalker.getInstance(OPTS, scope)); }
 48     public static StackFrame[] getStackFrames(Continuation cont)           { return getStackFrames(cont.stackWalker(OPTS)); }
 49 
 50     public static StackFrame[] getLiveStackFrames(ContinuationScope scope) { return getStackFrames(LiveStackFrame.getStackWalker(OPTS, scope)); }
 51     public static StackFrame[] getLiveStackFrames(Continuation cont)       { return getStackFrames(LiveStackFrame.getStackWalker(OPTS, cont.getScope(), cont)); }
 52 
 53     public static StackFrame[] getStackFrames(StackWalker walker) {
 54         return walker.walk(fs -> fs.collect(Collectors.toList())).toArray(StackFrame[]::new);
 55     }
 56 
 57     public static StackTraceElement[] toStackTraceElement(StackFrame[] fs) {
 58         StackTraceElement[] out = new StackTraceElement[fs.length];
 59         for (int i = 0; i < fs.length; i++) {
 60             out[i] = fs[i].toStackTraceElement();
 61         }
 62         return out;
 63     }
 64 
 65     public static boolean equals(StackFrame[] a, StackFrame[] b) {
 66         if (a.length != b.length) return false;
 67         for (int i = 0; i < a.length; i++) if (!equals(a[i], b[i])) return false;
 68         return true;
 69     }
 70 
 71     public static boolean equals(StackFrame a, StackFrame b) {
 72         if ( !(Objects.equals(a.getClassName(),     b.getClassName())
 73             && Objects.equals(a.getMethodName(),    b.getMethodName())
 74             // && Objects.equals(a.getByteCodeIndex(), b.getByteCodeIndex()) // TODO !!!!
 75             // && Objects.equals(a.getContinuationScopeName(), b.getContinuationScopeName())
 76             )) {
 77             System.out.println("XXXX\ta: " + a + " a.bci: " + a.getByteCodeIndex() + " a.toSTE: " + a.toStackTraceElement()
 78                                + "\n\tb: " + b + " b.bci: " + b.getByteCodeIndex() + " b.toSTE: " + b.toStackTraceElement());
 79             // System.out.println("XXXX\ta: " + a + " a.scope: " + a.getContinuationScopeName() + " a.bci: " + a.getByteCodeIndex() + " a.toSTE: " + a.toStackTraceElement()
 80             //                    + "\n\tb: " + b + " b.scope: " + b.getContinuationScopeName() + " b.bci: " + b.getByteCodeIndex() + " b.toSTE: " + b.toStackTraceElement());
 81             return false;
 82         }
 83         try {
 84             if ( !(Objects.equals(a.getDeclaringClass(), b.getDeclaringClass())
 85                 && Objects.equals(a.getMethodType(),     b.getMethodType()))) {
 86             System.out.println("YYYY\ta: " + a + " a.declaringClass: " + a.getDeclaringClass() + " a.methodType: " + a.getMethodType()
 87                                + "\n\tb: " + b + " b.declaringClass: " + b.getDeclaringClass() + " b.methodType: " + b.getMethodType());
 88             return false;
 89         }
 90         } catch(UnsupportedOperationException e) {}
 91         // assert Objects.equals(a.toStackTraceElement(), b.toStackTraceElement()) : "a" + a.toStackTraceElement() + " b: " + b.toStackTraceElement();
 92         if (!Objects.equals(a.toStackTraceElement(), b.toStackTraceElement()))
 93             return false;
 94 
 95         if (!(a instanceof LiveStackFrame && b instanceof LiveStackFrame))
 96             return true;
 97 
 98         LiveStackFrame la = (LiveStackFrame)a;
 99         LiveStackFrame lb = (LiveStackFrame)b;
100 
101         // if (!Arrays.equals(la.getMonitors(), lb.getMonitors())) return false;
102         // if (!slotsEqual(la.getLocals(), lb.getLocals())) return false;
103         // if (!slotsEqual(la.getStack(),  lb.getStack()))  return false;
104         
105         return true;
106     }
107 
108     public static String frameToString(StackFrame sf) {
109         if (!(sf instanceof LiveStackFrame))
110             return sf.toString();
111         
112         LiveStackFrame lsf = (LiveStackFrame) sf;
113         var sb = new StringBuilder();
114         sb.append(sf.toString()).append("\n");
115         if (lsf.getMonitors() != null) sb.append("Monitors: ").append(Arrays.toString(lsf.getMonitors())).append("\n");
116         if (lsf.getLocals() != null)   sb.append("Locals: ").append(slotsToString(lsf.getLocals())).append("\n");
117         if (lsf.getStack() != null)    sb.append("Stack:  ").append(slotsToString(lsf.getStack())).append("\n");
118         return sb.toString();
119     }
120 
121     private static boolean slotsEqual(Object[] a, Object[] b) {
122         if (a == b) return true;
123         if (a.length != b.length) return false;
124         for (int i=0; i<a.length; i++) if (!slotEquals(a[i], b[i])) return false;
125         return true;
126     }
127 
128     private static boolean slotEquals(Object a, Object b) {
129         if (!(a instanceof PrimitiveSlot || b instanceof PrimitiveSlot))
130             return Objects.equals(a, b);
131 
132         if (!(a instanceof PrimitiveSlot && b instanceof PrimitiveSlot))
133             return false;
134         PrimitiveSlot pa = (PrimitiveSlot)a;
135         PrimitiveSlot pb = (PrimitiveSlot)b;
136 
137         return pa.size() == pb.size() && switch(pa.size()) {
138             case 4 -> pa.intValue()  == pb.intValue();
139             case 8 -> pa.longValue() == pb.longValue();
140             default -> throw new AssertionError("Slot size is " + pa.size());
141         };
142     }
143 
144     private static String slotsToString(Object[] x) {
145         return "[" + Arrays.stream(x).map(StackWalkerHelper::slotToString).collect(Collectors.joining(", ")) + "]";
146     }
147 
148     private static String slotToString(Object x) {
149         if (!(x instanceof PrimitiveSlot)) {
150             return (x != null && x.getClass().isArray()) ? arrayToString(x) : Objects.toString(x);
151         }
152         PrimitiveSlot p = (PrimitiveSlot)x;
153         return switch(p.size()) {
154             case 4 -> intOrFloatToString(p.intValue());
155             case 8 -> longOrDoubleToString(p.longValue());
156             default -> throw new AssertionError("Slot size is " + p.size());
157         };
158     }
159 
160     private static String intOrFloatToString(int x) { return Integer.toString(x) + "/" + Float.toString(Float.intBitsToFloat(x)); }
161     private static String longOrDoubleToString(long x) { return Long.toString(x) + "/" + Double.toString(Double.longBitsToDouble(x)); }
162 
163     private static String arrayToString(Object array) {
164         assert array != null && array.getClass().isArray();
165         if (array.getClass().componentType().isPrimitive()) {
166             try {
167                 return (String)Arrays.class.getMethod("toString", new Class<?>[]{array.getClass()}).invoke(array);
168             } catch (Exception e) {
169                 throw new AssertionError(e);
170             }
171         } else return Arrays.toString((Object[])array);
172     }
173 }