Skip to content

Instantly share code, notes, and snippets.

@hofmeister
Created February 13, 2012 16:48
Show Gist options
  • Save hofmeister/1818106 to your computer and use it in GitHub Desktop.
Save hofmeister/1818106 to your computer and use it in GitHub Desktop.
Trace specific thread id in java thread using jstack
<?php
//Usage: php tracethread.php <pid> <tid>
//Get the <tid> from a jstack call
$pid = intval($_SERVER['argv'][1]);
$tid = $_SERVER['argv'][2];
echo "Tracing thread: $pid : $tid\n";
while(true) {
$dump = shell_exec("jstack $pid");
$startOffset = stripos($dump,$tid);
$endOffset = stripos($dump,"\n\n",$startOffset);
if ($startOffset === FALSE) {
echo "$tid not found\n";
break;
}
$trace = substr($dump,$startOffset,$endOffset-$startOffset);
$lines = explode("\n",$trace);
$out = implode("\n",array_slice($lines,0,400));
passthru('clear');
echo chr(27).'[0;0f';
echo "$out\n\n";
sleep(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment