My question relates to debugging - especially in java/on the jvm.
I would like to know how debugging in java works under the hood:
jdb/jvm
can match the breakpoint set in java source code to the bytecode currently under execution.Can someone please answer the above question and/or point me to documentation/specification relevant to jvm debugging and how it works?
Check out the javac documentation at: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
In particular, the javac will, by default, or with the explicit -g
option (and its derivatives) store the original source file and line number, in the .class file.
The Java debugger will be able to retrieve that information when executing byte codes at runtime, and will match this with your source code.
That is why, if your runtime classpath is not in sync with your source file (i.e. the 'wrong' jar / .class files are in the runtime classpath), the debugger will sometimes show the wrong line number, or even line numbers that don't seem executable. This is a classic signature of the 'bad' runtime classpath issue (or at least inconsistency between your source and the compiled code).
[Edited] Technically, you can reduce the size of your .class/.jar files by using the -g:none
, but really that is most frequently a good use of disk-space, since the source/line numbers information will become very handy for both the debugger, and also the stack traces the JVM might end up printing for you.