Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug Arthas In IDEA #222

Closed
hengyunabc opened this issue Oct 11, 2018 · 12 comments
Closed

Debug Arthas In IDEA #222

hengyunabc opened this issue Oct 11, 2018 · 12 comments

Comments

@hengyunabc
Copy link
Collaborator

hengyunabc commented Oct 11, 2018

Tips:

  1. It is better to run as-package.sh before start debugging, it will install the newest version.
  2. If you want to debug Arthas core like Commands, please check the second part.

The first part, debug Arthas how to attach to target JVM.

Debug com.taobao.arthas.core.Arthas

Start com.taobao.arthas.core.Arthas

$ ./as.sh --debug-attach
Arthas script version: 3.0.5
Found existing java process, please choose one and hit RETURN.
* [1]: 56707 org.jetbrains.idea.maven.server.RemoteMavenServer
  [2]: 56612
  [3]: 442
  [4]: 56879 Demo
4
Calculating attach execution time...
Attaching to 56879 using version 3.0.5.20180927185309...
Listening for transport dt_socket at address: 8888

Actually as.sh start the java process:

java -agentlib:jdwp=transport=dt_socket,address=8888,server=y,suspend=y -Djava.awt.headless=true -Xbootclasspath/a:/Users/hengyunabc/.sdkman/candidates/java/current/lib/tools.jar -jar /Users/hengyunabc/.arthas/lib/3.0.5.20180927185309/arthas/arthas-core.jar -pid 56879 -target-ip 127.0.0.1 -telnet-port 3658 -http-port 8563 -core /Users/hengyunabc/.arthas/lib/3.0.5.20180927185309/arthas/arthas-core.jar -agent /Users/hengyunabc/.arthas/lib/3.0.5.20180927185309/arthas/arthas-agent.jar

The main class of arthas-core.jar is com.taobao.arthas.core.Arthas.

IDEA remote debug

  1. Edit Configurations

    image

  2. Add new remote configuration

    image

  3. Add breakpoint at com.taobao.arthas.core.Arthas#main, then start debugging

    image

@hengyunabc
Copy link
Collaborator Author

hengyunabc commented Oct 11, 2018

The second part, debug arthas code running in the target JVM, this is the part that developers care about.

  1. Start Demo in debug mode

    Write a simple java Demo:

    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.atomic.AtomicInteger;
    public class Demo {
        static class Counter {
            private static AtomicInteger count = new AtomicInteger(0);
            public static void increment() {
                count.incrementAndGet();
            }
            public static int value() {
                return count.get();
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            while (true) {
                Counter.increment();
                System.out.println("counter: " + Counter.value());
                TimeUnit.SECONDS.sleep(1);
            }
        }
    }

    Complie and start:

    javac Demo.java
    java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000 Demo
    
  2. IDEA add new remote configuration

    image

  3. Add breakpoint at com.taobao.arthas.agent.AgentBootstrap#agentmain, then start debugging

    image

  4. Run as.sh to attach to the Demo jvm

    $ ./as.sh
    Arthas script version: 3.0.4
    Found existing java process, please choose one and hit RETURN.
    * [1]: 59234 Demo
    [2]: 56707 org.jetbrains.idea.maven.server.RemoteMavenServer
    [3]: 56612
    [4]: 442
    
    Calculating attach execution time...
    Attaching to 59234 using version 3.0.5.20180927185309...
  5. IDEA stop at breakpoint

    image

@zonghaishang
Copy link

Nice , :-)

@Hearen
Copy link
Contributor

Hearen commented Oct 12, 2018

@hengyunabc This is pretty readable now. Thanks for the help.

But still there is an issue about the first part debugging Arthas core.

  1. booting Arthas via JPDA_SUSPEND=y bin/as.sh debug;
  2. configuring arthas remote;
  3. starting via debug.

Indeed it will stop at the arthas.main and I can check the attachAgent details, but still after this (attaching the arthas agent), the arthas remote will be disconnected as:

Connected to the target VM, address: 'localhost:8888', transport: 'socket'
Disconnected from the target VM, address: 'localhost:8888', transport: 'socket'

I can NOT still debug the command implementation in arthas core as hoped.

Like tt -D

image

@ralf0131 Please help me out if anything I missed out.

@Hearen
Copy link
Contributor

Hearen commented Oct 12, 2018

I think I got the point now as for #222 (comment)

the second part is actually used to debug the arthas.core commands.

image

I will update the https://github.com/alibaba/arthas/blob/master/CONTRIBUTING.md to make it easier for other contributors. @hengyunabc Sounds okay?

@shentuUrey
Copy link

IDEA remete debug -> IDEA remote debug

@hengyunabc
Copy link
Collaborator Author

@shentuUrey Thanks, fixed.

@kylelck
Copy link

kylelck commented Dec 7, 2018

@Hearen In the second part,i encounter a problem that can not debug the arthas.core commands.Can u tell me how did u solve it?just like 'help' ,i don't debug in HelpCommand.

@hengyunabc
Copy link
Collaborator Author

Debug arthas-boot.jar

arthas-boot.jar is a normal java application. If you want to debug it, just start to debug it in IDE.

You can also start it with remote debugging arguments: java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000 -jar arthas-boot.jar

@libinglong
Copy link

libinglong commented Dec 18, 2018

Debug arthas-boot.jar

arthas-boot.jar is a normal java application. If you want to debug it, just start to debug it in IDE.

You can also start it with remote debugging arguments: java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000 -jar arthas-boot.jar

thank you @hengyunabc
both of the ways work.
but the first way can't send ctrl+c signal to embedded terminal(in intellij idea).
the second way works well for me!

@kylelck
Copy link

kylelck commented Dec 19, 2018

Debug arthas-boot.jar

arthas-boot.jar is a normal java application. If you want to debug it, just start to debug it in IDE.

You can also start it with remote debugging arguments: java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000 -jar arthas-boot.jar

Thank you!

@aihes
Copy link
Contributor

aihes commented Apr 8, 2019

如果在第二部分的调试过程中,出现了如下问题
WechatIMG386
WechatIMG388

可以启动as.sh,然后shutdown,重启arthas之后再试。

成功之后的样子
WechatIMG399

@felayman
Copy link

felayman commented Oct 24, 2019

the way like this (Hearen#20) is ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants