Disclaimer: This article is a blogger original article, please indicate the source. https://blog.csdn.net/u010597819/article/details/88554000
Start arthas-bootstrap
- Start, if there is no parameter list is automatically started java process list of the current system, waits for the user to select
- Select the attach process, and attach
- Start arthas-core splicing command line parameter is specified as comprising a agent arthas-agent.jar
- Start arthas-core.jar
- If only attach to exit
- If not only attach, then start arthas-client, entrance: com.taobao.arthas.client.TelnetConsole.Main
Start arthas-core
// "${JAVA_HOME}"/bin/java \
// ${opts} \
// -jar "${arthas_lib_dir}/arthas-core.jar" \
// -pid ${TARGET_PID} \
// -target-ip ${TARGET_IP} \
// -telnet-port ${TELNET_PORT} \
// -http-port ${HTTP_PORT} \
// -core "${arthas_lib_dir}/arthas-core.jar" \
// -agent "${arthas_lib_dir}/arthas-agent.jar"
例如:
java -jar ${HOME}/.arthas/lib/3.1.0/arthas/arthas-core.jar
-pid 7320
-target-ip 127.0.0.1
-telnet-port 3658
-http-port 8563
-core "${HOME}/.arthas/lib/3.1.0/arthas/arthas-core.jar"
-agent "${HOME}/.arthas/lib/3.1.0/arthas/arthas-agent.jar"
- core entrance Arthas.main, create Arthas
- attach proxy agent, attachAgent
- Traversing the current system of virtual machine list VirtualMachine.list, acquisition parameters specified in the pid process to attach
- attach the target virtual machine: VirtualMachine. attach
- Jvm judge jdk version two, and if not print warn log
- Load Acting as agent target jvm process
virtualMachine.loadAgent(configure.getArthasAgent(),
configure.getArthasCore() + ";" + configure.toString());
C:\Users\Gallant\.arthas\lib\3.1.0\arthas\arthas-agent.jar=C:\Users\Gallant\.arthas\lib\3.1.0\arthas\arthas-core.jar;;telnetPort=3658;httpPort=8563;ip=127.0.0.1;arthasAgent=C:\\Users\\Gallant\\.arthas\\lib\\3.1.0\\arthas\\arthas-agent.jar;sessionTimeout=1800;arthasCore=C:\\Users\\Gallant\\.arthas\\lib\\3.1.0\\arthas\\arthas-core.jar;javaPid=7320;
Start arthas-agent
- After the target process attach agent, agentmain agent-class method callback jvm class specified (callback start time is the same method premain premain-class category), main method of correction AgentBootstrap
- Load spy jar package to BootstrapClassLoader and get classloader agent jar of
- Initialization spy spy
- Load AdviceWeaver class
- 获取AdviceWeaver类的methodOnBegin、methodOnReturnEnd、methodOnThrowingEnd、methodOnInvokeBeforeTracing、methodOnInvokeAfterTracing、methodOnInvokeThrowTracing、resetArthasClassLoader方法
- Binding and Spy
- Binding bind
- The args parameter deserialize Configure objects
- To get the monitoring process pid (javaPid)
- Whether to acquire ArthasBootstrap instance judgment call has been bound, it is not binding
- ArthasBootstrap bind bind
arthas-core binding
- ArthasBootstrap bind bind
- Creating shellserver (ShellServerImpl) and bind port
- Create a built-in command packet BuiltinCommandPack
- The built-in command packet and shellserver bound to register as a resolver service
- Depending on the configuration of ip and port registration services: TelnetTermServer, HttpTermServer
- shellServer start listening: listen
- Traversal registered service registered handler (termHandler = TermServerTermHandler) and start the listener namely: TelnetTermServer, HttpTermServer
- Assumed to be telnet service model
- Creating netty service NettyTelnetTtyBootstrap based on ip port, start the parameters for the factory: providing accept implemented as termHandler.accept, accept the parameters for the new TermImpl
bootstrap.start(new Consumer<TtyConnection>() {
@Override
public void accept(final TtyConnection conn) {
termHandler.handle(new TermImpl(Helper.loadKeymap(), conn));
}
}).get(connectionTimeout, TimeUnit.MILLISECONDS);
- Start service start delegating telnet: NettyTelnetBootstrap.start start
- Create an event handler: TelnetChannelHandler
- Binding ip port and add a listener
- Event processing handler: TelnetChannelHandler, factory factory get property: TelnetTtyConnection
- TelnetChannelHandler read data channelRead, called the current connection instance read data: NettyTelnetConnection.receive
- Delegated state handle data DATA of the current state, the current delegated session (i.e. current instance of Connection) reading data append, to flush the cache data exceeds the binding handle to the read data onData
- Currently bound handler for the factory plant get acquired (TelnetTtyConnection), TelnetTtyConnection.onData
- write data decoder (BinaryDecoder.write), to read data to the local and ByteBuffer callback onChar.accept (ReadBuffer.accept)
- ReadBuffer call readHandler read data (TtyEventDecoder.accept), readerHandler open when the connection is opened disposed Connection eventDecoder (TtyEventDecoder)
- TtyEventDecoder received data accept, eventHandler, readHandler read data, eventHandler, readHandler echoHandler set within TermImpl constructor: DefaultTermStdinHandler, EventHandler
- EventHandler method termImpl read command implemented according to the specified call
- Reads standard input data DefaultTermStdinHandler.accept, call printed to standard output term, and the instruction in the queue instance is bound ReadLine
- The instruction placed in the event queue: EventQueue.append
- Client instructions read completion
- TelnetTtyConnection will check check Accept callback handler.accept connection open, which is anonymous achieved Consumer.accept
- term handle (TermServerTermHandler) handle client implementation termImpl, i.e. direct process delegate shellServer handle termImpl
- Create a session (ShellImpl) according Term
- Set the welcome page for the session
- Set close handle session of
- Initializing session
- session read command readline, delegating execution term, term delegate instance execution ReadLine
- Read instruction from the instruction queue and the EventQueue encapsulated as objects KeyEvent
- Examples of interaction Interaction handle to handle events
- Acquiring a corresponding method according KeyEvent event handling, processing is completed Complete e.g.
- That call CompletionHandler data processing
- CompletionHandler accept data processing
- Read the command line
- Parse command line CliToken list
- CommandManagerCompletionHandler处理CompletionAdaptor
- Delegate commandManager processing complete
- Processing commands completeSingleCommand, completeCommands
- Completion processing command, i.e. CompletionAdaptor.complete, the adapter delegate Completion Processing complete
- The index output to standard output
- Accept processing
- RequestHandler process the received request, the processing assigned to lineHandler: ShellLineHandler.handle
- According to the command line to create and execute tasks Job
- ShellImpl according to the command line to create a Job
- Job creation JobController
- Creating Process and associate Command, commandManager lines create a corresponding Command on command
- JobImpl Binding Process
- Implementation of Job, set the term for the Process and the implementation of the session
- The encapsulation process is performed CommandProcessTask submitted to the thread pool, CommandProcessImpl handle process delegated
- 即Command的processHandler处理:new ProcessImpl(command, remaining, command.processHandler(), ProcessOutput);
- For example: MonitorCommand, encapsulated as AnnotatedCommandImpl, processHandle as: ProcessHandler
- ProcessHandler processing command CommandProcess, delegate processing process AnnotatedCommandImpl
- Create a class instance and an order processing CommandProcess, such as: MonitorCommand.process
- MonitorCommand delegate command Enhance Technology parent process, i.e., the corresponding class Enhance Technology be enhanced, i.e. the corresponding listening weaving (e.g.: MonitorAdviceListener), Enhancer.enhance
- After the completion of TermServerListenHandler NettyTelnetTtyBootstrap start to handle the event started, if fails to start, turn off the server
- If the server starts successfully, start the scheduled task was shellServer listening session is closed; the listener callback handler returns a success status
arthas-core enhance enhancement of
- Creating Enhance example
- Adding to enhance Instrumentation
- Instrumentation modified class definition retransform
- Callback enhanced custom Enhancer.transform
- Call the ASM API enhancement processing to the specified class, custom class to change the implementation AdviceWeaver
- ASM visitMethod callback method of enhancing the specified method, to the weaving process the bytecode: Enhanced callback operation by Method.invoke method: ON_BEFORE_METHOD, ON_RETURN_METHOD, ON_THROWS_METHOD, BEFORE_INVOKING_METHOD, AFTER_INVOKING_METHOD, THROW_INVOKING_METHOD