1 /*
   2  * Copyright (c) 2018, 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.util.concurrent.atomic.AtomicInteger;
  27 import java.util.Arrays;
  28 import java.util.Objects;
  29 
  30 public class LiveFrames {
  31     public static void main(String[] args) {
  32         LiveFrames obj = new LiveFrames();
  33         obj.test1();
  34     }
  35 
  36     static final ContinuationScope FOO = new ContinuationScope() {};
  37     
  38     public void test1() {
  39         final AtomicInteger res = new AtomicInteger(0);
  40         Continuation cont = new Continuation(FOO, ()-> {
  41             double r = 0;
  42             for (int k = 1; k < 4; k++) {
  43                 int x = 3;
  44                 String s = "abc";
  45                 r += foo(k);
  46             }
  47             res.set((int)r);
  48         });
  49         
  50         int i = 0;
  51         while (!cont.isDone()) {
  52             cont.run();
  53             System.gc();
  54 
  55             System.out.println("^&^ UNMOUNTED");
  56             testStackWalk(LiveStackFrame.getStackWalker(cont));
  57             System.out.println("^&^ END UNMOUNTED");
  58         }
  59     }
  60     
  61     static double foo(int a) {
  62         long x = 8;
  63         String s = "yyy";
  64         String r = bar(a + 1);
  65         return Integer.parseInt(r)+1;
  66     }
  67     
  68     static String bar(long b) {
  69         double x = 9.99;
  70         String s = "zzz";
  71         String r = baz(b + 1);
  72         return "" + r;
  73     }
  74     
  75     static String baz(long b) {
  76         double x = 9.99;
  77         String s = "zzz";
  78         Continuation.yield(FOO);
  79 
  80         testStackWalk(LiveStackFrame.getStackWalker());
  81 
  82         long r = b+1;
  83         return "" + r;
  84     }
  85 
  86     static void testStackWalk(StackWalker walker) {
  87         // StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
  88         System.out.println("^&^ start");
  89         walker.forEach(f -> {
  90             try {
  91                 System.out.println("^&^ --==--");
  92                 // System.out.println("^&^ " + f);
  93                 LiveStackFrame lf = (LiveStackFrame)f;
  94                 System.out.println("^&^ locals: " + Arrays.toString(lf.getLocals()));
  95                 System.out.println("^&^ stack: " + Arrays.toString(lf.getStack()));
  96             } catch (Throwable t) {
  97                 t.printStackTrace();
  98                 throw t;
  99             }
 100         });
 101         System.out.println("^&^ end");
 102     }
 103     
 104 
 105     static void assertEquals(Object actual, Object expected) {
 106         if (!Objects.equals(actual, expected)) {
 107             throw new AssertionError("Expected: " + expected + ", actual: " + actual);
 108         }
 109     }
 110 }