1 4 package com.tc.util.runtime; 5 6 import com.tc.process.StreamCollector; 7 import com.tc.test.TestConfigObject; 8 import com.tc.util.concurrent.ThreadUtil; 9 10 import java.io.File ; 11 12 public class ThreadDump { 13 14 public static void main(String args[]) { 15 dumpThreadsOnce(); 16 17 System.err.flush(); 19 System.out.flush(); 20 try { 21 Thread.sleep(5000); 22 } catch (InterruptedException e) { 23 e.printStackTrace(); 24 } 25 } 26 27 public static void dumpThreadsOnce() { 28 dumpThreadsMany(1, 0L); 29 } 30 31 public static void dumpThreadsMany(int iterations, long delay) { 32 for (int i = 0; i < iterations; i++) { 33 if (Os.isWindows()) { 34 doWindowsDump(); 35 } else { 36 doUnixDump(); 37 } 38 ThreadUtil.reallySleep(delay); 39 } 40 } 41 42 public static void dumpProcessGroup() { 43 if (!Os.isUnix()) { throw new AssertionError ("unix only"); } 44 doSignal(new String [] { "-3" }, 0); 45 } 46 47 private static void doUnixDump() { 48 int pid; 49 try { 50 pid = GetPid.getPID(); 51 } catch (Throwable t) { 52 System.err.println("Got Exception trying to get the process ID. Sending Kill signal to entire process group. " 53 + t.getMessage()); 54 System.err.flush(); 55 pid = 0; 56 } 57 58 doSignal(new String [] { "-3" }, pid); 59 } 60 61 private static void doWindowsDump() { 62 doSignal(new String [] {}, GetPid.getPID()); 63 } 64 65 private static void doSignal(String [] args, int pid) { 66 File program = SignalProgram.PROGRAM; 67 68 try { 69 String [] cmd = new String [1 + args.length + 1]; 70 cmd[0] = program.getAbsolutePath(); 71 System.arraycopy(args, 0, cmd, 1, args.length); 72 73 cmd[cmd.length - 1] = Integer.toString(pid); 74 75 Process p = Runtime.getRuntime().exec(cmd); 76 p.getOutputStream().close(); 77 StreamCollector err = new StreamCollector(p.getErrorStream()); 78 StreamCollector out = new StreamCollector(p.getInputStream()); 79 err.start(); 80 out.start(); 81 82 p.waitFor(); 83 84 err.join(); 85 out.join(); 86 87 System.err.print(err.toString()); 88 System.err.flush(); 89 System.out.print(out.toString()); 90 System.out.flush(); 91 92 } catch (Exception e) { 93 e.printStackTrace(); 94 } 95 } 96 97 private static class SignalProgram { 98 static final File PROGRAM; 99 100 static { 101 PROGRAM = getSignalProgram(); 102 } 103 104 private static File getSignalProgram() { 105 File rv = null; 106 107 if (Os.isWindows()) { 108 try { 109 rv = new File (TestConfigObject.getInstance().executableSearchPath(), "SendSignal.EXE"); 110 } catch (Exception e) { 111 throw new RuntimeException (e); 112 } 113 } else { 114 File binKill = new File ("/bin/kill"); 115 File usrBinKill = new File ("/usr/bin/kill"); 116 117 if (binKill.exists()) { 118 rv = binKill; 119 } else if (usrBinKill.exists()) { 120 rv = usrBinKill; 121 } 122 } 123 124 if (rv != null) { 125 if (rv.exists() && rv.isFile()) { return rv; } 126 System.err.println("Cannot find signal program: " + rv.getAbsolutePath()); 127 System.err.flush(); 128 } 129 130 throw new RuntimeException ("Cannot find signal program"); 131 } 132 } 133 } 134 | Popular Tags |