forked from btraceio/btrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
314 lines (290 loc) · 12 KB
/
Main.java
File metadata and controls
314 lines (290 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) 2008-2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.btrace.client;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import sun.misc.Signal;
import sun.misc.SignalHandler;
import com.sun.btrace.CommandListener;
import com.sun.btrace.comm.Command;
import com.sun.btrace.comm.ExitCommand;
import com.sun.btrace.comm.PrintableCommand;
import com.sun.btrace.util.Messages;
/**
* This is the main class for a simple command line
* BTrace client. It is possible to create a GUI
* client using the Client class.
*
* @author A. Sundararajan
*/
public final class Main {
public static volatile boolean exiting;
private static boolean DEBUG;
private static boolean TRUSTED;
private static boolean DUMP_CLASSES;
private static String OUTPUT_FILE;
private static String DUMP_DIR;
private static String PROBE_DESC_PATH;
public static final boolean TRACK_RETRANSFORM;
public static final int BTRACE_DEFAULT_PORT = 2020;
public static final String BTRACE_DEFAULT_HOST = "localhost";
private static final Console con;
private static final PrintWriter out;
static {
DEBUG = Boolean.getBoolean("com.sun.btrace.debug");
if (isDebug()) debugPrint("btrace debug mode is set");
TRACK_RETRANSFORM = Boolean.getBoolean("com.sun.btrace.trackRetransforms");
if (isDebug() && TRACK_RETRANSFORM) debugPrint("trackRetransforms flag is set");
TRUSTED = Boolean.getBoolean("com.sun.btrace.unsafe");
TRUSTED |= Boolean.getBoolean("com.sun.btrace.trusted");
if (isDebug() && TRUSTED) debugPrint("btrace trusted mode is set");
DUMP_CLASSES = Boolean.getBoolean("com.sun.btrace.dumpClasses");
if (isDebug() && DUMP_CLASSES) debugPrint("dumpClasses flag is set");
DUMP_DIR = System.getProperty("com.sun.btrace.dumpDir", ".");
if (DUMP_CLASSES) {
if (isDebug()) debugPrint("dumpDir is " + DUMP_DIR);
}
PROBE_DESC_PATH = System.getProperty("com.sun.btrace.probeDescPath", ".");
con = System.console();
out = getOutWriter(con);
}
@SuppressWarnings("DefaultCharset")
private static PrintWriter getOutWriter(Console con) {
return (con != null)? con.writer() : new PrintWriter(System.out);
}
public static void main(String[] args) {
int port = BTRACE_DEFAULT_PORT;
String host = BTRACE_DEFAULT_HOST;
String classPath = ".";
String includePath = null;
int count = 0;
boolean hostDefined = false;
boolean portDefined = false;
boolean classpathDefined = false;
boolean includePathDefined = false;
String statsdDef = "";
OUTER:
for (String arg : args) {
switch (arg) {
case "-v":
DEBUG = true;
break OUTER;
case "--version":
System.out.println(Messages.get("btrace.version"));
return;
}
}
if (args.length < 2) {
usage();
}
for (;;) {
if (args[count].charAt(0) == '-') {
if (args.length <= count+1) {
usage();
}
if (args[count].equals("-p") && !portDefined) {
try {
port = Integer.parseInt(args[++count]);
if (isDebug()) debugPrint("accepting port " + port);
} catch (NumberFormatException nfe) {
usage();
}
portDefined = true;
} else if (args[count].equals("-u")) {
TRUSTED = true;
if (isDebug()) debugPrint("btrace trusted mode is set");
} else if (args[count].equals("-o")) {
OUTPUT_FILE = args[++count];
if (isDebug()) debugPrint("outputFile is " + OUTPUT_FILE);
} else if (args[count].equals("-d")) {
DUMP_CLASSES = true;
DUMP_DIR = args[++count];
if (isDebug()) debugPrint("dumpDir is " + DUMP_DIR);
} else if (args[count].equals("-pd")) {
PROBE_DESC_PATH = args[++count];
if (isDebug()) debugPrint("probeDescDir is " + PROBE_DESC_PATH);
} else if ((args[count].equals("-cp") ||
args[count].equals("-classpath"))
&& !classpathDefined) {
classPath = args[++count];
if (isDebug()) debugPrint("accepting classpath " + classPath);
classpathDefined = true;
} else if (args[count].equals("-I") && !includePathDefined) {
includePath = args[++count];
if (isDebug()) debugPrint("accepting include path " + includePath);
includePathDefined = true;
} else if (args[count].equals("-statsd")) {
statsdDef = args[++count];
} else if (args[count].equals("-v")) {
// already processed
} else if (args[count].equals("-host") && !hostDefined) {
host = args[++count];
hostDefined = true;
} else {
usage();
}
count++;
if (count >= args.length) {
break;
}
} else {
break;
}
}
if (! portDefined) {
if (isDebug()) debugPrint("assuming default port " + port);
}
if (! classpathDefined) {
if (isDebug()) debugPrint("assuming default classpath '" + classPath + "'");
}
if (args.length < (count + 1)) {
usage();
}
String pid = args[count];
String fileName = args[count + 1];
String[] btraceArgs = new String[args.length - count - 2];
if (btraceArgs.length > 0) {
System.arraycopy(args, count + 2, btraceArgs, 0, btraceArgs.length);
}
try {
Client client = new Client(port, OUTPUT_FILE, PROBE_DESC_PATH,
DEBUG, TRACK_RETRANSFORM, TRUSTED, DUMP_CLASSES, DUMP_DIR, statsdDef);
if (! new File(fileName).exists()) {
errorExit("File not found: " + fileName, 1);
}
byte[] code = client.compile(fileName, classPath, includePath);
if (code == null) {
errorExit("BTrace compilation failed", 1);
}
if (!hostDefined)
client.attach(pid, null, classPath);
registerExitHook(client);
if (con != null) {
registerSignalHandler(client);
}
if (isDebug()) debugPrint("submitting the BTrace program");
client.submit(host, fileName, code, btraceArgs, createCommandListener(client));
} catch (IOException exp) {
errorExit(exp.getMessage(), 1);
}
}
private static CommandListener createCommandListener(Client client) {
return new CommandListener() {
@Override
public void onCommand(Command cmd) throws IOException {
int type = cmd.getType();
if (cmd instanceof PrintableCommand) {
((PrintableCommand)cmd).print(out);
out.flush();
} else if (type == Command.EXIT) {
exiting = true;
out.flush();
ExitCommand ecmd = (ExitCommand)cmd;
System.exit(ecmd.getExitCode());
}
}
};
}
private static void registerExitHook(final Client client) {
if (isDebug()) debugPrint("registering shutdown hook");
Runtime.getRuntime().addShutdownHook(new Thread(
new Runnable() {
@Override
public void run() {
if (! exiting) {
try {
if (isDebug()) debugPrint("sending exit command");
client.sendExit(0);
} catch (IOException ioexp) {
if (isDebug()) debugPrint(ioexp.toString());
}
}
}
}));
}
private static void registerSignalHandler(final Client client) {
if (isDebug()) debugPrint("registering signal handler for SIGINT");
Signal.handle(new Signal("INT"),
new SignalHandler() {
@Override
public void handle(Signal sig) {
try {
con.printf("Please enter your option:\n");
con.printf("\t1. exit\n\t2. send an event\n\t3. send a named event\n\t4. flush console output\n");
con.flush();
String option = con.readLine();
option = option.trim();
if (option == null) {
return;
}
switch (option) {
case "1":
System.exit(0);
case "2":
if (isDebug()) debugPrint("sending event command");
client.sendEvent();
break;
case "3":
con.printf("Please enter the event name: ");
String name = con.readLine();
if (name != null) {
if (isDebug()) debugPrint("sending event command");
client.sendEvent(name);
}
break;
case "4":
out.flush();
break;
default:
con.printf("invalid option!\n");
break;
}
} catch (IOException ioexp) {
if (isDebug()) debugPrint(ioexp.toString());
}
}
});
}
private static void usage() {
System.err.println(Messages.get("btrace.usage"));
System.exit(1);
}
private static boolean isDebug() {
return DEBUG;
}
private static boolean isUnsafe() {
return TRUSTED;
}
private static void debugPrint(String msg) {
System.out.println("DEBUG: " + msg);
}
private static void errorExit(String msg, int code) {
exiting = true;
System.err.println(msg);
System.exit(code);
}
}