corda/classpath/avian/Traces.java
Joel Dice ff19ab6c13 add avian.trace.port property
Setting this property (e.g. -Davian.trace.port=5555) will cause the VM
to start an extra daemon thread which listens on the specified TCP
port for incoming connections and dumps stack traces for all running
threads to that socket.  You can retrieve that dump using e.g. netcat:

  nc localhost 5555
2013-03-08 14:47:27 -07:00

62 lines
1.6 KiB
Java

package avian;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class Traces {
private static final String Newline = System.getProperty("line.separator");
private static String traceAllThreads() {
StringBuilder buffer = new StringBuilder();
Thread[] threads = new Thread[Thread.activeCount()];
int count = Thread.enumerate(threads);
for (int i = 0; i < count; ++i) {
traceThread(threads[i], buffer);
}
return buffer.toString();
}
private static String traceThread(Thread thread) {
StringBuilder buffer = new StringBuilder();
traceThread(thread, buffer);
return buffer.toString();
}
private static void traceThread(Thread thread, StringBuilder buffer) {
buffer.append(thread).append(Newline);
for (StackTraceElement e: thread.getStackTrace()) {
buffer.append("\tat ").append(e).append(Newline);
}
}
public static void startTraceListener(final String host, final int port) {
Thread t = new Thread(new Runnable() {
public void run() {
try {
ServerSocketChannel server = ServerSocketChannel.open();
server.socket().bind(new InetSocketAddress(host, port));
while (true) {
SocketChannel c = server.accept();
try {
c.write(ByteBuffer.wrap(traceAllThreads().getBytes()));
} finally {
c.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.setDaemon(true);
t.start();
}
}