arthas source of learning

Disclaimer: This article is a blogger original article, please indicate the source. https://blog.csdn.net/u010597819/article/details/88554000

Start arthas-bootstrap

  1. Start, if there is no parameter list is automatically started java process list of the current system, waits for the user to select
  2. Select the attach process, and attach
  3. Start arthas-core splicing command line parameter is specified as comprising a agent arthas-agent.jar
  4. Start arthas-core.jar
  5. If only attach to exit
  6. 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"
  1. core entrance Arthas.main, create Arthas
  2. attach proxy agent, attachAgent
  3. Traversing the current system of virtual machine list VirtualMachine.list, acquisition parameters specified in the pid process to attach
  4. attach the target virtual machine: VirtualMachine. attach
  5. Jvm judge jdk version two, and if not print warn log
  6. 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

  1. 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
  2. Load spy jar package to BootstrapClassLoader and get classloader agent jar of
  3. Initialization spy spy
  4. Load AdviceWeaver class
  5. 获取AdviceWeaver类的methodOnBegin、methodOnReturnEnd、methodOnThrowingEnd、methodOnInvokeBeforeTracing、methodOnInvokeAfterTracing、methodOnInvokeThrowTracing、resetArthasClassLoader方法
  6. Binding and Spy
  7. Binding bind
  8. The args parameter deserialize Configure objects
  9. To get the monitoring process pid (javaPid)
  10. Whether to acquire ArthasBootstrap instance judgment call has been bound, it is not binding
  11. ArthasBootstrap bind bind

arthas-core binding

  1. ArthasBootstrap bind bind
  2. Creating shellserver (ShellServerImpl) and bind port
  3. Create a built-in command packet BuiltinCommandPack
  4. The built-in command packet and shellserver bound to register as a resolver service
  5. Depending on the configuration of ip and port registration services: TelnetTermServer, HttpTermServer
  6. shellServer start listening: listen
  7. Traversal registered service registered handler (termHandler = TermServerTermHandler) and start the listener namely: TelnetTermServer, HttpTermServer
  8. Assumed to be telnet service model
  9. 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);
  1. Start service start delegating telnet: NettyTelnetBootstrap.start start
  2. Create an event handler: TelnetChannelHandler
  3. Binding ip port and add a listener
  4. Event processing handler: TelnetChannelHandler, factory factory get property: TelnetTtyConnection
  5. TelnetChannelHandler read data channelRead, called the current connection instance read data: NettyTelnetConnection.receive
  6. 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
  7. Currently bound handler for the factory plant get acquired (TelnetTtyConnection), TelnetTtyConnection.onData
  8. write data decoder (BinaryDecoder.write), to read data to the local and ByteBuffer callback onChar.accept (ReadBuffer.accept)
  9. ReadBuffer call readHandler read data (TtyEventDecoder.accept), readerHandler open when the connection is opened disposed Connection eventDecoder (TtyEventDecoder)
  10. TtyEventDecoder received data accept, eventHandler, readHandler read data, eventHandler, readHandler echoHandler set within TermImpl constructor: DefaultTermStdinHandler, EventHandler
  11. EventHandler method termImpl read command implemented according to the specified call
  12. Reads standard input data DefaultTermStdinHandler.accept, call printed to standard output term, and the instruction in the queue instance is bound ReadLine
  13. The instruction placed in the event queue: EventQueue.append
  14. Client instructions read completion
  15. TelnetTtyConnection will check check Accept callback handler.accept connection open, which is anonymous achieved Consumer.accept
  16. term handle (TermServerTermHandler) handle client implementation termImpl, i.e. direct process delegate shellServer handle termImpl
  17. Create a session (ShellImpl) according Term
  18. Set the welcome page for the session
  19. Set close handle session of
  20. Initializing session
  21. session read command readline, delegating execution term, term delegate instance execution ReadLine
  22. Read instruction from the instruction queue and the EventQueue encapsulated as objects KeyEvent
  23. Examples of interaction Interaction handle to handle events
  24. Acquiring a corresponding method according KeyEvent event handling, processing is completed Complete e.g.
  25. That call CompletionHandler data processing
  26. CompletionHandler accept data processing
  27. Read the command line
  28. Parse command line CliToken list
  29. CommandManagerCompletionHandler处理CompletionAdaptor
  30. Delegate commandManager processing complete
  31. Processing commands completeSingleCommand, completeCommands
  32. Completion processing command, i.e. CompletionAdaptor.complete, the adapter delegate Completion Processing complete
  33. The index output to standard output
  34. Accept processing
  35. RequestHandler process the received request, the processing assigned to lineHandler: ShellLineHandler.handle
  36. According to the command line to create and execute tasks Job
  37. ShellImpl according to the command line to create a Job
  38. Job creation JobController
  39. Creating Process and associate Command, commandManager lines create a corresponding Command on command
  40. JobImpl Binding Process
  41. Implementation of Job, set the term for the Process and the implementation of the session
  42. The encapsulation process is performed CommandProcessTask submitted to the thread pool, CommandProcessImpl handle process delegated
  43. 即Command的processHandler处理:new ProcessImpl(command, remaining, command.processHandler(), ProcessOutput);
  44. For example: MonitorCommand, encapsulated as AnnotatedCommandImpl, processHandle as: ProcessHandler
  45. ProcessHandler processing command CommandProcess, delegate processing process AnnotatedCommandImpl
  46. Create a class instance and an order processing CommandProcess, such as: MonitorCommand.process
  47. 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
  48. After the completion of TermServerListenHandler NettyTelnetTtyBootstrap start to handle the event started, if fails to start, turn off the server
  49. 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

  1. Creating Enhance example
  2. Adding to enhance Instrumentation
  3. Instrumentation modified class definition retransform
  4. Callback enhanced custom Enhancer.transform
  5. Call the ASM API enhancement processing to the specified class, custom class to change the implementation AdviceWeaver
  6. 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

Guess you like

Origin blog.csdn.net/u010597819/article/details/88554000