Arthas home page
https://alibaba.github.io/arthas/index.html
Copy the code
What is Arthas?
Arthas is an open source Java diagnostic tool for Alibaba that developers love.
Arthas can help you when you are stuck with a problem like the following:
From which JAR is this class loaded? Why are all kinds of class-related exceptions reported?
Why didn’t the code I changed execute? Did I not commit? Got the branch wrong?
If you encounter a problem, you cannot debug it online. Can you only re-publish it by logging?
There is a problem with a user’s data processing online, but it cannot be debugged online, and it cannot be reproduced offline!
Is there a global view of the health of the system?
Is there any way to monitor the real-time health of the JVM?
How to quickly locate application hot spots, generate flame map?
Quick to use
The installation
curl -O https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar
Copy the code
Print help information:
java -jar arthas-boot.jar -h
Copy the code
If the download speed is slow, you can use aliyun’s image:
java -jar arthas-boot.jar --repo-mirror aliyun --use-http
Copy the code
More installation methods can refer to, not here on the installation to do too much description, very simple
https://alibaba.github.io/arthas/install-detail.html
Copy the code
Basic operation use, do not want to see can directly skip
- Help — View the command help information
- Cat — Prints the contents of a file, similar to the cat command in Linux
- Echo – Prints parameters, similar to the echo command in Linux
- Grep — Match lookup, similar to the grep command in Linux
- Tee — copies standard input to standard output and the specified file, similar to the tee command in Linux
- PWD — Returns the current working directory, similar to the Linux command
- CLS — Clears the current screen area
- Session — View information about the current session
- Reset — Resets enhanced classes. This will restore all Arthas enhanced classes and reset all enhanced classes when Arthas server is closed
- Version – Prints the Arthas version number loaded by the current target Java process
- History – Prints command history
- Quit — Exit the current Arthas client, leaving other Arthas clients unaffected
- Stop — Close the Arthas server and exit all Arthas clients
- Keymap — Arthas list of shortcuts and custom shortcuts
The JVM related
- Dashboard – Real-time data panel for the current system
- Thread – Views the thread stack information for the current JVM
- JVM – View information about the current JVM
- Sysprop – View and modify system properties of the JVM
- Sysenv – View environment variables for the JVM
- Vmoptions – View and modify options for diagnostics in the JVM
- Perfcounter – View Perf Counter information for the current JVM
- Logger – View and modify logger
- Getstatic — View the static properties of the class
- Ognl — Executes ogNL expressions
- Mbean – View information about mbeans
- Heapdump — Dumps Java heap, similar to the heapdump function of the jmap command
The class/this correlation
- Sc – View information about classes loaded by the JVM
- Sm — View method information for loaded classes
- Jad – Decompiles the source code of a loaded class
- MC — memory compiler, memory compiler. Java files are.class files
- Re-define — Load an external. Class file, re-define it into the JVM
- Dump — Dumps loaded class byte code to a specific directory
- Classloader – view the classloader inheritance tree, urls, class loading information, and use classloader to getResource
Monitor/watch/trace
Please note that these commands, through the bytecode enhancement technology to realize, will insert some aspects in the specified class methods to implement the data statistics and observation, so online, use pretest, please try to clear need classes, methods and conditions for observation, diagnosis end to stop or will enhance the reset command.
- Monitor — method performs monitoring
- Watch – method performs data observations
- Trace — calls to the method internally and prints the time spent on each node on the method path
- Stack – Outputs the call path where the current method is called
- Tt – time tunnel of method execution data, recording input and return information for each call to the specified method, and observing these different time calls
Profiler/flame figure
- Profiler – Samples the application using async-profiler to generate a flame map
options
- Options — View or set the Arthas global switch
The pipe
Arthas supports the use of pipes for further processing, the results of the above command such as sm Java. Lang. String * | grep ‘index’
- Grep — Searches for results that meet the criteria
- Plaintext — Removes ANSI colors from the result of a command
- Wc — Count the output by row
Background Asynchronous Task
Asynchronous background tasks come in handy when there is an occasional problem online, such as requiring a watch condition that may occur only once a day. See
Alibaba. Making. IO/arthas/asyn…
- Use > to override the result to the log file, use & to specify that the command is running in the background, and the session break does not affect the task execution (the default life cycle is 1 day).
- Jobs — Lists all jobs
- Kill — Forcibly terminates a task
- Fg — Brings suspended tasks to the foreground for execution
- Bg — Puts suspended tasks to background execution
Code hotfix
Attention!!!!!!
- New fields/methods are not allowed
- Println (system.out.println) {system.out.println (system.out.println) {system.out.println (system.out.println)
public class MathGame {
public static void main(String[] args) throws InterruptedException {
MathGame game = new MathGame();
while (true) { game.run(); TimeUnit.SECONDS.sleep(1); // This doesn't work because the code keeps runningwhileIn the System. Out.println ("in loop"); }} public void run() throws InterruptedException {// This is valid because the run() function completes system.out.println ("call run()");
try {
int number = random.nextInt();
List<Integer> primeFactors = primeFactors(number);
print(number, primeFactors);
} catch (Exception e) {
System.out.println(String.format("illegalArgumentCount:%3d, ", illegalArgumentCount) + e.getMessage()); }}Copy the code
- Re-define will not restore the original class. Re-define may fail (for example, adding a new field). See the JDK documentation.
- The reset command has no effect on the re-define class. To reset, re-define the original bytecode.
- Redefine the commands and jad/watch/trace/monitor/tt command will conflict. Once re-define is executed, re-executing the above command will reset re-Define’s bytecode. Re-define and Retransform are two different mechanisms in the JDK. When you use both mechanisms to update bytecode, only the last one will take effect.
Hot fixes begin!!
Find/decompile source files
Use SC to find files, using Fastjson as a reference
Sc com.alibaba. Fastjson * queries the class whose path is prefixed by the class path, or you can use the specific classpath
com.alibaba.fastjson.JSON
com.alibaba.fastjson.JSONAware
com.alibaba.fastjson.JSONObject
com.alibaba.fastjson.JSONStreamAware
Copy the code
Decompilate the source file, using the JSON class as the decompilation reference
Jad com. Alibaba. Fastjson JSON, can see through the following code decompiled source
ClassLoader:
+-org.springframework.boot.loader.LaunchedURLClassLoader@1be6f5c3
+-sun.misc.Launcher$AppClassLoader@55f96302
+-sun.misc.Launcher$ExtClassLoader@ 4195 f16 Location: file: / usr/hdyl/XDZ - enterprise - service/XDZ - enterprise - service - 0.0.1 - the SNAPSHOT. The jar! The/BOOT - INF/lib/fastjson - 1.2.67. Jar! / /* * Decompiled with CFR. * * Could not load the following classes: * com.alibaba.fastjson.JSONArray * com.alibaba.fastjson.JSONAware... */ package com.alibaba.fastjson; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONAware; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONStreamAware; . public abstract class JSON implements JSONStreamAware, JSONAware { public static TimeZone defaultTimeZone = TimeZone.getDefault(); public static Locale defaultLocale = Locale.getDefault(); public static String DEFAULT_TYPE_KEY ="@type";
static final SerializeFilter[] emptyFilters = new SerializeFilter[0];
public static String DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static int DEFAULT_PARSER_FEATURE;
public static int DEFAULT_GENERATE_FEATURE;
private static final ConcurrentHashMap<Type, Type> mixInsMapper;
private static final ThreadLocal<byte[]> bytesLocal;
private static final ThreadLocal<char[]> charsLocal;
public static final String VERSION = "1.2.67";
private static void config(Properties properties) {
String featuresProperty = properties.getProperty("fastjson.serializerFeatures.MapSortField");
int mask = SerializerFeature.MapSortField.getMask();
if ("true".equals(featuresProperty)) {
DEFAULT_GENERATE_FEATURE |= mask;
} else if ("false".equals(featuresProperty)) {...
Copy the code
Through the source code we can locate the production environment code, and make changes to it
Modify the code
- Through step 1, you can see the problem with the source code. After finding the problem, go back to the editor (IDEA/Eclipse).
- Modify the source code by adding a line of code to it, for example
log.info("Test thermal repair")
Copy the code
- Code compilation (I’m using Maven for packaging here)
mvn clean compile -D skipTests -P pro
Copy the code
- Clean Clears the target file (if target is empty)
- Compile the project (a step that implements compiling bytecode)
- -d skipTests skips test methods
- -p pro specifies a mavenProfile.
- Locate the class file that needs to be modified, usually in the compiled package in the target directory
- Upload the class file to the server, and here my file is uploaded
Enter the custom class folder
cd /usr/local/arthas/classes
Upload files to this directory
rz -E JSON-hotfix.class
Copy the code
Three. Thermal repair
With the second step, the source code has been modified and uploaded to the server, and the next step is the hot fix
- Enter the Arthas console
java -jar arthas-boot.jar
Copy the code
- Locate the appropriate process and go to the services console
- Code hotfixes
redefine /usr/local/arthas/classes/JSON-hotfix.class
Copy the code
- Console return
# When success is returned, the code has been reloaded in the JVM
redefine success, size: 1
Copy the code
- At this point, the code hotfix is complete
Four. Problems arise
-
- Failed: Runtime failed to change the schema
- Compare old and new class files using Java P-P
- Check for new fields or methods
javap -p xxx.class
Copy the code
- For detailed notes, refer to topic ==[Code hotfix]==