Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ThreadReference/ThreadGroup/threadgroup001.java
41161 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.jdwp.ThreadReference.ThreadGroup;2425import java.io.*;26import java.util.*;2728import nsk.share.*;29import nsk.share.jpda.*;30import nsk.share.jdwp.*;3132public class threadgroup001 {33static final int JCK_STATUS_BASE = 95;34static final int PASSED = 0;35static final int FAILED = 2;36static final String PACKAGE_NAME = "nsk.jdwp.ThreadReference.ThreadGroup";37static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "threadgroup001";38static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";3940static final String JDWP_COMMAND_NAME = "ThreadReference.ThreadGroup";41static final int JDWP_COMMAND_ID = JDWP.Command.ThreadReference.ThreadGroup;4243public static void main (String argv[]) {44System.exit(run(argv,System.out) + JCK_STATUS_BASE);45}4647public static int run(String argv[], PrintStream out) {48return new threadgroup001().runIt(argv, out);49}5051public int runIt(String argv[], PrintStream out) {5253boolean success = true;5455try {56ArgumentHandler argumentHandler = new ArgumentHandler(argv);57Log log = new Log(out, argumentHandler);5859try {6061Binder binder = new Binder(argumentHandler, log);62log.display("Start debugee VM");63Debugee debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);64Transport transport = debugee.getTransport();65IOPipe pipe = debugee.createIOPipe();6667log.display("Waiting for VM_INIT event");68debugee.waitForVMInit();6970log.display("Querying for IDSizes");71debugee.queryForIDSizes();7273log.display("Resume debugee VM");74debugee.resume();7576log.display("Waiting for command: " + "ready");77String cmd = pipe.readln();78log.display("Received command: " + cmd);798081try {8283// get top level thread groups8485log.display("Getting IDs for top level thread groups");8687long[] threadGroupIDs = null;88int groups = 0;8990{91log.display("Create command packet " + "TopLevelThreadGroups");92CommandPacket command = new CommandPacket(JDWP.Command.VirtualMachine.TopLevelThreadGroups);9394log.display("Waiting for reply to command");95ReplyPacket reply = debugee.receiveReplyFor(command);96log.display("Valid reply packet received");9798log.display("Parsing reply packet:");99reply.resetPosition();100101groups = reply.getInt();102log.display(" groups: " + groups);103104threadGroupIDs = new long[groups];105106for (int i = 0; i < groups; i++) {107long threadGroupID = reply.getObjectID();108log.display(" #" + i + " threadGroupID: " + threadGroupID);109threadGroupIDs[i] = threadGroupID;110}111112if (groups < 0) {113log.complain("Negative number of thread groups returned while getting top level thread groups IDs: " + groups);114success = false;115}116117if (groups == 0) {118log.complain("No thread groups IDs returned while getting top level thread groups IDs: " + groups);119success = false;120}121122}123124// get threads from all thread groups found125126boolean found = false;127long checkedThreadGroupID = 0;128129long[] threadIDs = null;130int threads = 0;131132for (int i = 0; i < groups; i++) {133checkedThreadGroupID = threadGroupIDs[i];134log.display("Getting IDs of the thread from thread group: #" + i);135136log.display("Create command packet " +137"ThreadGroupReference.Children");138CommandPacket command = new CommandPacket(JDWP.Command.ThreadGroupReference.Children);139command.addObjectID(checkedThreadGroupID);140command.setLength();141142log.display("Waiting for reply to command");143ReplyPacket reply = debugee.receiveReplyFor(command);144log.display("Valid reply packet received");145146log.display("Parsing reply packet:");147reply.resetPosition();148149threads = reply.getInt();150log.display(" threads: " + threads);151152threadIDs = new long[threads];153154for (int j = 0; j < threads; j++) {155long threadID = reply.getObjectID();156log.display(" #" + j + " threadID: " + threadID);157threadIDs[j] = threadID;158}159160if (threads < 0) {161log.complain("Negative number of threads in the thread group: " + threads);162success = false;163}164165if (threads > 0) {166log.display("Testing threads from thread group: #" + i);167found = true;168}169}170171if (!found) {172log.complain("No threads found in all top level thread groups");173success = false;174}175176// begin test of JDWP command177178for (int i = 0; i < threads; i++) {179180long threadID = threadIDs[i];181182log.display("");183log.display("Getting ThreadGroup ID for " + i + " thread ID: "184+ threadID);185log.display("Create command " + JDWP_COMMAND_NAME186+ " with thread ID: " + threadID);187CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);188command.addObjectID(threadID);189command.setLength();190191log.display("Sending command packet:\n" + command);192transport.write(command);193194log.display("Waiting for reply packet");195ReplyPacket reply = new ReplyPacket();196transport.read(reply);197log.display("Reply packet received:\n" + reply);198199log.display("Checking reply packet header");200reply.checkHeader(command.getPacketID());201202log.display("Parsing reply packet:");203reply.resetPosition();204205long threadGroupID = reply.getObjectID();206log.display(" group: " + threadGroupID);207208if (threadGroupID != checkedThreadGroupID) {209log.complain("Unexpected threadGroupID returned for a thread: " + threadGroupID210+ " (expected: " + checkedThreadGroupID + ")");211success = false;212}213214if (! reply.isParsed()) {215log.complain("Extra trailing bytes found in reply packet at: " + reply.currentPosition());216success = false;217} else {218log.display("Reply packet parsed successfully");219}220221}222223// end test of JDWP command224225} catch (Exception e) {226log.complain("Caught exception while testing JDWP command: " + e);227success = false;228} finally {229log.display("");230log.display("Sending command: " + "quit");231pipe.println("quit");232233log.display("Waiting for debugee exits");234int code = debugee.waitFor();235if (code == JCK_STATUS_BASE + PASSED) {236log.display("Debugee PASSED with exit code: " + code);237} else {238log.complain("Debugee FAILED with exit code: " + code);239success = false;240}241}242243} catch (Exception e) {244log.complain("Caught unexpected exception while communicating with debugee: " + e);245e.printStackTrace(out);246success = false;247}248249if (!success) {250log.complain("TEST FAILED");251return FAILED;252}253254} catch (Exception e) {255out.println("Caught unexpected exception while starting the test: " + e);256e.printStackTrace(out);257out.println("TEST FAILED");258return FAILED;259}260261out.println("TEST PASSED");262return PASSED;263264}265266}267268269