src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/StreamMonitor.java
author jjg
Mon, 20 May 2019 10:57:57 -0700
changeset 54950 46ae54c3026d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8223663: Update links for tool guides Reviewed-by: alanb, erikj, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
     2
 * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.utilities;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
/** Reads all of the data from the given InputStream, and allows the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
    caller to wait for a given string to come in or watch for many
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
    possible strings. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
public class StreamMonitor implements Runnable {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  private BufferedReader input;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  private boolean printStreamContents;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  private String  waitString;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  private boolean waitStringSeen;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  private List    triggers = new LinkedList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  private List    triggersSeen = new LinkedList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  private String  prefixString;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  private boolean printContents;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  private StringBuffer captureBuffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  class Trigger {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    private String[] triggerStrings;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    private int      triggerVal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    Trigger(String str, int val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
      triggerStrings = new String[] { str };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
      triggerVal     = val;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    // Hack because we don't have a regexp library yet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    // This requires all strings to be matched.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    Trigger(String[] strs, int val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
      triggerStrings = strs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
      triggerVal     = val;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    boolean matches(String str) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
      for (int i = 0; i < triggerStrings.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
        if (str.indexOf(triggerStrings[i]) == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
          return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    boolean equals(String[] strs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
      if (strs.length != triggerStrings.length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
      for (int i = 0; i < strs.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
        if (!strs[i].equals(triggerStrings[i])) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
          return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  /** Equivalent to StreamMonitor(istr, null, false) */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  public StreamMonitor(InputStream istr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    this(istr, null, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  public StreamMonitor(InputStream istr, String prefixString, boolean printContents) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    input = new BufferedReader(new InputStreamReader(istr));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    this.prefixString = prefixString;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    this.printContents = printContents;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    Thread thr = new Thread(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    thr.setDaemon(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    thr.start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  /** Adds a "trigger", which the stream watches for and, if seen,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
      reports the trigger value of via the getTriggers() method.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
      Returns true if the addition was successful, false if the string
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
      was already present as a trigger. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  public boolean addTrigger(String str, int value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    return addTrigger(new String[] { str }, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  /** Adds a "trigger", which the stream watches for and, if seen,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      reports the trigger value of via the getTriggers() method.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
      Returns true if the addition was successful, false if the string
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      was already present as a trigger. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  public boolean addTrigger(String[] strs, int value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    for (Iterator iter = triggers.iterator(); iter.hasNext(); ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
      Trigger trigger = (Trigger) iter.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
      if (trigger.equals(strs)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    Trigger trigger = new Trigger(strs, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    return triggers.add(trigger);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  /** Removes a previously added trigger. Returns true if it was
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
      present, false if not. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  public boolean removeTrigger(String str) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    return removeTrigger(new String[] { str });
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  /** Removes a previously added trigger. Returns true if it was
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      present, false if not. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  public boolean removeTrigger(String[] strs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    for (ListIterator iter = triggers.listIterator(); iter.hasNext(); ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
      Trigger trigger = (Trigger) iter.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
      if (trigger.equals(strs)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
        iter.remove();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
        return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  /** Returns an List of java.lang.Integer objects indicating the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
      values of the triggers seen since the last call to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
      getTriggersSeen. If there were no triggers seen, returns an
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
      empty list; does not return null. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  public synchronized List getTriggersSeen() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    List tmpList = triggersSeen;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    triggersSeen = new LinkedList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    return tmpList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  /** Waits for the specified string to come in for the given period
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
      of time (measured in milliseconds). */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  public synchronized boolean waitFor(String str, long millis) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    waitString = str;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    waitStringSeen = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
      wait(millis);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    catch (InterruptedException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
    waitString = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    return waitStringSeen;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  public synchronized void startCapture() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    captureBuffer = new StringBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  public synchronized String stopCapture() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    String ret = captureBuffer.toString();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    captureBuffer = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    return ret;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  public void run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    byte[] buf = new byte[10240];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    boolean shouldContinue = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
      do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
        String str = input.readLine();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
        if (str == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
          shouldContinue = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
          if (printContents) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
            System.err.println(prefixString + ": " + str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
          synchronized (this) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
            if (captureBuffer != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
              captureBuffer.append(str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
              captureBuffer.append("\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
            // Check wait string
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
            if ((waitString != null) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
                (str.indexOf(waitString) != -1)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
              waitStringSeen = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
              notifyAll();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
            // Check all triggers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
            for (Iterator iter = triggers.iterator(); iter.hasNext(); ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
              Trigger trigger = (Trigger) iter.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
              if (trigger.matches(str)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
                triggersSeen.add(new Integer(trigger.triggerVal));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
              }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
      } while (shouldContinue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
    System.err.print("StreamMonitor ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    if (prefixString != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
      System.err.print("\"" + prefixString + "\" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    System.err.println("exiting");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
}