Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding how java debugging really works under the hood

My question relates to debugging - especially in java/on the jvm.

I would like to know how debugging in java works under the hood:

  • How does 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?

like image 348
balteo Avatar asked Oct 21 '22 11:10

balteo


1 Answers

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 it's derivatives) store the original source file and line number, in the .class file. The Java debugger wil 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 w ill become very handy for both the debugger, and also the stack traces the JVM might end up printing for you.

like image 90
Patrice M. Avatar answered Oct 23 '22 04:10

Patrice M.