Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/HeapwalkingDebuggee.java
41161 views
/*1* Copyright (c) 2006, 2020, 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*/22package nsk.share.jdi;2324import java.io.*;25import java.util.*;26import java.lang.reflect.Method;27import nsk.share.Log;28import nsk.share.ObjectInstancesManager;29import nsk.share.TestBug;30import nsk.share.jpda.DebugeeArgumentHandler;31import nsk.share.jpda.IOPipe;3233/*34* Debuggee class used in tests for heapwalking(tests for VirtualMachine.instanceCounts, ReferenceType.instances, ObjectReference.referrers).35* Handle commands related to creation of objects instances with given reference type36* and given referrers number, use for this purposes nsk.share.ObjectInstancesManager.37*/38public class HeapwalkingDebuggee extends AbstractJDIDebuggee {39protected ObjectInstancesManager objectInstancesManager;4041// reference of this type should be included in ObjectReference.referringObjects42public static Set<String> includedIntoReferrersCountTypes = new HashSet<String>();4344// reference of this type should be included in ReferenceType.instances45public static Set<String> includedIntoInstancesCountTypes = new HashSet<String>();4647static {48includedIntoInstancesCountTypes.add(ObjectInstancesManager.STRONG_REFERENCE);49includedIntoInstancesCountTypes.add(ObjectInstancesManager.WEAK_REFERENCE);50includedIntoInstancesCountTypes.add(ObjectInstancesManager.SOFT_REFERENCE);51includedIntoInstancesCountTypes.add(ObjectInstancesManager.PHANTOM_REFERENCE);52includedIntoInstancesCountTypes.add(ObjectInstancesManager.JNI_GLOBAL_REFERENCE);53includedIntoInstancesCountTypes.add(ObjectInstancesManager.JNI_LOCAL_REFERENCE);5455includedIntoReferrersCountTypes.add(ObjectInstancesManager.STRONG_REFERENCE);56includedIntoReferrersCountTypes.add(ObjectInstancesManager.WEAK_REFERENCE);57includedIntoReferrersCountTypes.add(ObjectInstancesManager.SOFT_REFERENCE);58includedIntoReferrersCountTypes.add(ObjectInstancesManager.PHANTOM_REFERENCE);59}6061//create number instance of class with given name, command format: createInstances:class_name:instance_count[:referrer_count:referrer_type]62static public final String COMMAND_CREATE_INSTANCES = "createInstances";6364//'delete'(make unreachable) number instance of class with given name, command format: deleteInstances:class_name:instance_count:referrer_count65static public final String COMMAND_DELETE_INSTANCES = "deleteInstances";6667//delete number referrers68static public final String COMMAND_DELETE_REFERRERS = "deleteReferrers";6970//create instance with all type referrers71static public final String COMMAND_CREATE_ALL_TYPE_REFERENCES = "createAllTypeReferences";7273// check jfr is active process74public static boolean isJFRActive;7576protected void init(String args[]) {77super.init(args);78objectInstancesManager = new ObjectInstancesManager(log);79isJFRActive = isJFRActive();80}8182public void initDebuggee(DebugeeArgumentHandler argHandler, Log log, IOPipe pipe, String args[], boolean callExit) {83super.initDebuggee(argHandler, log, pipe, args, callExit);84objectInstancesManager = new ObjectInstancesManager(log);85}8687public boolean parseCommand(String command) {88if (super.parseCommand(command))89return true;9091try {92StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(command));93tokenizer.whitespaceChars(':', ':');94tokenizer.wordChars('_', '_');95tokenizer.wordChars('$', '$');96tokenizer.wordChars('[', ']');97tokenizer.wordChars('|', '|');9899if (command.startsWith(COMMAND_CREATE_INSTANCES)) {100//createInstances:class_name:instance_count[:referrer_count:referrer_type]101102tokenizer.nextToken();103104if (tokenizer.nextToken() != StreamTokenizer.TT_WORD)105throw new TestBug("Invalid command format: " + command);106107String className = tokenizer.sval;108109if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER)110throw new TestBug("Invalid command format: " + command);111112int instanceCounts = (int) tokenizer.nval;113114int referrerCount = 1;115Set<String> referrerType = new HashSet<String>();116117if (tokenizer.nextToken() == StreamTokenizer.TT_NUMBER) {118referrerCount = (int) tokenizer.nval;119120if (tokenizer.nextToken() == StreamTokenizer.TT_WORD)121referrerType.addAll(Arrays.asList(tokenizer.sval.split("\\|")));122}123if (referrerType.isEmpty()) {124referrerType.add(ObjectInstancesManager.STRONG_REFERENCE);125}126127objectInstancesManager.createReferences(instanceCounts, className, referrerCount, referrerType);128129return true;130} else if (command.startsWith(COMMAND_DELETE_INSTANCES)) {131//deleteInstances:class_name:instance_count:referrer_count132133tokenizer.nextToken();134135if (tokenizer.nextToken() != StreamTokenizer.TT_WORD)136throw new TestBug("Invalid command format: " + command);137138String className = tokenizer.sval;139140if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER)141throw new TestBug("Invalid command format: " + command);142143int instanceCounts = (int) tokenizer.nval;144145objectInstancesManager.deleteAllReferrers(instanceCounts, className);146147return true;148} else if (command.startsWith(COMMAND_DELETE_REFERRERS)) {149tokenizer.nextToken();150151if (tokenizer.nextToken() != StreamTokenizer.TT_WORD)152throw new TestBug("Invalid command format: " + command);153154String className = tokenizer.sval;155156if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER)157throw new TestBug("Invalid command format: " + command);158159int referrersCount = (int) tokenizer.nval;160161Set<String> referrerTypes = new HashSet<String>();162if (tokenizer.nextToken() == StreamTokenizer.TT_WORD) {163referrerTypes.addAll(Arrays.asList(tokenizer.sval.split("\\|")));164}165166objectInstancesManager.deleteReferrers(className, referrersCount, referrerTypes);167168return true;169} else if (command.startsWith(COMMAND_CREATE_ALL_TYPE_REFERENCES)) {170tokenizer.nextToken();171172if (tokenizer.nextToken() != StreamTokenizer.TT_WORD)173throw new TestBug("Invalid command format: " + command);174175String className = tokenizer.sval;176177if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER)178throw new TestBug("Invalid command format: " + command);179180int instanceCounts = (int) tokenizer.nval;181182objectInstancesManager.createAllTypeReferences(className, instanceCounts);183184return true;185}186} catch (IOException e) {187throw new TestBug("Invalid command format: " + command);188}189190return false;191}192193// check if jfr is initialized194public static boolean isJFRActive() {195try {196Class cls = Class.forName("jdk.jfr.FlightRecorder");197Method method = cls.getDeclaredMethod("isInitialized", new Class[0]);198return (Boolean)method.invoke(cls, new Object[0]);199} catch(Exception e) {200return false;201}202}203204// is reference with given type should be included in ObjectReference.referringObjects205static public boolean isIncludedIntoReferrersCount(String referenceType) {206if (!ObjectInstancesManager.allReferenceTypes.contains(referenceType)) {207throw new TestBug("Invalid reference type: " + referenceType);208}209210return includedIntoReferrersCountTypes.contains(referenceType);211}212213// is reference with given type should be included in ReferenceType.instances214static public boolean isIncludedIntoInstancesCount(String referenceType) {215if (!ObjectInstancesManager.allReferenceTypes.contains(referenceType)) {216throw new TestBug("Invalid reference type: " + referenceType);217}218219return includedIntoInstancesCountTypes.contains(referenceType);220}221222public static void main(String args[]) {223HeapwalkingDebuggee debuggee = new HeapwalkingDebuggee();224debuggee.init(args);225debuggee.doTest();226}227}228229230