Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...