Commit 2cfae3b5 authored by Lubomir Bulej's avatar Lubomir Bulej

dislserver: cleaned up white space to simplify upcoming merges.

parent 6dbfc1f8
......@@ -12,144 +12,144 @@ import ch.usi.dag.disl.exception.DiSLInMethodException;
public abstract class DiSLServer {
public static final String PROP_DEBUG = "debug";
private static final boolean debug = Boolean.getBoolean(PROP_DEBUG);
private static final String PROP_PORT = "dislserver.port";
private static final int DEFAULT_PORT = 11217;
private static final int port = Integer.getInteger(PROP_PORT, DEFAULT_PORT);
private static final String PROP_TIME_STAT = "dislserver.timestat";
private static final boolean timeStat = Boolean.getBoolean(PROP_TIME_STAT);
private static final String PROP_CONT = "dislserver.continuous";
private static final boolean continuous = Boolean.getBoolean(PROP_CONT);
private static final String PROP_BYPASS = "dislserver.disablebypass";
private static final boolean bypass = ! Boolean.getBoolean(PROP_BYPASS);
private static final AtomicInteger aliveWorkers = new AtomicInteger();
private static final AtomicLong instrumentationTime = new AtomicLong();
private static DiSL disl;
private static ServerSocket listenSocket;
//
public static void main (final String [] args) {
try {
// use dynamic bypass
disl = new DiSL (bypass);
if (debug) {
System.out.println ("DiSL: starting instrumentation server...");
}
listenSocket = new ServerSocket (port);
if (debug) {
System.out.printf (
"DiSL: listening at %s:%d\n",
listenSocket.getInetAddress ().getHostAddress (),
listenSocket.getLocalPort ()
);
}
while (true) {
final Socket newClient = listenSocket.accept ();
if (debug) {
System.out.printf (
"DiSL: accepting connection from %s:%d\n",
newClient.getInetAddress ().getHostAddress (),
newClient.getPort ()
);
}
NetMessageReader sc = new NetMessageReader (newClient);
aliveWorkers.incrementAndGet ();
new Worker (sc, disl).start ();
}
} catch (final IOException ioe) {
reportError (new DiSLServerException (ioe));
} catch (final Throwable throwable) {
reportError (throwable);
}
}
private static void reportInnerError (final Throwable throwable) {
Throwable cause = throwable.getCause ();
while (cause != null && cause.getMessage () != null) {
System.err.println (" Inner error: " + cause.getMessage ());
cause = cause.getCause ();
}
}
public static void reportError (Throwable throwable) {
if (throwable instanceof DiSLException) {
System.err.print ("DiSL: error");
// report during which method it happened
if (throwable instanceof DiSLInMethodException) {
System.err.printf (
" (while instrumenting method \"%s\")", throwable.getMessage ()
);
// set inner error
throwable = throwable.getCause ();
}
reportOptionalMessage (throwable);
reportInnerError (throwable);
if (debug) {
throwable.printStackTrace ();
}
} else if (throwable instanceof DiSLServerException) {
System.err.print ("DiSL: server error");
reportOptionalMessage (throwable);
reportInnerError (throwable);
if (debug) {
throwable.printStackTrace ();
}
} else {
// some other exception
System.err.print ("DiSL: fatal error: ");
throwable.printStackTrace ();
}
}
private static void reportOptionalMessage (final Throwable throwable) {
final String message = throwable.getMessage ();
System.err.println ((message != null) ? ": "+ message : "");
}
public static void workerDone (final long instrTime) {
instrumentationTime.addAndGet (instrTime);
if (aliveWorkers.decrementAndGet () == 0) {
if (timeStat) {
System.out.printf (
"DiSL: instrumentation took %d milliseconds\n",
instrumentationTime.get () / 1000000
);
}
// no workers - shutdown
if (!continuous) {
disl.terminate ();
if (debug) {
System.out.println ("DiSL: shutting down instrumentation server...");
}
System.exit(0);
}
}
}
public static final String PROP_DEBUG = "debug";
private static final boolean debug = Boolean.getBoolean(PROP_DEBUG);
private static final String PROP_PORT = "dislserver.port";
private static final int DEFAULT_PORT = 11217;
private static final int port = Integer.getInteger(PROP_PORT, DEFAULT_PORT);
private static final String PROP_TIME_STAT = "dislserver.timestat";
private static final boolean timeStat = Boolean.getBoolean(PROP_TIME_STAT);
private static final String PROP_CONT = "dislserver.continuous";
private static final boolean continuous = Boolean.getBoolean(PROP_CONT);
private static final String PROP_BYPASS = "dislserver.disablebypass";
private static final boolean bypass = ! Boolean.getBoolean(PROP_BYPASS);
private static final AtomicInteger aliveWorkers = new AtomicInteger();
private static final AtomicLong instrumentationTime = new AtomicLong();
private static DiSL disl;
private static ServerSocket listenSocket;
//
public static void main (final String [] args) {
try {
// use dynamic bypass
disl = new DiSL (bypass);
if (debug) {
System.out.println ("DiSL: starting instrumentation server...");
}
listenSocket = new ServerSocket (port);
if (debug) {
System.out.printf (
"DiSL: listening at %s:%d\n",
listenSocket.getInetAddress ().getHostAddress (),
listenSocket.getLocalPort ()
);
}
while (true) {
final Socket newClient = listenSocket.accept ();
if (debug) {
System.out.printf (
"DiSL: accepting connection from %s:%d\n",
newClient.getInetAddress ().getHostAddress (),
newClient.getPort ()
);
}
NetMessageReader sc = new NetMessageReader (newClient);
aliveWorkers.incrementAndGet ();
new Worker (sc, disl).start ();
}
} catch (final IOException ioe) {
reportError (new DiSLServerException (ioe));
} catch (final Throwable throwable) {
reportError (throwable);
}
}
private static void reportInnerError (final Throwable throwable) {
Throwable cause = throwable.getCause ();
while (cause != null && cause.getMessage () != null) {
System.err.println (" Inner error: " + cause.getMessage ());
cause = cause.getCause ();
}
}
public static void reportError (Throwable throwable) {
if (throwable instanceof DiSLException) {
System.err.print ("DiSL: error");
// report during which method it happened
if (throwable instanceof DiSLInMethodException) {
System.err.printf (
" (while instrumenting method \"%s\")", throwable.getMessage ()
);
// set inner error
throwable = throwable.getCause ();
}
reportOptionalMessage (throwable);
reportInnerError (throwable);
if (debug) {
throwable.printStackTrace ();
}
} else if (throwable instanceof DiSLServerException) {
System.err.print ("DiSL: server error");
reportOptionalMessage (throwable);
reportInnerError (throwable);
if (debug) {
throwable.printStackTrace ();
}
} else {
// some other exception
System.err.print ("DiSL: fatal error: ");
throwable.printStackTrace ();
}
}
private static void reportOptionalMessage (final Throwable throwable) {
final String message = throwable.getMessage ();
System.err.println ((message != null) ? ": "+ message : "");
}
public static void workerDone (final long instrTime) {
instrumentationTime.addAndGet (instrTime);
if (aliveWorkers.decrementAndGet () == 0) {
if (timeStat) {
System.out.printf (
"DiSL: instrumentation took %d milliseconds\n",
instrumentationTime.get () / 1000000
);
}
// no workers - shutdown
if (!continuous) {
disl.terminate ();
if (debug) {
System.out.println ("DiSL: shutting down instrumentation server...");
}
System.exit(0);
}
}
}
}
......@@ -2,21 +2,21 @@ package ch.usi.dag.dislserver;
public class DiSLServerException extends Exception {
private static final long serialVersionUID = 5272000884539359236L;
private static final long serialVersionUID = 5272000884539359236L;
public DiSLServerException() {
super();
}
public DiSLServerException() {
super();
}
public DiSLServerException(String message, Throwable cause) {
super(message, cause);
}
public DiSLServerException(String message, Throwable cause) {
super(message, cause);
}
public DiSLServerException(String message) {
super(message);
}
public DiSLServerException(String message) {
super(message);
}
public DiSLServerException(Throwable cause) {
super(cause);
}
public DiSLServerException(Throwable cause) {
super(cause);
}
}
package ch.usi.dag.dislserver;
public class NetMessage {
private byte[] control;
private byte[] control;
private byte[] classCode;
public NetMessage(byte[] control, byte[] classCode) {
......@@ -10,11 +10,11 @@ public class NetMessage {
this.classCode = classCode;
}
public byte[] getControl() {
return control;
}
public byte[] getControl() {
return control;
}
public byte[] getClassCode() {
return classCode;
}
public byte[] getClassCode() {
return classCode;
}
}
......@@ -9,59 +9,59 @@ import java.net.Socket;
public class NetMessageReader {
private final Socket socket;
private final DataInputStream is;
private final DataOutputStream os;
public NetMessageReader(Socket socket) throws IOException {
this.socket = socket;
is = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
os = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
}
public NetMessage readMessage() throws IOException {
// protocol:
// java int - control string length (ctl)
// java int - class code length (ccl)
// bytes[ctl] - control string (contains class name)
// bytes[ccl] - class code
int controlLength = is.readInt();
int classCodeLength = is.readInt();
// allocate buffer for class reading
byte[] control = new byte[controlLength];
byte[] classCode = new byte[classCodeLength];
// read class
is.readFully(control);
is.readFully(classCode);
return new NetMessage(control, classCode);
}
public void close() throws IOException {
is.close();
socket.close();
}
public void sendMessage(NetMessage nm) throws IOException {
// protocol:
// java int - control string (ctl)
// java int - class code length (ccl)
// bytes[ctl] - control string
// bytes[ccl] - class code
os.writeInt(nm.getControl().length);
os.writeInt(nm.getClassCode().length);
os.write(nm.getControl());
os.write(nm.getClassCode());
os.flush();
}
private final Socket socket;
private final DataInputStream is;
private final DataOutputStream os;
public NetMessageReader(Socket socket) throws IOException {
this.socket = socket;
is = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
os = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
}
public NetMessage readMessage() throws IOException {
// protocol:
// java int - control string length (ctl)
// java int - class code length (ccl)
// bytes[ctl] - control string (contains class name)
// bytes[ccl] - class code
int controlLength = is.readInt();
int classCodeLength = is.readInt();
// allocate buffer for class reading
byte[] control = new byte[controlLength];
byte[] classCode = new byte[classCodeLength];
// read class
is.readFully(control);
is.readFully(classCode);
return new NetMessage(control, classCode);
}
public void close() throws IOException {
is.close();
socket.close();
}
public void sendMessage(NetMessage nm) throws IOException {
// protocol:
// java int - control string (ctl)
// java int - class code length (ccl)
// bytes[ctl] - control string
// bytes[ccl] - class code
os.writeInt(nm.getControl().length);
os.writeInt(nm.getClassCode().length);
os.write(nm.getControl());
os.write(nm.getClassCode());
os.flush();
}
}
......@@ -15,167 +15,167 @@ import ch.usi.dag.disl.util.Constants;
public class Worker extends Thread {
private static final boolean debug = Boolean
.getBoolean(DiSLServer.PROP_DEBUG);
private static final String PROP_UNINSTR = "dislserver.uninstrumented";
private static final String uninstrPath =
System.getProperty(PROP_UNINSTR, null);
private static final String PROP_INSTR = "dislserver.instrumented";
private static final String instrPath =
System.getProperty(PROP_INSTR, null);
// used for replays
private static final byte[] emptyByteArray = new byte[0];
private final NetMessageReader sc;
private final DiSL disl;
private final AtomicLong instrumentationTime = new AtomicLong();
Worker(NetMessageReader sc, DiSL disl) {
this.sc = sc;
this.disl = disl;
}
public void run() {
try {
instrumentationLoop();
sc.close();
}
catch (Throwable e) {
DiSLServer.reportError(e);
}
finally {
DiSLServer.workerDone(instrumentationTime.get());
}
}
private void instrumentationLoop() throws Exception {
try {
while (true) {
NetMessage nm = sc.readMessage();
// communication closed by the client
if (nm.getControl().length == 0 && nm.getClassCode().length == 0) {
return;
}
byte[] instrClass;
try {
long startTime = System.nanoTime();
instrClass = instrument(new String(nm.getControl()),
nm.getClassCode());
instrumentationTime.addAndGet(System.nanoTime() - startTime);
}
catch (Exception e) {
// instrumentation error
// send the client a description of the server-side error
String errToReport = e.getMessage();
// during debug send the whole message
if(debug) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
errToReport = sw.toString();
}
// error protocol:
// control contains the description of the server-side error
// class code is an array of size zero
String errMsg = "Instrumentation error for class "
+ new String(nm.getControl()) + ": " + errToReport;
sc.sendMessage(new NetMessage(errMsg.getBytes(),
emptyByteArray));
throw e;
}
NetMessage replyData = null;
if(instrClass != null) {
// class was modified - send modified data
replyData = new NetMessage(emptyByteArray, instrClass);
}
else {
// zero length means no modification
replyData = new NetMessage(emptyByteArray, emptyByteArray);
}
sc.sendMessage(replyData);
}
}
catch (IOException e) {
throw new DiSLServerException(e);
}
}
private byte[] instrument(String className, byte[] origCode)
throws DiSLServerException, DiSLException {
// backup for empty class name
if(className == null || className.isEmpty()) {
className = UUID.randomUUID().toString();
}
// dump uninstrumented
if (uninstrPath != null) {
dump(className, origCode, uninstrPath);
}
// instrument
byte[] instrCode = disl.instrument(origCode);
// dump instrumented
if (instrPath != null && instrCode != null) {
dump(className, instrCode, instrPath);
}
return instrCode;
}
private void dump(String className, byte[] codeAsBytes, String path)
throws DiSLServerException {
try {
// extract the class name and package name
int i = className.lastIndexOf(Constants.PACKAGE_INTERN_DELIM);
String onlyClassName = className.substring(i + 1);
String packageName = className.substring(0, i + 1);
// construct path to the class
String pathWithPkg = path + File.separator + packageName;
// create directories
new File(pathWithPkg).mkdirs();
// dump the class code
FileOutputStream fo = new FileOutputStream(pathWithPkg
+ onlyClassName + Constants.CLASS_EXT);
fo.write(codeAsBytes);
fo.close();
}
catch (FileNotFoundException e) {
throw new DiSLServerException(e);
}
catch (IOException e) {
throw new DiSLServerException(e);
}
}
private static final boolean debug = Boolean
.getBoolean(DiSLServer.PROP_DEBUG);
private static final String PROP_UNINSTR = "dislserver.uninstrumented";
private static final String uninstrPath =
System.getProperty(PROP_UNINSTR, null);
private static final String PROP_INSTR = "dislserver.instrumented";
private static final String instrPath =
System.getProperty(PROP_INSTR, null);
// used for replays
private static final byte[] emptyByteArray = new byte[0];
private final NetMessageReader sc;
private final DiSL disl;
private final AtomicLong instrumentationTime = new AtomicLong();
Worker(NetMessageReader sc, DiSL disl) {
this.sc = sc;
this.disl = disl;
}
public void run() {
try {
instrumentationLoop();
sc.close();
}
catch (Throwable e) {
DiSLServer.reportError(e);
}
finally {
DiSLServer.workerDone(instrumentationTime.get());
}
}
private void instrumentationLoop() throws Exception {
try {
while (true) {
NetMessage nm = sc.readMessage();
// communication closed by the client
if (nm.getControl().length == 0 && nm.getClassCode().length == 0) {
return;
}
byte[] instrClass;
try {
long startTime = System.nanoTime();
instrClass = instrument(new String(nm.getControl()),
nm.getClassCode());
instrumentationTime.addAndGet(System.nanoTime() - startTime);
}
catch (Exception e) {
// instrumentation error
// send the client a description of the server-side error
String errToReport = e.getMessage();
// during debug send the whole message
if(debug) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
errToReport = sw.toString();
}