enhanced the thread dump to make it usable for scripted remote-debugging

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4732 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 17 years ago
parent e024e3b9cf
commit d0e2830e01

@ -1,7 +1,8 @@
#(plain)#
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <head>
<title>YaCy '#[clientname]#': Thread Dump</title> <title>YaCy Debugging: Thread Dump</title>
#%env/templates/metas.template%# #%env/templates/metas.template%#
</head> </head>
<body id="Threaddump"> <body id="Threaddump">
@ -16,8 +17,11 @@
<div> <div>
#(dump)#::<tt>#[content]#</tt>#(/dump)# <tt>#[content]#</tt>
</div> </div>
#%env/templates/footer.template%# #%env/templates/footer.template%#
</body> </body>
</html> </html>
::
#[content]#
#(/plain)#

@ -75,69 +75,89 @@ public class Threaddump_p {
StringBuffer buffer = new StringBuffer(1000); StringBuffer buffer = new StringBuffer(1000);
if (post != null && post.containsKey("createThreaddump")) { if (post != null && post.containsKey("createThreaddump")) {
boolean plain = post.get("plain", "false").equals("true");
int sleep = post.getInt("sleep", 0); // a sleep before creation of a thread dump can be used for profiling
if (sleep > 0) try {Thread.sleep(sleep);} catch (InterruptedException e) {}
prop.put("dump", "1"); prop.put("dump", "1");
// Thread dump // Thread dump
Map<Thread,StackTraceElement[]> stackTraces = Thread.getAllStackTraces(); Map<Thread,StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
Date dt = new Date(); Date dt = new Date();
String versionstring = yacyVersion.combined2prettyVersion(sb.getConfig("version","0.1")); String versionstring = yacyVersion.combined2prettyVersion(sb.getConfig("version","0.1"));
buffer.append("************* Start Thread Dump " + dt + " *******************").append("<br />"); bufferappend(buffer, plain, "************* Start Thread Dump " + dt + " *******************");
buffer.append("<br /> YaCy Version: " + versionstring + "<br />"); bufferappend(buffer, plain, "");
buffer.append("Total Memory = " + (Runtime.getRuntime().totalMemory())).append("<br />"); bufferappend(buffer, plain, "YaCy Version: " + versionstring);
buffer.append("Used&nbsp;&nbsp;Memory = " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())).append("<br />"); bufferappend(buffer, plain, "Total Memory = " + (Runtime.getRuntime().totalMemory()));
buffer.append("Free&nbsp;&nbsp;Memory = " + (Runtime.getRuntime().freeMemory())).append("<br />"); bufferappend(buffer, plain, "Used&nbsp;&nbsp;Memory = " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
buffer.append(" --- --- --- --- <br /><br />"); bufferappend(buffer, plain, "Free&nbsp;&nbsp;Memory = " + (Runtime.getRuntime().freeMemory()));
bufferappend(buffer, plain, "");
appendStackTraces(sb.getRootPath(), buffer, stackTraces, Thread.State.BLOCKED); bufferappend(buffer, plain, "");
appendStackTraces(sb.getRootPath(), buffer, stackTraces, Thread.State.RUNNABLE);
appendStackTraces(sb.getRootPath(), buffer, stackTraces, Thread.State.TIMED_WAITING); appendStackTraces(sb.getRootPath(), buffer, stackTraces, plain, Thread.State.BLOCKED);
appendStackTraces(sb.getRootPath(), buffer, stackTraces, Thread.State.WAITING); appendStackTraces(sb.getRootPath(), buffer, stackTraces, plain, Thread.State.RUNNABLE);
appendStackTraces(sb.getRootPath(), buffer, stackTraces, Thread.State.NEW); appendStackTraces(sb.getRootPath(), buffer, stackTraces, plain, Thread.State.TIMED_WAITING);
appendStackTraces(sb.getRootPath(), buffer, stackTraces, Thread.State.TERMINATED); appendStackTraces(sb.getRootPath(), buffer, stackTraces, plain, Thread.State.WAITING);
appendStackTraces(sb.getRootPath(), buffer, stackTraces, plain, Thread.State.NEW);
buffer.append("************* End Thread Dump " + dt + " *******************").append("<br />"); appendStackTraces(sb.getRootPath(), buffer, stackTraces, plain, Thread.State.TERMINATED);
prop.put("dump_content", buffer.toString()); bufferappend(buffer, plain, "************* End Thread Dump " + dt + " *******************");
} else {
prop.put("dump", "0"); prop.put("plain_content", buffer.toString());
prop.put("plain", (plain) ? 1 : 0);
} }
return prop; // return from serverObjects respond() return prop; // return from serverObjects respond()
} }
private static void appendStackTraces(File rootPath, StringBuffer buffer, Map<Thread,StackTraceElement[]> stackTraces, Thread.State stateIn) { private static void appendStackTraces(File rootPath, StringBuffer buffer, Map<Thread,StackTraceElement[]> stackTraces, boolean plain, Thread.State stateIn) {
buffer.append("THREADS WITH STATES: " + stateIn.toString()).append("<br />").append("<br />"); bufferappend(buffer, plain, "THREADS WITH STATES: " + stateIn.toString());
bufferappend(buffer, plain, "");
File classPath = new File(rootPath, "source"); File classPath = new File(rootPath, "source");
for (Thread thread: stackTraces.keySet()) { for (Thread thread: stackTraces.keySet()) {
StackTraceElement[] stackTraceElements = stackTraces.get(thread); StackTraceElement[] stackTraceElements = stackTraces.get(thread);
StackTraceElement ste; StackTraceElement ste;
String line; String line;
if (stateIn.equals(thread.getState())) { String tracename = "";
buffer.append("Thread= " + thread.getName() + " " + (thread.isDaemon()?"daemon":"") + " id=" + thread.getId() + " " + thread.getState().toString()).append("<br />"); File classFile;
if ((stateIn.equals(thread.getState())) && (stackTraceElements.length > 0)) {
if (plain) {
classFile = getClassFile(classPath, stackTraceElements[stackTraceElements.length - 1].getClassName());
tracename = classFile.getName();
if (tracename.endsWith(".java")) tracename = tracename.substring(0, tracename.length() - 5);
if (tracename.length() > 20) tracename = tracename.substring(0, 20);
while (tracename.length() < 20) tracename = tracename + "_";
tracename = "[" + tracename + "] ";
}
bufferappend(buffer, plain, tracename + "Thread= " + thread.getName() + " " + (thread.isDaemon()?"daemon":"") + " id=" + thread.getId() + " " + thread.getState().toString());
for (int i = 0; i < stackTraceElements.length; i++) { for (int i = 0; i < stackTraceElements.length; i++) {
ste = stackTraceElements[i]; ste = stackTraceElements[i];
if (i == 0) { if (i == 0) {
line = getLine(classPath, ste.getClassName(), ste.getLineNumber()); line = getLine(getClassFile(classPath, ste.getClassName()), ste.getLineNumber());
} else { } else {
line = null; line = null;
} }
if ((line != null) && (line.length() > 0)) { if ((line != null) && (line.length() > 0)) {
buffer.append("at " + htmlTools.encodeUnicode2html(ste.toString(), true)).append(" [").append(line).append("]<br />"); bufferappend(buffer, plain, tracename + "at " + htmlTools.encodeUnicode2html(ste.toString(), true) + " [" + line.trim() + "]");
} else { } else {
buffer.append("at " + htmlTools.encodeUnicode2html(ste.toString(), true)).append("<br />"); bufferappend(buffer, plain, tracename + "at " + htmlTools.encodeUnicode2html(ste.toString(), true));
} }
} }
buffer.append("<br />"); bufferappend(buffer, plain, "");
} }
} }
buffer.append("<br />"); bufferappend(buffer, plain, "");
} }
private static String getLine(File sourcePath, String classname, int line) { private static File getClassFile(File sourcePath, String classname) {
// find class
String classPath = classname.replace('.', '/') + ".java"; String classPath = classname.replace('.', '/') + ".java";
File file = new File(sourcePath, classPath); File file = new File(sourcePath, classPath);
return file;
}
private static String getLine(File file, int line) {
// find class
if (!file.exists()) return ""; if (!file.exists()) return "";
try { try {
String lineString = nxTools.line(serverFileUtils.read(file), line); String lineString = nxTools.line(serverFileUtils.read(file), line);
@ -147,4 +167,14 @@ public class Threaddump_p {
return "@EXCEPTION: " + e.getMessage(); return "@EXCEPTION: " + e.getMessage();
} }
} }
private static void bufferappend(StringBuffer buffer, boolean plain, String a) {
buffer.append(a);
if (plain) {
buffer.append("\n");
} else {
buffer.append("<br />");
}
}
} }

Loading…
Cancel
Save