1 /*
2  * Copyright (c) 2002, 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 /*
26  * @test
27  *
28  * @summary converted from VM Testbase nsk/jdb/wherei/wherei001.
29  * VM Testbase keywords: [jpda, jdb]
30  * VM Testbase readme:
31  * DECSRIPTION
32  * A positive test case for the 'wherei <thread id>' command.
33  * The test checks if jdb correctly reports stack trace for
34  * every checked thread id.
35  * COMMENTS
36  *
37  * @library /vmTestbase
38  *          /test/lib
39  * @run driver jdk.test.lib.FileInstaller . .
40  * @build nsk.jdb.wherei.wherei001.wherei001
41  *        nsk.jdb.wherei.wherei001.wherei001a
42  * @run main/othervm PropertyResolvingWrapper nsk.jdb.wherei.wherei001.wherei001
43  *      -arch=${os.family}-${os.simpleArch}
44  *      -waittime=5
45  *      -debugee.vmkind=java
46  *      -transport.address=dynamic
47  *      -jdb=${test.jdk}/bin/jdb
48  *      -java.options="${test.vm.opts} ${test.java.opts}"
49  *      -workdir=.
50  *      -debugee.vmkeys="${test.vm.opts} ${test.java.opts}"
51  */
52 
53 package nsk.jdb.wherei.wherei001;
54 
55 import nsk.share.*;
56 import nsk.share.jdb.*;
57 
58 import java.io.*;
59 import java.util.*;
60 
61 public class wherei001 extends JdbTest {
62 
main(String argv[])63     public static void main (String argv[]) {
64         System.exit(run(argv, System.out) + JCK_STATUS_BASE);
65     }
66 
run(String argv[], PrintStream out)67     public static int run(String argv[], PrintStream out) {
68         debuggeeClass =  DEBUGGEE_CLASS;
69         firstBreak = FIRST_BREAK;
70         lastBreak = LAST_BREAK;
71         return new wherei001().runTest(argv, out);
72     }
73 
74     static final String PACKAGE_NAME    = "nsk.jdb.wherei.wherei001";
75     static final String TEST_CLASS      = PACKAGE_NAME + ".wherei001";
76     static final String DEBUGGEE_CLASS  = TEST_CLASS + "a";
77     static final String FIRST_BREAK     = DEBUGGEE_CLASS + ".main";
78     static final String LAST_BREAK      = DEBUGGEE_CLASS + ".lastBreak";
79     static final String DEBUGGEE_THREAD = PACKAGE_NAME + ".MyThread";
80 
runCases()81     protected void runCases() {
82         String[] reply;
83         Paragrep grep;
84         int count;
85         Vector v;
86         String found;
87         String[] threads;
88 
89         jdb.setBreakpointInMethod(LAST_BREAK);
90         reply = jdb.receiveReplyFor(JdbCommand.cont);
91 
92         threads = jdb.getThreadIds(DEBUGGEE_THREAD);
93 
94         if (threads.length != 5) {
95             log.complain("jdb should report 5 instance of " + DEBUGGEE_THREAD);
96             log.complain("Found: " + threads.length);
97             success = false;
98         }
99 
100         for (int i = 0; i < threads.length; i++) {
101             if (!checkStack(threads[i])) {
102                 success = false;
103             }
104         }
105 
106         jdb.contToExit(1);
107     }
108 
checkStack(String threadId)109     private boolean checkStack (String threadId) {
110         Paragrep grep;
111         String[] reply;
112         String found;
113         int count;
114         Vector v;
115         boolean result = true;
116         String[] func = { "func5", "func4", "func3", "func2", "func1", "run" };
117 
118         reply = jdb.receiveReplyFor(JdbCommand.wherei + threadId);
119 
120         grep = new Paragrep(reply);
121         for (int i = 0; i < func.length; i++) {
122             count = grep.find(DEBUGGEE_THREAD + "." + func[i]);
123             if (count != 1) {
124                 log.complain("Contents of stack trace is incorrect for thread " + threadId);
125                 log.complain("Searched for: " + DEBUGGEE_THREAD + "." + func[i]);
126                 log.complain("Count : " + count);
127                 result= false;
128             }
129         }
130         return result;
131     }
132 }
133