0

I want to control execution of java program without debugger, for example there is a class like below

1. class Foo {
2.    void bar() {
3.        print("i am 3rd line of class Foo");
4.        print("i am 4th...");
5.        if(true)
6.            print("i am 6th");
7.    }
8. }

I want to know when java is going to execute any of the above lines and until i don't tell it to continue execution it should wait for me!

If you are familer with debugger in eclipse IDE you would definately know that if we place breakpoint somewhere in java source code the debugger pauses the execution at that line and waits until we hit step in or next button in order to execute next line or statement of program. Basically it is controlling the execution of program plus it is getting every bit of information like declared variables, current running function, current executing line, etc. I also want the same functionality but instead of debugger i would like to use my own java code. You can say that i want to debug java code with in the code.

For example: If java is executing line number 3, it should inform me that i am (java) going to execute line number 3 and until i don't tell it to continue it should wait.

Another example:

5. void bar() {
6.    // Inform me that java is going to execute line no. 7  
7.    print("Printing something...");
8.    // Again inform me that java is executing line no. 9
9.    print("bar");
10.}

In nutshell i want to controll execution of my program line by line, i don't care about performence just want to achieve my goal!

I tried to make this question as simple as possible but if you have still problem in understanding (as it is a little tricky), let me know in the comment box below!

Thank you in advance ;)

4
  • 1
    Learning about JDI might help. Or not. It allows you to write a program that acts as the debugger :) Commented May 15, 2020 at 12:25
  • That is good but I don't want to use external program, and instead of this I could use Eclipse or any other ide. Basically I want to control java program within it Commented May 15, 2020 at 12:37
  • Have you considered JMX (Java Management Extensions)? Commented May 15, 2020 at 12:37
  • @vsfDawg would it work without external tools?, I mean it should work inside of my java program Commented May 15, 2020 at 12:40

2 Answers 2

3

I can think of two ways to do this, and possibly a third. All of them are are a lot of work. I would not recommend any of them.

  1. Implement a bytecode interpreter in Java. Then implement functionality that allows you to stop and start as you want to do.

    Variation: implement a Java interpreter that works off a Java parse tree.

  2. Use a "bytecode engineering" library to modify the bytecodes at load time to inject hook calls into the code that can be used to control execution. If you want line-by-line control of the execution, you need to inject a hook call between each statement in each method or constructor.

    Variation: implement your own custom Java bytecode compiler to do the same thing at compile time.

  3. Implement a java agent using JVMTI, and a Java application to "drive" it. JDI is another way to do it, coming in at a higher level. However, this is tantamount to implementing your own (custom) debugger, and you can only do this with an external application. (A JVM cannot use these APIs to control itself.)


If you are going this to help you debug code, I recommend that you just use a debugger.

If you doing this because is would be a cool language feature, I think you are better of designing a new programming language. (But not that this is not a new idea. Read about metaprogramming.)

If you are doing this just for fun ... knock yourself out. It is your time.

Sign up to request clarification or add additional context in comments.

1 Comment

A java agent based on java.lang.instument is good enough with a variation of 2 and 3. If the debug information is not erased, you can inject additional calls on visitLineNumber in ASM for example.
0

You can use System.in.read() wherever you want the program to wait. But the program will move forward only for enter key press. Something like this should answer your question:

void bar() {
    print("info message"); //info message like "I am going to do something"
    System.in.read(); //waiting for enter key press
    print("Printing something...");
    System.in.read(); //again waiting for enter key press
    print("bar");
}

IMO: this is pretty crude way of debugging (if this is being done for debugging purposes), since there are free IDEs available with lot of cool/advanced debugging functionality.

1 Comment

Thankyou for answering but I want to control execution at runtime. For example there could be a thread 'a' that could be monitoring execution and when any line starts to execute it should pause the running thread 'b' until thread 'a' resumes it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.