annotate agent/src/share/classes/sun/jvm/hotspot/debugger/InputLexer.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
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 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
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.debugger;
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
a61af66fc99e Initial load
duke
parents:
diff changeset
29 /** InputLexer is the lexer through which the current set of debuggers
a61af66fc99e Initial load
duke
parents:
diff changeset
30 see the debug server. It provides the ability to read all of the
a61af66fc99e Initial load
duke
parents:
diff changeset
31 types the debuggers are interested in. All read operations are
a61af66fc99e Initial load
duke
parents:
diff changeset
32 blocking. */
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 public class InputLexer {
a61af66fc99e Initial load
duke
parents:
diff changeset
35 public InputLexer(BufferedInputStream in) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
36 this.in = in;
a61af66fc99e Initial load
duke
parents:
diff changeset
37 pushedBack = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
38 }
a61af66fc99e Initial load
duke
parents:
diff changeset
39
a61af66fc99e Initial load
duke
parents:
diff changeset
40 public void close() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
41 in.close();
a61af66fc99e Initial load
duke
parents:
diff changeset
42 }
a61af66fc99e Initial load
duke
parents:
diff changeset
43
a61af66fc99e Initial load
duke
parents:
diff changeset
44 /** Parses a boolean (really either a 0 or 1 integer in US-ASCII
a61af66fc99e Initial load
duke
parents:
diff changeset
45 encoding) on the input stream */
a61af66fc99e Initial load
duke
parents:
diff changeset
46 public boolean parseBoolean() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
47 int val = parseInt();
a61af66fc99e Initial load
duke
parents:
diff changeset
48 return (val != 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
49 }
a61af66fc99e Initial load
duke
parents:
diff changeset
50
a61af66fc99e Initial load
duke
parents:
diff changeset
51 /** Parses an int in US-ASCII encoding on the input stream */
a61af66fc99e Initial load
duke
parents:
diff changeset
52 public int parseInt() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
53 long l = parseLong();
a61af66fc99e Initial load
duke
parents:
diff changeset
54 long mask = 0xFFFFFFFF00000000L;
a61af66fc99e Initial load
duke
parents:
diff changeset
55 if ((l & mask) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
56 throw new IOException("Overflow error reading int from debug server (read " + l + ")");
a61af66fc99e Initial load
duke
parents:
diff changeset
57 }
a61af66fc99e Initial load
duke
parents:
diff changeset
58 return (int) l;
a61af66fc99e Initial load
duke
parents:
diff changeset
59 }
a61af66fc99e Initial load
duke
parents:
diff changeset
60
a61af66fc99e Initial load
duke
parents:
diff changeset
61 /** Parses a long in US-ASCII encoding on the input stream */
a61af66fc99e Initial load
duke
parents:
diff changeset
62 public long parseLong() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
63 skipWhitespace();
a61af66fc99e Initial load
duke
parents:
diff changeset
64 byte b = readByte();
a61af66fc99e Initial load
duke
parents:
diff changeset
65 if (!Character.isDigit((char) b)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
66 error();
a61af66fc99e Initial load
duke
parents:
diff changeset
67 }
a61af66fc99e Initial load
duke
parents:
diff changeset
68 long l = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
69 while (Character.isDigit((char) b)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
70 l *= 10;
a61af66fc99e Initial load
duke
parents:
diff changeset
71 l += (b - '0');
a61af66fc99e Initial load
duke
parents:
diff changeset
72 b = readByte();
a61af66fc99e Initial load
duke
parents:
diff changeset
73 }
a61af66fc99e Initial load
duke
parents:
diff changeset
74 pushBack(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
75 return l;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 }
a61af66fc99e Initial load
duke
parents:
diff changeset
77
a61af66fc99e Initial load
duke
parents:
diff changeset
78 /** Parses an address in the form 0x12345678 in US-ASCII encoding on
a61af66fc99e Initial load
duke
parents:
diff changeset
79 the input stream */
a61af66fc99e Initial load
duke
parents:
diff changeset
80 public long parseAddress() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
81 skipWhitespace();
a61af66fc99e Initial load
duke
parents:
diff changeset
82 byte b;
a61af66fc99e Initial load
duke
parents:
diff changeset
83 if ((b = readByte()) != '0') {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 error();
a61af66fc99e Initial load
duke
parents:
diff changeset
85 }
a61af66fc99e Initial load
duke
parents:
diff changeset
86 b = readByte();
a61af66fc99e Initial load
duke
parents:
diff changeset
87 if (b != 'x') {
a61af66fc99e Initial load
duke
parents:
diff changeset
88 error();
a61af66fc99e Initial load
duke
parents:
diff changeset
89 }
a61af66fc99e Initial load
duke
parents:
diff changeset
90 long val = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
91 while (isHexDigit((char) (b = readByte()))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
92 val *= 16;
a61af66fc99e Initial load
duke
parents:
diff changeset
93 val += Character.digit((char) b, 16);
a61af66fc99e Initial load
duke
parents:
diff changeset
94 }
a61af66fc99e Initial load
duke
parents:
diff changeset
95 pushBack(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
96 return val;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 }
a61af66fc99e Initial load
duke
parents:
diff changeset
98
a61af66fc99e Initial load
duke
parents:
diff changeset
99 public void skipByte() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 readByte();
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 /** Reads binary data; one byte */
a61af66fc99e Initial load
duke
parents:
diff changeset
104 public byte readByte() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 if (pushedBack) {
a61af66fc99e Initial load
duke
parents:
diff changeset
106 pushedBack = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
107 return backBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
108 }
a61af66fc99e Initial load
duke
parents:
diff changeset
109 return readByteInternal();
a61af66fc99e Initial load
duke
parents:
diff changeset
110 }
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 /** Reads a block of binary data in BLOCKING fashion */
a61af66fc99e Initial load
duke
parents:
diff changeset
113 public void readBytes(byte[] buf, int off, int len) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 int startIdx = off;
a61af66fc99e Initial load
duke
parents:
diff changeset
115 int numRead = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
116 if (pushedBack) {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 buf[startIdx] = backBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
118 pushedBack = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
119 ++startIdx;
a61af66fc99e Initial load
duke
parents:
diff changeset
120 ++numRead;
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122 while (numRead < len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
123 numRead += in.read(buf, startIdx + numRead, len - numRead);
a61af66fc99e Initial load
duke
parents:
diff changeset
124 }
a61af66fc99e Initial load
duke
parents:
diff changeset
125 // if (numRead != len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
126 // throw new IOException("Only read " + numRead + " out of " +
a61af66fc99e Initial load
duke
parents:
diff changeset
127 // len + " bytes requested");
a61af66fc99e Initial load
duke
parents:
diff changeset
128 // }
a61af66fc99e Initial load
duke
parents:
diff changeset
129 }
a61af66fc99e Initial load
duke
parents:
diff changeset
130
a61af66fc99e Initial load
duke
parents:
diff changeset
131 /** Reads binary data; one 16-bit character in big-endian format */
a61af66fc99e Initial load
duke
parents:
diff changeset
132 public char readChar() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
133 int hi = ((int) readByte()) & 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
134 int lo = ((int) readByte()) & 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
135 return (char) ((hi << 8) | lo);
a61af66fc99e Initial load
duke
parents:
diff changeset
136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
137
a61af66fc99e Initial load
duke
parents:
diff changeset
138 /** Reads binary data; one 32-bit unsigned int in big-endian format.
a61af66fc99e Initial load
duke
parents:
diff changeset
139 Returned as a long. */
a61af66fc99e Initial load
duke
parents:
diff changeset
140 public long readUnsignedInt() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
141 long b1 = ((long) readByte()) & 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
142 long b2 = ((long) readByte()) & 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
143 long b3 = ((long) readByte()) & 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
144 long b4 = ((long) readByte()) & 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
145
a61af66fc99e Initial load
duke
parents:
diff changeset
146 return ((b1 << 24) | (b2 << 16) | (b3 << 8) | b4);
a61af66fc99e Initial load
duke
parents:
diff changeset
147 }
a61af66fc99e Initial load
duke
parents:
diff changeset
148
a61af66fc99e Initial load
duke
parents:
diff changeset
149 /** Reads binary data; a US-ASCII string of the specified length */
a61af66fc99e Initial load
duke
parents:
diff changeset
150 public String readByteString(int len) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
151 byte[] b = new byte[len];
a61af66fc99e Initial load
duke
parents:
diff changeset
152 for (int i = 0; i < len; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 b[i] = readByte();
a61af66fc99e Initial load
duke
parents:
diff changeset
154 }
a61af66fc99e Initial load
duke
parents:
diff changeset
155 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
156 return new String(b, "US-ASCII");
a61af66fc99e Initial load
duke
parents:
diff changeset
157 }
a61af66fc99e Initial load
duke
parents:
diff changeset
158 catch (UnsupportedEncodingException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 throw new IOException(e.toString());
a61af66fc99e Initial load
duke
parents:
diff changeset
160 }
a61af66fc99e Initial load
duke
parents:
diff changeset
161 }
a61af66fc99e Initial load
duke
parents:
diff changeset
162
a61af66fc99e Initial load
duke
parents:
diff changeset
163 /** Reads binary data; a Unicode string of the specified length */
a61af66fc99e Initial load
duke
parents:
diff changeset
164 public String readCharString(int len) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
165 char[] c = new char[len];
a61af66fc99e Initial load
duke
parents:
diff changeset
166 for (int i = 0; i < len; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
167 c[i] = readChar();
a61af66fc99e Initial load
duke
parents:
diff changeset
168 }
a61af66fc99e Initial load
duke
parents:
diff changeset
169 return new String(c);
a61af66fc99e Initial load
duke
parents:
diff changeset
170 }
a61af66fc99e Initial load
duke
parents:
diff changeset
171
a61af66fc99e Initial load
duke
parents:
diff changeset
172 //----------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
173 // Internals only below this point
a61af66fc99e Initial load
duke
parents:
diff changeset
174 //
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 private void skipWhitespace() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 byte b;
a61af66fc99e Initial load
duke
parents:
diff changeset
178 while (Character.isWhitespace((char) (b = readByte()))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
179 }
a61af66fc99e Initial load
duke
parents:
diff changeset
180 pushBack(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
181 }
a61af66fc99e Initial load
duke
parents:
diff changeset
182
a61af66fc99e Initial load
duke
parents:
diff changeset
183 private boolean isHexDigit(char c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
184 return (('0' <= c && c <= '9') ||
a61af66fc99e Initial load
duke
parents:
diff changeset
185 ('a' <= c && c <= 'f') ||
a61af66fc99e Initial load
duke
parents:
diff changeset
186 ('A' <= c && c <= 'F'));
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 private void pushBack(byte b) {
a61af66fc99e Initial load
duke
parents:
diff changeset
190 if (pushedBack) {
a61af66fc99e Initial load
duke
parents:
diff changeset
191 throw new InternalError("Only one character pushback supported");
a61af66fc99e Initial load
duke
parents:
diff changeset
192 }
a61af66fc99e Initial load
duke
parents:
diff changeset
193 backBuf = b;
a61af66fc99e Initial load
duke
parents:
diff changeset
194 pushedBack = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196
a61af66fc99e Initial load
duke
parents:
diff changeset
197 private byte readByteInternal() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
198 int i = in.read();
a61af66fc99e Initial load
duke
parents:
diff changeset
199 if (i == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
200 throw new IOException("End-of-file reached while reading from server");
a61af66fc99e Initial load
duke
parents:
diff changeset
201 }
a61af66fc99e Initial load
duke
parents:
diff changeset
202 return (byte) i;
a61af66fc99e Initial load
duke
parents:
diff changeset
203 }
a61af66fc99e Initial load
duke
parents:
diff changeset
204
a61af66fc99e Initial load
duke
parents:
diff changeset
205 private void error() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
206 throw new IOException("Error parsing output of debug server");
a61af66fc99e Initial load
duke
parents:
diff changeset
207 }
a61af66fc99e Initial load
duke
parents:
diff changeset
208
a61af66fc99e Initial load
duke
parents:
diff changeset
209 private BufferedInputStream in;
a61af66fc99e Initial load
duke
parents:
diff changeset
210 private boolean pushedBack;
a61af66fc99e Initial load
duke
parents:
diff changeset
211 private byte backBuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
212 }