通过 DiagnosticCommand 支持调用 jcmd 里的命令
- Dominant language
- Java
- Stars
- 37.5k
- Forks
- 7.6k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 4
Description
```java
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.MalformedObjectNameException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.ReflectionException;
import java.lang.management.ManagementFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JcmdInProcess {
private static final String DIAGNOSTIC_COMMAND_MBEAN_NAME = "com.sun.management:type=DiagnosticCommand";
public static void main(String[] args) {
try {
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName diagnosticCommandMBean = new ObjectName(DIAGNOSTIC_COMMAND_MBEAN_NAME);
// 1. Thread.print (类似 jcmd Thread.print)
System.out.println("=== Invoking Thread.print ===");
String threadPrintResult = invokeDiagnosticCommand(mbeanServer, diagnosticCommandMBean,
"threadPrint", new Object[]{new String[]{}}, new String[]{String[].class.getName()});
System.out.println(threadPrintResult);
System.out.println("============================\n");
// 2. VM.flags (类似 jcmd VM.flags)
System.out.println("=== Invoking VM.flags ===");
String vmFlagsResult = invokeDiagnosticCommand(mbeanServer, diagnosticCommandMBean,
"vmFlags", new Object[]{new String[]{"-all"}}, new String[]{String[].class.getName()}); // "-all" 是一个示例参数
System.out.println(vmFlagsResult);
System.out.println("=========================\n");
// 3. GC.heap_dump (类似 jcmd GC.heap_dump /path/to/heapdump.hprof)
// 注意:这会在当前 JVM 中触发堆转储
System.out.println("=== Invoking GC.heap_dump ===");
Path heapDumpPath = Paths.get("heapdump_from_java_" + System.currentTimeMillis() + ".hprof");
try {
String heapDumpResult = invokeDiagnosticCommand(mbeanServer, diagnosticCommandMBean,
"gcHeapDump",
new Object[]{new String[]{heapDumpPath.toAbsolutePath().toString(), "-all"}}, // "-all" (or true) for live objects only; omit for all
new String[]{String[].class.getName()}
);
System.out.println("GC.heap_dump result: " + heapDumpResult);
System.out.println("Heap dump created at: " + heapDumpPath.toAbsolutePath());
} catch (Exception e) {
System.err.println("Failed to create heap dump: " + e.getMessage());
// 如果堆转储失败,打印更详细的错误信息,因为 MBeanException 会包装实际的错误
if (e instanceof MBeanException && e.getCause() != null) {
e.getCause().printStackTrace();
} else {
e.printStackTrace();
}
} finally {
// 可选:删除生成的堆转储文件
// Files.deleteIfExists(heapDumpPath);
}
System.out.println("===========================\n");
// 4. 获取可用的诊断命令 (类似 jcmd help)
System.out.println("=== Invoking help (listing available commands) ===");
String helpResult = invokeDiagnosticCommand(mbeanServer, diagnosticCommandMBean,
"help", new Object[]{new String[]{}}, new String[]{String[].class.getName()});
System.out.println(helpResult);
System.out.println("================================================\n");
} catch (MalformedObjectNameException e) {
System.err.println("Error creating ObjectName: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.err.println("An unexpected error occurred: " + e.getMessage());
e.printStackTrace();
}
}
private static String invokeDiagnosticCommand(MBeanServer mbeanServer, ObjectName mbeanName,
String operationName, Object[] params, String[] signature)
throws InstanceNotFoundException, MBeanException, ReflectionException {
Object result = mbeanServer.invoke(mbeanName, operationName, params, signature);
if (result instanceof String) {
return (String) result;
}
return String.valueOf(result); // Or handle other types if expected
}
}
```
Contributor guide
Research direction
Start with the DiagnosticCommand MBean entry point and the provided JcmdInProcess example, especially invokeDiagnosticCommand and its Thread.print, VM.flags, GC.heap_dump, and help calls. Determine where Arthas should expose this capability and what command set and behavior would define completion; the issue does not name repository files or tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100