This discussion is archived
8 Replies Latest reply: Apr 22, 2008 3:52 PM by 800575 RSS

Please help - Detecting Current Line in source code

843810 Newbie
Currently Being Moderated
In Trace.java package and specifically in EventThread.java to print the stack frame we used two steps
Step (1):
try {
StackFrame sf = event.thread().frame(0);
println("\n**** " + sf.location().sourceName() +
":" + sf.location().lineNumber() +
"in method " + sf.location().method().name() + "() ****");
printStackFrame(sf);
} catch(Exception e) {
//foo
}
Which calls:
Step (2):
void printStackFrame(StackFrame sf) {
try {
for(LocalVariable localvar: sf.visibleVariables()) {
println(localvar.typeName()+ " " localvar.name() " = " + sf.getValue(localvar));
}
} catch(AbsentInformationException e) {
println("Got AbsentInformationException");
}
}

The output is like this:

**** Foo.java:6 in method main() ****
java.lang.String[] args = instance of java.lang.String[0] (id=43)

**** Foo.java:7 in method main() ****
java.lang.String[] args = instance of java.lang.String[0] (id=43)
int e = 4

**** Foo.java:8 in method main() ****
java.lang.String[] args = instance of java.lang.String[0] (id=43)
int e = 4
int g = 5

**** Foo.java:9 in method main() ****
java.lang.String[] args = instance of java.lang.String[0] (id=43)
int e = 4
int g = 5
int i = 0
....

My Questions are:
1- Is there is a way to print only the variables at current line only.
2- Is there is a way to get the .java code corresponding to the line executed.
3- During the trace method when we are cross System.out.println method, is there is a way to make that happened during the trace method.

Thanks a lot.

Iman

Edited by: Iman_S on Apr 8, 2008 8:59 AM

Edited by: Iman_S on Apr 9, 2008 6:23 AM
  • 1. Re: Please help - Detecting Current Line in source code
    843810 Newbie
    Currently Being Moderated
    Iman ,

    If you are requesting the Step Event Line Wise (STEP_INTO_LINE) then when you handle the event you will be handling one step each time.The code corresponding to setting the step request as i explained is as follows:

    begin
              StepRequest sreq = mgr.createStepRequest(mainThread,
                        StepRequest.STEP_LINE, StepRequest.STEP_INTO);
    end

    If you set your request like this then for example when you iterate through the StackFrame 's visible local variables , you will get only the variables visible at this line. The above code basically request a step event each time the target Virtual machine steps into a line.

    About the source code part , check out http://java.sun.com/javase/6/docs/jdk/api/jpda/jdi/index.html . You can get the source name and path.

    The last question i dont understand what do you mean by it ? ...
  • 2. Re: Please help - Detecting Current Line in source code
    843810 Newbie
    Currently Being Moderated
    Thanks a lot for your help. In my last question I ment, when the tracing process go through a line contains
    System.out.println(x); I want at that moment see that in the output trace.
    For example if I have the follwing code:
    class Foo {
    public static void main(String[] args)
    {           
         int e = 4;
         int g = 5;
         for(int i=0; i<2; i++)
         System.out.println("i=" +i);
    }

    }
    The trace is:
    **** Foo.java:6in method main() ****
    java.lang.String[] args = instance of java.lang.String[0] (id=43)

    **** Foo.java:8in method main() ****
    java.lang.String[] args = instance of java.lang.String[0] (id=43)
    int e = 4

    **** Foo.java:9in method main() ****
    java.lang.String[] args = instance of java.lang.String[0] (id=43)
    int e = 4
    int g = 5

    **** Foo.java:10in method main() ****
    java.lang.String[] args = instance of java.lang.String[0] (id=43)
    int e = 4
    int g = 5
    int i = 0

    **** Foo.java:9in method main() ****
    java.lang.String[] args = instance of java.lang.String[0] (id=43)
    int e = 4
    int g = 5
    int i = 0

    **** Foo.java:10in method main() ****
    java.lang.String[] args = instance of java.lang.String[0] (id=43)
    int e = 4
    int g = 5
    int i = 1

    **** Foo.java:9in method main() ****
    java.lang.String[] args = instance of java.lang.String[0] (id=43)
    int e = 4
    int g = 5
    int i = 1

    **** Foo.java:11in method main() ****
    java.lang.String[] args = instance of java.lang.String[0] (id=43)
    int e = 4
    int g = 5

    as you can see there is no
    i=0
    i=1
    which is the output during the trace process. Is there is a way to make that happend.
    Thanks
    Iman

    Edited by: Iman_S on Apr 10, 2008 1:58 AM
  • 3. Re: Please help - Detecting Current Line in source code
    843810 Newbie
    Currently Being Moderated
    Yes, this is because you need to redirect the output stream from the target virtual machine to the caller's virtual machine.

    begin
    Process p = vm.process();
    InputStream inStream = p.getInputStream()
    end

    p.getInputStream() gets the input stream of the subprocess. The stream obtains data piped from the standard output stream of the process represented by this Process object.
    After you have acquired the stream you read from it when its available.
  • 4. Re: Please help - Detecting Current Line in source code
    800575 Newbie
    Currently Being Moderated
    My Questions are:
    +1- Is there is a way to print only the variables at current line only.+
    I don't understand the question. In your example, you print out the variables that are in scope at the line at which you are stopped..
    Isn't that what you want"

    +2- Is there is a way to get the .java code corresponding to the line executed.+
    Look at the jdb code in <jdk>/demo/jpda/examples.jar. jdb shows the line from the .java file at which the debuggee is stopped.

    +3- During the trace method when we are cross System.out.println method, is there is a way to make that happened during the trace method.+
    jdb basically does this ok; look at it.
  • 5. Re: Please help - Detecting Current Line in source code
    843810 Newbie
    Currently Being Moderated
    Thanks a lot for your reply. Maybe I did not ask my question properly, but I am learning form you. What I meant by the first question and according to the above code is:

    In debugging line where int g=5;

    class Foo {
    public static void main(String[] args)
    {
    int e = 4;
    *==> int g = 5;*
    for(int i=0; i<2; i++)
    System.out.println("i=" +i);
    }
    }
    The output of the trace prints:

    **** Foo.java:9in method main() ****
    java.lang.String[] args = instance of java.lang.String[0] (id=43)
    int e = 4
    int g = 5

    What I need is just to print:
    Int g = 5

    I do not want int e = 4 and the previous lines to be printed ..

    Thanks again.
  • 6. Re: Please help - Detecting Current Line in source code
    800575 Newbie
    Currently Being Moderated
    Sorry, I still don't understand the issue:
    - if you are stopped at
    int g = 5;

    and use JDI to get all the visible vars, I don't think it will return 'g' since g does not yet have a value.

    - what local variables do you expect to see if you are stopped at the following line:
    for (int i = 0; i < 2; i++)
    ?

    StackFrame.visibleVariables() just returns all variables that are 'visible' at the location of the StackFrame.
    It doesn't know anything about what java code is at the location, eg, whether it is a variable declaration
    or a for statement. If you want to do that sort of thing, you have to do something like get the line number from
    JDI and then go find the source code file and grovel around in it to try to figure out what java statements are
    at which line numbers (probably by using the compiler api).
  • 7. Re: Please help - Detecting Current Line in source code
    843810 Newbie
    Currently Being Moderated
    Thanks a lot for your reply. I think you understood what I need. I know JDI does not know about what the instruction is and I was trying to match the trace code with the line number to sort my trace with .java code.
    For Example you asked :
    what local variables do you expect to see if you are stopped at the following line:
    for (int i = 0; i < 2; i++ )?

    I expect to get the following :

    1- to find the line number at where is for (int i = 0; i < 2; i++)
    2- to know the value of i which is 0 for the first time.How can I exclude the other variables in that frame?

    Thanks a lot for your comments. It really helps
  • 8. Re: Please help - Detecting Current Line in source code
    800575 Newbie
    Currently Being Moderated
    But, when you are stopped at the beginning of the for( int i = 0; ... statement variable i is not yet visible so you can't display it.
    If you did a 'step' into the first line of the body of the 'for', then visibleVariables() will return i, but it will also return
    all other local variables that have values at that point in the code.

    So, I don't think there is a way in JDI to find one visible variable without finding all of them. Well, I suppose
    you could stop at the beginning of a method and then step thru the method, one line at a time, and each
    time you are stopped, do a StackFrame.visibleVariables(), remember the result, and then look at the difference between successive
    calls to see if any new visible variables have appeared (or any existing ones have disappeared).