cvmsh.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,119 行 · 第 1/3 页
JAVA
1,119 行
/* * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */import java.io.*;import java.util.Vector;import java.lang.reflect.Constructor;import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetException;import java.net.SocketException;import java.net.InetAddress;import sun.misc.VMInspector;/* * cvmsh provides a shell for developers to use. It is provide basic features * like launching applications either synchronously or in background threads, * as well as inspection tools to look query the state of the VM. */public class cvmsh{ final static int DEFAULT_PORT = 4321; protected int port = 0; protected Class serverSocketClass = null; protected Object serverSocket = null; protected CVMSHServer server = null; protected boolean isVerbose = false; protected boolean isServerMode = false; protected boolean hasQuitRequest = false; protected boolean shellIsRunning = false; // JVMPI data dump service: protected boolean jvmpiSupported; protected Class cvmJvmpiClass; static final String[] helpMsg = { "Commands:", " help - prints this list", " quit - exits this shell nicely", " quit! - exits this shell by calling OS exit()", " detach - detaches the client (if applicable) but", " leaves the VM and server running", "", " sysinfo - dumps misc system information", " gc - request a full GC cycle", " memstat - prints VM heap memory usage", " enableGC - enables the GC", " disableGC - disables the GC", " keepObjectsAlive true|false - forces the GC to keep all objs alive (or not)", "", " verbose - enables verbose output", " quiet - disables verbose output", " port=<port number> - sets the server port number to listen on", " startServer - starts server thread", " stopServer - stops server thread", "", " Dumpers:", " ========", " print <objAddr> - print object as string", " dumpObject <objAddr> - dump the specified object", " dumpClassBlock <cbAddr> - dump the specified class", " dumpObjectReferences <objAddr> - dump all references to the obj", " dumpClassReferences <classname> - dump all references to instances of the class", " dumpClassBlocks <classname> - dump all CBs of specified name", " dumpHeap [simple|verbose|stats] - dump heap contents or info", " - does a simple(default), verbose, or stats dump.", "", " Capturing and Comparing Heap states:", " ====================================", " captureHeapState [<comment>] - capture the current heap state", " releaseHeapState <id> - release the specified heap state", " releaseAllHeapStates - release all heap states", " listHeapStates - list captured heap states", " dumpHeapState <id> [obj|class] - dump the specified heap state", " - sort by obj or obj's class or none if unspecified.", " compareHeapState <id1> <id2> - compares the objs in the heap states", "", " Thread Queries:", " ===============", " listAllThreads - list all the currently live threads", " dumpAllThreads - dump the stack trace of all the currently", " live threads", " dumpStack <eeAddr> - dump the stack trace of the specified thread", "", " Misc utilities:", " ===============", " time <command> - computes time to execute specified command", " run <class> [args ...] - runs the specified app synchronously", " bg <class> [args ...] - runs the specified app in a new thread", }; static final String[] jvmpiHelpMsg = { "", " JVMPI utilities:", " ================", " jvmpiDumpData - requests a JVMPI data dump", }; public cvmsh() { try { serverSocketClass = Class.forName("java.net.ServerSocket"); } catch (ClassNotFoundException e) { } // Get the CVMJVMPI class if present: try { cvmJvmpiClass = Class.forName("sun.misc.CVMJVMPI"); jvmpiSupported = true; } catch (ClassNotFoundException e) { cvmJvmpiClass = null; jvmpiSupported = false; } } void doHelp() { printHelp(helpMsg); printJVMPIHelp(); } void printHelp(String[] msg) { for (int i = 0; i < msg.length; i++) { System.out.println(msg[i]); } } protected void printJVMPIHelp() { if (jvmpiSupported) { printHelp(jvmpiHelpMsg); } } public boolean quitRequested() { return hasQuitRequest; } void doGC() { if (VMInspector.gcIsDisabled()) { System.err.println("ERROR: GC is currently disabled. " + "Re-enable GC before invoking gc."); return; } java.lang.Runtime.getRuntime().gc(); doMemstat(); } void doMemstat() { Runtime r = java.lang.Runtime.getRuntime(); System.out.println("free memory = " + r.freeMemory()); System.out.println("total memory = " + r.totalMemory()); } void keepObjectsAlive(CmdStream cmd) { String token = getToken(cmd); try { boolean keepAlive = getBoolean(token); VMInspector.keepAllObjectsAlive(keepAlive); } catch (IllegalArgumentException e) { System.err.println("ERROR: Expecting true or false instead of:" + token); } } Object getObject(String token) { Object obj = null; try { long addr = getLong(token); try { obj = VMInspector.addrToObject(addr); } catch (IllegalStateException e) { System.err.println("ERROR: Need to disable GC before " + "calling print"); } catch (IllegalArgumentException e) { System.err.println("ERROR: address " + token + " is not a valid object"); } } catch (NumberFormatException e) { System.err.println("ERROR: Invalid address format: " + token); } return obj; } long getAddr(String token) { long addr = 0L; try { addr = getLong(token); } catch (NumberFormatException e) { System.err.println("ERROR: Invalid address format: " + token); } return addr; } void print(CmdStream cmd) { String token = getToken(cmd); Object obj = getObject(token); if (obj != null) { if (obj instanceof String) { String s = (String)obj; System.out.println("String " + token + ": length= " + s.length()); System.out.println(" value= \"" + s + "\""); } else { System.out.println("Object " + token + ": " + obj); } } } void dumpObject(CmdStream cmd) { String token = getToken(cmd); long addr = getAddr(token); if (addr != 0L) { VMInspector.dumpObject(addr); } } void dumpClassBlock(CmdStream cmd) { String token = getToken(cmd); long addr = getAddr(token); if (addr != 0L) { VMInspector.dumpClassBlock(addr); } } void dumpObjectReferences(CmdStream cmd) { String token = getToken(cmd); long addr = getAddr(token); if (addr != 0L) { VMInspector.dumpObjectReferences(addr); } } void dumpClassReferences(CmdStream cmd) { String token = getToken(cmd); if (token != "") { VMInspector.dumpClassReferences(token); } else { System.err.println("ERROR: Classname not specified"); } } void dumpClassBlocks(CmdStream cmd) { String token = getToken(cmd); if (token != "") { VMInspector.dumpClassBlocks(token); } else { System.err.println("ERROR: Classname not specified"); } } void dumpHeap(CmdStream cmd) { String token = getToken(cmd); if (token.equals("")) { VMInspector.dumpHeapSimple(); } else if (token.equals("simple")) { VMInspector.dumpHeapSimple(); } else if (token.equals("verbose")) { VMInspector.dumpHeapVerbose(); } else if (token.equals("stats")) { VMInspector.dumpHeapStats(); } else { System.err.println("ERROR: Unsupported heap dump type: " + token); } } void captureHeapState(CmdStream cmd) { String name = cmd.input; VMInspector.captureHeapState(name); } void releaseHeapState(CmdStream cmd) { String token = getToken(cmd); try { int id = getInt(token); VMInspector.releaseHeapState(id); } catch (NumberFormatException e) { System.err.println("ERROR: Invalid ID format: " + token); } } void dumpHeapState(CmdStream cmd) { String idToken = getToken(cmd); String sortKeyToken = getToken(cmd); try { int id = getInt(idToken); int sortKey = VMInspector.SORT_NONE; if (sortKeyToken.equals("obj")) { sortKey = VMInspector.SORT_BY_OBJ; } else if (sortKeyToken.equals("class")) { sortKey = VMInspector.SORT_BY_OBJCLASS; } VMInspector.dumpHeapState(id, sortKey); } catch (NumberFormatException e) { System.err.println("ERROR: Invalid ID format: " + idToken); } } void compareHeapState(CmdStream cmd) { String token1 = getToken(cmd); String token2 = getToken(cmd); String currentToken = token1; try { int id1 = getInt(token1); currentToken = token2; int id2 = getInt(token2); VMInspector.compareHeapState(id1, id2); } catch (NumberFormatException e) { System.err.println("ERROR: Invalid ID format: " + currentToken); } } // Thread Queries: void dumpStack(CmdStream cmd) { String token = getToken(cmd); try { int ee = getInt(token); VMInspector.dumpStack(ee); } catch (NumberFormatException e) { System.err.println("ERROR: Invalid EE format: " + token); } } class BackgroundThread extends Thread { String className; String[] args; cvmsh shell; BackgroundThread(cvmsh shell, String className, String[] args) { this.className = className; this.args = args; this.shell = shell; } public void run() { shell.run(className, args, false); } } void run(String className, String[] args, boolean background) { if (!background) { try { Class cls = Class.forName(className); Class[] argClses = {String[].class}; Method mainMethod = cls.getMethod("main", argClses); Object[] argObjs = {args}; mainMethod.invoke(null, argObjs); } catch (InvocationTargetException ite) { ite.getTargetException().printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } else { try { BackgroundThread t = new BackgroundThread(this, className, args); t.start(); } catch (Exception e) { e.printStackTrace(); } } } // JVMPI utilties:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?