Created
July 9, 2014 03:57
-
-
Save moriyoshi/4b17b53a16a81b5eaee6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.LinkedList; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.FutureTask; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.BufferedReader; | |
import com.sun.jna.Library; | |
import com.sun.jna.Native; | |
public class Test { | |
private static int _NR_gettid = 186; | |
interface LibC extends Library { | |
public static LibC INSTANCE = (LibC)Native.loadLibrary("c", LibC.class); | |
public int syscall(int n); | |
} | |
public static void main(String[] args) throws Exception { | |
final List<String> lines = new LinkedList<String>(); | |
final FutureTask<Integer> threadId = new FutureTask<Integer>( | |
new Callable<Integer>() { | |
@Override | |
public Integer call() { | |
return LibC.INSTANCE.syscall(_NR_gettid); | |
} | |
} | |
); | |
final Thread t1 = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
threadId.run(); | |
for (int i = 0; i < 10; i++) { | |
System.out.println("*"); | |
Thread.sleep(1000); | |
} | |
} catch (InterruptedException e) { | |
System.out.println("interrupted"); | |
} | |
} | |
}); | |
final Thread t2 = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
final int id = threadId.get(); | |
final ProcessBuilder pb = new ProcessBuilder("strace", "-p", Integer.toString(id)); | |
pb.redirectErrorStream(true); | |
final Process p = pb.start(); | |
final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); | |
try { | |
for (;;) { | |
try { | |
final int exitValue = p.exitValue(); | |
System.out.printf("strace terminated with status %d", exitValue); | |
break; | |
} catch (IllegalThreadStateException e) { | |
} | |
final String line = in.readLine(); | |
lines.add(line); | |
} | |
} finally { | |
in.close(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
t1.start(); | |
t2.start(); | |
t1.join(); | |
t2.join(); | |
for (final String line: lines) { | |
System.out.println(line); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment