1 /*
   2  * Copyright (c) 2011, 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 package vm.mlvm.share.jpda;
  25 
  26 import vm.mlvm.share.Env;
  27 import vm.mlvm.share.MlvmTest;
  28 import vm.share.options.Option;
  29 
  30 public abstract class Debuggee extends MlvmTest {
  31 
  32     @Option(name="debuggee.iterations", default_value="1", description="Iterations to run on debuggee")
  33     public long _iterations = 1;
  34 
  35     @Option(name="debuggee.hangAt", default_value="", description="Hang up in specified point")
  36     public String _hangAt = "";
  37 
  38     static {
  39         setName("Debuggee");
  40     }
  41 
  42     private boolean _isWarmingUp;
  43 
  44     public final boolean isWarmingUp() {
  45         return _isWarmingUp;
  46     }
  47 
  48     public final long getWarmupsCount() {
  49         return _iterations;
  50     }
  51 
  52     /**
  53      * Used in static methods for getting access to Debuggee instance
  54      *
  55      * @return The current debuggee instance (there should be only one)
  56      */
  57     public static Debuggee getDebuggeeInstance() {
  58         return (Debuggee) MlvmTest.getInstance();
  59     }
  60 
  61     @Override
  62     public boolean run() throws Throwable {
  63         startUp();
  64         boolean result = false;
  65         try {
  66 
  67             _isWarmingUp = true;
  68             Env.traceNormal("Warming up (" + _iterations + " iterations)");
  69             for (long w = _iterations - 1; w > 0; w--)
  70                 warmUp();
  71             _isWarmingUp = false;
  72 
  73             Env.traceNormal("Starting main test");
  74             result = runDebuggee();
  75 
  76         } finally {
  77             tearDown();
  78         }
  79 
  80         return result;
  81     }
  82 
  83     protected void startUp() throws Throwable {
  84     }
  85 
  86     protected void warmUp() throws Throwable {
  87     }
  88 
  89     protected abstract boolean runDebuggee() throws Throwable;
  90 
  91     protected void tearDown() throws Throwable {
  92     }
  93 
  94     public final void hangUpIfNeeded(String at) throws InterruptedException {
  95         if (!_isWarmingUp && at.equals(_hangAt)) {
  96             Env.traceNormal("Hanging at " + at);
  97             hangupImpl();
  98         } else {
  99             if ( isWarmingUp() )
 100                 Env.traceDebug("Passing " + at);
 101             else
 102                 Env.traceNormal("Passing " + at);
 103         }
 104     }
 105 
 106     protected void hangupImpl() throws InterruptedException {
 107         for (;;)
 108             Thread.sleep(60000);
 109     }
 110 }