1 /* 2 * Copyright (c) 2015, 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 /* 25 * @test 26 * @bug 8087315 27 * @summary Get old method's stack trace elements after GC 28 * @library /testlibrary 29 * @modules java.compiler 30 * java.instrument 31 * jdk.jartool/sun.tools.jar 32 * @build RedefineClassHelper 33 * @run main RedefineClassHelper 34 * @run main/othervm -javaagent:redefineagent.jar RedefineRunningMethodsWithBacktrace 35 */ 36 public class RedefineRunningMethodsWithBacktrace { 37 38 public static String newB = 39 "class RedefineRunningMethodsWithBacktrace$B {" + 40 " static int count1 = 0;" + 41 " static int count2 = 0;" + 42 " public static volatile boolean stop = false;" + 43 " static void localSleep() { " + 44 " try{ " + 45 " Thread.currentThread().sleep(10);" + 46 " } catch(InterruptedException ie) { " + 47 " } " + 48 " } " + 49 " public static void infinite() { " + 50 " System.out.println(\"infinite called\");" + 51 " }" + 52 " public static void throwable() { " + 53 " throw new RuntimeException(\"throwable called\");" + 54 " }" + 55 "}"; 56 57 public static String evenNewerB = 58 "class RedefineRunningMethodsWithBacktrace$B {" + 59 " static int count1 = 0;" + 60 " static int count2 = 0;" + 61 " public static volatile boolean stop = false;" + 62 " static void localSleep() { " + 63 " try{ " + 64 " Thread.currentThread().sleep(1);" + 65 " } catch(InterruptedException ie) { " + 66 " } " + 67 " } " + 68 " public static void infinite() { }" + 69 " public static void throwable() { " + 70 " throw new RuntimeException(\"throwable called\");" + 71 " }" + 72 "}"; 73 74 static class B { 75 static int count1 = 0; 76 static int count2 = 0; 77 public static volatile boolean stop = false; 78 static void localSleep() { 79 try{ 80 Thread.currentThread().sleep(10);//sleep for 10 ms 81 } catch(InterruptedException ie) { 82 } 83 } 84 85 public static void infinite() { 86 while (!stop) { count1++; localSleep(); } 87 } 88 public static void throwable() { 89 // add some stuff to the original constant pool 90 String s1 = new String ("string1"); 91 String s2 = new String ("string2"); 92 String s3 = new String ("string3"); 93 String s4 = new String ("string4"); 94 String s5 = new String ("string5"); 95 String s6 = new String ("string6"); 96 String s7 = new String ("string7"); 97 String s8 = new String ("string8"); 98 String s9 = new String ("string9"); 99 String s10 = new String ("string10"); 100 String s11 = new String ("string11"); 101 String s12 = new String ("string12"); 102 String s13 = new String ("string13"); 103 String s14 = new String ("string14"); 104 String s15 = new String ("string15"); 105 String s16 = new String ("string16"); 106 String s17 = new String ("string17"); 107 String s18 = new String ("string18"); 108 String s19 = new String ("string19"); 109 throw new RuntimeException("throwable called"); 110 } 111 } 112 113 private static void touchRedefinedMethodInBacktrace(Throwable throwable) { 114 System.out.println("touchRedefinedMethodInBacktrace: "); 115 throwable.printStackTrace(); 116 // Make sure that we can convert the backtrace, which is referring to 117 // the redefined method, to a StrackTraceElement[] without crashing. 118 StackTraceElement[] stackTrace = throwable.getStackTrace(); 119 for (int i = 0; i < stackTrace.length; i++) { 120 StackTraceElement frame = stackTrace[i]; 121 if (frame.getClassName() == null) { 122 System.out.println("\nTest failed: trace[" + i + "].getClassName() returned null"); 123 } 124 if (frame.getMethodName() == null) { 125 System.out.println("\nTest failed: trace[" + i + "].getMethodName() returned null"); 126 } 127 } 128 } 129 130 private static Throwable getThrowableInB() { 131 Throwable t = null; 132 try { 133 B.throwable(); 134 } catch (Exception e) { 135 t = e; 136 // Don't print here because Throwable will cache the constructed stacktrace 137 // e.printStackTrace(); 138 } 139 return t; 140 } 141 142 143 public static void main(String[] args) throws Exception { 144 145 new Thread() { 146 public void run() { 147 B.infinite(); 148 } 149 }.start(); 150 151 Throwable t1 = getThrowableInB(); 152 153 RedefineClassHelper.redefineClass(B.class, newB); 154 155 System.gc(); 156 157 Throwable t2 = getThrowableInB(); 158 159 B.infinite(); 160 161 for (int i = 0; i < 20 ; i++) { 162 String s = new String("some garbage"); 163 System.gc(); 164 } 165 166 RedefineClassHelper.redefineClass(B.class, evenNewerB); 167 System.gc(); 168 169 Throwable t3 = getThrowableInB(); 170 171 for (int i = 0; i < 20 ; i++) { 172 B.infinite(); 173 String s = new String("some garbage"); 174 System.gc(); 175 } 176 177 touchRedefinedMethodInBacktrace(t1); 178 touchRedefinedMethodInBacktrace(t2); 179 touchRedefinedMethodInBacktrace(t3); 180 181 // purge should clean everything up. 182 B.stop = true; 183 184 for (int i = 0; i < 20 ; i++) { 185 B.infinite(); 186 String s = new String("some garbage"); 187 System.gc(); 188 } 189 } 190 }