thread dump获取
1. 发送信号* In Unix, use "kill -3 <pid>" where pid is the Process ID of the JVM.( kill 信号列表)
* In Windows, press CTRL+BREAK on the window where the JVM is running.
2. jstack -l pid.
(1). jstack入口
public class JStack {
public static void main(String[] args) throws Exception {
...
String pid = args[optionCount];
String params[];
if (locks) {
params = new String[] { "-l" };
} else {
params = new String[0];
}
runThreadDump(pid, params);
...
}
// Attach to pid and perform a thread dump
private static void runThreadDump(String pid, String args[])
throws Exception {
VirtualMachine vm = null;
try {
vm = VirtualMachine.attach(pid);
} catch (Exception x) {
...
}
// Cast to HotSpotVirtualMachine as this is implementation specific
// method.
InputStream in = ((HotSpotVirtualMachine) vm)
.remoteDataDump((Object[]) args);
// read to EOF and just print output
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
in.close();
vm.detach();
}
(2).执行方式
LinuxVirtualMachine.execute
/**
* Execute the given command in the target VM.
*/
@Override
InputStream execute(String cmd, Object... args) throws AgentLoadException, IOException
{
...
int s = socket();
// connect to target VM
try {
connect(s, p);
}
catch (IOException x) {
close(s);
throw x;
}
IOException ioe = null;
// connected - write request,发送command到target j