annotate agent/src/share/classes/sun/jvm/hotspot/utilities/StreamMonitor.java @ 17524:89152779163c

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