java javac 调试信息选项 -g:vars 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5746894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the javac debugging information option -g:vars do?
提问by Miguel
What exactly the -g:vars
(local variable debugging information) option of javac provides as output.
-g:vars
javac的(局部变量调试信息)选项究竟提供了什么作为输出。
Doing some tests, there is no addition information (example no difference between -g:source,lines and -g:source,lines,vars.
做一些测试,没有附加信息(例如 -g:source,lines 和 -g:source,lines,vars 之间没有区别。
Does some one have an example of a these local variable debugging information?
有人有这些局部变量调试信息的例子吗?
采纳答案by karakuricoder
From the javadocs:
从javadocs:
-g Generate all debugging information, including local variables. By default, only line number and source file information is generated.
-g 生成所有调试信息,包括局部变量。默认情况下,只生成行号和源文件信息。
This does not produce visible output at compile time but is used in debugging during run time.
这不会在编译时产生可见的输出,但在运行时用于调试。
回答by ataylor
The -g:vars
option will insert a LocalVariableTable into your class file. For example, with this test class:
该-g:vars
选项会将 LocalVariableTable 插入到您的类文件中。例如,使用这个测试类:
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:
可以查看类文件中的调试信息javap -l Test
。没有-g
参数,只有一个 LineNumberTable。这是 JVM 用来生成您在堆栈跟踪中看到的行号的内容。如果使用 编译-g:vars
,您会注意到现在有一个 LocalVariableTable,如下所示:
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
:
如果您有可用的源代码,您通常不需要它来进行调试。但是,如果您没有源,它可能很有用。例如,运行jdb Test
和不运行-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
.
如果类是用-g:vars
.