SickBoy
SickBoy

Reputation: 66

Controlling java program excecution without debugger

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 ;)

Upvotes: 0

Views: 403

Answers (2)

Stephen C
Stephen C

Reputation: 718778

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.

Upvotes: 3

Shubhzgang
Shubhzgang

Reputation: 324

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.

Upvotes: 0

Related Questions