5 Replies Latest reply: Sep 5, 2008 5:15 PM by 843810 RSS

    Is there a JDK API to see if a particular class has debug information?

    843810
      Howdy!

      How can I know if a class loaded in the JVM has debug information (i.e. it has been compiled with javac -g)?

      Alternatively, what is the right way to assess if particular class (loaded in a VM) has debug information (from within the same VM)?

      (I am planning to use this information to infer if it is the debug build or the optimized build that JVM runs).

      Regards,
      Kedar
        • 1. Re: Is there a JDK API to see if a particular class has debug information?
          800575
          I don't know of a reasonable way to do what you want. The only way I know of is to run your app in debug mode and then use JDI on yourself to discover if line number information is present or not for a class. But, I doubt if you want to do this since your app will run more slowly. Also, calling JDI on yourself is fraught with danger.
          • 2. Re: Is there a JDK API to see if a particular class has debug information?
            800575
            Hmm, I was thinking of the line number table which is generated by default by javac but can be suppressed. I believe it is also omitted from Windows JRE's to save space but am not sure of that.

            But the same technique could be used to check for the existence of a local variable table.
            • 3. Re: Is there a JDK API to see if a particular class has debug information?
              843810
              Jim,

              Thanks for your response. I wanted to do something using this information in GlassFish, where I have implemented a command named "asadmin version" that tells user more about given GlassFish installation.

              I wanted to extend the "Serviceability" promise of this command, e.g. by providing the information whether or not a given GlassFish installation uses debug build of application server classes. A reasonable assumption is if a particular class (say GlassFish main class) has debug info, the installation uses debug build.

              I guess I'll have to wait to realize this. Do you have any other suggestion to achieve this?

              Thanks,
              Kedar
              • 4. Re: Is there a JDK API to see if a particular class has debug information?
                800575
                There are three kinds of 'debug' info that can be in a .class file:
                - source file name
                - line numbers
                - local variable names and scopes.

                The generation of these is controlled by the -g:<...> javac option.
                -g Generate all debugging info
                -g:none Generate no debugging info
                -g:{lines,vars,source} Generate only some debugging info

                You actually can detect the presence of source file names and line numbers in a class if you can provoke some method in/under that class into throwing an exception. You can then catch the exception, get the stack trace associated with it, and examine the java.lang.StackTraceElement s to find a frame for the class of interest and then checking to see if the file name and line number are available.

                I still don't see a way to detect the presence of local variable information without using JPDA (or locating the .class file yourself and reading it to see if it contains a LocalVariableTable).

                By 'debug build', you probably mean that the classes were compiled with -g meaning all three types of information are in the .class files.
                A 'non debug build' probably means that -g was not used to compile which means that the class files will contain source file names and line numbers, but no local variable info. Note that such .class files are still debuggable, you just can't view/modify local variables. But you can still do all other debugging operations such as set bkpts, step, view/modify fields in objects, ...
                • 5. Re: Is there a JDK API to see if a particular class has debug information?
                  843810
                  Thanks for all this information. Yes, in our builds, "-g" is not passed by default (it is, if release engineer explicitly does so) to "javac" which according to you will generate class files with source file names and line numbers and no local variable information.

                  Such a build (without any -g) is probably debuggable but unless local variable information is available, I think it is difficult to debug a particular problem. So, developers often require builds where class files contain local variable table.

                  Short of using JPDA, is it safe to analyze class file and look for LocalVariableTable to infer this?

                  [I have been using terms like debug builds/optimized builds to relate to the native builds we referred to (for a C program, for example)].

                  Thanks again.

                  PS - javac is rather too brief about this -- it could have said that line numbers and source names information is generated "by default" (i.e. no -g on command line).