Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/HiddenClass/events/DebuggerBase.java
41161 views
/*1* Copyright (c) 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*/2223package nsk.jdi.HiddenClass.events;2425import com.sun.jdi.ClassType;26import com.sun.jdi.ClassObjectReference;27import com.sun.jdi.Field;28import com.sun.jdi.Method;29import com.sun.jdi.ReferenceType;30import com.sun.jdi.ThreadReference;31import com.sun.jdi.Value;32import com.sun.jdi.VirtualMachine;3334import com.sun.jdi.event.EventSet;35import com.sun.jdi.request.EventRequest;36import com.sun.jdi.request.EventRequestManager;37import com.sun.jdi.request.BreakpointRequest;38import com.sun.jdi.request.ClassPrepareRequest;39import com.sun.jdi.request.ClassUnloadRequest;40import com.sun.jdi.request.ModificationWatchpointRequest;4142import java.io.PrintStream;43import java.util.ArrayList;44import java.util.List;45import jdk.test.lib.Asserts;4647import nsk.share.Log;48import nsk.share.jdi.ArgumentHandler;49import nsk.share.jdi.Binder;50import nsk.share.jdi.Debugee;51import nsk.share.jpda.IOPipe;5253// This class is the test debugger base class54public class DebuggerBase {55public static final int PASSED = 0;56public static final int FAILED = 2;57public static final int JCK_STATUS_BASE = 95;5859public static final String COMMAND_READY = "ready";60public static final String COMMAND_RUN = "run";61public static final String COMMAND_DONE = "done";62public static final String COMMAND_ERROR = "error";63public static final String COMMAND_QUIT = "quit";6465public final Log log;6667private VirtualMachine vm;68private Debugee debuggee = null;69private IOPipe pipe = null;70private EventRequestManager erManager = null;7172protected DebuggerBase(ArgumentHandler argHandler) {73log = new Log(System.out, argHandler);74}7576VirtualMachine vm() {77return vm;78}7980// Find a ReferenceType by a given type name.81ReferenceType getReferenceType(String typeName) {82List<ReferenceType> list = vm.classesByName(typeName);8384Asserts.assertFalse(list.size() == 0, "FAIL: type not found: " + typeName);85Asserts.assertFalse(list.size() > 1, "FAIL: multiple types found: " + typeName);86log.display(" Found type: " + typeName);87return list.get(0);88}8990// Find a Field by a given ReferenceType and a field name.91Field findField(ReferenceType refType, String fieldName) {92Field field = refType.fieldByName(fieldName);93String fullName = refType.name() + "::" + fieldName;9495Asserts.assertNotNull(field, "FAIL: field not found: " + fullName);96log.display(" Found field: " + fullName);97return field;98}99100// Find a Method by a given ReferenceType and a method name.101Method findMethod(ReferenceType refType, String methodName) {102List<Method> list = refType.methodsByName(methodName);103String fullName = refType.name() + "::" + methodName;104105Asserts.assertFalse(list.size() == 0, "FAIL: method not found: " + fullName);106Asserts.assertFalse(list.size() > 1, "FAIL: multiple methods found: " + fullName);107108log.display(" Found method: " + fullName);109return list.get(0);110}111112// Invoke a given static method in debuggee VM at an eventpoint.113boolean invokeStaticMethod(ThreadReference thread, Method methodToInvoke) {114boolean failedStatus = false;115List<? extends Value> args = new ArrayList<>();116int flags = (ClassObjectReference.INVOKE_NONVIRTUAL |117ClassObjectReference.INVOKE_SINGLE_THREADED);118try {119log.display(" invoking method: " + methodToInvoke);120ClassType type = (ClassType)methodToInvoke.declaringType();121Value val = type.invokeMethod(thread, methodToInvoke, args, flags);122log.display(" method getHCField returned result: " + val);123} catch (Exception ex) {124log.complain("Exception in HC::getHCField method invocation: " + ex);125failedStatus = true;126}127return failedStatus;128}129130protected void launchDebuggee(ArgumentHandler argHandler, String debuggeeName) {131Binder binder = new Binder(argHandler, log);132log.display("\n# Connecting to debuggee");133134debuggee = binder.bindToDebugee(debuggeeName);135debuggee.redirectStderr(log, "debuggee >");136137pipe = debuggee.createIOPipe();138vm = debuggee.VM();139erManager = vm.eventRequestManager();140141log.display("# Resuming debuggee");142debuggee.resume();143}144145protected boolean shutdownDebuggee() {146boolean debuggeeFailed = false;147log.display("\n# Shutting down debuggee");148149// wait for debuggee exits and analize its exit code150log.display("# Waiting for debuggee terminating");151int debuggeeStatus = debuggee.endDebugee();152if (debuggeeStatus == PASSED + JCK_STATUS_BASE) {153log.display("# Debuggee PASSED with exit code: " + debuggeeStatus);154} else {155log.complain("# Debuggee FAILED with exit code: " + debuggeeStatus);156debuggeeFailed = true;157}158return debuggeeFailed;159}160161protected EventRequest enableBreakpointRequest(Method method) {162log.display("\n# Creating BreakpointRequest");163BreakpointRequest request = erManager.createBreakpointRequest(method.location());164Asserts.assertNotNull(request, "FAIL: unable to create BreakpointRequest");165166// enable event request167request.enable();168log.display(" Enabled BreakpointRequest");169return request;170}171172protected EventRequest enableClassPrepareRequest(String classFilter) {173log.display("\n# Creating ClassPrepareRequest");174ClassPrepareRequest request = erManager.createClassPrepareRequest();175Asserts.assertNotNull(request, "FAIL: unable to create ClassPrepareRequest");176177if (classFilter != null) {178log.display(" Adding filter to ClassPrepareRequest: " + classFilter);179request.addClassFilter(classFilter);180}181// enable event request182request.enable();183log.display(" Enabled ClassPrepareRequest");184return request;185}186187protected EventRequest enableClassUnloadRequest(String classFilter) {188log.display("\n# Creating request for ClassUnloadEvent");189ClassUnloadRequest request = erManager.createClassUnloadRequest();190Asserts.assertNotNull(request, "FAIL: unable to create ClassUnloadRequest");191192if (classFilter != null) {193log.display(" Adding filter to ClassUnloadRequest: " + classFilter);194request.addClassFilter(classFilter);195}196// enable event request197request.enable();198log.display(" Enabled ClassUnloadRequest");199return request;200}201202protected EventRequest enableModificationWatchpointRequest(Field field, String classFilter) {203log.display("\n# Creating request for ModificationWatchpointRequest");204205ModificationWatchpointRequest request = erManager.createModificationWatchpointRequest(field);206Asserts.assertNotNull(request, "FAIL: unable to create ModificationWatchpointRequest");207208if (classFilter != null) {209log.display(" Adding filter to ModificationWatchpointRequest: " + classFilter);210request.addClassFilter(classFilter);211}212// enable event request213request.enable();214log.display(" Enabled ModificationWatchpointRequest");215return request;216}217218protected void disableRequest(EventRequest eq, String reqestName) {219// disable event requests to prevent appearance of further events220if (eq != null && eq.isEnabled()) {221log.display(" Disabling " + reqestName);222eq.disable();223}224}225226// sync on the COMMAND_READY227protected void readyCmdSync() {228// wait for READY signal from debugee229log.display("\n# Waiting for command: " + COMMAND_READY);230String command = pipe.readln();231Asserts.assertFalse(command == null || !command.equals(COMMAND_READY),232"FAIL: unexpected debuggee's command: " + command);233log.display("\n# Got command: " + COMMAND_READY);234}235236// sync on the COMMAND_RUN237protected void runCmdSync() {238// activate debugee239log.display("\n# Sending command: " + COMMAND_RUN);240pipe.println(COMMAND_RUN);241}242243// sync on the COMMAND_DONE244protected void doneCmdSync() {245// wait for DONE signal from debugee246log.display("\n# Waiting for command: " + COMMAND_DONE);247String command = pipe.readln();248Asserts.assertFalse(command == null || !command.equals(COMMAND_DONE),249"FAIL: unexpected debuggee's command: " + command);250log.display("\n# Got command: " + COMMAND_DONE);251}252253// sync on the COMMAND_QUIT254protected void quitCmdSync() {255// force debugee to exit256log.display("\n# Sending command: " + COMMAND_QUIT);257pipe.println(COMMAND_QUIT);258}259}260261262