Miguel
Miguel

Reputation: 243

What does the javac debugging information option -g:vars do?

What exactly the -g:vars (local variable debugging information) option of javac provides as output.

Doing some tests, there is no addition information (example no difference between -g:source,lines and -g:source,lines,vars.

Does some one have an example of a these local variable debugging information?

Upvotes: 24

Views: 19828

Answers (2)

ataylor
ataylor

Reputation: 66099

The -g:vars option will insert a LocalVariableTable into your class file. For example, with this test class:

public class Test {
    public static void main(String[] args) {
        int mylocal = 1;
        System.out.println("" + mylocal);
    }
}

You can take a look at the debugging information in the class file with javap -l Test. With no -g arguments, there is just a LineNumberTable. This is what the JVM uses to generate the line numbers you see in stacktraces. If you compile with -g:vars, you'll notice there is now a LocalVariableTable that looks like this:

LocalVariableTable: 
 Start  Length  Slot  Name   Signature
 0      3      0    args       [Ljava/lang/String;
 2      1      1    mylocal       I

This captures the name and type of each parameter and local variable by its position on the stack.

You don't normally need this for debugging if you have the source available. However if you don't have the source it can be useful. For example, run jdb Test with and without -g:vars:

Initializing jdb...
> stop in Test.main
Deferring breakpoint Test.main.
It will be set after the class is loaded.
> run
main[1] next
main[1] next
main[1] locals
Method arguments:
args = instance of java.lang.String[0] (id=354)
Local variables:
mylocal = 1

You'll only get the list of locals if the class was compiled with -g:vars.

Upvotes: 34

karakuricoder
karakuricoder

Reputation: 1075

From the javadocs:

-g Generate all debugging information, including local variables. By default, only line number and source file information is generated.

This does not produce visible output at compile time but is used in debugging during run time.

Upvotes: 14

Related Questions