views:

201

answers:

3

I'm creating a global exception handling which collects some information before shutting down in some cases. One of this information is the current thread dump. i do this with following code:

ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);

The problem is to write the information into a analyzable format for TDA. Is there a "simple" way to format the information instead of writing the format on my own?

EDIT: I'd like to have a full thread dump so I can find problematic threads. The above mentioned method delivers an array of ThreadInfo-Objects, so i have the data. My problem is that the written output isn't in a format TDA recognizes as thread dump.

A: 

If you're on windows, then bwithers has described a way to signal the JVM to thread dump.

There's also a pure java approach, but I'm not sure that the output is in standard format.

John
A: 

Just use the code TDA uses itself to dump JMX-data: MBeanDumper.java

Ingo
+1  A: 

If you don't want to copy the TDA code (It is LGPL, after all) you can also use the Attach API to get data in the standard format. As far as I know, the only JVM built-in code to do the dump is native code in the Attach agent.

String selfName = ManagementFactory.getRuntimeMXBean().getName();
final int selfPid = Integer.valueOf(selfName.substring(0, selfName.indexOf('@')));                

HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine.attach(Integer.toString(selfPid));

InputStream sDump = vm.remoteDataDump(new Object[]{"-l"}); // lowercase L for lock dump

The data dump will return the dump in a stream of character data.

gibbss