1 /*
2  * Copyright (c) 2001, 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 nsk.jdi.StepRequest.addInstanceFilter;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 import com.sun.jdi.*;
31 import com.sun.jdi.event.*;
32 import com.sun.jdi.request.*;
33 
34 import java.util.*;
35 import java.io.*;
36 
37 /**
38  * The test for the implementation of an object of the type
39  * StepRequest.
40  *
41  * The test checks that results of the method
42  * <code>com.sun.jdi.StepRequest.addInstanceFilter()</code>
43  * complies with its spec.
44  *
45  * The test checks up on the following assertion:
46  *    Restricts the events generated by this request to those
47  *    in which the currently executing instance is the object specified.
48  * The cases to test include invocations of the method addInstanceFilter() on
49  * two StepRequest objects with different array element (instances) arguments.
50  * (1) For StepRequest2, a testing thread will not execute an ObjectRefernce
51  * instance used as the filter;
52  * hence, the test expects this Step event will not be received.
53  * (2) For StepRequest1, a testing thread will do execute an ObjectRefernce
54  * instance used as the filter;
55  * hence, the test expects this Step event will be received.
56  *
57  * The test works as follows.
58  * - The debugger resumes the debuggee and waits for the BreakpointEvent.
59  * - The debuggee creates a special array with three Object instances,
60  *   and two threads, thread1 and thread2, and invokes
61  *   the methodForCommunication to be suspended and
62  *   to inform the debugger with the event.
63  * - Upon getting the BreakpointEvent, the debugger
64  *   - sets up a StepRequest within the method
65  *     within the method in the class TestClass
66  *     whose array element instances #0 and #1
67  *     will be calling by the thread1 and the thread2 accordingly,
68  *   - invokes addInstanceFilter() on array element #0 for the thread1
69  *     and #2 for the thread2,
70  *     thus restricting the Watchpoint event only to thread1,
71  *   - resumes debuggee's main thread, and
72  *   - waits for the event.
73  * - Debuggee's main thread starts both threads.
74  * - Upon getting the event, the debugger performs the checks required.
75  */
76 
77 public class instancefilter001 extends TestDebuggerType1 {
78 
main(String argv[])79     public static void main (String argv[]) {
80         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
81     }
82 
run(String argv[], PrintStream out)83     public static int run (String argv[], PrintStream out) {
84         debuggeeName = "nsk.jdi.StepRequest.addInstanceFilter.instancefilter001a";
85         return new instancefilter001().runThis(argv, out);
86     }
87 
88     private String testedClassName =
89       "nsk.jdi.StepRequest.addInstanceFilter.TestClass";
90 
91 
testRun()92     protected void testRun() {
93 
94         if ( !vm.canUseInstanceFilters() ) {
95             display("......vm.canUseInstanceFilters == false :: test cancelled");
96             vm.exit(Consts.JCK_STATUS_BASE);
97             return;
98         }
99 
100         EventRequest eventRequest1 = null;
101         EventRequest eventRequest2 = null;
102 
103         ThreadReference thread1 = null;
104         ThreadReference thread2 = null;
105 
106         String thread1Name = "thread1";
107         String thread2Name = "thread2";
108 
109         String property1 = "StepRequest1";
110         String property2 = "StepRequest2";
111 
112         String         arrayName = "objTC";
113 
114         ObjectReference instance1 = null;
115         ObjectReference instance2 = null;
116 
117         for (int i = 0; ; i++) {
118 
119             if (!shouldRunAfterBreakpoint()) {
120                 vm.resume();
121                 break;
122             }
123 
124             display(":::::: case: # " + i);
125 
126             switch (i) {
127 
128             case 0:
129                 thread1 = (ThreadReference) debuggeeClass.getValue(
130                         debuggeeClass.fieldByName(thread1Name));
131                 eventRequest1 = setting2StepRequest (thread1,
132                         EventRequest.SUSPEND_ALL, property1);
133                 instance1 = (ObjectReference)
134                 ((ArrayReference) debuggeeClass.getValue(
135                         debuggeeClass.fieldByName(arrayName)) ).getValue(0);
136                 ((StepRequest) eventRequest1).addInstanceFilter(instance1);
137 
138                 thread2 = (ThreadReference) debuggeeClass.getValue(
139                         debuggeeClass.fieldByName(thread2Name));
140                 eventRequest2 = setting2StepRequest (thread2,
141                         EventRequest.SUSPEND_ALL, property2);
142                 instance2 = (ObjectReference)
143                 ((ArrayReference) debuggeeClass.getValue(
144                         debuggeeClass.fieldByName(arrayName)) ).getValue(2);
145                 ((StepRequest) eventRequest2).addInstanceFilter(instance2);
146 
147                 display("......waiting for StepEvent in expected thread");
148                 Event newEvent = eventHandler.waitForRequestedEvent(new EventRequest[]{eventRequest1}, waitTime, true);
149 
150                 if ( !(newEvent instanceof StepEvent)) {
151                     setFailedStatus("ERROR: new event is not StepEvent");
152                     throw new Failure("** unexpected event **");
153                 } else {
154                     Location location = ((StepEvent) newEvent).location();
155                     StepRequest stepR = (StepRequest) newEvent.request();
156                     String property = (String) stepR.getProperty("number");
157                     display("       got new StepEvent with property 'number' == " + property);
158                     display("       thread name == " + stepR.thread().name());
159                     display("       size == " + stepR.size() + "; depth == " + stepR.depth());
160                     display("       lineNumber == " + location.lineNumber());
161 
162                     if ( !property.equals(property1) ) {
163                         setFailedStatus("ERROR: property is not : " + property1);
164                     }
165                 }
166 
167                 vm.resume();
168                 break;
169 
170             default:
171                 throw new Failure("** default case 2 **");
172             }
173         }
174         return;
175     }
176 
setting2StepRequest( ThreadReference thread, int suspendPolicy, String property )177     private StepRequest setting2StepRequest ( ThreadReference thread,
178                                               int             suspendPolicy,
179                                               String          property        )
180             throws Failure {
181         try {
182             display("......setting up StepRequest:");
183             display("       thread: " + thread + "; property: " + property);
184 
185             StepRequest
186             str = eventRManager.createStepRequest(thread, StepRequest.STEP_LINE, StepRequest.STEP_INTO);
187             str.putProperty("number", property);
188             str.setSuspendPolicy(suspendPolicy);
189 
190             display("      StepRequest has been set up");
191             return str;
192         } catch ( Exception e ) {
193             throw new Failure("** FAILURE to set up StepRequest **");
194         }
195     }
196 }
197