1 /*
2  * Copyright (c) 2002, 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 
25 /*
26  * @test
27  *
28  * @summary converted from VM Testbase nsk/jdb/unwatch/unwatch001.
29  * VM Testbase keywords: [jpda, jdb]
30  * VM Testbase readme:
31  * DECSRIPTION
32  * A positive test case for the 'unwatch access <class id>.<field name>' command.
33  * There are two test cases:
34  *  - unwatch access for the fields defined in class,
35  *  - unwatch access for the fields defined in inner class.
36  * At first phase of testing the access watch is set for all checked fields with
37  * "watch access" command. Then debugged application invokes the methods (updateFields)
38  * in which all checked fields participate in assigned expressions. Thus the jdb
39  * should report the access event for the fields.
40  * At seconds phase of testing all access watch monitors are deleted with
41  * the tested command. Then updateFields methods are invoked in debuggee again.
42  * The test passes jdb reports only once an access event for every checked fields.
43  * Correct report message in jdb stdout should contain full name of the field
44  * and "access encountered" words.
45  * The test consists of two program:
46  *   watch001.java - launches jdb and debuggee, writes commands to jdb, reads the jdb output,
47  *   watch001a.java - the debugged application.
48  * COMMENTS
49  *
50  * @library /vmTestbase
51  *          /test/lib
52  * @build nsk.jdb.unwatch.unwatch001.unwatch001a
53  * @run main/othervm
54  *      nsk.jdb.unwatch.unwatch001.unwatch001
55  *      -arch=${os.family}-${os.simpleArch}
56  *      -waittime=5
57  *      -debugee.vmkind=java
58  *      -transport.address=dynamic
59  *      -jdb=${test.jdk}/bin/jdb
60  *      -java.options="${test.vm.opts} ${test.java.opts}"
61  *      -workdir=.
62  *      -debugee.vmkeys="${test.vm.opts} ${test.java.opts}"
63  */
64 
65 package nsk.jdb.unwatch.unwatch001;
66 
67 import nsk.share.*;
68 import nsk.share.jdb.*;
69 
70 import java.io.*;
71 import java.util.*;
72 
73 public class unwatch001 extends JdbTest {
74 
main(String argv[])75     public static void main (String argv[]) {
76         System.exit(run(argv, System.out) + JCK_STATUS_BASE);
77     }
78 
run(String argv[], PrintStream out)79     public static int run(String argv[], PrintStream out) {
80         debuggeeClass =  DEBUGGEE_CLASS;
81         firstBreak = FIRST_BREAK;
82         lastBreak = LAST_BREAK;
83         return new unwatch001().runTest(argv, out);
84     }
85 
86     static final String PACKAGE_NAME       = "nsk.jdb.unwatch.unwatch001";
87     static final String TEST_CLASS         = PACKAGE_NAME + ".unwatch001";
88     static final String DEBUGGEE_CLASS     = TEST_CLASS + "a";
89     static final String DEBUGGEE_CLASS2    = DEBUGGEE_CLASS + "$CheckedFields";
90     static final String FIRST_BREAK        = DEBUGGEE_CLASS + ".main";
91     static final String LAST_BREAK         = DEBUGGEE_CLASS + ".breakHere";
92 
93     static String[] checkedFields  = { "fS1", "FS0" };
94     static String[] checkedFields2 = { "fP1", "fU1", "fR1"};
95 
runCases()96     protected void runCases() {
97         String[] reply;
98         Paragrep grep;
99         int count;
100         Vector v;
101         String found;
102 
103         jdb.setBreakpointInMethod(LAST_BREAK);
104 
105         reply = jdb.receiveReplyFor(JdbCommand.fields + DEBUGGEE_CLASS);
106 
107         reply = jdb.receiveReplyFor(JdbCommand.fields + DEBUGGEE_CLASS2);
108 
109         watchFields (DEBUGGEE_CLASS, checkedFields);
110         watchFields (DEBUGGEE_CLASS2, checkedFields2);
111 
112         for (int i = 0; i < (checkedFields.length + checkedFields2.length + 2); i++) {
113             reply = jdb.receiveReplyFor(JdbCommand.cont);
114         }
115 
116         unwatchFields (DEBUGGEE_CLASS, checkedFields);
117         unwatchFields (DEBUGGEE_CLASS2, checkedFields2);
118 
119         // excessive number of cont commands in case if unwatch command does not work.
120         jdb.contToExit(checkedFields.length + checkedFields2.length + 1);
121 
122         reply = jdb.getTotalReply();
123         if (!checkFields (DEBUGGEE_CLASS, reply, checkedFields)) {
124             success = false;
125         }
126         if (!checkFields (DEBUGGEE_CLASS2, reply, checkedFields2)) {
127             success = false;
128         }
129     }
130 
watchFields(String className, String[] checkedFields)131     private void watchFields (String className, String[] checkedFields) {
132         String[] reply;
133 
134         for (int i = 0; i < checkedFields.length; i++) {
135             reply = jdb.receiveReplyFor(JdbCommand.watch + " access " + className + "." + checkedFields[i]);
136         }
137 
138     }
139 
unwatchFields(String className, String[] checkedFields)140     private void unwatchFields (String className, String[] checkedFields) {
141         String[] reply;
142 
143         for (int i = 0; i < checkedFields.length; i++) {
144             reply = jdb.receiveReplyFor(JdbCommand.unwatch + " access " + className + "." + checkedFields[i]);
145         }
146 
147     }
148 
checkFields(String className, String[] reply, String[] checkedFields)149     private boolean checkFields (String className, String[] reply, String[] checkedFields) {
150         Paragrep grep;
151         String found;
152         boolean result = true;
153         int count;
154         Vector v = new Vector();
155 
156         grep = new Paragrep(reply);
157         v.add("access encountered");
158         for (int i = 0; i < checkedFields.length; i++) {
159             v.removeAllElements();
160             v.add("access encountered");
161             v.add(className + "." + checkedFields[i]);
162 
163             count = grep.find(v);
164             if (count != 1) {
165                 log.complain("jdb reported wrong number of access to the field " + className + "." + checkedFields[i]);
166                 log.complain("Should be 1, reported: " + count);
167                 result = false;
168             }
169         }
170         return result;
171     }
172 }
173