当前位置:网站首页>Arthas source code learning-1
Arthas source code learning-1
2022-06-25 15:24:00 【Running pig ZC】
Previous preparation
1: Source download :https://github.com/alibaba/arthas.git
2: Import to idea, I choose to use it directly idea Download and open
3: Overall directory structure
attach The preparatory work
Find the startup class and debug mode
com.taobao.arthas.boot.Bootstrap#main
// Get some configuration
CLI cli = CLIConfigurator.define(Bootstrap.class);
Get a series of annotations on the startup class and the contents inside , Finally, a client is returned
Wrap the client as a command line
// Wrap the client as a command line
CommandLine commandLine = cli.parse(Arrays.asList(args));
take client Inject into the startup class
CLIConfigurator.inject(commandLine, bootstrap);
Filtering process
pid = ProcessUtils.select(bootstrap.isVerbose(), telnetPortPid, bootstrap.getSelect());
adopt jps The command lists the processes
Map<Long, String> processMap = listProcessByJps(v);
findJps Where is the command
// Get system variables
String javaHome = System.getProperty("java.home");
obtain jps The order set of , There may be more than one , Return to the first
if (jpsList.isEmpty()) {
AnsiLog.debug("Can not find jps under :" + javaHome);
String javaHomeEnv = System.getenv("JAVA_HOME");
AnsiLog.debug("Try to find jps under env JAVA_HOME :" + javaHomeEnv);
for (String path : paths) {
File jpsFile = new File(javaHomeEnv, path);
if (jpsFile.exists()) {
AnsiLog.debug("Found jps: " + jpsFile.getAbsolutePath());
jpsList.add(jpsFile);
}
}
}
return jps The catalog of , Now we know jps Under which directory , It will be implemented later jps command , List all java process
structure jps command , there v It stands for verbose, It's equivalent to what we often do jps -lvm Medium v
perform jps command ,
List<String> lines = ExecutingCommand.runNative(command);
Middle pair jps Format the returned data string
Get PID, Mapping with the corresponding process , Store in a map in
The process number and the corresponding jps line Print on the console , Then block it
int count = 1;
for (String process : processMap.values()) {
if (count == 1) {
System.out.println("* [" + count + "]: " + process);
} else {
System.out.println(" [" + count + "]: " + process);
}
count++;
}
as follows , Next, let's input the corresponding process number , Here select the previously run TestArthasApplication, choice 5
Next, do some parameter verification
// Parameter checking
if (choice <= 0 || choice > processMap.size()) {
return -1;
}
............
............
We pressed 5 after , The final choice is PID=12558 , As we expected
Another round of parameter verification ,-- id Check and so on , And will prompt some error messages
private static void checkTelnetPortPid(Bootstrap bootstrap, long telnetPortPid, long targetPid) {
if (telnetPortPid > 0 && targetPid != telnetPortPid) {
AnsiLog.error("The telnet port {} is used by process {} instead of target process {}, you will connect to an unexpected process.",
bootstrap.getTelnetPortOrDefault(), telnetPortPid, targetPid);
AnsiLog.error("1. Try to restart arthas-boot, select process {}, shutdown it first with running the 'stop' command.",
telnetPortPid);
AnsiLog.error("2. Or try to stop the existing arthas instance: java -jar arthas-client.jar 127.0.0.1 {} -c \"stop\"", bootstrap.getTelnetPortOrDefault());
AnsiLog.error("3. Or try to use different telnet port, for example: java -jar arthas-boot.jar --telnet-port 9998 --http-port -1");
System.exit(1);
}
}
Get arthas Of home, Then get the source code directory
CodeSource codeSource = Bootstrap.class.getProtectionDomain().getCodeSource();
file:/C:/Users/zhucc/IdeaProjects/arthas/boot/target/classes/
List arthas lib The catalog of , It's mainly about getting arthas edition
List<String> versionList = listNames(ARTHAS_LIB_DIR);
Get the local version arthas
String localLastestVersion = null;
if (!versionList.isEmpty()) {
localLastestVersion = versionList.get(versionList.size() - 1);
}
// Get the latest version of arthas, Download tool
String remoteLastestVersion = DownloadUtils.readLatestReleaseVersion();
public static String readLatestReleaseVersion() {
InputStream inputStream = null;
try {
URLConnection connection = openURLConnection(ARTHAS_LATEST_VERSIONS_URL);
inputStream = connection.getInputStream();
return IOUtils.toString(inputStream).trim();
} catch (Throwable t) {
AnsiLog.error("Can not read arthas version from: " + ARTHAS_LATEST_VERSIONS_URL);
AnsiLog.debug(t);
} finally {
IOUtils.close(inputStream);
}
return null;
}
Check whether you want to download
localLastestVersion.compareTo(remoteLastestVersion) < 0
else {
if (remoteLastestVersion != null) {
if (localLastestVersion.compareTo(remoteLastestVersion) < 0) {
AnsiLog.info("local lastest version: {}, remote lastest version: {}, try to download from remote.",
localLastestVersion, remoteLastestVersion);
needDownload = true;
}
}
}
Get arthas My feeling path
check process id
if (telnetPortPid > 0 && pid == telnetPortPid) {
AnsiLog.info("The target process already listen port {}, skip attach.", bootstrap.getTelnetPortOrDefault());
}
Start class loading
By class url Class loader to load files on disk , get files
To obtain by reflection main Method
Assemble parameters in the command
if (bootstrap.getCommand() != null) {
telnetArgs.add("-c");
telnetArgs.add(bootstrap.getCommand());
}
if (bootstrap.getBatchFile() != null) {
telnetArgs.add("-f");
telnetArgs.add(bootstrap.getBatchFile());
}
if (bootstrap.getHeight() != null) {
telnetArgs.add("--height");
telnetArgs.add("" + bootstrap.getHeight());
}
if (bootstrap.getWidth() != null) {
telnetArgs.add("--width");
telnetArgs.add("" + bootstrap.getWidth());
}
Set a context in the current thread , and invoke just telnetConsole.main Method
stay telnetConsole.main Break point , Has entered
com.taobao.arthas.client.TelnetConsole#process(java.lang.String[], java.awt.event.ActionListener)
call Start the same interface as the class , Set the parameters , wait , No more details here , This is followed by a series of parameter verifications
TelnetConsole telnetConsole = new TelnetConsole();
CLI cli = CLIConfigurator.define(TelnetConsole.class);
CommandLine commandLine = cli.parse(Arrays.asList(args));
CLIConfigurator.inject(commandLine, telnetConsole);
Get a terminal Terminal
final ConsoleReader consoleReader = new ConsoleReader(System.in, System.out);
consoleReader.setHandleUserInterrupt(true);
Terminal terminal = consoleReader.getTerminal();
// support catch ctrl+c event
terminal.disableInterruptCharacter();
if (terminal instanceof UnixTerminal) {
((UnixTerminal) terminal).disableLitteralNextCharacter();
}
Connecting terminals , After connecting , Return to success – STATUS_OK
org.apache.commons.net.SocketClient#connect(java.lang.String, int)
边栏推荐
- One question per day,
- Sampling method and descriptive statistical function in R language
- [paper notes] semi supervised object detection (ssod)
- Paddlepaddle paper reproduction course biggan learning experience
- [paper notes] contextual transformer networks for visual recognition
- System Verilog — interface
- Solution of push code failure in idea
- Iterator failure condition
- Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detection
- Design and implementation of timer
猜你喜欢
Paddlepaddle paper reproduction course biggan learning experience
Learning notes on February 18, 2022 (C language)
SPARQL learning notes of query, an rrdf query language
Time stamp calculation and audio-visual synchronization of TS stream combined video by ffmpeg protocol concat
Iterator failure condition
Yolov5 Lite: fewer parameters, higher accuracy and faster detection speed
Learning notes on February 5, 2022 (C language)
Install Kali extension 1: (kali resolution problem)
Source code analysis of zeromq lockless queue
Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detection
随机推荐
Leetcode121 timing of buying and selling stocks
Golang channel reading data
Solve valueerror: invalid literal for int() with base 10
Some usage records about using pyqt5
Common operations in VIM
How to download and install Weka package
Function of getinstance() method
About%*s and%* s
Leetcode122 timing of buying and selling stocks II
Sequential programming 1
QT animation loading and closing window
Learning notes on February 5, 2022 (C language)
Weka download and installation
[paper notes] rethinking and improving relative position encoding for vision transformer
Several common optimization methods
Pytorch | how to save and load pytorch models?
One code per day - day one
QT excel table read / write library - qtxlsx
Modal and modeless dialogs for QT
If a thread overflows heap memory or stack memory, will other threads continue to work