2

If fields defined in a Java class as 'private' are only accessible from within their own class - how does the debugging tool show them and their values when we are walking through an executing code?

Picture-In-Picture Screenshot for illustration: enter image description here

My best guess is that the JVM has a debugging mode that allows a special debugging package to access these. Running in debugging mode (such as a debug configuration from the IDE) allows this special package (which the IDE knows to use) to do its thing.

It's just interesting... No work is reliant on your answer here :)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
DraxDomax
  • 1,008
  • 1
  • 9
  • 28
  • 1
    Yes, the JVM has a special debugger API. You can ask the JVM for quite a lot of interesting stuff, you can get a full dump of all existing objects for example. Debugger and similar tools use those interfaces to get full access to everything. – Zabuzard Apr 15 '20 at 13:28
  • 2
    For "special debugger API", see [Java Platform Debugger Architecture (JPDA)](https://docs.oracle.com/javase/8/docs/technotes/guides/jpda/). – Andreas Apr 15 '20 at 13:35
  • 2
    Note that `private` is not meant to provide any *security* to a program. Java offers many ways to access whatever you want, if you really need to. For example the Reflection API from within Java code, or mentioned interfaces for external applications. `private` is just a tool for code design and architecture, not security. To prevent someone from accidentally doing mistakes, not to prevent someone who does it intentionally. – Zabuzard Apr 15 '20 at 13:45
  • The term you want to read about is reflection. See for example https://www.oracle.com/technical-resources/articles/java/javareflection.html. Or here: https://stackoverflow.com/questions/1196192/how-to-read-the-value-of-a-private-field-from-a-different-class-in-java – tobi Apr 15 '20 at 13:26
  • That second link is actually useful for a "generic object snapshooting" tool I want to develop to help me with post-execution debugging, thanks! – DraxDomax Apr 15 '20 at 13:28
  • Debuggers don't use reflection, they use the [Java Platform Debugger Architecture (JPDA)](https://docs.oracle.com/javase/8/docs/technotes/guides/jpda/). – Andreas Apr 15 '20 at 13:34
  • Note though that the reflection API is a legal way to get access to **private fields** within Java code. So while it is definitely not what a debugger uses, it can be used to access what OP asked for. – Zabuzard Apr 15 '20 at 13:43
  • Thank you, @Andreas, I'd heard of that but wasn't aware anymore that it existed. – tobi Apr 15 '20 at 13:43

1 Answers1

4

In a JVM based on the OpenJDK codebase, a debugger uses a JVMTI agent to access the state of the program being debugged. The agent makes calls to the JVMTI native API. This allows agent (and hence the debugger) to read and write the values of object fields, local variables and so on. The JVMTI APIs ignore accessibility. (In the same way that the JNI APIs do.)

For more information, read the JVM Tools Interface documentation.

For a broader view, see the Java Platform Debugger Architecture documentation. JVMTI is part of that architecture.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216