11 Replies Latest reply: Jul 21, 2008 4:42 PM by 843810 RSS

    local variables visibility

    843810
      Hello,

      I am writing a code that uses JDI , what im trying to do is while
      executing a class an event is fired when a new local variable is
      created/modified. That worked well for sequential algorithms , but any
      variables inside If , for, while statments acts as is they are
      invisible. I am using stackframe.getVisibleVariables(); I searched
      about it and what i came with is that variables have a visibility
      range.

      So if that's my problem how can i solve it ?
        • 1. Re: local variables visibility
          796365
          Cross-posted duplicate and answered here:
          http://forum.java.sun.com/thread.jspa?messageID=10166530
          • 2. Re: local variables visibility
            843810
            Mr Jim Holmlund said to me :

            <*In order to extract local variable information,you have to compile the files with javac -g .... (javac -g filename.java)*
            That causes javac to create the LocalVariableTable attribute in the .class file.  JPDA uses this to allow you to get/set
            local variables.

            Once a thread is suspended you can get all its frames by calling        ThreadReference.frames()

            and for each frame (for a method that was compiled with -g) you can call StackFrame.getValue(s) to access local variables.>
            *<the frame.visibleVariables is needed to get the List<LocalVariable> so you can examine them.>*
            See this code for using visibleVariables Method:
             List locals;
                       try {
             
                    locals = frame.visibleVariables();
            
                        } catch (AbsentInformationException e) {                
                                
                       system.out.println("Local variable information not available." +                   
                                            " Compile with -g to generate variable  information");
                        return;
                        }  catch(NativeMethodException e)      {
            
                        system.out.printl("native methode...");
                             
                        return;
                        } catch(InvalidStackFrameException e)      {
            
                        system.out.printl("frame that has no local variables at the current frame location....");
                             
                        return;
            }
            For more informations about :visibleVariables() Method go to see it in the JPDA APIs in the StackFrame interface ,th elink is: [http://java.sun.com/javase/6/docs/jdk/api/jpda/jdi/index.html]
            and go to the subject posted here about Cannot get information about local variables on a StackFrame in the link:
            [http://forum.java.sun.com/thread.jspa?threadID=5142248&tstart=40]

            Hope this helps.
            • 3. Re: local variables visibility
              843810
              I am already compiling with -g. What i mean is stackframe.visibleVariables() returns the methods visible variables it doesnt return the variables defined inside blocks.

              For example:

              1 public void algorithm(int radius, String response)
              2     {
              3          double area=0,cirumference=0;
              4                    
              5          if(response.equals("A"))
              6          {
              7               area = (radius * radius * 3.14);
              8          }
              9          else{
              10               cirumference = (2 * radius * 3.14);
              11          }          
              12     }

              When i pass the above example to my code which on each StepEvent it scans visible variables at the current line. It will be able to notice the changes and initialization of variables "area" and "circumference".


              But the Following Example wont:

              1 public void algorithm(int radius, String response)
              2     {
              3          
              4                    
              5          if(response.equals("A"))
              6          {
              7               double area = (radius * radius * 3.14);
              8          }
              9          else{
              10               double cirumference = (2 * radius * 3.14);
              11          }          
              12     }


              In Example 2 , it doesnt take notice of the creation of variables "area" and "circumference" because they are defined inside blocks and thus not visible to the method.

              If am what i am saying is incorrect i would appreciate someone correcting me, else i would appreciate a work around that problem.


              thanks in advance for your help
              • 4. Re: local variables visibility
                843810
                Hello,

                According to the jdi specification, and the Jim holmlund explications ;The local variable is invisible if :
                - there is no local variable information for this method. (when AbsentInformationException occured):
                you are looking at the frame for a method that was not compiled with -g, ie a JDK class from rt.jar. Those
                are not compiled with -g. You should find the location;for each frame to show the class.method that frame is executing.

                - this stack frame has become invalid. Once the frame's thread is resumed, the stack frame is no longer valid. ( InvalidStackFrameException):
                you are looking at frame that was compiled with -g but that has no local variables at the current frame location.
                if your method looks like this:
                {
                :
                {
                float f1;
                String str = "Hi there Aaaa!";
                :
                }
                System.out.println("oh");
                :

                If you are stoped at the println, then the local vars f1 and str won't be returned by visibleVariables() because they are not defined at the location of the printlin. If no other local vars are in scope at that point, they visibleVariables will return a 0 length list.

                - the current method is native.(NativeMethodException ):
                So it's an operation not valid for a native method.(we can not know it's local variables)

                I think that only you did not stopped at any part of the tow blocs above.What is the event you are using to stopping the running of your application , then to extract the current local variables.
                I would like to know what are you doing with JDI?, because your question is very important and if you know the correct answer of it, put it here please,
                • 5. Re: local variables visibility
                  843810
                  Well All the conditions u r saying is met.
                  1) I am compiling using -g
                  2) When i am setting up the StepRequest i used StepRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
                  3) Its not a native method

                  Setting up StepRequest:

                  StepRequest sreq = mgr.createStepRequest(mainThread, StepRequest.STEP_LINE, StepRequest.STEP_INTO);
                  sreq.setSuspendPolicy(EventRequest.SUSPEND_ALL);
                       sreq.enable();

                  I am using JDI in an algorithm visualization project i am currently writing.

                  Edited by: 3ashmawy on Mar 26, 2008 4:46 AM
                  • 6. Re: local variables visibility
                    800575
                    We are looking into this. Might be javac and/or JDI bugs (I presume you are using Sun's JDK). In your example:

                    6 {
                    7 double area = (radius * radius * 3.14);
                    8 }
                    9 else{

                    If you stop at 7, then you won't see 'area' because you are not yet in the live range of 'area' as defined in the Local Variable Table of the class file.
                    If you then do a 'next', you will stop at line 8. Now, you should be in the live range of 'area' and so you should see it. That you don't see it
                    is the probable bug we are looking at.
                    I'll let you know what we decide.

                    Thanks
                    jjh
                    • 7. Re: local variables visibility
                      843810
                      Thank you so much for the effort. Please do let me know when you figure it out
                      • 8. Re: local variables visibility
                        800575
                        Turns out the VM Spec had a problem in this area (the LocalVariableTable attribute) that was fixed in the 3rd edition for JDK 6.
                        However, javac was not updated per the new spec. See:
                        http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6680704
                        • 9. Re: local variables visibility
                          843810
                          Thanks alot man :D I guess ill wait for an update.
                          • 10. Re: local variables visibility
                            843810
                            Hey - does anyone have any updates on this issue!

                            cheers,
                            Andrew
                            • 11. Re: local variables visibility
                              843810
                              resurrecting thread - anybody have an update on this?