Giter Site home page Giter Site logo

dumpclass's Introduction

Dumpclass

Dump classes from running JVM process by sa-jdi.jar.

  • Support wildcard match

  • Support multi classloader

Download dumpclass.jar

You can download dumpclass.jar from maven center.

http://search.maven.org/#search%7Cga%7C1%7Cdumpclass

wget http://search.maven.org/remotecontent?filepath=io/github/hengyunabc/dumpclass/0.0.2/dumpclass-0.0.2.jar -O dumpclass.jar

Usage

Usage:
 pattern                  : class name wildcard match pattern
 --noStat                 : do not print dump stat (default: false)
 --sensitive              : class name wildcard match sensitive. In mac os, try
                            this option. (default: false)
 -c (--classLoaderPrefix) : classLoaderPrefix (default: false)
 -o (--outputDir) VAL     : outputDir
 -p (--pid) N             : pid

Example:
  java -jar dumpclass.jar -p 4345 *StringUtils
  java -jar dumpclass.jar -p 4345 -o /tmp/dump *StringUtils
  java -jar dumpclass.jar -p 4345 -o /tmp/dump --classLoaderPrefix  *StringUtils
Use the specified sa-jdi.jar:
  java -cp './dumpclass.jar:$JAVA_HOME/lib/sa-jdi.jar' io.github.hengyunabc.dumpclass.DumpMain 4345 *StringUtils

Trouble

  • Try to use sudo
Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
sun.jvm.hotspot.debugger.DebuggerException: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal$BsdDebuggerLocalWorkerThread.execute(BsdDebuggerLocal.java:169)
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal.attach(BsdDebuggerLocal.java:287)
	at sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:671)
	at sun.jvm.hotspot.HotSpotAgent.setupDebuggerDarwin(HotSpotAgent.java:659)
	at sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:341)
	at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:304)
	at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:140)
	at sun.jvm.hotspot.tools.Tool.start(Tool.java:185)
	at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
	at sun.jvm.hotspot.tools.jcore.ClassDump.main(ClassDump.java:180)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at io.github.hengyunabc.dumpclass.DumpMain.run(DumpMain.java:132)
	at io.github.hengyunabc.dumpclass.DumpMain.doMain(DumpMain.java:68)
	at io.github.hengyunabc.dumpclass.DumpMain.main(DumpMain.java:61)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at io.github.hengyunabc.dumpclass.DumpMain$2.run(DumpMain.java:184)
	at java.lang.Thread.run(Thread.java:745)
Caused by: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal.attach0(Native Method)
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal.access$100(BsdDebuggerLocal.java:65)
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal$1AttachTask.doit(BsdDebuggerLocal.java:278)
	at sun.jvm.hotspot.debugger.bsd.BsdDebuggerLocal$BsdDebuggerLocalWorkerThread.run(BsdDebuggerLocal.java:144)
  • Make sure use the same jdk version.
Attaching to process ID 53577, please wait...
Exception in thread "main" java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at io.github.hengyunabc.dumpclass.DumpMain.main(DumpMain.java:101)
Caused by: java.lang.InternalError: void* type hasn't been seen when parsing int*
	at sun.jvm.hotspot.HotSpotTypeDataBase.recursiveCreateBasicPointerType(HotSpotTypeDataBase.java:721)
	at sun.jvm.hotspot.HotSpotTypeDataBase.lookupType(HotSpotTypeDataBase.java:134)
	at sun.jvm.hotspot.HotSpotTypeDataBase.lookupOrCreateClass(HotSpotTypeDataBase.java:631)
	at sun.jvm.hotspot.HotSpotTypeDataBase.createType(HotSpotTypeDataBase.java:751)
	at sun.jvm.hotspot.HotSpotTypeDataBase.readVMTypes(HotSpotTypeDataBase.java:195)
	at sun.jvm.hotspot.HotSpotTypeDataBase.<init>(HotSpotTypeDataBase.java:89)
	at sun.jvm.hotspot.HotSpotAgent.setupVM(HotSpotAgent.java:403)
	at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:305)
	at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:140)
	at sun.jvm.hotspot.tools.Tool.start(Tool.java:185)
	at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
	at sun.jvm.hotspot.tools.jcore.ClassDump.main(ClassDump.java:180)
	... 5 more
  • One class loaded by multi ClassLoader

Try to use HSDB. After attach java process, "Tools", "Class Browser".

sudo java -classpath "$JAVA_HOME/lib/sa-jdi.jar" sun.jvm.hotspot.HSDB

complie dumpclass.jar

mvn clean package
ls -alh target

License

Apache License V2

dumpclass's People

Contributors

hengyunabc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dumpclass's Issues

改为自动加载sa-jdi.jar

  • 首先尝试从当前的ClassLoader里查找,有没有相应的sa-jdi.jar 里的类,如果有,说明用户在 -cp 里指定了sa-jdi.jar,则直接使用即可。
  • 如果从当前ClassLoader里查找不到sa-jdi.jar,则尝试用外部自动加载。加载之后,再运行。

jdk 11下面出错

因为即使加了各种的 --add-exports,在 jdk.hotspot.agent 里,是在 sun.jvm.hotspot.tools.jcore.ClassDump.run() 里 ,用Class.forName 来加载filter类的。用户自定义的类加载不到。

    public void run() {
        // Ready to go with the database...
        try {
            if (classFilter == null) {
                // If not already set, the name of the filter comes from a System property.
                // If we have a pkgList, pass it, otherwise let the filter read
                // its own System property for the list of classes.
                String filterClassName = System.getProperty("sun.jvm.hotspot.tools.jcore.filter",
                                                            "sun.jvm.hotspot.tools.jcore.PackageNameFilter");
                try {
                    Class filterClass = Class.forName(filterClassName);
                    if (pkgList == null) {
                        classFilter = (ClassFilter) filterClass.newInstance();
                    } else {
                        Constructor con = filterClass.getConstructor(String.class);
                        classFilter = (ClassFilter) con.newInstance(pkgList);
                    }
                } catch(Exception exp) {
                    System.err.println("Warning: Can not create class filter!");
                }
            }

/Library/Java/JavaVirtualMachines/jdk-11.0.4.jdk/Contents/Home/bin/java --add-modules ALL-SYSTEM --add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED --add-exports=jdk.hotspot.agent/sun.jvm.hotspot.runtime=ALL-UNNAMED --add-exports=jdk.hotspot.agent/sun.jvm.hotspot.memory=ALL-UNNAMED --add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED --add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED --add-exports=jdk.hotspot.agent/sun.jvm.hotspot.tools.jcore=ALL-UNNAMED sun.jvm.hotspot.tools.jcore.ClassDump

http://hengyunabc.github.io/dumpclass/

dumpclass时报java.lang.reflect.InvocationTargetException错误

在使用java -jar .\dumpclass.jar -p 26532 com.restful.demo.* -o D:\dump --classLoaderPrefix参数dump class时报如下错误,请问该如何解决,谢谢!

PS D:\QA\AOP-toolbox\tools\dumpclass> java -jar .\dumpclass.jar -p 26532 com.restful.demo.* -o D:\dump --classLoaderPrefix
can not find sa-jdi.jar from classpath, try to load it from java.home.
Attaching to process ID 26532, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.231-b11
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at io.github.hengyunabc.dumpclass.DumpMain$2.run(DumpMain.java:184)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at io.github.hengyunabc.dumpclass.DumpMain.run(DumpMain.java:132)
        at io.github.hengyunabc.dumpclass.DumpMain.doMain(DumpMain.java:68)
        at io.github.hengyunabc.dumpclass.DumpMain.main(DumpMain.java:61)
        ... 6 more
Caused by: java.lang.InternalError
        at sun.jvm.hotspot.tools.jcore.ClassWriter.writeIndex(ClassWriter.java:106)
        at sun.jvm.hotspot.tools.jcore.ClassWriter.writeMethod(ClassWriter.java:550)
        at sun.jvm.hotspot.tools.jcore.ClassWriter.writeMethods(ClassWriter.java:431)
        at sun.jvm.hotspot.tools.jcore.ClassWriter.write(ClassWriter.java:93)
        at io.github.hengyunabc.dumpclass.DumpWrapperFilter.dumpKlass(DumpWrapperFilter.java:99)
        at io.github.hengyunabc.dumpclass.DumpWrapperFilter.canInclude(DumpWrapperFilter.java:31)
        at sun.jvm.hotspot.tools.jcore.ClassDump.dumpKlass(ClassDump.java:138)
        at sun.jvm.hotspot.tools.jcore.ClassDump.access$000(ClassDump.java:38)
        at sun.jvm.hotspot.tools.jcore.ClassDump$1.visit(ClassDump.java:109)
        at sun.jvm.hotspot.memory.Dictionary.classesDo(Dictionary.java:68)
        at sun.jvm.hotspot.memory.SystemDictionary.classesDo(SystemDictionary.java:190)
        at sun.jvm.hotspot.tools.jcore.ClassDump.run(ClassDump.java:105)
        at sun.jvm.hotspot.tools.Tool.startInternal(Tool.java:260)
        at sun.jvm.hotspot.tools.Tool.start(Tool.java:223)
        at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
        at sun.jvm.hotspot.tools.jcore.ClassDump.main(ClassDump.java:180)
        ... 13 more

当有重名类时,会出错

sudo java -jar target/dumpclass.jar 53577 *StringUtils
can not find sa-jdi.jar from classpath, try to load it from java.home.
Attaching to process ID 53577, please wait...
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at io.github.hengyunabc.dumpclass.DumpMain.main(DumpMain.java:101)
Caused by: java.lang.InternalError: void* type hasn't been seen when parsing int*
    at sun.jvm.hotspot.HotSpotTypeDataBase.recursiveCreateBasicPointerType(HotSpotTypeDataBase.java:721)
    at sun.jvm.hotspot.HotSpotTypeDataBase.lookupType(HotSpotTypeDataBase.java:134)
    at sun.jvm.hotspot.HotSpotTypeDataBase.lookupOrCreateClass(HotSpotTypeDataBase.java:631)
    at sun.jvm.hotspot.HotSpotTypeDataBase.createType(HotSpotTypeDataBase.java:751)
    at sun.jvm.hotspot.HotSpotTypeDataBase.readVMTypes(HotSpotTypeDataBase.java:195)
    at sun.jvm.hotspot.HotSpotTypeDataBase.<init>(HotSpotTypeDataBase.java:89)
    at sun.jvm.hotspot.HotSpotAgent.setupVM(HotSpotAgent.java:403)
    at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:305)
    at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:140)
    at sun.jvm.hotspot.tools.Tool.start(Tool.java:185)
    at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
    at sun.jvm.hotspot.tools.jcore.ClassDump.main(ClassDump.java:180)
    ... 5 more

dumpclass 得到的class文件与javac编译得到的class 文件hash存在差异的原因?

1.通过javac HelloWorld.java 得到 class 文件 HelloWorld.class,使用md5的方式得到该class文件Hash值为「A」;通过 dumpclass 的方式从进程中恢复的 HelloWorld.class 文件,使用md5的方式得到该文件Hash值为「B」。
现象:A != B ,想咨询这个问题的原因,为何恢复后程序的class文件不同?
2.如果 java 直接运行通过 dumpclass 得到的class文件,会报错 “Expected stackmap frame at this location”,类似于没有通过JVM验证,但是加上参数 -noverify 就又可以正常运行了。这个实验似乎与上面Hash值不同的现象呼应。想咨询这里面存在某种机制么,类似于JVM运行时保护?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.