Use the class in Sa-jdi.jar dump memory

Source: Internet
Author: User

Objective

When analyzing a jar package, it was found that he loaded the key class in the way it was used by the runtime classloader . Do not bother to analyze the algorithm, you can use their jdk own tools out of the dump need class .

Body

There are two ways to dump a class file from a running Java process,

    • Using the agent Attatch to the process, and then using instrumentation and Classfiletransformer to get to the class bytecode.

    • Use the tools in the Sd-jdi.jar

This article describes the use of sd-jdi.jar dump. sd-jdi.jar sun.jvm.hotspot.tools.jcore.ClassDumpcan dump class content into a file.

ClassDumpCan be set in two System properties :

    • Sun.jvm.hotspot.tools.jcore.filter the class name of the filter
    • Directory of Sun.jvm.hotspot.tools.jcore.outputDir output
Example

First write a filter class

import sun.jvm.hotspot.tools.jcore.ClassFilter;import sun.jvm.hotspot.oops.InstanceKlass;import sun.jvm.hotspot.tools.jcore.ClassDump;public class MyFilter implements ClassFilter {    @Override    public boolean canInclude(InstanceKlass kls) {        String klassName = kls.getName().asString();        return klassName.startsWith("com/fr/license/selector/");    }}  

The code is obvious, and the function is the dump com/fr/license/selector/ byte code of all classes that begin with.

and compile it into a class file.

To use this first need to sa-jdi.jar add to java the classpath inside.

Go to the filter directory of the class file that you just wrote. Perform

java  -Dsun.jvm.hotspot.tools.jcore.filter=MyFilter  -Dsun.jvm.hotspot.tools.jcore.outputDir=d:\dump  sun.jvm.hotspot.tools.jcore.ClassDump 5308

MyFilterchange to your own class name for the 5308 target Java process pid (can be used to jps view). d:\dumpthe corresponding file will then be generated class .

Problem solving
    • If you click on the app's EXE directly to launch the application, using JPS to get the PID, there may be no way to attach, so we have to find the command to start, such as the bat script inside.

    • General large applications will bring their own JRE, we will use the above technology, replace the JRE, for our, to normal dump, otherwise there will be a version mismatch.
    • Windows also needs to be sawindbg.dll placed in the jre/bin/ same directory as Java.exe. Otherwise, you may encounter problems that do not load this DLL.

At last

The first step in Java application is to find the command to start, which is convenient for later analysis. Do not use the exe startup app in general

Use the class in Sa-jdi.jar dump memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.