1 /*
2  * Copyright (c) 2007, 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 /*
25  * @test
26  *
27  * @summary converted from VM Testbase nsk/jvmti/scenarios/hotswap/HS204/hs204t003.
28  * VM Testbase keywords: [quick, jpda, jvmti, onload_only_caps, noras, redefine, feature_hotswap]
29  * VM Testbase readme:
30  * Description ::
31  *     The test pop initializer frame, redefines class and pops thread's current frame (constructor).
32  *     The test enables ClassPrepare event. A TempThread (dummy thread) is created and
33  *     it starts creating MyThread object (It can even be normal class doesn;t needed to be a thread).
34  *     On receiving ClassPrepare event for MyThread, It would enable field access event
35  *     (JVMTI_EVENT_FIELD_ACCESS), adds access watch for static field 'intState' of MyThread.
36  *     Inside the MyThread constructor, watch field is accessed(incremented). Which causes callback.
37  *     Callback for 'FiledAccess', this redefines ./MyThread.java  class to ./newclass00/MyThread.java
38  *     and suspends the currently executing thread ('TempThread').
39  *     The main thread pops executing frame in suspended thread ('TempThread') and resumes its execution.
40  *     'TempThread' would reenter to constructor again.
41  *     Test is said to be pass, if MyThread is from ./newclass00/MyThread.
42  * The notes from removed README
43  * 1. Enable event ClassPrepare.
44  * 2. Upon ClassPrepare occurrence, enable FieldAccessWatch for a field to be initialized by the initializer.
45  * 3. Upon accessing the field by the initializer, redefine the class and pop a currently executed
46  *  frame of the initializer within incoming FieldAccess callback.
47  *
48  * @library /vmTestbase
49  *          /test/lib
50  * @run driver jdk.test.lib.FileInstaller . .
51  * @build nsk.jvmti.scenarios.hotswap.HS204.hs204t003.hs204t003
52  *
53  * @comment compile newclassXX to bin/newclassXX
54  * @run driver nsk.share.ExtraClassesBuilder
55  *      newclass00
56  *
57  * @build ExecDriver
58  * @run main/othervm/native PropertyResolvingWrapper ExecDriver --java
59  *      "-agentlib:hs204t003=pathToNewByteCode=./bin -waittime=5 package=nsk samples=100 mode=compiled"
60  *      nsk.jvmti.scenarios.hotswap.HS204.hs204t003.hs204t003
61  */
62 
63 package nsk.jvmti.scenarios.hotswap.HS204.hs204t003;
64 
65 import nsk.share.jvmti.RedefineAgent;
66 public class hs204t003 extends RedefineAgent {
popFrame(Thread thread)67     public native boolean popFrame(Thread thread) ;
68 
hs204t003(String[]arg)69     public hs204t003(String[]arg) {
70         super(arg);
71     }
72 
main(String[] arg)73     public static void main(String[] arg) {
74         arg = nsk.share.jvmti.JVMTITest.commonInit(arg);
75 
76         hs204t003 hsCase = new hs204t003(arg);
77         System.exit(hsCase.runAgent());
78         }
79 
agentMethod()80         public boolean  agentMethod() {
81         boolean passed = false;
82         MyThread mthread =null;
83                 try {
84             TempThread temp = new TempThread();
85             temp.start();
86             Thread.sleep(10000);
87             popFrame(temp);
88             temp.join();
89             mthread = temp.mthread;
90             mthread.start();
91                         mthread.join();
92                 } catch(Exception exp) {
93                         exp.printStackTrace();
94                 } finally {
95             if ( mthread != null ) {
96                 System.out.println(" agentMethod() ::  field state = "+mthread.getIntState());
97                 if ( redefineAttempted() &&
98                         isRedefined() &&
99                         mthread.getIntState() == 10   &&
100                         agentStatus() ) {
101                     passed = true;
102                 }
103             }
104         }
105         System.out.println(" agentMethod() =  "+passed+".");
106         return passed;
107         }
108 }
109 class TempThread extends Thread {
110     public MyThread mthread;
TempThread()111     public TempThread() {
112         super("TempThread.");
113     }
114 
run()115     public void run() {
116         System.out.println(" run for TempThread ");
117         mthread = new MyThread();
118     }
119 
120 }
121