How to play backwards-dynamic debug JetBrains clion Transcript

Source: Internet
Author: User

Statement

Clion Program Copyright JetBrains All, registration code authorization for JetBrains and its paid users all, this article only from the interest, the study of its program operation principle and registration code generation algorithm.

    • No complete source code is released.
    • Online Check the next, there are registered machines, so want to key students do not look for me: P
Background

In the previous article: Clion Registration code algorithm reverse analysis record

We combine jdb, Jd-gui and other tools, static analysis of the clion in the confusion of the clion.jar class information, successfully obtained the Clion registration code algorithm.

However, if you can analyze the code path in dynamic debugging, is it not more enjoyable to take the registration code and calculate the method?
This article will be from the perspective of dynamic debugging to carry out the reverse journey, as a supplement to the previous article.

Key techniques and tools used in this article:

    • Java-verbose
    • A HotSpot? Serviceability Agent
    • JetBrains IntelliJ Idea 14.3
    • JetBrains clion 1.0.4
    • Jd-gui 1.2
    • Jinfo
Difficulty in dynamic debugging

Because there is no source code, and the Clion class is confusing and there are many runtime-generated classes, it is not possible jd-gui to get Java files directly by simply using a tool like this.

If the name and control flow is only confused, in fact, it is relatively simple situation, not too abnormal situation, jd-gui you can get Java code, if the confusion is very strong, there is usually a byte code can be seen, so we need to the bytecode or confusing code reading feel more skilled.

If this is the relatively simple case above, we can patiently reconstruct or patch The decompile Java code to achieve the purpose of debugging.

Clion is different, it also has some runtime generated classes, coupled with confusion, which makes it difficult to prepare for dynamic debugging by simply deserializing.

Gets the class name where the entry function resides

The way is always more difficult, let's start to prepare for dynamic debugging it!

Through the analysis of the previous blog post, we are already familiar with some code paths that the Clion program launches, first to find the entry function of the program main , and rebuild it. How to find it?

    • Method 1: Use the startup script to find

You can look $CLION_HOME/bin at the startup parameters by looking at the following clion.sh :

133Main_class_name="$CL _main_class_name"134 if[-Z"$MAIN _class_name"]; Then      #注意这里135Main_class_name="Com.intellij.idea.Main"136 fi...176Ld_library_path="$IDE _bin_home:$LD _library_path" "$JDK/bin/java" 177   $AGENT 178   "-xbootclasspath/a:$IDE _home/lib/boot.jar" 179-classpath"$CLASSPATH"  the   $VM _options "-djb.vmoptionsfile=$VM _options_files_used" 181   "-xx:errorfile=$HOME/java_error_in_cl_%p.log" 182-djb.restart.code= the-didea.paths.selector=clion10183   $IDE _properties_property 184   $IDE _jvm_args 185   $REQUIRED _jvm_args       #以及这里186   $MAIN _class_name 187   "[email protected]"188Ec=$?
    • Method 2: Use Jinfo to get

can also let Clion first run up, through jinfo to obtain, first through jps and ps -ef | grep clion to confirm the Clion pid :

[haoran@localhostTools]$ jps27120Jps4001Main26953Main16911Launcher

Then enter jinfo pid , here is jinfo 26953 :

[Email protected] tools]$ Jinfo26953Attaching to process ID26953, please wait ... Error Attaching to Process:sun. JVM. Hotspot. Runtime. Vmversionmismatchexception: Supported versions is25.45-b02. Target VM is25.40-b25sun.jvm.hotspot.debugger.DebuggerException:Sun. JVM. Hotspot. Runtime. Vmversionmismatchexception: Supported versions is25.45-b02. Target VM is25.40-b25 at Sun. JVM. Hotspot. Hotspotagent. Setupvm(hotspotagent. Java:435) at Sun. JVM. Hotspot. Hotspotagent. Go(hotspotagent. Java:305) at Sun. JVM. Hotspot. Hotspotagent. Attach(hotspotagent. Java: $) 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. Jinfo. Main(Jinfo. Java:138)

found that because jre/jvm the version does not match, there is an exception, how to solve it?
clion.shstart by looking at how it is specified jvm :

It can be found that the path of the $IDE_HOME clion installation is $JAVA_HOME more forward than the order of the search, and we adjust this order to avoid jre/jvm the issue of version mismatch.

After adjusting, use again jinfo pid to view, get the following information:

The reason for enumerating the JRE/JVM version mismatch, and the way to get the class of the program entry function through Jinfo, is to clear the barrier for subsequent use of the SA to dump the runtime's class and repair Java files.

New Entry Class

We've got main the name of the class where the function is com.intellij.idea.Main , but by analyzing it and discovering it's not clion.jar in, where is it?

We analyze the jar where the class resides by adding-verbose to the Clion startup parameter and redirecting the result to a file.

As shown below:

Next, jd-gui you can decompile bootstrap.jar/com/intellij/idea/Main.class it and import its Java code into our newly created Java file with the same name:

Fix Entry Class

Such Java code is not compiled, and because many of the missing classes are very important dependencies, it is not possible to get the code to run through a simple mask.

So we start fixing the missing classes, how do we fix them?

We can use the HotSpot? Serviceability Agent (SA) capabilities provided to get the class at runtime. Of course, getting the runtime's class is not the only way.

Say it dry, classfilter:

Compile this classFilter , and follow the steps below to run it:
1. run clion.
2. Locate the PID of the clion.
3. start the following command:

Java-CP$JAVA _home/lib/sa-jdi. Jar:. -dsun. JVM. Hotspot. Tools. Jcore. Filter= $filterName-dsun. JVM. Hotspot. Tools. Jcore. OutputDir= $dumpedClassOutputDir Sun. JVM. Hotspot. Tools. Jcore. Classdump$clion _pid# $filterName: Our custom Classfilter# $dumpedClassOutputDir: directory where dump-out class files are stored# $clion _pid:clion process PID.

Look at the classes we dump out:

By this step, you have succeeded more than half, and next, join the classes in the way you like classPath it!

Fix Classpath

What are you waiting for, hurry up run :

Well, sure enough, many classes or missing, for the class generated in the run, according to the above method to find, for already in jar the class, find out classPath what else.

Remember the above jinfo , the sacrifice:

Add these to the ClassPath list.

Fix resources, runtime parameters, and options for the JVM

The program has become more and more close to the state of running, there are some fine, run for the year!

Q. Is there a feeling of buying a fan, applying silica gel, and setting the switch?
A. This is the kind of DIY engineers feel it ~ ^^

You will find that in subsequent runs, there will also be many anomalies, no relationship, through analysis, to know that there are still some things not fixed, including:

  • Resource/resources
  • Run-time parameters
  • VM Options

    Among the clion vm options we still get through the jinfo way:

    - xss2m -xms256m -xmx768m -XX: maxpermsize=m- xx: reservedcodecachesize=. M-xx: +USECONCMARKSWEEPGC ...- XX: errorfile=/home/haoran/java_error_in_cl_%p. Log- Djb. restart.code= -didea. Paths.selector=clion10 - Didea. platform.prefix=clion - Didea. no.jre.check=true 
Let the program get moving!

After finishing the fine pieces of Kung fu, to see our results:

    • The ornate stack frames!

    • The gorgeous locals!

    • Gorgeous the registration code algorithm!

Conclusion

What the? No dynamic debugging process?

Interested in the registration code algorithm students, please refer to the previous blog: clion registration code algorithm reverse analysis of the record.

I think a few of the gorgeous things above are enough for you

    • Step In/over/out
    • Evaluate expressions
    • Stack frame In/out

Feel for you to help or bo you a smile, point a recommendation:]

How to play backwards-dynamic debug JetBrains clion Transcript

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.