Home | History | Annotate | Download | only in VMDebug
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *
     15  *  See the License for the specific language governing permissions and
     16  *  limitations under the License.
     17  */
     18 
     19 package org.apache.harmony.jpda.tests.jdwp.VMDebug;
     20 
     21 import java.lang.reflect.Method;
     22 import java.io.*;
     23 import java.util.*;
     24 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
     25 import org.apache.harmony.jpda.tests.share.SyncDebuggee;
     26 
     27 /**
     28  * This class provides a test for VMDebug functions.
     29  */
     30 public class VMDebugDebuggee extends SyncDebuggee {
     31     public static final long SLEEP_TIME = 20;
     32 
     33     public static final class DebugResult implements Serializable {
     34         public boolean error_occured  = false;
     35         public boolean is_debugger_connected = false;
     36         public boolean is_debugging_enabled = false;
     37         public long last_debugger_activity = -1;
     38 
     39         public DebugResult(boolean is_error, boolean is_debugger_connected, boolean is_debugger_enabled, long last_debugger_activity) {
     40             this.error_occured = is_error;
     41             this.is_debugger_connected = is_debugger_connected;
     42             this.is_debugging_enabled = is_debugger_enabled;
     43             this.last_debugger_activity = last_debugger_activity;
     44         }
     45 
     46         @Override
     47         public String toString() {
     48             return "DebugResult { error: " + error_occured
     49                                + ", connected: " + is_debugger_connected
     50                                + ", enabled: " + is_debugging_enabled
     51                                + ", last_activity: " + last_debugger_activity + " }";
     52         }
     53     }
     54 
     55     private void sendResult(boolean is_error, boolean is_debugger_connected, boolean is_debugger_enabled, long last_debugger_activity) {
     56         ByteArrayOutputStream bs  = new ByteArrayOutputStream();
     57         try (ObjectOutputStream st = new ObjectOutputStream(bs)) {
     58             st.writeObject(new DebugResult(is_error, is_debugger_connected, is_debugger_enabled, last_debugger_activity));
     59             st.flush();
     60         } catch (Exception e) {
     61             logWriter.println("Failed to serialize debug result!");
     62         }
     63         synchronizer.sendMessage(Base64.getEncoder().encodeToString(bs.toByteArray()));
     64     }
     65 
     66     public static DebugResult readResult(String args) throws IllegalArgumentException {
     67         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(args)))) {
     68             return (DebugResult) ois.readObject();
     69         } catch (Exception e) {
     70             return null;
     71         }
     72     }
     73 
     74     @Override
     75     public void run() {
     76         // Tell the test we have started.
     77         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     78         // The debugger will have done something before sending this so the last activity is reset.
     79         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
     80 
     81         boolean error_occured  = false;
     82         boolean is_debugger_connected = false;
     83         boolean is_debugging_enabled = false;
     84         long last_debugger_activity = -1;
     85         try {
     86             // Wait 20 milliseconds so that last_debugger_activity will be non-zero and definitely
     87             // more than 10.
     88             Thread.sleep(SLEEP_TIME);
     89 
     90             Class<?> vmdebug = Class.forName("dalvik.system.VMDebug");
     91             Method isDebuggerConnectedMethod = vmdebug.getDeclaredMethod("isDebuggerConnected");
     92             Method isDebuggingEnabledMethod = vmdebug.getDeclaredMethod("isDebuggingEnabled");
     93             Method lastDebuggerActivityMethod = vmdebug.getDeclaredMethod("lastDebuggerActivity");
     94 
     95             is_debugger_connected = (boolean)isDebuggerConnectedMethod.invoke(null);
     96             is_debugging_enabled = (boolean)isDebuggingEnabledMethod.invoke(null);
     97             last_debugger_activity = (long)lastDebuggerActivityMethod.invoke(null);
     98         } catch (NoSuchMethodException e) {
     99             error_occured = true;
    100             logWriter.println("Unable to find one of the VMDebug methods!" + e);
    101         } catch (ClassNotFoundException e) {
    102             error_occured = true;
    103             logWriter.println("Could not find VMDebug");
    104         } catch (Exception e) {
    105             error_occured = true;
    106             logWriter.println("Other exception occured " + e);
    107         }
    108         sendResult(
    109             error_occured, is_debugger_connected, is_debugging_enabled, last_debugger_activity);
    110         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    111     }
    112 
    113     public static void main(String [] args) {
    114         runDebuggee(VMDebugDebuggee.class);
    115     }
    116 }
    117 
    118