Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/JDIBase.java
41161 views
/*1* Copyright (c) 2020, 2021, 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.share.jdi;2425import com.sun.jdi.IntegerValue;26import com.sun.jdi.Location;27import com.sun.jdi.Method;28import com.sun.jdi.ReferenceType;29import com.sun.jdi.ThreadReference;30import com.sun.jdi.VirtualMachine;31import com.sun.jdi.event.BreakpointEvent;32import com.sun.jdi.event.Event;33import com.sun.jdi.event.EventIterator;34import com.sun.jdi.event.EventQueue;35import com.sun.jdi.event.EventSet;36import com.sun.jdi.event.ThreadDeathEvent;37import com.sun.jdi.event.ThreadStartEvent;38import com.sun.jdi.request.BreakpointRequest;39import com.sun.jdi.request.EventRequest;40import com.sun.jdi.request.EventRequestManager;41import java.util.List;42import nsk.share.Log;4344public class JDIBase {4546// Exit code constants47public static final int PASSED = 0;48public static final int FAILED = 2;49public static final int PASS_BASE = 95;505152// Log helpers53private final String sHeader1 = "\n=> " + this.getClass().getName().replace(".", "/") + " ";5455private static final String56sHeader2 = "--> debugger: ",57sHeader3 = "##> debugger: ";5859public final void log1(String message) {60logHandler.display(sHeader1 + message);61}6263public final void log2(String message) {64logHandler.display(sHeader2 + message);65}6667public final void log3(String message) {68logHandler.complain(sHeader3 + message);69}7071protected Log logHandler;7273// common variables used by tests74protected Debugee debuggee;75protected ArgumentHandler argsHandler;76protected VirtualMachine vm;77protected ReferenceType debuggeeClass;78protected static int testExitCode = PASSED;79protected long waitTime;8081// used by tests with breakpoint communication82protected EventRequestManager eventRManager;83protected EventQueue eventQueue;84protected EventSet eventSet;85protected EventIterator eventIterator;8687// additional fields initialized during breakpoint communication88protected Location breakpLocation = null;89protected BreakpointEvent bpEvent;9091protected final BreakpointRequest settingBreakpoint(ThreadReference thread,92ReferenceType testedClass,93String methodName,94String bpLine,95String property)96throws JDITestRuntimeException {9798log2("......setting up a breakpoint:");99log2(" thread: " + thread + "; class: " + testedClass +100"; method: " + methodName + "; line: " + bpLine);101102List alllineLocations = null;103Location lineLocation = null;104BreakpointRequest breakpRequest = null;105106try {107Method method = (Method) testedClass.methodsByName(methodName).get(0);108109alllineLocations = method.allLineLocations();110111int n =112((IntegerValue) testedClass.getValue(testedClass.fieldByName(bpLine))).value();113if (n > alllineLocations.size()) {114log3("ERROR: TEST_ERROR_IN_settingBreakpoint(): number is out of bound of method's lines");115} else {116lineLocation = (Location) alllineLocations.get(n);117breakpLocation = lineLocation;118try {119breakpRequest = eventRManager.createBreakpointRequest(lineLocation);120breakpRequest.putProperty("number", property);121breakpRequest.addThreadFilter(thread);122breakpRequest.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);123} catch (Exception e1) {124log3("ERROR: inner Exception within settingBreakpoint() : " + e1);125breakpRequest = null;126}127}128} catch (Exception e2) {129log3("ERROR: ATTENTION: outer Exception within settingBreakpoint() : " + e2);130breakpRequest = null;131}132133if (breakpRequest == null) {134log2(" A BREAKPOINT HAS NOT BEEN SET UP");135throw new JDITestRuntimeException("**FAILURE to set up a breakpoint**");136}137138log2(" a breakpoint has been set up");139return breakpRequest;140}141142protected final void getEventSet() throws JDITestRuntimeException {143try {144eventSet = eventQueue.remove(waitTime);145if (eventSet == null) {146throw new JDITestRuntimeException("** TIMEOUT while waiting for event **");147}148eventIterator = eventSet.eventIterator();149} catch (Exception e) {150throw new JDITestRuntimeException("** EXCEPTION while waiting for event ** : " + e);151}152}153154// Special version of getEventSet for ThreadStartEvent/ThreadDeathEvent.155// When ThreadStartRequest and/or ThreadDeathRequest are enabled,156// we can get the events from system threads unexpected for tests.157// The method skips ThreadStartEvent/ThreadDeathEvent events158// for all threads except the expected one.159protected void getEventSetForThreadStartDeath(String threadName) throws JDITestRuntimeException {160boolean gotDesiredEvent = false;161while (!gotDesiredEvent) {162getEventSet();163Event event = eventIterator.nextEvent();164if (event instanceof ThreadStartEvent evt) {165if (evt.thread().name().equals(threadName)) {166gotDesiredEvent = true;167} else {168log2("Got ThreadStartEvent for wrong thread: " + event);169}170} else if (event instanceof ThreadDeathEvent evt) {171if (evt.thread().name().equals(threadName)) {172gotDesiredEvent = true;173} else {174log2("Got ThreadDeathEvent for wrong thread: " + event);175}176} else {177// not ThreadStartEvent nor ThreadDeathEvent178gotDesiredEvent = true;179}180}181// reset the iterator before return182eventIterator = eventSet.eventIterator();183}184185protected void breakpointForCommunication() throws JDITestRuntimeException {186187log2("breakpointForCommunication");188getEventSet();189190Event event = eventIterator.nextEvent();191if (event instanceof BreakpointEvent) {192bpEvent = (BreakpointEvent) event;193return;194}195196throw new JDITestRuntimeException("** event '" + event + "' IS NOT a breakpoint **");197}198199}200201202